diff --git a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt index ab8348e0c..e47764c7c 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt @@ -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) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpeedTracker.kt b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpeedTracker.kt index 23b2cd156..f02917b4d 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpeedTracker.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpeedTracker.kt @@ -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 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 timeoutBySpeed( } } +fun speedLevelToSpeed(level: Int) = 2f.pow(level).roundToInt() + inline fun MutableObjectIntMap.fold(initial: R, operation: (acc: R, T, Int) -> R): R { var accumulator = initial forEach { key, value -> accumulator = operation(accumulator, key, value) } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt index 4ed3682ac..2bd019042 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt @@ -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, diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt index 32cb157de..e142b8aaf 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt @@ -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, 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, 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 diff --git a/app/src/main/kotlin/com/jamal/composeprefs3/ui/prefs/SliderPref.kt b/app/src/main/kotlin/com/jamal/composeprefs3/ui/prefs/SliderPref.kt index 9e05e3621..b2c05e11e 100644 --- a/app/src/main/kotlin/com/jamal/composeprefs3/ui/prefs/SliderPref.kt +++ b/app/src/main/kotlin/com/jamal/composeprefs3/ui/prefs/SliderPref.kt @@ -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), )