Просмотр исходного кода

| PageRefreshLayout.scope 错误

drake 5 лет назад
Родитель
Сommit
468657d031

+ 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.1'
+implementation 'com.github.liangjingkanji:Net:2.0.2'
 ```
 
 

+ 0 - 6
net/src/main/java/com/drake/net/scope/AndroidScope.kt

@@ -21,9 +21,6 @@ import kotlin.coroutines.EmptyCoroutineContext
  */
 open class AndroidScope() : CoroutineScope {
 
-    /**
-     * 使用该构造函数创建对象可以跟随[LifecycleOwner]的生命周期自动取消作用域
-     */
     constructor(
         lifecycleOwner: LifecycleOwner,
         lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY
@@ -46,9 +43,6 @@ open class AndroidScope() : CoroutineScope {
     final override val coroutineContext: CoroutineContext =
         Dispatchers.Main.immediate + exceptionHandler + Job()
 
-    /**
-     * 开启一个作用域
-     */
     fun launch(
         block: suspend CoroutineScope.() -> Unit
     ): AndroidScope {

+ 1 - 1
net/src/main/java/com/drake/net/scope/DialogCoroutineScope.kt

@@ -33,7 +33,7 @@ import com.drake.net.R
 class DialogCoroutineScope(
     val activity: FragmentActivity,
     var dialog: Dialog? = null,
-    cancelable: Boolean = true
+    val cancelable: Boolean = true
 ) : AndroidScope(), LifecycleObserver {
 
     init {

+ 11 - 9
net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt

@@ -11,7 +11,6 @@ import android.view.View
 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.cancel
 
 @Suppress("unused", "MemberVisibilityCanBePrivate", "NAME_SHADOWING")
@@ -20,8 +19,7 @@ class PageCoroutineScope(
 ) : AndroidScope() {
 
     val index get() = page.index
-
-    private var manual = false
+    var manual = false
 
     init {
         page.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
@@ -37,9 +35,7 @@ class PageCoroutineScope(
 
     override fun catch(e: Throwable) {
         super.catch(e)
-        if (page.state == RefreshState.Refreshing) {
-            page.showError()
-        } else page.finish(false)
+        if (page.stateEnabled) page.showError() else page.finish(false)
     }
 
     override fun finally(e: Throwable?) {
@@ -55,6 +51,7 @@ class PageCoroutineScope(
 
     /**
      * 自动判断是添加数据还是覆盖数据, 以及数据为空或者NULL时[showEmpty]
+     *
      * @param hasMore 如果不传数据, 默认已加载完全部(建议此时可以关闭[PageRefreshLayout]的加载更多功能)
      */
     fun addData(
@@ -68,29 +65,34 @@ class PageCoroutineScope(
 
     /**
      * 显示空缺省页
-     * 此操作会导致观察者取消订阅
      */
     fun showEmpty() {
         page.showEmpty()
+        manual = true
         cancel()
     }
 
     /**
      * 显示内容缺省页
-     * 默认情况会自动执行不需要手动调用
+     *
+     * 一般情况会自动执行不需要手动调用
      */
     fun showContent() {
         page.showContent()
+        manual = true
         cancel()
     }
 
     /**
      * 结束刷新或者加载
-     * 默认情况会自动执行不需要手动调用
+     *
+     * 一般情况会自动执行不需要手动调用
+     *
      * @param success 是否成功
      */
     fun finish(success: Boolean = true) {
         page.finish(success)
+        manual = true
         cancel()
     }
 

+ 2 - 12
net/src/main/java/com/drake/net/utils/ScopeUtils.kt

@@ -160,16 +160,11 @@ fun scope(block: suspend CoroutineScope.() -> Unit): AndroidScope {
     return AndroidScope().launch(block = block)
 }
 
-/**
- * 跟随当前页面[LifecycleOwner]生命周期的异步作用域
- *
- * @param lifeEvent 自定义取消作用域的生命周期
- */
 fun LifecycleOwner.scopeLife(
     lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     block: suspend CoroutineScope.() -> Unit
 ): AndroidScope {
-    return AndroidScope(this).launch(block = block)
+    return AndroidScope(this, lifeEvent).launch(block = block)
 }
 
 /**
@@ -182,11 +177,7 @@ fun scopeNet(block: suspend CoroutineScope.() -> Unit): AndroidScope {
     return NetCoroutineScope().launch(block = block)
 }
 
-/**
- * 网络请求的异步作用域
- * 自动显示错误信息吐司
- *
- */
+
 fun LifecycleOwner.scopeNetLife(
     lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     block: suspend CoroutineScope.() -> Unit
@@ -198,4 +189,3 @@ fun LifecycleOwner.scopeNetLife(
 
 
 
-

+ 25 - 5
sample/src/main/java/com/drake/net/sample/App.kt

@@ -3,6 +3,8 @@ package com.drake.net.sample
 import android.app.Application
 import com.drake.net.initNet
 import com.drake.statelayout.StateConfig
+import com.yanzhenjie.kalle.simple.SimpleUrlRequest
+import com.yanzhenjie.kalle.simple.cache.DiskCacheStore
 
 class App : Application() {
 
@@ -17,13 +19,31 @@ class App : Application() {
         }
 
         initNet("http://localhost.com") {
-            /*converter(object : JsonConverter() {
-                override fun <S> convert(succeed: Type, body: String): S? {
-                    return Moshi.Builder().build().adapter<S>(succeed).fromJson(body)
+
+            /*            converter(object : DefaultConvert() {
+                            override fun <S> convert(succeed: Type, body: String): S? {
+                                return Moshi.Builder().build().adapter<S>(succeed).fromJson(body)
+                            }
+
+                        })*/
+
+            addInterceptor {
+
+
+                val request = it.request()
+
+                if (request is SimpleUrlRequest) {
                 }
-            })*/
-        }
 
+                it.proceed(request)
+            }
+
+            val cacheStore = DiskCacheStore.newBuilder(cacheDir.absolutePath)
+                .password("cache")
+                .build()
+
+            cacheStore(cacheStore)
+        }
 
     }
 }

+ 5 - 2
sample/src/main/java/com/drake/net/sample/MainActivity.kt

@@ -4,6 +4,7 @@ import android.os.Bundle
 import androidx.appcompat.app.AppCompatActivity
 import com.drake.net.get
 import com.drake.net.utils.scopeDialog
+import com.yanzhenjie.kalle.simple.cache.CacheMode
 import kotlinx.android.synthetic.main.activity_main.*
 
 class MainActivity : AppCompatActivity() {
@@ -14,18 +15,20 @@ class MainActivity : AppCompatActivity() {
 
         setContentView(R.layout.activity_main)
 
+
+
+
         scopeDialog {
 
             val data = get<String>(
                 "https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md",
                 absolutePath = true
             ) {
-                cacheKey("wu")
+                cacheMode(CacheMode.HTTP_YES_THEN_WRITE_CACHE)
             }
 
             textView.text = data.await()
         }
-
     }
 
 }