Просмотр исходного кода

fix: 探索页网格数量不保存问题

HapeLee 5 месяцев назад
Родитель
Сommit
1c3ea16043

+ 2 - 0
app/src/main/java/io/legado/app/data/local/preferences/LocalPreferences.kt

@@ -13,4 +13,6 @@ object LocalPreferencesKeys {
     val SHOW_THEME_REFACTOR_TIP = booleanPreferencesKey("show_theme_refactor_tip")
     val SEARCH_LAYOUT_MODE = intPreferencesKey("search_layout_mode")
     val MATCH_MODE = intPreferencesKey("match_mode")
+    val EXPLORE_LAYOUT_GRID_PORTRAIT = intPreferencesKey("explore_layout_grid_portrait")
+    val EXPLORE_LAYOUT_GRID_LANDSCAPE = intPreferencesKey("explore_layout_grid_landscape")
 }

+ 30 - 3
app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt

@@ -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) }
     }