drake 5 years ago
parent
commit
224d861eff

+ 21 - 14
README.md

@@ -105,13 +105,8 @@ implementation 'com.github.liangjingkanji:Net:2.1.0'
 ### Post
 
 ```kotlin
-scopeLife {
-
-  val data = post<String>(
-    "https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md",
-    absolutePath = true
-  )
-
+scopeNetLife {
+  val data = post<String>("https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md")
   textView.text = data.await()
 }
 ```
@@ -123,7 +118,7 @@ scopeLife {
 ### Get
 
 ```kotlin
-scopeLife {
+scopeNetLife {
 
   val data = get<String>(
     "https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md",
@@ -141,7 +136,7 @@ scopeLife {
 ### 文件上传
 
 ```kotlin
-scopeLife {
+scopeNetLife {
 
   val data = post<String>(
     "https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md",
@@ -161,7 +156,7 @@ scopeLife {
 ### 文件下载
 
 ```kotlin
-scopeLife {
+scopeNetLife {
   download("/path", "下载目录"){
 
     // 进度监听
@@ -188,7 +183,7 @@ Context.downloadImg(url: String, with: Int = -1, height: Int = -1)
 示例
 
 ```kotlin
-scopeLife {
+scopeNetLife {
 
   val data = downImage(
     "https://cdn.sspai.com/article/ebe361e4-c891-3afd-8680-e4bad609723e.jpg?imageMogr2/quality/95/thumbnail/!2880x620r/gravity/Center/crop/2880x620/interlace/1".
@@ -610,7 +605,7 @@ abstract class DefaultConverter(
 
 
 
-## 缓存和网络请求
+## 缓存和网络请求
 
 很多App要求秒启动展示首页数据, 然后断网以后也可以展示缓存数据, 这种需求需要做到刷新UI数据两遍, 本框架同样方便实现
 
@@ -629,7 +624,7 @@ initNet("http://localhost.com") {
 可配置参数
 
 ```kotlin
-fun KalleConfig.Builder.openCache(
+fun KalleConfig.Builder.cacheEnabled(
     path: String = NetConfig.app.cacheDir.absolutePath, // 缓存保存位置, 默认应用缓存目录
     password: String = "cache" // 缓存密码, 默认cache
 ) 
@@ -676,6 +671,18 @@ scopeNetLife {
 2. cache: 该作用域内部允许抛出任何异常都不算错误, 这里的`cache`会比`scopeNetLife`先执行.
 3. 当缓存读取成功视为作用域执行成功, 默认情况即使后续的网络请求失败也不会提示错误信息(cache函数参数指定true则提示)
 
+
+
+```kotlin
+fun cache(
+  error: Boolean = false, // 缓存读取成功但网络请求失败是否吐司错误信息
+  animate: Boolean = false, // 缓存读取成功是否立即停止加载动画, 只有PageRefreshLayout有效
+  onCache: suspend CoroutineScope.() -> Unit
+): AndroidScope 
+```
+
+
+
 ## 轮循器
 
 本框架附带一个超级强大的轮循器`Interval`, 基本上包含轮循器所需要到所有功能
@@ -702,7 +709,7 @@ Interval(1, TimeUnit.SECONDS).subscribe {
 函数
 
 ```
-subscribe() // 即开启定时器, 订阅多个也会监听同一个计数器
+subscribe() // 订阅定时器, 订阅多个也会监听同一个计数器
 start() // 开始
 stop() // 结束
 pause() // 暂停

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

@@ -60,12 +60,13 @@ object NetConfig {
     internal var onStateError: Throwable.(view: View) -> Unit = {
 
         when (this) {
-            is ParseError, is RequestParamsException, is ResponseException, is NullPointerException -> onError(
+            is ParseError,
+            is RequestParamsException,
+            is ResponseException,
+            is NullPointerException -> onError(
                 this
             )
-            else -> {
-                printStackTrace()
-            }
+            else -> printStackTrace()
         }
     }
 }

+ 3 - 0
net/src/main/java/com/drake/net/convert/DefaultConvert.kt

@@ -74,6 +74,9 @@ abstract class DefaultConvert(
             .build()
     }
 
+    fun encrypt(body: String) {
+
+    }
 
     /**
      * 解析JSON数据

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

@@ -66,7 +66,7 @@ class Interval(
     private lateinit var ticker: ReceiveChannel<Unit>
 
     var count = start
-    var state = IntervalState.STATE_IDLE
+    var state = IntervalStatus.STATE_IDLE
         private set
 
     // <editor-fold desc="回调">
@@ -95,10 +95,10 @@ class Interval(
      * 开始
      */
     fun start() {
-        if (state == IntervalState.STATE_ACTIVE || state == IntervalState.STATE_PAUSE) {
+        if (state == IntervalStatus.STATE_ACTIVE || state == IntervalStatus.STATE_PAUSE) {
             return
         }
-        state = IntervalState.STATE_ACTIVE
+        state = IntervalStatus.STATE_ACTIVE
         launch()
     }
 
@@ -107,8 +107,8 @@ class Interval(
      * 停止
      */
     fun stop() {
-        if (state == IntervalState.STATE_IDLE) return
-        state = IntervalState.STATE_IDLE
+        if (state == IntervalStatus.STATE_IDLE) return
+        state = IntervalStatus.STATE_IDLE
         scope?.cancel()
         finishList.forEach {
             it.invoke(count)
@@ -121,8 +121,8 @@ class Interval(
      */
     fun switch() {
         when (state) {
-            IntervalState.STATE_ACTIVE -> stop()
-            IntervalState.STATE_IDLE -> start()
+            IntervalStatus.STATE_ACTIVE -> stop()
+            IntervalStatus.STATE_IDLE -> start()
             else -> return
         }
     }
@@ -131,8 +131,8 @@ class Interval(
      * 继续
      */
     fun resume() {
-        if (state != IntervalState.STATE_PAUSE) return
-        state = IntervalState.STATE_ACTIVE
+        if (state != IntervalStatus.STATE_PAUSE) return
+        state = IntervalStatus.STATE_ACTIVE
         launch(delay)
     }
 
@@ -140,8 +140,8 @@ class Interval(
      * 暂停
      */
     fun pause() {
-        if (state != IntervalState.STATE_ACTIVE) return
-        state = IntervalState.STATE_PAUSE
+        if (state != IntervalStatus.STATE_ACTIVE) return
+        state = IntervalStatus.STATE_PAUSE
         delay = System.currentTimeMillis() - countTime
         scope?.cancel()
     }
@@ -150,11 +150,11 @@ class Interval(
      * 重置
      */
     fun reset() {
-        if (state == IntervalState.STATE_IDLE) return
+        if (state == IntervalStatus.STATE_IDLE) return
         count = start
         scope?.cancel()
         delay = unit.toMillis(initialDelay)
-        if (state == IntervalState.STATE_ACTIVE) launch()
+        if (state == IntervalStatus.STATE_ACTIVE) launch()
     }
 
     /**

+ 1 - 1
net/src/main/java/com/drake/net/time/IntervalState.kt → net/src/main/java/com/drake/net/time/IntervalStatus.kt

@@ -10,6 +10,6 @@ package com.drake.net.time
 /**
  * 计时器的状态
  */
-enum class IntervalState {
+enum class IntervalStatus {
     STATE_ACTIVE, STATE_IDLE, STATE_PAUSE
 }

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

@@ -18,12 +18,8 @@ class MainActivity : AppCompatActivity() {
         content.onRefresh {
 
             scope {
-
-                val data = get<String>(
-                    "https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md",
-                    absolutePath = true
-                )
-
+                val data =
+                    get<String>("https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md")
                 textView.text = data.await()
             }