Browse Source

开启编译器强制替换废弃函数

drake 3 năm trước cách đây
mục cha
commit
8052362e48

+ 2 - 2
net/src/main/java/com/drake/net/Init.kt

@@ -29,7 +29,7 @@ import okhttp3.OkHttpClient
  *
  *
  * @see com.drake.net.convert.JSONConvert
  * @see com.drake.net.convert.JSONConvert
  */
  */
-@Deprecated("函数迁移", replaceWith = ReplaceWith("NetConfig.init(host, config)"))
+@Deprecated("函数迁移", replaceWith = ReplaceWith("NetConfig.init(host, config)"), DeprecationLevel.ERROR)
 fun initNet(host: String = "", config: OkHttpClient.Builder.() -> Unit = {}) {
 fun initNet(host: String = "", config: OkHttpClient.Builder.() -> Unit = {}) {
     NetConfig.host = host
     NetConfig.host = host
     val builder = OkHttpClient.Builder()
     val builder = OkHttpClient.Builder()
@@ -37,7 +37,7 @@ fun initNet(host: String = "", config: OkHttpClient.Builder.() -> Unit = {}) {
     NetConfig.okHttpClient = builder.toNetOkhttp().build()
     NetConfig.okHttpClient = builder.toNetOkhttp().build()
 }
 }
 
 
-@Deprecated("函数迁移", replaceWith = ReplaceWith("NetConfig.init(host, config)"))
+@Deprecated("函数迁移", replaceWith = ReplaceWith("NetConfig.init(host, config)"), DeprecationLevel.ERROR)
 fun initNet(host: String = "", config: OkHttpClient.Builder) {
 fun initNet(host: String = "", config: OkHttpClient.Builder) {
     NetConfig.host = host
     NetConfig.host = host
     NetConfig.okHttpClient = config.toNetOkhttp().build()
     NetConfig.okHttpClient = config.toNetOkhttp().build()

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

@@ -31,23 +31,20 @@ import com.drake.net.NetConfig.logEnabled
 import com.drake.net.NetConfig.requestInterceptor
 import com.drake.net.NetConfig.requestInterceptor
 import com.drake.net.NetConfig.runningCalls
 import com.drake.net.NetConfig.runningCalls
 import com.drake.net.convert.NetConverter
 import com.drake.net.convert.NetConverter
-import com.drake.net.exception.*
 import com.drake.net.interceptor.RequestInterceptor
 import com.drake.net.interceptor.RequestInterceptor
 import com.drake.net.interfaces.NetDialogFactory
 import com.drake.net.interfaces.NetDialogFactory
 import com.drake.net.interfaces.NetErrorHandler
 import com.drake.net.interfaces.NetErrorHandler
 import com.drake.net.okhttp.toNetOkhttp
 import com.drake.net.okhttp.toNetOkhttp
-import com.drake.net.utils.TipUtils
 import okhttp3.Call
 import okhttp3.Call
 import okhttp3.OkHttpClient
 import okhttp3.OkHttpClient
 import java.lang.ref.WeakReference
 import java.lang.ref.WeakReference
-import java.net.UnknownHostException
 import java.util.concurrent.ConcurrentLinkedQueue
 import java.util.concurrent.ConcurrentLinkedQueue
 
 
 
 
 /**
 /**
  * Net的全局配置
  * Net的全局配置
  *
  *
- * @property app 全局上下文, 一般执行[initNet]即可, 无需手动赋值
+ * @property app 全局上下文, 一般执行[NetConfig.init]即可, 无需手动赋值
  * @property host 全局的域名或者ip(baseUrl)
  * @property host 全局的域名或者ip(baseUrl)
  * @property runningCalls Net中正在运行的请求Call
  * @property runningCalls Net中正在运行的请求Call
  * @property requestInterceptor 请求拦截器
  * @property requestInterceptor 请求拦截器
@@ -90,7 +87,7 @@ object NetConfig {
     var dialogFactory: NetDialogFactory = NetDialogFactory
     var dialogFactory: NetDialogFactory = NetDialogFactory
 
 
     /** 对话框, 已废弃, 请使用[dialogFactory] */
     /** 对话框, 已废弃, 请使用[dialogFactory] */
-    @Deprecated("废弃", replaceWith = ReplaceWith("NetConfig.dialogFactory"))
+    @Deprecated("废弃", replaceWith = ReplaceWith("NetConfig.dialogFactory"), DeprecationLevel.ERROR)
     var onDialog: (FragmentActivity) -> Dialog = { activity ->
     var onDialog: (FragmentActivity) -> Dialog = { activity ->
         val progress = ProgressDialog(activity)
         val progress = ProgressDialog(activity)
         progress.setMessage(activity.getString(R.string.net_dialog_msg))
         progress.setMessage(activity.getString(R.string.net_dialog_msg))
@@ -98,44 +95,14 @@ object NetConfig {
     }
     }
 
 
     /** 全局错误处理器, 已废弃, 请使用[errorHandler] */
     /** 全局错误处理器, 已废弃, 请使用[errorHandler] */
-    @Deprecated("使用NetErrorHandler统一处理错误", replaceWith = ReplaceWith("NetConfig.errorHandler"))
+    @Deprecated("使用NetErrorHandler统一处理错误", replaceWith = ReplaceWith("NetConfig.errorHandler"), DeprecationLevel.ERROR)
     var onError: Throwable.() -> Unit = onError@{
     var onError: Throwable.() -> Unit = onError@{
-
-        val message = when (this) {
-            is UnknownHostException -> app.getString(R.string.net_host_error)
-            is URLParseException -> app.getString(R.string.net_url_error)
-            is NetConnectException -> app.getString(R.string.net_network_error)
-            is NetSocketTimeoutException -> app.getString(
-                R.string.net_connect_timeout_error,
-                message
-            )
-            is DownloadFileException -> app.getString(R.string.net_download_error)
-            is ConvertException -> app.getString(R.string.net_parse_error)
-            is RequestParamsException -> app.getString(R.string.net_request_error)
-            is ServerResponseException -> app.getString(R.string.net_server_error)
-            is NullPointerException -> app.getString(R.string.net_null_error)
-            is NoCacheException -> app.getString(R.string.net_no_cache_error)
-            is ResponseException -> message
-            is HttpFailureException -> app.getString(R.string.request_failure)
-            is NetException -> app.getString(R.string.net_error)
-            else -> app.getString(R.string.net_other_error)
-        }
-
-        if (logEnabled) printStackTrace()
-        TipUtils.toast(message)
     }
     }
 
 
 
 
     /** 网络请求自动处理缺省页时发生错误的处理逻辑 */
     /** 网络请求自动处理缺省页时发生错误的处理逻辑 */
-    @Deprecated("使用NetErrorHandler统一处理错误", replaceWith = ReplaceWith("NetConfig.errorHandler"))
+    @Deprecated("使用NetErrorHandler统一处理错误", replaceWith = ReplaceWith("NetConfig.errorHandler"), DeprecationLevel.ERROR)
     var onStateError: Throwable.(view: View) -> Unit = {
     var onStateError: Throwable.(view: View) -> Unit = {
-        when (this) {
-            is ConvertException,
-            is RequestParamsException,
-            is ResponseException,
-            is NullPointerException -> errorHandler.onError(this)
-            else -> if (logEnabled) printStackTrace()
-        }
     }
     }
 
 
     //<editor-fold desc="初始化">
     //<editor-fold desc="初始化">

+ 5 - 2
net/src/main/java/com/drake/net/interfaces/NetDialogFactory.kt

@@ -1,8 +1,9 @@
 package com.drake.net.interfaces
 package com.drake.net.interfaces
 
 
 import android.app.Dialog
 import android.app.Dialog
+import android.app.ProgressDialog
 import androidx.fragment.app.FragmentActivity
 import androidx.fragment.app.FragmentActivity
-import com.drake.net.NetConfig
+import com.drake.net.R
 
 
 fun interface NetDialogFactory {
 fun interface NetDialogFactory {
 
 
@@ -14,7 +15,9 @@ fun interface NetDialogFactory {
 
 
     companion object DEFAULT : NetDialogFactory {
     companion object DEFAULT : NetDialogFactory {
         override fun onCreate(activity: FragmentActivity): Dialog {
         override fun onCreate(activity: FragmentActivity): Dialog {
-            return NetConfig.onDialog.invoke(activity)
+            val progress = ProgressDialog(activity)
+            progress.setMessage(activity.getString(R.string.net_dialog_msg))
+            return progress
         }
         }
     }
     }
 }
 }

+ 37 - 2
net/src/main/java/com/drake/net/interfaces/NetErrorHandler.kt

@@ -2,6 +2,10 @@ package com.drake.net.interfaces
 
 
 import android.view.View
 import android.view.View
 import com.drake.net.NetConfig
 import com.drake.net.NetConfig
+import com.drake.net.R
+import com.drake.net.exception.*
+import com.drake.net.utils.TipUtils
+import java.net.UnknownHostException
 
 
 interface NetErrorHandler {
 interface NetErrorHandler {
 
 
@@ -11,12 +15,43 @@ interface NetErrorHandler {
      * 全局错误
      * 全局错误
      * @param e 发生的错误
      * @param e 发生的错误
      */
      */
-    fun onError(e: Throwable) = NetConfig.onError(e)
+    fun onError(e: Throwable) {
+        val message = when (e) {
+            is UnknownHostException -> NetConfig.app.getString(R.string.net_host_error)
+            is URLParseException -> NetConfig.app.getString(R.string.net_url_error)
+            is NetConnectException -> NetConfig.app.getString(R.string.net_network_error)
+            is NetSocketTimeoutException -> NetConfig.app.getString(
+                R.string.net_connect_timeout_error,
+                e.message
+            )
+            is DownloadFileException -> NetConfig.app.getString(R.string.net_download_error)
+            is ConvertException -> NetConfig.app.getString(R.string.net_parse_error)
+            is RequestParamsException -> NetConfig.app.getString(R.string.net_request_error)
+            is ServerResponseException -> NetConfig.app.getString(R.string.net_server_error)
+            is NullPointerException -> NetConfig.app.getString(R.string.net_null_error)
+            is NoCacheException -> NetConfig.app.getString(R.string.net_no_cache_error)
+            is ResponseException -> e.message
+            is HttpFailureException -> NetConfig.app.getString(R.string.request_failure)
+            is NetException -> NetConfig.app.getString(R.string.net_error)
+            else -> NetConfig.app.getString(R.string.net_other_error)
+        }
+
+        if (NetConfig.logEnabled) e.printStackTrace()
+        TipUtils.toast(message)
+    }
 
 
     /**
     /**
      * 自动缺省页错误
      * 自动缺省页错误
      * @param e 发生的错误
      * @param e 发生的错误
      * @param view 缺省页, StateLayout或者PageRefreshLayout
      * @param view 缺省页, StateLayout或者PageRefreshLayout
      */
      */
-    fun onStateError(e: Throwable, view: View) = NetConfig.onStateError(e, view)
+    fun onStateError(e: Throwable, view: View) {
+        when (e) {
+            is ConvertException,
+            is RequestParamsException,
+            is ResponseException,
+            is NullPointerException -> onError(e)
+            else -> if (NetConfig.logEnabled) e.printStackTrace()
+        }
+    }
 }
 }

+ 9 - 6
net/src/main/java/com/drake/net/okhttp/OkHttpExtension.kt

@@ -140,10 +140,11 @@ fun OkHttpClient.Builder.setRequestInterceptor(interceptor: RequestInterceptor)
  */
  */
 @Deprecated(
 @Deprecated(
     message = "使用NetErrorHandler统一处理错误",
     message = "使用NetErrorHandler统一处理错误",
-    replaceWith = ReplaceWith("setErrorHandler(NetErrorHandler())")
+    replaceWith = ReplaceWith("setErrorHandler(NetErrorHandler())"),
+    DeprecationLevel.ERROR
 )
 )
 fun OkHttpClient.Builder.onError(block: Throwable.() -> Unit) = apply {
 fun OkHttpClient.Builder.onError(block: Throwable.() -> Unit) = apply {
-    NetConfig.onError = block
+    // NetConfig.onError = block
 }
 }
 
 
 /**
 /**
@@ -151,10 +152,11 @@ fun OkHttpClient.Builder.onError(block: Throwable.() -> Unit) = apply {
  */
  */
 @Deprecated(
 @Deprecated(
     message = "使用NetErrorHandler统一处理错误",
     message = "使用NetErrorHandler统一处理错误",
-    replaceWith = ReplaceWith("setErrorHandler(NetErrorHandler())")
+    replaceWith = ReplaceWith("setErrorHandler(NetErrorHandler())"),
+    DeprecationLevel.ERROR
 )
 )
 fun OkHttpClient.Builder.onStateError(block: Throwable.(view: View) -> Unit) = apply {
 fun OkHttpClient.Builder.onStateError(block: Throwable.(view: View) -> Unit) = apply {
-    NetConfig.onStateError = block
+    // NetConfig.onStateError = block
 }
 }
 
 
 /**
 /**
@@ -163,11 +165,12 @@ fun OkHttpClient.Builder.onStateError(block: Throwable.(view: View) -> Unit) = a
  */
  */
 @Deprecated(
 @Deprecated(
     message = "使用NetDialogFactory创建",
     message = "使用NetDialogFactory创建",
-    replaceWith = ReplaceWith("setDialogFactory(NetDialogFactory())")
+    replaceWith = ReplaceWith("setDialogFactory(NetDialogFactory())"),
+    DeprecationLevel.ERROR
 )
 )
 fun OkHttpClient.Builder.onDialog(block: (FragmentActivity) -> Dialog) =
 fun OkHttpClient.Builder.onDialog(block: (FragmentActivity) -> Dialog) =
     apply {
     apply {
-        NetConfig.onDialog = block
+        // NetConfig.onDialog = block
     }
     }
 
 
 /**
 /**

+ 5 - 5
net/src/main/java/com/drake/net/request/RequestExtension.kt

@@ -21,9 +21,9 @@ import com.drake.net.body.peekString
 import com.drake.net.convert.NetConverter
 import com.drake.net.convert.NetConverter
 import com.drake.net.interfaces.ProgressListener
 import com.drake.net.interfaces.ProgressListener
 import com.drake.net.tag.NetLabel
 import com.drake.net.tag.NetLabel
-import okhttp3.OkHttpUtils
 import okhttp3.FormBody
 import okhttp3.FormBody
 import okhttp3.Headers
 import okhttp3.Headers
+import okhttp3.OkHttpUtils
 import okhttp3.Request
 import okhttp3.Request
 import java.net.URLDecoder
 import java.net.URLDecoder
 import java.util.concurrent.ConcurrentLinkedQueue
 import java.util.concurrent.ConcurrentLinkedQueue
@@ -71,22 +71,22 @@ var Request.kType: KType?
         setLabel(NetLabel.RequestKType(value))
         setLabel(NetLabel.RequestKType(value))
     }
     }
 
 
-@Deprecated("建议使用属性", ReplaceWith("id"))
+@Deprecated("建议使用属性", ReplaceWith("id"), DeprecationLevel.ERROR)
 fun Request.id(): Any? {
 fun Request.id(): Any? {
     return id
     return id
 }
 }
 
 
-@Deprecated("建议使用属性", ReplaceWith("group"))
+@Deprecated("建议使用属性", ReplaceWith("group"), DeprecationLevel.ERROR)
 fun Request.group(): Any? {
 fun Request.group(): Any? {
     return group
     return group
 }
 }
 
 
-@Deprecated("建议使用属性", ReplaceWith("logRecord"))
+@Deprecated("建议使用属性", ReplaceWith("logRecord"), DeprecationLevel.ERROR)
 fun Request.isLogRecord(): Boolean? {
 fun Request.isLogRecord(): Boolean? {
     return logRecord
     return logRecord
 }
 }
 
 
-@Deprecated("建议使用属性", ReplaceWith("kType"))
+@Deprecated("建议使用属性", ReplaceWith("kType"), DeprecationLevel.ERROR)
 fun Request.kType(): KType? {
 fun Request.kType(): KType? {
     return kType
     return kType
 }
 }

+ 5 - 2
net/src/main/java/com/drake/net/utils/FlowUtils.kt

@@ -24,7 +24,10 @@ import androidx.lifecycle.LifecycleOwner
 import com.drake.net.scope.AndroidScope
 import com.drake.net.scope.AndroidScope
 import kotlinx.coroutines.*
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.awaitClose
 import kotlinx.coroutines.channels.awaitClose
-import kotlinx.coroutines.flow.*
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.FlowCollector
+import kotlinx.coroutines.flow.callbackFlow
+import kotlinx.coroutines.flow.debounce
 
 
 /**
 /**
  * 收集Flow结果并过滤重复结果
  * 收集Flow结果并过滤重复结果
@@ -47,7 +50,7 @@ inline fun <T> Flow<T>.listen(
  * @param event 销毁时机
  * @param event 销毁时机
  * @param dispatcher 指定调度器
  * @param dispatcher 指定调度器
  */
  */
-@Deprecated("规范命名", ReplaceWith("launchIn"))
+@Deprecated("规范命名", ReplaceWith("launchIn"), DeprecationLevel.ERROR)
 @OptIn(InternalCoroutinesApi::class)
 @OptIn(InternalCoroutinesApi::class)
 inline fun <T> Flow<T>.scope(
 inline fun <T> Flow<T>.scope(
     owner: LifecycleOwner? = null,
     owner: LifecycleOwner? = null,

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

@@ -67,7 +67,7 @@ fun Fragment.scopeDialog(
  * 作用域开始执行时显示加载中缺省页
  * 作用域开始执行时显示加载中缺省页
  * 作用域正常结束时显示成功缺省页
  * 作用域正常结束时显示成功缺省页
  * 作用域抛出异常时显示错误缺省页
  * 作用域抛出异常时显示错误缺省页
- * 并且自动吐司错误信息, 可配置 [com.drake.net.NetConfig.onStateError]
+ * 并且自动吐司错误信息, 可配置 [com.drake.net.interfaces.NetErrorHandler.onStateError]
  * 自动打印异常日志
  * 自动打印异常日志
  * @receiver 当前视图会被缺省页包裹
  * @receiver 当前视图会被缺省页包裹
  *
  *
@@ -89,7 +89,7 @@ fun StateLayout.scope(
  * 2. 上拉加载自动结束
  * 2. 上拉加载自动结束
  * 3. 捕获异常
  * 3. 捕获异常
  * 4. 打印异常日志
  * 4. 打印异常日志
- * 5. 吐司部分异常[com.drake.net.NetConfig.onStateError]
+ * 5. 吐司部分异常[com.drake.net.interfaces.NetErrorHandler.onStateError]
  * 6. 判断添加还是覆盖数据
  * 6. 判断添加还是覆盖数据
  * 7. 自动显示缺省页
  * 7. 自动显示缺省页
  *
  *