Browse Source

+ brv 1.2.9
- RefreshCoroutineScope

drake 5 years ago
parent
commit
64be264b4d

+ 1 - 1
build.gradle

@@ -5,7 +5,7 @@ buildscript {
 
     ext {
         kotlin_version = '1.3.61'
-        brv_version = '1.2.7'
+        brv_version = '1.2.9'
         coroutine_version = '1.3.0'
         glide_version = '4.9.0'
     }

+ 10 - 9
net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt

@@ -24,14 +24,14 @@ import kotlin.coroutines.EmptyCoroutineContext
 @Suppress("unused", "MemberVisibilityCanBePrivate", "NAME_SHADOWING")
 open class NetCoroutineScope() : AndroidScope() {
 
-    protected var readCache = true
+    protected var isReadCache = true
     protected var onCache: (suspend CoroutineScope.() -> Unit)? = null
 
-    protected var cacheSucceed = false
+    protected var readCacheSucceed = false
         get() = if (onCache != null) field else false
 
     protected var error = true
-        get() = if (cacheSucceed) field else true
+        get() = if (readCacheSucceed) field else true
 
     var animate: Boolean = false
 
@@ -53,18 +53,17 @@ open class NetCoroutineScope() : AndroidScope() {
     ): NetCoroutineScope {
         launch(EmptyCoroutineContext) {
             start()
-            if (onCache != null && readCache) {
+            if (onCache != null && isReadCache) {
                 supervisorScope {
-                    cacheSucceed = try {
+                    readCacheSucceed = try {
                         onCache?.invoke(this)
                         true
                     } catch (e: Exception) {
                         false
                     }
-                    readCache(cacheSucceed)
+                    readCache(readCacheSucceed)
                 }
             }
-
             block()
         }.invokeOnCompletion {
             finally(it)
@@ -73,9 +72,11 @@ open class NetCoroutineScope() : AndroidScope() {
         return this
     }
 
-    protected open fun readCache(succeed: Boolean) {
+    /**
+     * 读取缓存回调
+     */
+    protected open fun readCache(succeed: Boolean) {}
 
-    }
 
     override fun handleError(e: Throwable) {
         NetConfig.onError(e)

+ 7 - 16
net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt

@@ -10,7 +10,7 @@ package com.drake.net.scope
 import android.view.View
 import com.drake.brv.PageRefreshLayout
 import com.drake.net.NetConfig
-import com.drake.net.R
+import kotlinx.coroutines.CancellationException
 import kotlinx.coroutines.cancel
 
 @Suppress("unused", "MemberVisibilityCanBePrivate", "NAME_SHADOWING")
@@ -33,11 +33,7 @@ class PageCoroutineScope(
     }
 
     override fun start() {
-        (page.getTag(R.id.cache_succeed) as? Boolean)?.let {
-            readCache = false
-            cacheSucceed = it
-        }
-
+        isReadCache = !page.loaded
         page.trigger()
     }
 
@@ -45,29 +41,24 @@ class PageCoroutineScope(
         if (succeed && !animate) {
             page.showContent()
         }
-        page.setTag(R.id.cache_succeed, succeed)
+        page.loaded = succeed
     }
 
     override fun catch(e: Throwable) {
         super.catch(e)
-
-        if (cacheSucceed) {
-            page.finish(false)
-        } else {
-            page.showError(e)
-        }
+        page.showError(e)
     }
 
     override fun finally(e: Throwable?) {
         super.finally(e)
-        if (e == null) {
+        if (e == null || e is CancellationException) {
             page.showContent()
-            page.trigger()
         }
+        page.trigger()
     }
 
     override fun handleError(e: Throwable) {
-        if (cacheSucceed) {
+        if (page.loaded || !page.stateEnabled) {
             NetConfig.onError(e)
         } else {
             NetConfig.onStateError(e, page)

+ 0 - 61
net/src/main/java/com/drake/net/scope/RefreshCoroutineScope.kt

@@ -1,61 +0,0 @@
-/*
- * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
- * Project:Net
- * Author:Drake
- * Date:12/20/19 2:20 PM
- */
-
-package com.drake.net.scope
-
-import android.view.View
-import com.drake.net.NetConfig
-import com.drake.net.R
-import com.scwang.smart.refresh.layout.SmartRefreshLayout
-import kotlinx.coroutines.cancel
-
-/**
- * 自动结束下拉刷新 协程作用域
- */
-class RefreshCoroutineScope(
-    val refresh: SmartRefreshLayout,
-    loadMore: Boolean = false
-) : NetCoroutineScope() {
-
-    init {
-        refresh.setEnableLoadMore(loadMore)
-        refresh.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
-            override fun onViewAttachedToWindow(v: View) {
-
-            }
-
-            override fun onViewDetachedFromWindow(v: View) {
-                cancel()
-            }
-        })
-    }
-
-    override fun start() {
-        readCache = refresh.getTag(R.id.cache_succeed) as? Boolean ?: true
-    }
-
-    override fun readCache(succeed: Boolean) {
-        refresh.finishRefresh()
-        refresh.setTag(R.id.cache_succeed, false)
-    }
-
-    override fun catch(e: Throwable) {
-        super.catch(e)
-        refresh.finishRefresh(false)
-    }
-
-    override fun finally(e: Throwable?) {
-        super.finally(e)
-        if (e == null) {
-            refresh.finishRefresh(true)
-        }
-    }
-
-    override fun handleError(e: Throwable) {
-        NetConfig.onError(e)
-    }
-}

+ 6 - 26
net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt

@@ -7,13 +7,10 @@
 
 package com.drake.net.scope
 
-import android.app.Activity
 import android.view.View
-import androidx.fragment.app.Fragment
 import com.drake.net.NetConfig
-import com.drake.net.R
 import com.drake.statelayout.StateLayout
-import com.drake.statelayout.state
+import kotlinx.coroutines.CancellationException
 import kotlinx.coroutines.cancel
 
 /**
@@ -21,13 +18,6 @@ import kotlinx.coroutines.cancel
  */
 class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
 
-
-    constructor(view: View) : this(view.state())
-
-    constructor(activity: Activity) : this(activity.state())
-
-    constructor(fragment: Fragment) : this(fragment.state())
-
     init {
         state.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
             override fun onViewAttachedToWindow(v: View?) {
@@ -40,10 +30,7 @@ class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
     }
 
     override fun start() {
-        (state.getTag(R.id.cache_succeed) as? Boolean)?.let {
-            readCache = false
-            cacheSucceed = it
-        }
+        isReadCache = !state.loaded
         state.trigger()
     }
 
@@ -51,30 +38,23 @@ class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
         if (succeed) {
             state.showContent()
         }
-        state.setTag(R.id.cache_succeed, succeed)
     }
 
     override fun catch(e: Throwable) {
         super.catch(e)
-        if (!cacheSucceed) {
-            state.showError(e)
-        }
+        state.showError(e)
     }
 
     override fun handleError(e: Throwable) {
-        if (cacheSucceed) {
-            super.handleError(e)
-        } else {
-            NetConfig.onStateError(e, state)
-        }
+        NetConfig.onStateError(e, state)
     }
 
     override fun finally(e: Throwable?) {
         super.finally(e)
-        if (e == null) {
+        if (e == null || e is CancellationException) {
             state.showContent()
-            state.trigger()
         }
+        state.trigger()
     }
 
 }

+ 0 - 18
net/src/main/java/com/drake/net/utils/ScopeUtils.kt

@@ -15,7 +15,6 @@ import androidx.lifecycle.LifecycleOwner
 import com.drake.brv.PageRefreshLayout
 import com.drake.net.scope.*
 import com.drake.statelayout.StateLayout
-import com.scwang.smart.refresh.layout.SmartRefreshLayout
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.InternalCoroutinesApi
 import kotlinx.coroutines.flow.Flow
@@ -70,23 +69,6 @@ fun StateLayout.scope(block: suspend CoroutineScope.() -> Unit): NetCoroutineSco
     return scope
 }
 
-
-/**
- * SmartRefreshLayout的异步作用域
- *
- * 自动结束下拉刷新
- *
- * 布局被销毁或者界面关闭作用域被取消
- */
-fun SmartRefreshLayout.scopeRefresh(
-    loadMore: Boolean = false,
-    block: suspend CoroutineScope.() -> Unit
-): NetCoroutineScope {
-    val scope = RefreshCoroutineScope(this, loadMore)
-    scope.launch(block)
-    return scope
-}
-
 /**
  * PageRefreshLayout的异步作用域
  *