Browse Source

轮询器支持绑定ViewModel自动取消

drake 3 years ago
parent
commit
7e09a4efad

+ 6 - 0
net/src/main/java/androidx/lifecycle/Scope.kt

@@ -18,6 +18,7 @@ package androidx.lifecycle
 
 import com.drake.net.scope.AndroidScope
 import com.drake.net.scope.NetCoroutineScope
+import com.drake.net.time.Interval
 import kotlinx.coroutines.CoroutineDispatcher
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
@@ -44,3 +45,8 @@ fun ViewModel.scopeNetLife(
     val scope = NetCoroutineScope(dispatcher = dispatcher).launch(block)
     return setTagIfAbsent(scope.toString(), scope)
 }
+
+/** 轮询器根据ViewModel生命周期自动取消 */
+fun Interval.life(viewModel: ViewModel) = apply {
+    viewModel.setTagIfAbsent(toString(), this)
+}

+ 12 - 10
net/src/main/java/com/drake/net/time/Interval.kt

@@ -28,6 +28,7 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
 import kotlinx.coroutines.channels.ReceiveChannel
 import kotlinx.coroutines.channels.TickerMode
 import kotlinx.coroutines.channels.ticker
+import java.io.Closeable
 import java.io.Serializable
 import java.util.concurrent.TimeUnit
 
@@ -59,7 +60,7 @@ open class Interval(
     private val unit: TimeUnit,
     private val start: Long = 0,
     private val initialDelay: Long = 0
-) : Serializable {
+) : Serializable, Closeable {
 
     constructor(
         period: Long,
@@ -88,9 +89,8 @@ open class Interval(
      * 每次轮循器计时都会调用该回调函数
      * 轮循器完成时会同时触发回调[block]和[finish]
      */
-    fun subscribe(block: (Long) -> Unit): Interval {
+    fun subscribe(block: (Long) -> Unit) = apply {
         subscribeList.add(block)
-        return this
     }
 
     /**
@@ -98,9 +98,8 @@ open class Interval(
      * @see stop 执行该函数也会回调finish
      * @see cancel 使用该函数取消轮询器不会回调finish
      */
-    fun finish(block: (Long) -> Unit): Interval {
+    fun finish(block: (Long) -> Unit) = apply {
         finishList.add(block)
-        return this
     }
 
     // </editor-fold>
@@ -111,8 +110,8 @@ open class Interval(
      * 开始
      * 如果当前为暂停状态将重新开始轮询
      */
-    fun start() {
-        if (state == IntervalStatus.STATE_ACTIVE) return
+    fun start() = apply {
+        if (state == IntervalStatus.STATE_ACTIVE) return this
         state = IntervalStatus.STATE_ACTIVE
         count = start
         launch()
@@ -195,13 +194,12 @@ open class Interval(
     fun life(
         lifecycleOwner: LifecycleOwner,
         lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP
-    ): Interval {
+    ) = apply {
         lifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
             override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
-                if (lifeEvent == event) scope?.cancel()
+                if (lifeEvent == event) cancel()
             }
         })
-        return this
     }
     //</editor-fold>
 
@@ -229,5 +227,9 @@ open class Interval(
             }
         }
     }
+
+    override fun close() {
+        cancel()
+    }
 }