Stateful thumbSizeDp
This commit is contained in:
1 parent
ef8adb00f4
commit
116dbead4b
8 files changed
+17
-82
No files matched your search
@@ -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<Boolean>
|
||||
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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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)),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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])
|
||||
|
||||
Reference in new issue
Block a user