Make timeoutSpeed pow of 2 (#2060)

* IntSliderPreference support display transforms

* Make timeoutSpeed pow of 2

* Fix conversion

* Fix conversion

---------

Co-authored-by: Dude so hot <djohn@fbi.gov>
This commit is contained in:
renovate[bot]andDude so hot authored and GitHub committed 2025-01-03 11:45:21 +08:00
1 parent 0561a9bcf6
commit b7c5c15280
5 files changed
+15 -8

No files matched your search

@@ -142,7 +142,7 @@ object Settings : DataStorePreferences(null) {
var multiThreadDownload by intPref("download_thread_2", 3)
var preloadImage by intPref("preload_image_2", 5)
var connTimeout by intPref("conn_timeout", 10)
var timeoutSpeed by intPref("timeout_speed", 128)
var timeoutSpeed by intPref("timeout_speed_level", 6)
var theme by intPref("theme_2", -1).observed { updateWhenThemeChanges() }
var readCacheSize by intPref("read_cache_size_2", 640)
var launchPage by intPref("launch_page_2", 0)
@@ -14,6 +14,8 @@ import io.ktor.client.plugins.timeout
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.HttpStatement
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
@@ -81,7 +83,7 @@ suspend inline fun <R> timeoutBySpeed(
watchdog.cancel()
resp.status.ensureSuccess()
val speedWatchdog = launch {
val timeoutSpeed = Settings.timeoutSpeed * 1024.0
val timeoutSpeed = speedLevelToSpeed(Settings.timeoutSpeed) * 1024.0
tracker.speedFlow(1.seconds).collect { speed ->
if (timeoutSpeed != 0.0 && speed < timeoutSpeed) {
throw LowSpeedException(url, speed.toLong())
@@ -96,6 +98,8 @@ suspend inline fun <R> timeoutBySpeed(
}
}
fun speedLevelToSpeed(level: Int) = 2f.pow(level).roundToInt()
inline fun <T, R> MutableObjectIntMap<T>.fold(initial: R, operation: (acc: R, T, Int) -> R): R {
var accumulator = initial
forEach { key, value -> accumulator = operation(accumulator, key, value) }
@@ -50,6 +50,7 @@ import com.hippo.ehviewer.spider.SpiderDen
import com.hippo.ehviewer.spider.SpiderQueen.Companion.SPIDER_INFO_FILENAME
import com.hippo.ehviewer.spider.readComicInfo
import com.hippo.ehviewer.spider.readCompatFromPath
import com.hippo.ehviewer.spider.speedLevelToSpeed
import com.hippo.ehviewer.ui.composing
import com.hippo.ehviewer.ui.keepNoMediaFileStatus
import com.hippo.ehviewer.ui.tools.observed
@@ -187,11 +188,12 @@ fun AnimatedVisibilityScope.DownloadScreen(navigator: DestinationsNavigator) = c
value = Settings::connTimeout,
)
IntSliderPreference(
maxValue = 1024,
minValue = 0,
step = 7,
maxValue = 10,
minValue = 4,
step = 5,
title = settingsDownloadTimeoutSpeed,
value = Settings::timeoutSpeed,
display = ::speedLevelToSpeed,
)
SwitchPreference(
title = settingsDownloadHAtHBlacklist,
@@ -102,10 +102,10 @@ fun SwitchPreference(title: String, summary: String? = null, value: KMutableProp
}
@Composable
fun IntSliderPreference(maxValue: Int, minValue: Int = 0, step: Int = maxValue - minValue - 1, showTicks: Boolean = true, title: String, summary: String? = null, value: KMutableProperty0<Int>, enabled: Boolean = true) {
fun IntSliderPreference(maxValue: Int, minValue: Int = 0, step: Int = maxValue - minValue - 1, showTicks: Boolean = true, title: String, summary: String? = null, value: KMutableProperty0<Int>, enabled: Boolean = true, display: (Int) -> Int = { it }) {
var v by remember { mutableIntStateOf(value.get()) }
fun set(float: Float) = value.set(float.roundToInt().also { v = it })
SliderPref(title = title, summary = summary, defaultValue = v.toFloat(), onValueChangeFinished = ::set, valueRange = minValue.toFloat()..maxValue.toFloat(), showValue = true, steps = step, showTicks = showTicks, enabled = enabled)
SliderPref(title = title, summary = summary, defaultValue = v.toFloat(), onValueChangeFinished = ::set, valueRange = minValue.toFloat()..maxValue.toFloat(), showValue = true, steps = step, showTicks = showTicks, enabled = enabled, display = { display(it.roundToInt()).toFloat() })
}
@Composable
@@ -34,6 +34,7 @@ fun SliderPref(
textColor: Color = MaterialTheme.colorScheme.onBackground,
enabled: Boolean = true,
leadingIcon: @Composable (() -> Unit)? = null,
display: (Float) -> Float = { it },
) {
var value by remember { mutableFloatStateOf(defaultValue) }
Column(
@@ -72,7 +73,7 @@ fun SliderPref(
)
if (showValue) {
RollingNumber(
number = value.roundToInt(),
number = display(value).roundToInt(),
color = textColor,
modifier = Modifier.weight(0.5f),
)