Quellcode durchsuchen

cache重命名为preview

drake vor 4 Jahren
Ursprung
Commit
56bca42f55

+ 1 - 1
README.md

@@ -107,7 +107,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
 // 支持自动下拉刷新和缺省页的(可选)
 implementation 'com.github.liangjingkanji:BRV:1.3.8'
 
-implementation 'com.github.liangjingkanji:Net:2.2.11'
+implementation 'com.github.liangjingkanji:Net:2.2.12'
 ```
 
 <br>

+ 3 - 3
docs/read-cache.md

@@ -21,15 +21,15 @@ initNet("http://182.92.97.186/") {
         // 然后执行这里(网络请求)
         tv_fragment.text = Post<String>("api", cache = CacheMode.NETWORK_YES_THEN_WRITE_CACHE).await()
         Log.d("日志", "网络请求")
-    }.cache {
+    }.preview {
         // 先执行这里(仅读缓存), 任何异常都视为读取缓存失败
         tv_fragment.text = Get<String>("api", cache = CacheMode.READ_CACHE).await()
         Log.d("日志", "读取缓存")
     }
     ```
 
-预读模式本质上就是创建一个`cache`附加作用域, 里面的所有异常崩溃都会被静默捕捉(算作缓存失败), 然后再执行scope本身,
-而且一旦缓存读取成功(cache内部无异常)即使网络请求失败也可以不提醒用户任何错误信息(可配置), 其实可以应用于其他场景
+预读模式本质上就是创建一个`preview`附加作用域, 里面的所有异常崩溃都会被静默捕捉(算作缓存失败), 会优先于`scope*`执行, 然后再执行scope本身,
+而且一旦缓存读取成功(`preview`内部无异常)即使网络请求失败也可以不提醒用户任何错误信息(可配置), 其实可以应用于其他场景
 
 <br>
 

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

@@ -35,10 +35,10 @@ import kotlin.coroutines.EmptyCoroutineContext
 open class NetCoroutineScope() : AndroidScope() {
 
     protected var isReadCache = true
-    protected var onCache: (suspend CoroutineScope.() -> Unit)? = null
+    protected var preview: (suspend CoroutineScope.() -> Unit)? = null
 
     protected var isCacheSucceed = false
-        get() = if (onCache != null) field else false
+        get() = if (preview != null) field else false
 
     protected var error = true
         get() = if (isCacheSucceed) field else true
@@ -59,10 +59,10 @@ open class NetCoroutineScope() : AndroidScope() {
     override fun launch(block: suspend CoroutineScope.() -> Unit): NetCoroutineScope {
         launch(EmptyCoroutineContext) {
             start()
-            if (onCache != null && isReadCache) {
+            if (preview != null && isReadCache) {
                 supervisorScope {
                     isCacheSucceed = try {
-                        onCache?.invoke(this)
+                        preview?.invoke(this)
                         true
                     } catch (e: Exception) {
                         false
@@ -98,23 +98,23 @@ open class NetCoroutineScope() : AndroidScope() {
     }
 
     /**
-     * 该函数一般用于缓存读取
+     * "预览"作用域, 该函数一般用于缓存读取
      * 只在第一次启动作用域时回调
      * 该函数在作用域[launch]之前执行
      * 函数内部所有的异常都不会被抛出, 也不会终止作用域执行
      *
      * @param error 是否在缓存读取成功但网络请求错误时吐司错误信息
      * @param animate 是否在缓存成功后依然显示加载动画
-     * @param onCache 该作用域内的所有异常都算缓存读取失败, 不会吐司和打印任何错误
+     * @param block 该作用域内的所有异常都算缓存读取失败, 不会吐司和打印任何错误
      */
-    fun cache(
+    fun preview(
         error: Boolean = false,
         animate: Boolean = false,
-        onCache: suspend CoroutineScope.() -> Unit
+        block: suspend CoroutineScope.() -> Unit
     ): AndroidScope {
         this.animate = animate
         this.error = error
-        this.onCache = onCache
+        this.preview = block
         return this
     }
 

+ 3 - 2
sample/src/main/java/com/drake/net/sample/ui/fragment/ReadCacheFragment.kt

@@ -45,9 +45,10 @@ class ReadCacheFragment : Fragment() {
 
         scopeNetLife {
             // 然后执行这里(网络请求)
-            tv_fragment.text = Post<String>("api", cache = CacheMode.NETWORK_YES_THEN_WRITE_CACHE).await()
+            tv_fragment.text =
+                Post<String>("api", cache = CacheMode.NETWORK_YES_THEN_WRITE_CACHE).await()
             Log.d("日志", "网络请求")
-        }.cache {
+        }.preview {
             // 先执行这里(仅读缓存), 任何异常都视为读取缓存失败
             tv_fragment.text = Get<String>("api", cache = CacheMode.READ_CACHE).await()
             Log.d("日志", "读取缓存")