|
|
@@ -10,8 +10,10 @@ import io.legado.app.domain.usecase.BookShelfKey
|
|
|
import io.legado.app.domain.usecase.ExploreBooksUseCase
|
|
|
import io.legado.app.domain.usecase.ResolveBookShelfStateUseCase
|
|
|
import io.legado.app.domain.usecase.SaveSearchBooksUseCase
|
|
|
+import android.content.res.Configuration
|
|
|
+import io.legado.app.data.local.preferences.LocalPreferencesKeys
|
|
|
+import io.legado.app.data.local.preferences.LocalPreferencesRepository
|
|
|
import io.legado.app.help.config.AppConfig
|
|
|
-import io.legado.app.utils.exploreLayoutGrid
|
|
|
import io.legado.app.utils.stackTraceStr
|
|
|
import kotlinx.collections.immutable.toImmutableList
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
|
@@ -19,6 +21,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
import kotlinx.coroutines.flow.asSharedFlow
|
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
+import kotlinx.coroutines.flow.first
|
|
|
import kotlinx.coroutines.flow.update
|
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.launch
|
|
|
@@ -49,6 +52,7 @@ class ExploreShowViewModel(
|
|
|
private val exploreBooksUseCase: ExploreBooksUseCase,
|
|
|
private val saveSearchBooksUseCase: SaveSearchBooksUseCase,
|
|
|
private val addToBookshelfUseCase: AddToBookshelfUseCase,
|
|
|
+ private val localPreferencesRepository: LocalPreferencesRepository,
|
|
|
) : ViewModel() {
|
|
|
|
|
|
private val _rawBooks = MutableStateFlow<List<SearchBook>>(emptyList())
|
|
|
@@ -58,7 +62,7 @@ class ExploreShowViewModel(
|
|
|
private val _displayState = MutableStateFlow(
|
|
|
ExploreShowDisplayState(
|
|
|
layoutState = AppConfig.exploreLayoutState,
|
|
|
- gridCount = appCtx.exploreLayoutGrid,
|
|
|
+ gridCount = if (appCtx.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) 7 else 3,
|
|
|
)
|
|
|
)
|
|
|
|
|
|
@@ -87,6 +91,7 @@ class ExploreShowViewModel(
|
|
|
init {
|
|
|
observeBookshelf()
|
|
|
combineUiState()
|
|
|
+ loadGridCount()
|
|
|
}
|
|
|
|
|
|
fun onIntent(intent: ExploreShowIntent) {
|
|
|
@@ -215,8 +220,30 @@ class ExploreShowViewModel(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun loadGridCount() {
|
|
|
+ viewModelScope.launch {
|
|
|
+ val isLandscape = appCtx.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
|
|
|
+ val key = if (isLandscape) {
|
|
|
+ LocalPreferencesKeys.EXPLORE_LAYOUT_GRID_LANDSCAPE
|
|
|
+ } else {
|
|
|
+ LocalPreferencesKeys.EXPLORE_LAYOUT_GRID_PORTRAIT
|
|
|
+ }
|
|
|
+ val default = if (isLandscape) 7 else 3
|
|
|
+ val count = localPreferencesRepository.getPreference(key, default).first()
|
|
|
+ _displayState.update { it.copy(gridCount = count) }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private fun saveGridCount(count: Int) {
|
|
|
- appCtx.exploreLayoutGrid = count
|
|
|
+ val isLandscape = appCtx.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
|
|
|
+ val key = if (isLandscape) {
|
|
|
+ LocalPreferencesKeys.EXPLORE_LAYOUT_GRID_LANDSCAPE
|
|
|
+ } else {
|
|
|
+ LocalPreferencesKeys.EXPLORE_LAYOUT_GRID_PORTRAIT
|
|
|
+ }
|
|
|
+ viewModelScope.launch {
|
|
|
+ localPreferencesRepository.updatePreference(key, count)
|
|
|
+ }
|
|
|
_displayState.update { it.copy(gridCount = count) }
|
|
|
}
|
|
|
|