From d773ab0295feba24ae4fc14f61e8aa310e40f4ba Mon Sep 17 00:00:00 2001
From: zuojincheng <lf_zuo@163.com>
Date: 星期五, 06 六月 2025 16:55:57 +0800
Subject: [PATCH] refactor(nfc): 优化写卡流程和状态更新
---
generallibrary/src/main/java/com/dayu/general/net/ApiManager.kt | 230 +++++++++++++++++++++++++++++---------------------------
1 files changed, 119 insertions(+), 111 deletions(-)
diff --git a/generallibrary/src/main/java/com/dayu/general/net/ApiManager.kt b/generallibrary/src/main/java/com/dayu/general/net/ApiManager.kt
index 6dab3dc..79e1dae 100644
--- a/generallibrary/src/main/java/com/dayu/general/net/ApiManager.kt
+++ b/generallibrary/src/main/java/com/dayu/general/net/ApiManager.kt
@@ -1,66 +1,84 @@
package com.dayu.general.net
import android.content.Context
-import android.text.TextUtils
+import android.util.Log
import com.dayu.baselibrary.business.BusinessProvider
-import com.dayu.baselibrary.net.subscribers.BaseProgressSubscriber
+import com.dayu.baselibrary.net.subscribers.ProgressSubscriber
import com.dayu.baselibrary.net.subscribers.SubscriberListener
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
-import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.functions.Function
import io.reactivex.rxjava3.schedulers.Schedulers
/**
+ * Copyright (C), 2023,
+ * Author: zuot
+ * Date: 2023-04-12 9:11
* Description:
- * Author: zuo
- * Date: 2025-03-06
*/
-class ApiManager {
+class ApiManager private constructor() {
- var apiService: ApiService? = null
+ companion object {
+ private const val TAG = "ApiManager"
- fun init() {
- if (apiManager == null) {
- apiManager =
- com.dayu.qiheonlinelibrary.net.ApiManager()
+ @Volatile
+ private var apiManager: ApiManager? = null
+
+ fun getInstance(): ApiManager {
+ return apiManager ?: synchronized(this) {
+ apiManager ?: ApiManager().also { apiManager = it }
+ }
}
}
- fun ApiManager() {
- apiService = RetrofitClient.getInstance().getApiService()
+ private val apiService: ApiService = RetrofitClient.getInstance().getApiService()
+
+ fun <T> requestGetLoading(
+ context: Context,
+ path: String,
+ tClass: Class<T>,
+ params: Map<String, Any>?,
+ listener: SubscriberListener<BaseResponse<T>>
+ ) {
+ request(context, false, path, true, tClass, params, listener)
}
- fun getInstance(): com.dayu.qiheonlinelibrary.net.ApiManager {
- return apiManager
+ fun <T> requestGetHideLoading(
+ context: Context,
+ path: String,
+ tClass: Class<T>,
+ params: Map<String, Any>?,
+ listener: SubscriberListener<BaseResponse<T>>
+ ) {
+ request(context, true, path, true, tClass, params, listener)
}
fun <T> requestPostLoading(
- context: Context?,
- path: String?,
- tClass: Class<T>?,
- params: Map<String?, Any?>?,
- listener: SubscriberListener<*>?
+ context: Context,
+ path: String,
+ tClass: Class<T>,
+ params: Map<String, Any>?,
+ listener: SubscriberListener<BaseResponse<T>>
) {
request(context, false, path, false, tClass, params, listener)
}
fun <T> requestPostHideLoading(
- context: Context?,
- path: String?,
- tClass: Class<T>?,
- params: Map<String?, Any?>?,
- listener: SubscriberListener<*>?
+ context: Context,
+ path: String,
+ tClass: Class<T>,
+ params: Map<String, Any>?,
+ listener: SubscriberListener<BaseResponse<T>>
) {
request(context, true, path, false, tClass, params, listener)
}
fun <T> requestPost(
- context: Context?,
- path: String?,
- tClass: Class<T>?,
- params: Map<String?, Any?>?,
- listener: SubscriberListener<*>?
+ context: Context,
+ path: String,
+ tClass: Class<T>,
+ params: Map<String, Any>?,
+ listener: SubscriberListener<BaseResponse<T>>
) {
request(context, false, path, false, tClass, params, listener)
}
@@ -76,22 +94,20 @@
* @param params Post璇锋眰鏃讹紝瀵瑰簲鐨勫弬鏁�
* @param listener 鍥炶皟璇锋眰
* @param <T>
- </T> */
+ */
fun <T> request(
- context: Context?,
+ context: Context,
hideLoading: Boolean,
- path: String?,
+ path: String,
isGet: Boolean,
- tClass: Class<T>?,
- params: Map<String?, Any?>?,
- listener: SubscriberListener<*>?
+ tClass: Class<T>,
+ params: Map<String, Any>?,
+ listener: SubscriberListener<BaseResponse<T>>
) {
- val observable: Observable<*>=if (isGet) {
- if (params == null) {
- apiService.requestGet(path)
- } else {
- apiService.requestGet(path, params)
- }
+
+ val observable = if (isGet) {
+ if (params == null) apiService.requestGet(path)
+ else apiService.requestGet(path, params)
} else {
if (params != null) {
apiService.requestPost(path, params)
@@ -100,77 +116,69 @@
}
}
- val mySubscriber: BaseProgressSubscriber<*>=ProgressSubscriber<Any?>(context, hideLoading, listener)
- observable.subscribeOn(Schedulers.io()).map
- object : Function<Any?, com.dayu.qiheonlinelibrary.net.BaseResponse<T>?> {
- override fun apply(o: Any): com.dayu.qiheonlinelibrary.net.BaseResponse<T> {
- if (o is com.dayu.qiheonlinelibrary.net.BaseResponse) {
- val tem: com.dayu.qiheonlinelibrary.net.BaseResponse =
- o as com.dayu.qiheonlinelibrary.net.BaseResponse
- val response: com.dayu.qiheonlinelibrary.net.BaseResponse<T> =
- com.dayu.qiheonlinelibrary.net.BaseResponse<T>()
- //鏈櫥褰曟垨鐧诲綍瓒呮椂锛岃閲嶆柊鐧诲綍
- if (tem.getCode() == 100401) {
- if (BusinessProvider.getBusinessProvider() != null) {
- BusinessProvider.getBusinessProvider().startLoginNavigotor.navigateToLogin(
- context
- )
- }
- }
- response.setCode(tem.getCode())
- response.setMsg(tem.getMsg())
- if (tClass != null) {
- if (TextUtils.isEmpty(
- tem.getData().toString()
- ) && BaseResult::class.java.isAssignableFrom(tClass)
- ) {
- response.setData(null)
- return response
- }
- if (tem.getData() is Map<*, *>) {
- try {
-// response.setData(MyJsonParser.getBeanFromMap((Map<String, Object>) tem.getData(), tClass));
- val jsonData: String =
- MyJsonParser.getJsontoMap(tem.getData() as Map<*, *>)
- response.setData(MyJsonParser.getBeanFromJson<T>(jsonData, tClass))
- } catch (e: Exception) {
- e.printStackTrace()
- }
- } else if (tem.getData() is List<*>) {
- try {
- response.setData(
- MyJsonParser.getListByJson<T>(
- MyJsonParser.getJsonbyList<Any>(
- tem.getData() as List<*>
- ), tClass
- ) as T
- )
- } catch (e: Exception) {
- e.printStackTrace()
- }
- } else if (tem.getData() is Int) {
- response.setData(tem.getData() as T)
- } else if (tem.getData() is Boolean) {
- response.setData(tem.getData() as T)
- }
- if (tClass.name is String && tem.getData() is String) {
- try {
- response.setData(tem.getData() as T)
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }
- return response
- }
- }
+ // 浣跨敤BaseResponse<T>浣滀负ProgressSubscriber鐨勬硾鍨嬬被鍨�
+ val mySubscriber = ProgressSubscriber<BaseResponse<T>>(context, hideLoading, listener);
- return null
- }
- }
+ observable
+ .subscribeOn(Schedulers.io())
+ .map(mapResponse(context, tClass))
.unsubscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mySubscriber)
}
-
-
-}
\ No newline at end of file
+
+ private fun <T> mapResponse(context: Context, tClass: Class<T>): Function<Any, BaseResponse<T>> {
+ return Function { rawResponse ->
+ when (rawResponse) {
+ is BaseResponse<*> -> {
+ val temp = rawResponse
+
+ // 鍒涘缓鏂扮殑鍝嶅簲瀵硅薄骞惰缃熀鏈睘鎬�
+ val response = BaseResponse<T>().apply {
+ code = temp.code
+ msg = temp.msg ?: ""
+ success = temp.success
+ }
+
+ // 澶勭悊token澶辨晥鐨勬儏鍐�
+ if (temp.code == NetConstans.TOKEN_INVALID) {
+ // 鍙互鍦ㄨ繖閲屾坊鍔犻噸瀹氬悜鍒扮櫥褰曢〉闈㈢殑閫昏緫
+ BusinessProvider.getBusinessProvider().startLoginNavigotor.navigateToLogin(context)
+ return@Function response
+ }
+
+ // 鏍规嵁鍐呭绫诲瀷杩涜澶勭悊
+ when (val content = temp.content) {
+ is Map<*, *> -> {
+ try {
+ val jsonData = MyJsonParser.getJsontoMap(content as Map<String, Any>)
+ response.content = MyJsonParser.getBeanFromJson(jsonData, tClass)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error parsing map content", e)
+ }
+ }
+ is List<*> -> {
+ try {
+ @Suppress("UNCHECKED_CAST")
+ response.content = MyJsonParser.getListByJson(
+ MyJsonParser.getJsonbyList(content),
+ tClass
+ ) as T
+ } catch (e: Exception) {
+ Log.e(TAG, "Error parsing list content", e)
+ }
+ }
+ is Int, is String, is Boolean -> {
+ @Suppress("UNCHECKED_CAST")
+ response.content = content as T
+ }
+ }
+
+ response
+ }
+ else -> throw IllegalArgumentException("Unexpected response type")
+ }
+ }
+ }
+
+}
--
Gitblit v1.8.0