泛型使用技巧

2022-04-20  本文已影响0人  fyg
Snip20220420_5.png
fun <T> parseResponse(iLoadingView: ILoadingView,onSuccess: ((T) -> Unit),onError: ((AppException) -> Unit)? = null) {
        when (status) {
            SUCCESS -> {
                /**
                    由于RootResultForRetrofit.isCommunicationOk 需要接收 RootResultForRetrofit 类型,
                     data 实际类型是 RootResultForRetrofit<CollectListResultInfo>,但编译时不识别,我们需要和他套上真实的类型,这样就可以调用isCommunicationOk 方法判断了
                     把data 类型改为 (RootResultForRetrofit<T>)类型,
                    data class Resource<T>(val status: Status,
                    val data: RootResultForRetrofit<T>?,val message: String?,val throwable: Throwable?)
                    这里没法判断 业务接口是否调用成功,需要脱一层外衣*/
                if (RootResultForRetrofit.isCommunicationOk(data)) {
                    onSuccess?.invoke(data!!.mData)
                } else {
                    onError?.invoke(AppException(data.mErrorCode, data.mErrorMsg))
                }
            }
            ERROR -> {
                JXToast.showShort(ParseNetThrowable.parseThrowable(throwable).toString())
            }
            LOADING_START -> {
                iLoadingView?.showLoadingDialog()
            }
            LOADING_END -> {
                iLoadingView?.dismissLoadingDialog()
            }
        }
    }

Snip20220420_10.png

data class Resource<T>(
    val status: Status,
    val data: RootResultForRetrofit<T>?,
    val message: String?,
    val throwable: Throwable?
) {

    fun parseResponse(
        iLoadingView: ILoadingView,
        onSuccess: ((T) -> Unit),
        onError: ((AppException) -> Unit)? = null
    ) {
        when (status) {
            SUCCESS -> {

                if (RootResultForRetrofit.isCommunicationOk(data)) {
                    onSuccess?.invoke(data!!.mData)
                } else {
                    data?.let {
                        onError?.invoke(AppException(data.mErrorCode, data.mErrorMsg))
                    }
                }
            }
            ERROR -> {
                JXToast.showShort(ParseNetThrowable.parseThrowable(throwable).toString())
            }
            LOADING_START -> {
                iLoadingView?.showLoadingDialog()
            }
            LOADING_END -> {
                iLoadingView?.dismissLoadingDialog()
            }
        }
    }



    companion object {
        fun <T> success(data: RootResultForRetrofit<T>?): Resource<T> =
            Resource(status = SUCCESS, data = data, message = null, throwable = null)
        fun <T> error(data: RootResultForRetrofit<T>?, message: String, throwable: Throwable): Resource<T> =
            Resource(status = ERROR, data = data, message = message, throwable = throwable)
        fun <T> loadingStart(data: RootResultForRetrofit<T>?): Resource<T> =
            Resource(status = LOADING_START, data = data, message = null, throwable = null)
        fun <T> loadingEnd(data: RootResultForRetrofit<T>?): Resource<T> =
            Resource(status = LOADING_END, data = data, message = null, throwable = null)

    }
}


Snip20220420_14.png

private fun <T> parseResponse(iLoadingView:ILoadingView, resource: State<RootResultForRetrofit<T>>,
                                  onSuccess: ((T) -> Unit), onError: ((AppException) -> Unit)? = null) {
        resource.let { resource ->
            when (resource.status) {
                SUCCESS -> {
                    resource.data?.apply {
                        if (RootResultForRetrofit.isCommunicationOk(this)){
                            onSuccess?.invoke(resource.data!!.mData)
                        }else{
                            onError?.invoke(AppException(resource.data.mErrorCode, resource.data.mErrorMsg))
                        }
                    }

                    mTestBtn.append("SUCCESS  ")
                }
                ERROR -> {
                    JXToast.showShort(ParseNetThrowable.parseThrowable(resource.throwable).toString())
                    mTestBtn.append("ERROR    ")
                }
                LOADING_START -> {
                    iLoadingView?.showLoadingDialog()
                    mTestBtn.append("  LOADING_START  ")
                }
                LOADING_END -> {
                    iLoadingView?.dismissLoadingDialog()
                    mTestBtn.append("  LOADING_END  ")
                }
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读