rebase
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
package com.mogo.utils;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-03-23
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class WorkThreadHandler {
|
||||
|
||||
private Looper mThreadLooper;
|
||||
private Handler mHandler;
|
||||
private HandlerThread mThread;
|
||||
|
||||
public static WorkThreadHandler newInstance( String name ) {
|
||||
return new WorkThreadHandler( name );
|
||||
}
|
||||
|
||||
private WorkThreadHandler( String name ) {
|
||||
// private constructor
|
||||
mThread = new HandlerThread( name );
|
||||
mThread.start();
|
||||
mThreadLooper = mThread.getLooper();
|
||||
mHandler = new Handler( mThreadLooper );
|
||||
}
|
||||
|
||||
private WorkThreadHandler() {
|
||||
// private constructor
|
||||
this( "work-thread-handler" );
|
||||
}
|
||||
|
||||
private static final class InstanceHolder {
|
||||
private static final WorkThreadHandler INSTANCE = new WorkThreadHandler();
|
||||
}
|
||||
|
||||
public static WorkThreadHandler getInstance() {
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
public Looper getLooper() {
|
||||
return mThreadLooper;
|
||||
}
|
||||
|
||||
private Object readResolve() {
|
||||
// 阻止反序列化,必须实现 Serializable 接口
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private Object sToken = new Object();
|
||||
|
||||
public boolean post( Runnable r ) {
|
||||
return mHandler != null && mHandler.post( r );
|
||||
}
|
||||
|
||||
public boolean postDelayed( Runnable r, long delayMillis ) {
|
||||
return mHandler != null && mHandler.postDelayed( r, delayMillis );
|
||||
}
|
||||
|
||||
public Handler getWorkThreadHandler() {
|
||||
return mHandler;
|
||||
}
|
||||
|
||||
public boolean postOnceDelayed( Runnable r, long delayMillis ) {
|
||||
if ( mHandler == null ) {
|
||||
return false;
|
||||
} else {
|
||||
mHandler.removeCallbacks( r, sToken );
|
||||
return mHandler.postDelayed( r, delayMillis );
|
||||
}
|
||||
}
|
||||
|
||||
public void removeCallbacks( Runnable runnable ) {
|
||||
if ( mHandler != null ) {
|
||||
mHandler.removeCallbacks( runnable );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.mogo.utils.network
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.mogo.eagle.core.data.BaseResponse
|
||||
import com.mogo.utils.network.exception.ApiException
|
||||
import com.mogo.utils.network.exception.CommonException.Companion.NETWORK_EXCEPTION
|
||||
import com.mogo.utils.network.exception.CommonException.Companion.NULL_EXCEPTION
|
||||
import com.mogo.utils.network.exception.CommonException.Companion.NULL_REQUEST_DATA_API_EXCEPTION
|
||||
import kotlinx.coroutines.*
|
||||
import java.net.SocketTimeoutException
|
||||
import java.net.UnknownHostException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.TimeoutException
|
||||
|
||||
class Request<T> {
|
||||
private lateinit var loader: suspend () -> T
|
||||
|
||||
private var start: (() -> Unit)? = null
|
||||
|
||||
private var onSuccess: ((T) -> Unit)? = null
|
||||
|
||||
private var onError: ((java.lang.Exception) -> Unit)? = null
|
||||
|
||||
private var onComplete: (() -> Unit)? = null
|
||||
|
||||
private var addLifecycle: LifecycleOwner? = null
|
||||
|
||||
infix fun loader(loader: suspend () -> T) {
|
||||
this.loader = loader
|
||||
}
|
||||
|
||||
infix fun start(start: (() -> Unit)?) {
|
||||
this.start = start
|
||||
}
|
||||
|
||||
infix fun onSuccess(onSuccess: ((T) -> Unit)?) {
|
||||
this.onSuccess = onSuccess
|
||||
}
|
||||
|
||||
infix fun onError(onError: ((java.lang.Exception) -> Unit)?) {
|
||||
this.onError = onError
|
||||
}
|
||||
|
||||
infix fun onComplete(onComplete: (() -> Unit)?) {
|
||||
this.onComplete = onComplete
|
||||
}
|
||||
|
||||
infix fun addLifecycle(addLifecycle: LifecycleOwner?) {
|
||||
this.addLifecycle = addLifecycle
|
||||
}
|
||||
|
||||
fun request(requestKey: String? = null) {
|
||||
|
||||
//todo list 缓存result ,在停止时关闭
|
||||
GlobalScope.launch(context = Dispatchers.Main) {
|
||||
|
||||
start?.invoke()
|
||||
try {
|
||||
val deferred = GlobalScope.async(Dispatchers.IO, start = CoroutineStart.LAZY) {
|
||||
loader()
|
||||
}
|
||||
|
||||
requestKey?.let {
|
||||
deferredMap[requestKey] = deferred as Deferred<Any>
|
||||
}
|
||||
|
||||
val result = deferred.await()
|
||||
if (result != null && result is BaseResponse<*>) {
|
||||
if (result.code == 0 || result.code == 200) {
|
||||
onSuccess?.invoke(result)
|
||||
} else {
|
||||
throw ApiException(result.code, result.msg)
|
||||
}
|
||||
} else {
|
||||
throw NULL_REQUEST_DATA_API_EXCEPTION
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
//数据打点
|
||||
if (e == null) {
|
||||
onError?.invoke(NULL_EXCEPTION)
|
||||
}
|
||||
when (e) {
|
||||
is UnknownHostException -> onError?.invoke(NETWORK_EXCEPTION)
|
||||
is TimeoutException -> onError?.invoke(NETWORK_EXCEPTION)
|
||||
is SocketTimeoutException -> onError?.invoke(NETWORK_EXCEPTION)
|
||||
else -> onError?.invoke(java.lang.Exception(e.message))
|
||||
}
|
||||
} finally {
|
||||
requestKey?.let {
|
||||
if (deferredMap.contains(requestKey)) {
|
||||
deferredMap.remove(requestKey)
|
||||
}
|
||||
}
|
||||
onComplete?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline fun <T> request(requestKey: String? = "", buildRequest: Request<T>.() -> Unit) {
|
||||
Request<T>().apply(buildRequest).request(requestKey)
|
||||
}
|
||||
|
||||
private val deferredMap = ConcurrentHashMap<String, Deferred<Any>>()
|
||||
|
||||
fun cancel(requestKey: String) {
|
||||
if (deferredMap.contains(requestKey)) {
|
||||
deferredMap[requestKey]!!.cancel(CancellationException("manual cancel !"))
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun <T : Any> apiCall(call: suspend () -> BaseResponse<T>): BaseResponse<T> {
|
||||
return call.invoke()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.mogo.utils.network.exception
|
||||
|
||||
class ApiException : CommonException {
|
||||
|
||||
constructor(code: Int, msg: String) : super(code, msg)
|
||||
|
||||
fun getErrorMsg():String{
|
||||
return msg
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.mogo.utils.network.exception
|
||||
|
||||
open class CommonException :Exception{
|
||||
|
||||
companion object{
|
||||
val NETWORK_EXCEPTION = CommonException(1, "network is error")
|
||||
val NULL_EXCEPTION = CommonException(1, "exception is null")
|
||||
val NULL_REQUEST_DATA_API_EXCEPTION = CommonException(1, "request data is null")
|
||||
}
|
||||
|
||||
protected var code: Int = 0
|
||||
protected var msg: String = ""
|
||||
|
||||
constructor(code: Int, msg: String) : super(msg) {
|
||||
this.code = code
|
||||
this.msg = msg
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user