Skip to content

Commit

Permalink
fix: #233 支持最新的lifecycle-runtime-ktx
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Nov 29, 2024
1 parent fb0bb1c commit 86a0777
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions net/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 6 additions & 4 deletions net/src/main/java/androidx/lifecycle/Scope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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)
}
14 changes: 12 additions & 2 deletions net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions net/src/main/java/com/drake/net/scope/ViewCoroutineScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
Expand Down

0 comments on commit 86a0777

Please sign in to comment.