From 86a0777e275eceea9fec9befffd9a439c9551576 Mon Sep 17 00:00:00 2001 From: drake Date: Sat, 30 Nov 2024 00:55:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#233=20=E6=94=AF=E6=8C=81=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=9A=84lifecycle-runtime-ktx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- net/build.gradle | 1 + net/src/main/java/androidx/lifecycle/Scope.kt | 10 ++++++---- .../java/com/drake/net/scope/NetCoroutineScope.kt | 14 ++++++++++++-- .../java/com/drake/net/scope/PageCoroutineScope.kt | 4 ++-- .../com/drake/net/scope/StateCoroutineScope.kt | 4 ++-- .../java/com/drake/net/scope/ViewCoroutineScope.kt | 4 ++-- 7 files changed, 26 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index 3dadf9405..a4f75fbbd 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ buildscript { ext { - kotlin_version = '1.7.10' + kotlin_version = '1.8.10' brv_version = '1.5.2' coroutine_version = '1.6.1' okhttp_version = "4.10.0" diff --git a/net/build.gradle b/net/build.gradle index 1abf5968d..cb34d2a1c 100644 --- a/net/build.gradle +++ b/net/build.gradle @@ -30,6 +30,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.startup:startup-runtime:1.0.0' implementation 'androidx.documentfile:documentfile:1.0.1' + implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2" compileOnly "com.squareup.okhttp3:okhttp:$okhttp_version" diff --git a/net/src/main/java/androidx/lifecycle/Scope.kt b/net/src/main/java/androidx/lifecycle/Scope.kt index 4dec188e1..8639908dc 100644 --- a/net/src/main/java/androidx/lifecycle/Scope.kt +++ b/net/src/main/java/androidx/lifecycle/Scope.kt @@ -39,7 +39,8 @@ fun ViewModel.scopeLife( block: suspend CoroutineScope.() -> Unit ): AndroidScope { val scope = AndroidScope(dispatcher = dispatcher).launch(block) - return setTagIfAbsent(scope.toString(), scope) + addCloseable(scope) + return scope } /** @@ -51,10 +52,11 @@ fun ViewModel.scopeNetLife( block: suspend CoroutineScope.() -> Unit ): NetCoroutineScope { val scope = NetCoroutineScope(dispatcher = dispatcher).launch(block) - return setTagIfAbsent(scope.toString(), scope) + addCloseable(scope) + return scope } /** 轮询器根据ViewModel生命周期自动取消 */ fun Interval.life(viewModel: ViewModel) = apply { - viewModel.setTagIfAbsent(toString(), this) -} + viewModel.addCloseable(this) +} \ No newline at end of file diff --git a/net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt b/net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt index 1bb425998..836d4d7eb 100644 --- a/net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt +++ b/net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt @@ -28,7 +28,12 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import com.drake.net.Net import com.drake.net.NetConfig -import kotlinx.coroutines.* +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.supervisorScope import kotlin.coroutines.EmptyCoroutineContext @@ -97,7 +102,12 @@ open class NetCoroutineScope( } override fun catch(e: Throwable) { - catch?.invoke(this@NetCoroutineScope, e) ?: if (!previewBreakError) handleError(e) + val catch = catch + if (catch != null) { + catch.invoke(this@NetCoroutineScope, e) + } else if (!previewBreakError) { + handleError(e) + } } /** diff --git a/net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt b/net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt index ae2312aee..075864fa4 100644 --- a/net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt +++ b/net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt @@ -27,7 +27,7 @@ package com.drake.net.scope import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.ViewTreeLifecycleOwner +import androidx.lifecycle.findViewTreeLifecycleOwner import com.drake.brv.PageRefreshLayout import com.drake.net.NetConfig import kotlinx.coroutines.CancellationException @@ -43,7 +43,7 @@ class PageCoroutineScope( val index get() = page.index init { - ViewTreeLifecycleOwner.get(page)?.lifecycle?.addObserver(object : LifecycleEventObserver { + page.findViewTreeLifecycleOwner()?.lifecycle?.addObserver(object : LifecycleEventObserver { override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { if (event == Lifecycle.Event.ON_DESTROY) cancel() } diff --git a/net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt b/net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt index 21b56bbe1..ef968bbe0 100644 --- a/net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt +++ b/net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt @@ -27,7 +27,7 @@ package com.drake.net.scope import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.ViewTreeLifecycleOwner +import androidx.lifecycle.findViewTreeLifecycleOwner import com.drake.net.NetConfig import com.drake.statelayout.StateLayout import kotlinx.coroutines.CancellationException @@ -43,7 +43,7 @@ class StateCoroutineScope( ) : NetCoroutineScope(dispatcher = dispatcher) { init { - ViewTreeLifecycleOwner.get(state)?.lifecycle?.addObserver(object : LifecycleEventObserver { + state.findViewTreeLifecycleOwner()?.lifecycle?.addObserver(object : LifecycleEventObserver { override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { if (event == Lifecycle.Event.ON_DESTROY) cancel() } diff --git a/net/src/main/java/com/drake/net/scope/ViewCoroutineScope.kt b/net/src/main/java/com/drake/net/scope/ViewCoroutineScope.kt index 09078f401..726751d3d 100644 --- a/net/src/main/java/com/drake/net/scope/ViewCoroutineScope.kt +++ b/net/src/main/java/com/drake/net/scope/ViewCoroutineScope.kt @@ -28,7 +28,7 @@ import android.view.View import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.ViewTreeLifecycleOwner +import androidx.lifecycle.findViewTreeLifecycleOwner import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers @@ -42,7 +42,7 @@ class ViewCoroutineScope( ) : NetCoroutineScope(dispatcher = dispatcher) { init { - ViewTreeLifecycleOwner.get(view)?.lifecycle?.addObserver(object : LifecycleEventObserver { + view.findViewTreeLifecycleOwner()?.lifecycle?.addObserver(object : LifecycleEventObserver { override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { if (event == Lifecycle.Event.ON_DESTROY) cancel() }