Jelajahi Sumber

| PageRefreshLayout.scope 错误

drake 5 tahun lalu
induk
melakukan
f61b287a47

+ 1 - 1
README.md

@@ -77,7 +77,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
 // 支持自动下拉刷新和缺省页的, 可选
 implementation 'com.github.liangjingkanji:BRV:1.1.7'
 
-implementation 'com.github.liangjingkanji:Net:2.0.0'
+implementation 'com.github.liangjingkanji:Net:2.0.1'
 ```
 
 

+ 5 - 4
net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt

@@ -12,17 +12,17 @@ import com.drake.brv.BindingAdapter
 import com.drake.brv.PageRefreshLayout
 import com.drake.net.NetConfig
 import com.scwang.smart.refresh.layout.constant.RefreshState
-import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.cancel
 
 @Suppress("unused", "MemberVisibilityCanBePrivate", "NAME_SHADOWING")
 class PageCoroutineScope(
-    val page: PageRefreshLayout,
-    val block: suspend CoroutineScope.(PageCoroutineScope) -> Unit
+    val page: PageRefreshLayout
 ) : AndroidScope() {
 
     val index get() = page.index
 
+    private var manual = false
+
     init {
         page.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
             override fun onViewAttachedToWindow(v: View) {
@@ -44,7 +44,7 @@ class PageCoroutineScope(
 
     override fun finally(e: Throwable?) {
         super.finally(e)
-        if (e == null) {
+        if (e == null && !manual) {
             if (page.stateEnabled) page.showContent() else page.finish()
         }
     }
@@ -63,6 +63,7 @@ class PageCoroutineScope(
         hasMore: BindingAdapter.() -> Boolean = { false }
     ) {
         page.addData(data, bindingAdapter, hasMore)
+        manual = true
     }
 
     /**

+ 5 - 3
net/src/main/java/com/drake/net/utils/ScopeUtils.kt

@@ -132,9 +132,11 @@ fun SmartRefreshLayout.scopeRefresh(
  *
  * 布局被销毁或者界面关闭作用域被取消
  */
-fun PageRefreshLayout.scope(block: suspend CoroutineScope.() -> Unit): AndroidScope {
-    val scope = StateCoroutineScope(this)
-    scope.launch(block = block)
+fun PageRefreshLayout.scope(block: suspend CoroutineScope.(PageCoroutineScope) -> Unit): AndroidScope {
+    val scope = PageCoroutineScope(this)
+    scope.launch {
+        block(scope)
+    }
     return scope
 }
 

+ 1 - 0
sample/src/main/java/com/drake/net/sample/MainActivity.kt

@@ -25,6 +25,7 @@ class MainActivity : AppCompatActivity() {
 
             textView.text = data.await()
         }
+
     }
 
 }