From 7e8b0a58005d03d2b83350da74c0d4595a76afd2 Mon Sep 17 00:00:00 2001 From: GG Bond Date: Sat, 23 Dec 2023 15:03:04 +0800 Subject: [PATCH] No longer need restart to switch HC --- .../com/hippo/ehviewer/EhApplication.kt | 5 +++-- .../ehviewer/ui/settings/AdvancedScreen.kt | 17 +++++++++++++++-- .../com/hippo/ehviewer/util/LazyDelegate.kt | 19 +++++++++++++++++++ app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt index fd6b7cda8..6e188d0e1 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt @@ -54,6 +54,7 @@ import com.hippo.ehviewer.util.isAtLeastP import com.hippo.ehviewer.util.isAtLeastQ import com.hippo.ehviewer.util.isAtLeastS import com.hippo.ehviewer.util.isCronetAvailable +import com.hippo.ehviewer.util.resettableLazy import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext @@ -193,7 +194,7 @@ class EhApplication : Application(), SingletonImageLoader.Factory { } companion object { - val ktorClient by lazy { + var ktorClient by resettableLazy { if (Settings.enableCronet && isCronetAvailable) { HttpClient(CronetEngine) { install(HttpCookies) { @@ -217,7 +218,7 @@ class EhApplication : Application(), SingletonImageLoader.Factory { } } - val noRedirectKtorClient by lazy { + var noRedirectKtorClient by resettableLazy { HttpClient(ktorClient.engine) { followRedirects = false install(HttpCookies) { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt index f05b3119b..efdcd69d6 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AdvancedScreen.kt @@ -36,6 +36,8 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import com.hippo.ehviewer.BuildConfig +import com.hippo.ehviewer.EhApplication.Companion.ktorClient +import com.hippo.ehviewer.EhApplication.Companion.noRedirectKtorClient import com.hippo.ehviewer.EhDB import com.hippo.ehviewer.R import com.hippo.ehviewer.Settings @@ -145,6 +147,11 @@ fun AdvancedScreen(navigator: DestinationsNavigator) { summary = if (enableCronet) "Cronet" else "OkHttp", ) { coroutineScope.launch { + fun reset() { + ktorClient = ktorClient.apply { close() } + noRedirectKtorClient = noRedirectKtorClient.apply { close() } + } + dialogState.awaitPermissionOrCancel( title = R.string.settings_advanced_http_engine, showCancelButton = false, @@ -153,14 +160,20 @@ fun AdvancedScreen(navigator: DestinationsNavigator) { SingleChoiceSegmentedButtonRow(modifier = Modifier.align(Alignment.Center)) { SegmentedButton( selected = !enableCronet, - onClick = { enableCronet = false }, + onClick = { + enableCronet = false + reset() + }, shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2), ) { Text("OkHttp") } SegmentedButton( selected = enableCronet, - onClick = { enableCronet = true }, + onClick = { + enableCronet = true + reset() + }, shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2), enabled = isCronetAvailable, ) { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/LazyDelegate.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/LazyDelegate.kt index 0c6cc9d32..8e53bcc03 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/LazyDelegate.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/LazyDelegate.kt @@ -6,11 +6,30 @@ import kotlin.reflect.KMutableProperty0 import kotlin.reflect.KProperty fun unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer) +fun resettableLazy(initializer: () -> T) = ResettableLazy(initializer) interface Lazy { var value: T } +class ResettableLazy(private val initializer: () -> T) { + + private var value: T? = null + + operator fun getValue(thisRef: Any?, prop: KProperty<*>): T { + return value ?: initializer().apply { value = this } + } + + operator fun setValue(thisRef: Any?, prop: KProperty<*>, value: T) { + check(this.value == value) { "New values aren't accepted to reset this delegated property" } + invalidate() + } + + private fun invalidate() { + value = null + } +} + fun lazyMut(init: () -> KMutableProperty0) = object : Lazy { private val lazy by lazy { init() } override var value diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 2b7dfb301..4a0ee804c 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -365,7 +365,7 @@ 已保存日志至 %s 阅读缓存大小 App 界面语言 - Http 引擎(重启生效) + Http 引擎 [实验性] 触摸溢出系数 增大此项将降低滑动手势敏感度 导出数据 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0154cac8d..735c905a0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -409,7 +409,7 @@ Logcat dumped to %s Read cache size App language - Http Engine (Restart required) + Http Engine [Experimental] Touch slop factor Increasing this will reduce swipe gesture sensitivity Export data