From 116dbead4b05fa747b769eaf258ce15de6e932a7 Mon Sep 17 00:00:00 2001 From: GG Bond Date: Fri, 24 Nov 2023 20:52:17 +0800 Subject: [PATCH] Stateful thumbSizeDp --- .../kotlin/com/hippo/ehviewer/Settings.kt | 2 +- .../legacy/AutoStaggeredGridLayoutManager.kt | 72 ++----------------- .../hippo/ehviewer/ui/scene/DownloadsScene.kt | 3 - .../ehviewer/ui/scene/FavoritesFragment.kt | 3 +- .../ehviewer/ui/scene/GalleryDetailScene.kt | 8 +-- .../ehviewer/ui/scene/GalleryListFragment.kt | 3 +- .../ui/scene/GalleryPreviewFragment.kt | 6 +- .../hippo/ehviewer/ui/settings/EhScreen.kt | 2 +- 8 files changed, 17 insertions(+), 82 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt index eb99fc179..38640a36d 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt @@ -103,6 +103,7 @@ object Settings : DataStorePreferences(null) { val listMode = intPref("list_mode_2", 0) val detailSize = intPref("detail_size_2", 0) + val thumbSizeDp = intPref("thumb_size_", 120) val needSignInFlow: Flow var downloadScheme by stringOrNullPref("image_scheme", null) @@ -145,7 +146,6 @@ object Settings : DataStorePreferences(null) { var preloadThumbAggressively by boolPref("preload_thumb_aggressively", false) var downloadOriginImage by boolPref("download_origin_image", false) var enableCronet by boolPref("enable_cronet", true) - var thumbSizeDp by intPref("thumb_size_", 120) var recentFavCat by intPref("recent_fav_cat", FavListUrlBuilder.FAV_CAT_LOCAL) var defaultFavSlot by intPref("default_favorite_slot", -2) var securityDelay by intPref("require_unlock_delay", 0) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/legacy/AutoStaggeredGridLayoutManager.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/legacy/AutoStaggeredGridLayoutManager.kt index 07acaab81..d3076797d 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/legacy/AutoStaggeredGridLayoutManager.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/legacy/AutoStaggeredGridLayoutManager.kt @@ -16,19 +16,12 @@ package com.hippo.ehviewer.ui.legacy import android.view.View -import androidx.compose.runtime.Composable -import androidx.compose.ui.platform.LocalConfiguration import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.StaggeredGridLayoutManager -import com.hippo.ehviewer.Settings -import kotlin.math.roundToInt -class AutoStaggeredGridLayoutManager(columnSize: Int, orientation: Int) : - StaggeredGridLayoutManager(1, orientation) { +class AutoStaggeredGridLayoutManager(columnSize: Int, orientation: Int) : StaggeredGridLayoutManager(1, orientation) { private var mColumnSize = columnSize private var mColumnSizeChanged = true - private var mStrategy = 0 - var supportsPredictiveItemAnimations = true fun setColumnSize(columnSize: Int) { if (columnSize == mColumnSize) { @@ -38,75 +31,18 @@ class AutoStaggeredGridLayoutManager(columnSize: Int, orientation: Int) : mColumnSizeChanged = true } - fun setStrategy(strategy: Int) { - if (strategy == mStrategy) { - return - } - mStrategy = strategy - mColumnSizeChanged = true - } + override fun supportsPredictiveItemAnimations() = false - override fun supportsPredictiveItemAnimations(): Boolean { - return supportsPredictiveItemAnimations && super.supportsPredictiveItemAnimations() - } - - override fun onMeasure( - recycler: RecyclerView.Recycler, - state: RecyclerView.State, - widthSpec: Int, - heightSpec: Int, - ) { + override fun onMeasure(recycler: RecyclerView.Recycler, state: RecyclerView.State, widthSpec: Int, heightSpec: Int) { if (mColumnSizeChanged && mColumnSize > 0) { val totalSpace = if (orientation == VERTICAL) { - check( - View.MeasureSpec.EXACTLY == View.MeasureSpec.getMode( - widthSpec, - ), - ) { "RecyclerView need a fixed width for AutoStaggeredGridLayoutManager" } View.MeasureSpec.getSize(widthSpec) - paddingRight - paddingLeft } else { - check( - View.MeasureSpec.EXACTLY == View.MeasureSpec.getMode( - heightSpec, - ), - ) { "RecyclerView need a fixed height for AutoStaggeredGridLayoutManager" } View.MeasureSpec.getSize(heightSpec) - paddingTop - paddingBottom } - val spanCount = when (mStrategy) { - STRATEGY_MIN_SIZE -> getSpanCountForMinSize( - totalSpace, - mColumnSize, - ) - - STRATEGY_SUITABLE_SIZE -> getSpanCountForSuitableSize( - totalSpace, - mColumnSize, - ) - - else -> getSpanCountForMinSize( - totalSpace, - mColumnSize, - ) - } - setSpanCount(spanCount) + spanCount = (totalSpace / mColumnSize).coerceAtLeast(1) mColumnSizeChanged = false } super.onMeasure(recycler, state, widthSpec, heightSpec) } } - -const val STRATEGY_MIN_SIZE = 0 -const val STRATEGY_SUITABLE_SIZE = 1 -fun getSpanCountForSuitableSize(total: Int, single: Int): Int { - return (total / single.toFloat()).roundToInt().coerceAtLeast(1) -} - -@Composable -fun calculateSuitableSpanCount(): Int { - val totalSpace = LocalConfiguration.current.screenWidthDp - return getSpanCountForSuitableSize(totalSpace, Settings.thumbSizeDp) -} - -fun getSpanCountForMinSize(total: Int, single: Int): Int { - return (total / single).coerceAtLeast(1) -} diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/DownloadsScene.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/DownloadsScene.kt index 7eede63a5..bed7c7486 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/DownloadsScene.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/DownloadsScene.kt @@ -103,7 +103,6 @@ import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder import com.hippo.ehviewer.ui.legacy.CheckBoxDialogBuilder import com.hippo.ehviewer.ui.legacy.EditTextDialogBuilder import com.hippo.ehviewer.ui.legacy.HandlerDrawable -import com.hippo.ehviewer.ui.legacy.STRATEGY_MIN_SIZE import com.hippo.ehviewer.ui.legacy.ViewTransition import com.hippo.ehviewer.ui.main.DEFAULT_ASPECT import com.hippo.ehviewer.ui.main.requestOf @@ -296,8 +295,6 @@ class DownloadsScene : SearchBarScene() { }, ), ) - layoutManager.setStrategy(STRATEGY_MIN_SIZE) - layoutManager.supportsPredictiveItemAnimations = false recyclerView.layoutManager = layoutManager val interval = resources.getDimensionPixelOffset(R.dimen.gallery_list_interval) val decoration = object : RecyclerView.ItemDecoration() { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesFragment.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesFragment.kt index 9b5a86c05..60ad0a348 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesFragment.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesFragment.kt @@ -350,8 +350,9 @@ fun FavouritesScreen(navigator: NavController) { } } else { val gridInterval = dimensionResource(R.dimen.gallery_grid_interval) + val thumbWidth by Settings.thumbSizeDp.collectAsState() FastScrollLazyVerticalStaggeredGrid( - columns = StaggeredGridCells.Adaptive(Settings.thumbSizeDp.dp), + columns = StaggeredGridCells.Adaptive(thumbWidth.dp), modifier = combinedModifier, verticalItemSpacing = gridInterval, horizontalArrangement = Arrangement.spacedBy(gridInterval), diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index cc905a4e8..57759a973 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -126,6 +126,7 @@ import com.hippo.ehviewer.client.parser.HomeParser import com.hippo.ehviewer.client.parser.TorrentResult import com.hippo.ehviewer.client.parser.format import com.hippo.ehviewer.coil.justDownload +import com.hippo.ehviewer.collectAsState import com.hippo.ehviewer.dao.DownloadInfo import com.hippo.ehviewer.dao.Filter import com.hippo.ehviewer.dao.FilterMode @@ -144,7 +145,6 @@ import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder import com.hippo.ehviewer.ui.legacy.CheckBoxDialogBuilder import com.hippo.ehviewer.ui.legacy.CoilImageGetter import com.hippo.ehviewer.ui.legacy.GalleryRatingBar.OnUserRateListener -import com.hippo.ehviewer.ui.legacy.calculateSuitableSpanCount import com.hippo.ehviewer.ui.main.EhPreviewItem import com.hippo.ehviewer.ui.main.GalleryCommentCard import com.hippo.ehviewer.ui.main.GalleryDetailErrorTip @@ -912,7 +912,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: NavController) ) { val galleryDetail = galleryInfo.asGalleryDetail() val windowSizeClass = calculateWindowSizeClass(activity) - val columnCount = calculateSuitableSpanCount() + val thumbWidth by Settings.thumbSizeDp.collectAsState() val readText = stringResource(R.string.read) val downloadText = stringResource(R.string.download) var readButtonText by rememberSaveable { mutableStateOf(readText) } @@ -1023,7 +1023,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: NavController) when (windowSizeClass.widthSizeClass) { WindowWidthSizeClass.Medium, WindowWidthSizeClass.Compact -> LazyVerticalGrid( - columns = GridCells.Fixed(columnCount), + columns = GridCells.Adaptive(thumbWidth.dp), contentPadding = contentPadding, modifier = modifier.padding(horizontal = keylineMargin), horizontalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.strip_item_padding)), @@ -1074,7 +1074,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: NavController) } WindowWidthSizeClass.Expanded -> LazyVerticalGrid( - columns = GridCells.Fixed(columnCount), + columns = GridCells.Adaptive(thumbWidth.dp), contentPadding = contentPadding, modifier = modifier.padding(horizontal = keylineMargin), horizontalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.strip_item_padding)), diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryListFragment.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryListFragment.kt index 7bfe84efa..feb1057fd 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryListFragment.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryListFragment.kt @@ -882,8 +882,9 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: NavController) { } } else { val gridInterval = dimensionResource(R.dimen.gallery_grid_interval) + val thumbWidth by Settings.thumbSizeDp.collectAsState() FastScrollLazyVerticalStaggeredGrid( - columns = StaggeredGridCells.Adaptive(Settings.thumbSizeDp.dp), + columns = StaggeredGridCells.Adaptive(thumbWidth.dp), modifier = combinedModifier, state = gridState, verticalItemSpacing = gridInterval, diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryPreviewFragment.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryPreviewFragment.kt index 4f79164b5..523182e56 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryPreviewFragment.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryPreviewFragment.kt @@ -54,9 +54,9 @@ import com.hippo.ehviewer.client.EhUrl import com.hippo.ehviewer.client.data.GalleryDetail import com.hippo.ehviewer.client.data.GalleryPreview import com.hippo.ehviewer.coil.justDownload +import com.hippo.ehviewer.collectAsState import com.hippo.ehviewer.ktbuilder.imageRequest import com.hippo.ehviewer.ui.MainActivity -import com.hippo.ehviewer.ui.legacy.calculateSuitableSpanCount import com.hippo.ehviewer.ui.main.EhPreviewItem import com.hippo.ehviewer.ui.navToReader import com.hippo.ehviewer.ui.tools.FastScrollLazyVerticalGrid @@ -74,7 +74,6 @@ fun GalleryPreviewScreen(galleryDetail: GalleryDetail, toNextPageArg: Boolean, n val context = LocalContext.current fun onPreviewClick(index: Int) = context.navToReader(galleryDetail.galleryInfo, index) val scrollBehaviour = TopAppBarDefaults.pinnedScrollBehavior() - val columnCount = calculateSuitableSpanCount() val state = rememberLazyGridState() val coroutineScope = rememberCoroutineScope { Dispatchers.IO } val pages = galleryDetail.pages @@ -136,8 +135,9 @@ fun GalleryPreviewScreen(galleryDetail: GalleryDetail, toNextPageArg: Boolean, n ) }, ) { paddingValues -> + val thumbWidth by Settings.thumbSizeDp.collectAsState() FastScrollLazyVerticalGrid( - columns = GridCells.Fixed(columnCount), + columns = GridCells.Adaptive(thumbWidth.dp), modifier = Modifier.nestedScroll(scrollBehaviour.nestedScrollConnection).padding(horizontal = dimensionResource(id = R.dimen.gallery_list_margin_h)), state = state, contentPadding = paddingValues, diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt index 8b6737904..5d6c8b1be 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt @@ -257,7 +257,7 @@ fun EhScreen(navigator: DestinationsNavigator) { minValue = 80, step = 7, title = stringResource(id = R.string.settings_eh_thumb_size), - value = Settings::thumbSizeDp, + value = Settings.thumbSizeDp::value, ) val thumbResolution = Settings::thumbResolution.observed val summary2 = stringResource(id = R.string.settings_eh_thumb_resolution_summary, stringArrayResource(id = R.array.thumb_resolution_entries)[thumbResolution.value])