-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: save explore contents to the view model
Additionally add refresh button
- Loading branch information
Showing
5 changed files
with
179 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 41 additions & 7 deletions
48
app/src/main/java/live/shirabox/shirabox/ui/screen/explore/ExploreScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,74 @@ | ||
package live.shirabox.shirabox.ui.screen.explore | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.pullrefresh.PullRefreshIndicator | ||
import androidx.compose.material.pullrefresh.pullRefresh | ||
import androidx.compose.material.pullrefresh.rememberPullRefreshState | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.zIndex | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavController | ||
import live.shirabox.core.util.Util | ||
import live.shirabox.shirabox.ui.component.top.TopBar | ||
|
||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@ExperimentalMaterial3Api | ||
@Composable | ||
fun ExploreScreen(navController: NavController) { | ||
fun ExploreScreen( | ||
navController: NavController, | ||
model: ExploreViewModel = viewModel(factory = Util.viewModelFactory { | ||
ExploreViewModel() | ||
}) | ||
) { | ||
|
||
val lazyListState = rememberLazyListState() | ||
val pullRefreshState = rememberPullRefreshState(model.refreshing.value, { model.refresh() }) | ||
|
||
Scaffold( | ||
modifier = Modifier.pullRefresh(pullRefreshState), | ||
snackbarHost = { AppUpdateSnackbarHost() }, | ||
contentWindowInsets = WindowInsets(0.dp) | ||
) { | ||
LazyColumn( | ||
Box ( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(it), | ||
state = lazyListState, | ||
verticalArrangement = Arrangement.spacedBy(16.dp) | ||
) { | ||
item { TopBar(navController) } | ||
item { BaseMediaScreen(lazyListState = lazyListState) } | ||
contentAlignment = Alignment.TopCenter | ||
){ | ||
LazyColumn( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.zIndex(1f), | ||
state = lazyListState, | ||
verticalArrangement = Arrangement.spacedBy(16.dp) | ||
) { | ||
item { TopBar(navController) } | ||
item { BaseMediaScreen(model = model, lazyListState = lazyListState) } | ||
} | ||
|
||
PullRefreshIndicator( | ||
modifier = Modifier.zIndex(2f), | ||
refreshing = model.refreshing.value, | ||
state = pullRefreshState, | ||
scale = true, | ||
contentColor = MaterialTheme.colorScheme.primary | ||
) | ||
} | ||
} | ||
|
||
} | ||
|
79 changes: 79 additions & 0 deletions
79
app/src/main/java/live/shirabox/shirabox/ui/screen/explore/ExploreViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package live.shirabox.shirabox.ui.screen.explore | ||
|
||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.flow.emitAll | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.onCompletion | ||
import kotlinx.coroutines.launch | ||
import live.shirabox.core.model.Content | ||
import live.shirabox.core.model.ContentType | ||
import live.shirabox.data.catalog.shikimori.ShikimoriRepository | ||
|
||
class ExploreViewModel : ViewModel() { | ||
val populars = MutableStateFlow(emptyList<Content>()) | ||
val ongoings = MutableStateFlow(emptyList<Content>()) | ||
|
||
val contentObservationStatus = mutableStateOf(ObservationStatus(Status.Loading)) | ||
val popularsPage = mutableIntStateOf(1) | ||
val refreshing = mutableStateOf(false) | ||
|
||
fun fetchOngoings() { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
ShikimoriRepository.fetchOngoings(1, ContentType.ANIME) | ||
.catch { | ||
it.printStackTrace() | ||
contentObservationStatus.value = ObservationStatus(Status.Failure, it as Exception) | ||
emitAll(emptyFlow()) | ||
}.collectLatest { | ||
ongoings.emit(it) | ||
contentObservationStatus.value = ObservationStatus(Status.Success) | ||
} | ||
} | ||
} | ||
|
||
fun fetchPopulars() { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
ShikimoriRepository.fetchPopulars(1..popularsPage.intValue, ContentType.ANIME) | ||
.catch { | ||
it.printStackTrace() | ||
contentObservationStatus.value = ObservationStatus(Status.Failure, it as Exception) | ||
emitAll(emptyFlow()) | ||
}.onCompletion { | ||
delay(1000L) | ||
refreshing.value = false | ||
} | ||
.collectLatest { | ||
populars.emit(it) | ||
contentObservationStatus.value = ObservationStatus(Status.Success) | ||
} | ||
} | ||
} | ||
|
||
fun refresh() { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
popularsPage.intValue = 1 | ||
contentObservationStatus.value = ObservationStatus(Status.Loading) | ||
refreshing.value = true | ||
|
||
fetchOngoings() | ||
fetchPopulars() | ||
} | ||
} | ||
|
||
data class ObservationStatus( | ||
val status: Status, | ||
val exception: Exception? = null | ||
) | ||
|
||
enum class Status { | ||
Loading, Success, Failure | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters