浏览代码

fix: #233 支持最新的lifecycle-runtime-ktx

drake 5 月之前
父节点
当前提交
86a0777e27

+ 1 - 1
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"

+ 1 - 0
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"
 

+ 6 - 4
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)
+}

+ 12 - 2
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)
+        }
     }
 
     /**

+ 2 - 2
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()
             }

+ 2 - 2
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()
             }

+ 2 - 2
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()
             }