Browse Source

| 优化函数签名

drake 5 years ago
parent
commit
be4fdb7abd

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

@@ -55,7 +55,7 @@ inline fun <reified M> CoroutineScope.get(
         if (response.isSucceed) {
             response.succeed()
         } else {
-            throw ResponseException(response.failed(), response.code())
+            throw ResponseException(response.code(), response.failed())
         }
     } else {
         throw CancellationException()
@@ -87,7 +87,7 @@ inline fun <reified M> CoroutineScope.post(
         if (response.isSucceed) {
             response.succeed()
         } else {
-            throw ResponseException(response.failed(), response.code())
+            throw ResponseException(response.code(), response.failed())
         }
     } else {
         throw CancellationException()
@@ -181,7 +181,7 @@ inline fun <reified M> syncGet(
     return if (response.isSucceed) {
         response.succeed()
     } else {
-        throw ResponseException(response.failed(), response.code())
+        throw ResponseException(response.code(), response.failed())
     }
 }
 
@@ -205,7 +205,7 @@ inline fun <reified M> syncPost(
     return if (response.isSucceed) {
         response.succeed()
     } else {
-        throw ResponseException(response.failed(), response.code())
+        throw ResponseException(response.code(), response.failed())
     }
 }
 

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

@@ -11,6 +11,7 @@ import android.app.Application
 import android.app.Dialog
 import android.view.View
 import androidx.fragment.app.FragmentActivity
+import com.drake.net.connect.OkHttpConnectFactory
 import com.drake.net.error.RequestParamsException
 import com.drake.net.error.ResponseException
 import com.drake.net.error.ServerResponseException

+ 2 - 2
net/src/main/java/com/drake/net/OkHttpConnectFactory.java → net/src/main/java/com/drake/net/connect/OkHttpConnectFactory.java

@@ -2,9 +2,9 @@
  * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
  * Project:Net
  * Author:Drake
- * Date:12/30/19 1:41 PM
+ * Date:1/10/20 11:35 PM
  */
-package com.drake.net;
+package com.drake.net.connect;
 
 import android.os.Build;
 

+ 5 - 1
net/src/main/java/com/drake/net/error/RequestParamsException.kt

@@ -10,4 +10,8 @@ package com.drake.net.error
 /**
  * 404
  */
-class RequestParamsException(code: Int) : Throwable(code.toString())
+class RequestParamsException(
+    val code: Int,
+    val msg: String = "",
+    val tag: Any? = null
+) : Throwable("$code: $msg")

+ 5 - 2
net/src/main/java/com/drake/net/error/ResponseException.kt

@@ -13,5 +13,8 @@ package com.drake.net.error
  * @param code 网络请求错误码
  * @param tag 应对错误码判断为错时但是后端又返回了需要使用的数据(建议后端修改). 一般在Convert中设置数据
  */
-class ResponseException(val msg: String, val code: Int, val tag: Any? = null) :
-    Throwable("code: $code, msg: $msg")
+class ResponseException(
+    val code: Int,
+    val msg: String = "",
+    val tag: Any? = null
+) : Throwable("$code: $msg")

+ 5 - 1
net/src/main/java/com/drake/net/error/ServerResponseException.kt

@@ -10,4 +10,8 @@ package com.drake.net.error
 /**
  * 500
  */
-class ServerResponseException(code: Int) : Throwable(code.toString())
+class ServerResponseException(
+    val code: Int,
+    val msg: String = "",
+    val tag: Any? = null
+) : Throwable("$code: $msg")

+ 17 - 22
net/src/main/java/com/drake/net/utils/Interval.kt → net/src/main/java/com/drake/net/time/Interval.kt

@@ -2,17 +2,18 @@
  * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
  * Project:Net
  * Author:Drake
- * Date:12/22/19 3:59 PM
+ * Date:1/10/20 11:25 PM
  */
 
 @file:UseExperimental(ObsoleteCoroutinesApi::class)
 
-package com.drake.net.utils
+package com.drake.net.time
 
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleEventObserver
 import androidx.lifecycle.LifecycleOwner
 import com.drake.net.scope.AndroidScope
+import com.drake.net.utils.scope
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import kotlinx.coroutines.cancel
 import kotlinx.coroutines.channels.ReceiveChannel
@@ -59,7 +60,7 @@ class Interval(
 
     private val receiveList: MutableList<(Long) -> Unit> = mutableListOf()
     private val finishList: MutableList<(Long) -> Unit> = mutableListOf()
-    private var _state = TickerState.STATE_IDLE
+    private var _state = IntervalState.STATE_IDLE
     private var countTime = 0L
     private var delay = 0L
     private var scope: AndroidScope? = null
@@ -94,10 +95,10 @@ class Interval(
      * 开始
      */
     fun start() {
-        if (_state == TickerState.STATE_ACTIVE || _state == TickerState.STATE_PAUSE) {
+        if (_state == IntervalState.STATE_ACTIVE || _state == IntervalState.STATE_PAUSE) {
             return
         }
-        _state = TickerState.STATE_ACTIVE
+        _state = IntervalState.STATE_ACTIVE
         launch()
     }
 
@@ -106,8 +107,8 @@ class Interval(
      * 停止
      */
     fun stop() {
-        if (_state == TickerState.STATE_IDLE) return
-        _state = TickerState.STATE_IDLE
+        if (_state == IntervalState.STATE_IDLE) return
+        _state = IntervalState.STATE_IDLE
         scope?.cancel()
         finishList.forEach {
             it.invoke(count)
@@ -120,8 +121,8 @@ class Interval(
      */
     fun switch() {
         when (state) {
-            TickerState.STATE_ACTIVE -> stop()
-            TickerState.STATE_IDLE -> start()
+            IntervalState.STATE_ACTIVE -> stop()
+            IntervalState.STATE_IDLE -> start()
             else -> return
         }
     }
@@ -130,8 +131,8 @@ class Interval(
      * 继续
      */
     fun resume() {
-        if (_state != TickerState.STATE_PAUSE) return
-        _state = TickerState.STATE_ACTIVE
+        if (_state != IntervalState.STATE_PAUSE) return
+        _state = IntervalState.STATE_ACTIVE
         launch(delay)
     }
 
@@ -139,8 +140,8 @@ class Interval(
      * 暂停
      */
     fun pause() {
-        if (_state != TickerState.STATE_ACTIVE) return
-        _state = TickerState.STATE_PAUSE
+        if (_state != IntervalState.STATE_ACTIVE) return
+        _state = IntervalState.STATE_PAUSE
         delay = System.currentTimeMillis() - countTime
         scope?.cancel()
     }
@@ -149,11 +150,11 @@ class Interval(
      * 重置
      */
     fun reset() {
-        if (_state == TickerState.STATE_IDLE) return
+        if (_state == IntervalState.STATE_IDLE) return
         count = start
         scope?.cancel()
         delay = unit.toMillis(initialDelay)
-        if (_state == TickerState.STATE_ACTIVE) launch()
+        if (_state == IntervalState.STATE_ACTIVE) launch()
     }
 
     /**
@@ -197,11 +198,5 @@ class Interval(
             }
         }
     }
+}
 
-    /**
-     * 计时器的状态
-     */
-    enum class TickerState {
-        STATE_ACTIVE, STATE_IDLE, STATE_PAUSE
-    }
-}

+ 15 - 0
net/src/main/java/com/drake/net/time/IntervalState.kt

@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
+ * Project:Net
+ * Author:Drake
+ * Date:1/10/20 11:25 PM
+ */
+
+package com.drake.net.time
+
+/**
+ * 计时器的状态
+ */
+enum class IntervalState {
+    STATE_ACTIVE, STATE_IDLE, STATE_PAUSE
+}

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

@@ -8,8 +8,6 @@
 package com.drake.net.utils
 
 import android.app.Dialog
-import android.os.Handler
-import android.os.Looper
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.lifecycle.Lifecycle
@@ -105,17 +103,6 @@ fun PageRefreshLayout.scope(block: suspend CoroutineScope.(PageCoroutineScope) -
     return scope
 }
 
-/**
- * 在主线程运行
- */
-fun runMain(block: () -> Unit) {
-    if (Looper.myLooper() == Looper.getMainLooper()) {
-        block()
-    } else {
-        Handler(Looper.getMainLooper()).post { block() }
-    }
-}
-
 /**
  * 异步作用域
  *

+ 13 - 0
net/src/main/java/com/drake/net/utils/SuspendUtils.kt

@@ -7,6 +7,8 @@
 
 package com.drake.net.utils
 
+import android.os.Handler
+import android.os.Looper
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.supervisorScope
@@ -43,4 +45,15 @@ suspend fun tryScope(
             error(e)
         }
     }
+}
+
+/**
+ * 在主线程运行
+ */
+fun runMain(block: () -> Unit) {
+    if (Looper.myLooper() == Looper.getMainLooper()) {
+        block()
+    } else {
+        Handler(Looper.getMainLooper()).post { block() }
+    }
 }

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

@@ -44,7 +44,6 @@ class MainActivity : AppCompatActivity() {
             }
 
         }.autoRefresh()
-
     }
 }