From 1c3ea16043ee52f5be8bedb816aca804f377d5f4 Mon Sep 17 00:00:00 2001 From: HapeLee <63206378+HapeLee@users.noreply.github.com> Date: Tue, 9 Jun 2026 22:07:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A2=E7=B4=A2=E9=A1=B5=E7=BD=91?= =?UTF-8?q?=E6=A0=BC=E6=95=B0=E9=87=8F=E4=B8=8D=E4=BF=9D=E5=AD=98=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../local/preferences/LocalPreferences.kt | 2 ++ .../ui/book/explore/ExploreShowViewModel.kt | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/local/preferences/LocalPreferences.kt b/app/src/main/java/io/legado/app/data/local/preferences/LocalPreferences.kt index 1fd62d84e..d1e8083dc 100644 --- a/app/src/main/java/io/legado/app/data/local/preferences/LocalPreferences.kt +++ b/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") } diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt index f7e2f98d9..fe3c8ff81 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt +++ b/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>(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) } }