No longer need restart to switch HC
This commit is contained in:
1 parent
69252ae8a4
commit
7e8b0a5800
5 files changed
+39
-6
No files matched your search
@@ -54,6 +54,7 @@ import com.hippo.ehviewer.util.isAtLeastP
|
|||||||
import com.hippo.ehviewer.util.isAtLeastQ
|
import com.hippo.ehviewer.util.isAtLeastQ
|
||||||
import com.hippo.ehviewer.util.isAtLeastS
|
import com.hippo.ehviewer.util.isAtLeastS
|
||||||
import com.hippo.ehviewer.util.isCronetAvailable
|
import com.hippo.ehviewer.util.isCronetAvailable
|
||||||
|
import com.hippo.ehviewer.util.resettableLazy
|
||||||
import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor
|
import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor
|
||||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||||
@@ -193,7 +194,7 @@ class EhApplication : Application(), SingletonImageLoader.Factory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val ktorClient by lazy {
|
var ktorClient by resettableLazy {
|
||||||
if (Settings.enableCronet && isCronetAvailable) {
|
if (Settings.enableCronet && isCronetAvailable) {
|
||||||
HttpClient(CronetEngine) {
|
HttpClient(CronetEngine) {
|
||||||
install(HttpCookies) {
|
install(HttpCookies) {
|
||||||
@@ -217,7 +218,7 @@ class EhApplication : Application(), SingletonImageLoader.Factory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val noRedirectKtorClient by lazy {
|
var noRedirectKtorClient by resettableLazy {
|
||||||
HttpClient(ktorClient.engine) {
|
HttpClient(ktorClient.engine) {
|
||||||
followRedirects = false
|
followRedirects = false
|
||||||
install(HttpCookies) {
|
install(HttpCookies) {
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
|
|||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import com.hippo.ehviewer.BuildConfig
|
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.EhDB
|
||||||
import com.hippo.ehviewer.R
|
import com.hippo.ehviewer.R
|
||||||
import com.hippo.ehviewer.Settings
|
import com.hippo.ehviewer.Settings
|
||||||
@@ -145,6 +147,11 @@ fun AdvancedScreen(navigator: DestinationsNavigator) {
|
|||||||
summary = if (enableCronet) "Cronet" else "OkHttp",
|
summary = if (enableCronet) "Cronet" else "OkHttp",
|
||||||
) {
|
) {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
|
fun reset() {
|
||||||
|
ktorClient = ktorClient.apply { close() }
|
||||||
|
noRedirectKtorClient = noRedirectKtorClient.apply { close() }
|
||||||
|
}
|
||||||
|
|
||||||
dialogState.awaitPermissionOrCancel(
|
dialogState.awaitPermissionOrCancel(
|
||||||
title = R.string.settings_advanced_http_engine,
|
title = R.string.settings_advanced_http_engine,
|
||||||
showCancelButton = false,
|
showCancelButton = false,
|
||||||
@@ -153,14 +160,20 @@ fun AdvancedScreen(navigator: DestinationsNavigator) {
|
|||||||
SingleChoiceSegmentedButtonRow(modifier = Modifier.align(Alignment.Center)) {
|
SingleChoiceSegmentedButtonRow(modifier = Modifier.align(Alignment.Center)) {
|
||||||
SegmentedButton(
|
SegmentedButton(
|
||||||
selected = !enableCronet,
|
selected = !enableCronet,
|
||||||
onClick = { enableCronet = false },
|
onClick = {
|
||||||
|
enableCronet = false
|
||||||
|
reset()
|
||||||
|
},
|
||||||
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2),
|
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2),
|
||||||
) {
|
) {
|
||||||
Text("OkHttp")
|
Text("OkHttp")
|
||||||
}
|
}
|
||||||
SegmentedButton(
|
SegmentedButton(
|
||||||
selected = enableCronet,
|
selected = enableCronet,
|
||||||
onClick = { enableCronet = true },
|
onClick = {
|
||||||
|
enableCronet = true
|
||||||
|
reset()
|
||||||
|
},
|
||||||
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2),
|
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2),
|
||||||
enabled = isCronetAvailable,
|
enabled = isCronetAvailable,
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -6,11 +6,30 @@ import kotlin.reflect.KMutableProperty0
|
|||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
|
fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
|
||||||
|
fun <T> resettableLazy(initializer: () -> T) = ResettableLazy(initializer)
|
||||||
|
|
||||||
interface Lazy<T> {
|
interface Lazy<T> {
|
||||||
var value: T
|
var value: T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ResettableLazy<T>(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 <T> lazyMut(init: () -> KMutableProperty0<T>) = object : Lazy<T> {
|
fun <T> lazyMut(init: () -> KMutableProperty0<T>) = object : Lazy<T> {
|
||||||
private val lazy by lazy { init() }
|
private val lazy by lazy { init() }
|
||||||
override var value
|
override var value
|
||||||
|
|||||||
@@ -365,7 +365,7 @@
|
|||||||
<string name="settings_advanced_dump_logcat_to">已保存日志至 %s</string>
|
<string name="settings_advanced_dump_logcat_to">已保存日志至 %s</string>
|
||||||
<string name="settings_advanced_read_cache_size">阅读缓存大小</string>
|
<string name="settings_advanced_read_cache_size">阅读缓存大小</string>
|
||||||
<string name="settings_advanced_app_language_title">App 界面语言</string>
|
<string name="settings_advanced_app_language_title">App 界面语言</string>
|
||||||
<string name="settings_advanced_http_engine">Http 引擎(重启生效)</string>
|
<string name="settings_advanced_http_engine">Http 引擎</string>
|
||||||
<string name="settings_advanced_touch_slop">[实验性] 触摸溢出系数</string>
|
<string name="settings_advanced_touch_slop">[实验性] 触摸溢出系数</string>
|
||||||
<string name="settings_advanced_touch_slop_summary">增大此项将降低滑动手势敏感度</string>
|
<string name="settings_advanced_touch_slop_summary">增大此项将降低滑动手势敏感度</string>
|
||||||
<string name="settings_advanced_export_data">导出数据</string>
|
<string name="settings_advanced_export_data">导出数据</string>
|
||||||
|
|||||||
@@ -409,7 +409,7 @@
|
|||||||
<string name="settings_advanced_dump_logcat_to">Logcat dumped to %s</string>
|
<string name="settings_advanced_dump_logcat_to">Logcat dumped to %s</string>
|
||||||
<string name="settings_advanced_read_cache_size">Read cache size</string>
|
<string name="settings_advanced_read_cache_size">Read cache size</string>
|
||||||
<string name="settings_advanced_app_language_title">App language</string>
|
<string name="settings_advanced_app_language_title">App language</string>
|
||||||
<string name="settings_advanced_http_engine">Http Engine (Restart required)</string>
|
<string name="settings_advanced_http_engine">Http Engine</string>
|
||||||
<string name="settings_advanced_touch_slop">[Experimental] Touch slop factor</string>
|
<string name="settings_advanced_touch_slop">[Experimental] Touch slop factor</string>
|
||||||
<string name="settings_advanced_touch_slop_summary">Increasing this will reduce swipe gesture sensitivity</string>
|
<string name="settings_advanced_touch_slop_summary">Increasing this will reduce swipe gesture sensitivity</string>
|
||||||
<string name="settings_advanced_export_data">Export data</string>
|
<string name="settings_advanced_export_data">Export data</string>
|
||||||
|
|||||||
Reference in new issue
Block a user