Skip to content

Commit f2663cb

Browse files
committed
更新Retrofit+协程封装
1 parent 887c451 commit f2663cb

File tree

33 files changed

+523
-413
lines changed

33 files changed

+523
-413
lines changed

app/src/main/java/com/fuusy/jetpackkt/MainApp.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.fuusy.jetpackkt
22

33
import android.app.Application
44
import android.content.Context
5+
import androidx.multidex.BuildConfig
56
import androidx.multidex.MultiDex
67
import com.alibaba.android.arouter.launcher.ARouter
78
import com.fuusy.common.loadsir.EmptyCallback
89
import com.fuusy.common.loadsir.ErrorCallback
910
import com.fuusy.common.loadsir.LoadingCallback
1011
import com.kingja.loadsir.core.LoadSir
1112

13+
1214
class MainApp : Application() {
1315

1416
override fun onCreate() {

common/src/main/java/com/fuusy/common/base/BaseFragment.kt

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,24 @@ package com.fuusy.common.base
22

33
import android.content.Context
44
import android.os.Bundle
5-
import android.util.Log
65
import android.view.LayoutInflater
76
import android.view.View
87
import android.view.ViewGroup
98
import android.widget.Toast
109
import androidx.databinding.DataBindingUtil
1110
import androidx.databinding.ViewDataBinding
1211
import androidx.fragment.app.Fragment
13-
import androidx.lifecycle.Observer
1412
import com.fuusy.common.R
1513
import com.fuusy.common.databinding.BaseFragmentLayoutBinding
16-
import com.fuusy.common.loadsir.EmptyCallback
17-
import com.fuusy.common.loadsir.ErrorCallback
18-
import com.fuusy.common.network.IStateView
1914
import com.fuusy.common.support.Constants
20-
import com.fuusy.common.support.NetStateHelper
2115
import com.fuusy.common.utils.SpUtils
16+
import com.fuusy.common.utils.ToastUtil
2217
import com.fuusy.common.view.LoadingDialog
23-
import com.kingja.loadsir.callback.Callback
2418
import com.kingja.loadsir.core.LoadService
25-
import com.kingja.loadsir.core.LoadSir
26-
import retrofit2.HttpException
27-
import java.net.SocketTimeoutException
2819

2920
private const val TAG = "BaseFragment"
3021

31-
abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment(),
32-
NetStateHelper.OnReloadListener {
22+
abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment() {
3323

3424
var mBinding: T? = null
3525
var mViewModel: VM? = null
@@ -38,18 +28,6 @@ abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment(
3828
private lateinit var loadService: LoadService<Any>
3929
private lateinit var mBaseContainBinding: BaseFragmentLayoutBinding
4030

41-
//请求失败时View
42-
private lateinit var errorView: View
43-
44-
//请求成功时View
45-
private lateinit var successView: View
46-
47-
//请求成功但数据为空时View
48-
private lateinit var emptyView: View
49-
private lateinit var netStateHelper: NetStateHelper
50-
51-
52-
5331
override fun onCreateView(
5432
inflater: LayoutInflater,
5533
container: ViewGroup?,
@@ -58,9 +36,8 @@ abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment(
5836
mBaseContainBinding =
5937
DataBindingUtil.inflate(inflater, R.layout.base_fragment_layout, container, false)
6038
mBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
61-
netStateHelper = NetStateHelper(context,container,mBinding?.root!!,mBaseContainBinding.baseContainer,this)
6239

63-
showSuccess()
40+
mBaseContainBinding.baseContainer.addView(mBinding?.root)
6441
return mBaseContainBinding.root
6542
}
6643

@@ -73,21 +50,6 @@ abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment(
7350
super.onViewCreated(view, savedInstanceState)
7451
mLoadingDialog = LoadingDialog(view.context, false)
7552
mViewModel = getViewModel()
76-
77-
mViewModel?.loadingLiveData?.observe(viewLifecycleOwner, Observer {
78-
if (it) {
79-
//show loading
80-
showLoading()
81-
} else {
82-
Log.d(TAG, "onViewCreated: not show loading")
83-
dismissLoading()
84-
}
85-
})
86-
mViewModel?.errorLiveData?.observe(viewLifecycleOwner, Observer {
87-
Log.d(TAG, "onViewCreated: error ")
88-
showError()
89-
throwableHandler(it)
90-
})
9153
initData()
9254
}
9355

@@ -115,26 +77,25 @@ abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment(
11577
mLoadingDialog.dismissDialog()
11678
}
11779

80+
private var time: Long = 0
81+
private var oldMsg: String? = null
11882

119-
private fun throwableHandler(e: Throwable) {
120-
when (e) {
121-
is SocketTimeoutException -> showToast("连接超时")
122-
is HttpException -> {
123-
if (e.code() == 504) {
124-
showToast("网络异常,请检查您的网络状态")
125-
} else if (e.code() == 404) {
126-
showToast("请求地址不存在")
127-
}
83+
/**
84+
* 相同msg 只显示一个。
85+
*/
86+
fun showToast(msg: String) {
87+
if (msg != oldMsg) {
88+
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
89+
time = System.currentTimeMillis()
90+
} else {
91+
if (System.currentTimeMillis() - time > 2000) {
92+
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
93+
time = System.currentTimeMillis()
12894
}
129-
else -> e.message?.let { showToast(it) }
13095
}
96+
oldMsg = msg
13197
}
13298

133-
fun showToast(msg: String) {
134-
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
135-
}
136-
137-
13899
protected fun isLogin(): Boolean {
139100
val userName = SpUtils.getString(Constants.SP_KEY_USER_INFO_NAME)
140101
if (userName == null || userName.isEmpty()) {
@@ -143,27 +104,4 @@ abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel> : Fragment(
143104
return true
144105
}
145106

146-
147-
/**
148-
* 根据net请求状态,数据为null
149-
*/
150-
protected fun showEmpty() {
151-
netStateHelper.showEmpty()
152-
}
153-
154-
protected fun showError() {
155-
netStateHelper.showError()
156-
}
157-
158-
protected fun showSuccess() {
159-
netStateHelper.showSuccess()
160-
}
161-
162-
override fun onReload() {
163-
onRetry()
164-
}
165-
166-
protected fun onRetry() {
167-
Log.d(TAG, "onRetry: ")
168-
}
169107
}

common/src/main/java/com/fuusy/common/base/BaseRepository.kt

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,75 @@
11
package com.fuusy.common.base
22

33
import android.util.Log
4+
import com.fuusy.common.network.net.StateLiveData
45
import com.fuusy.common.network.BaseResp
6+
import com.fuusy.common.network.DataState
57
import com.fuusy.common.network.ResState
68
import kotlinx.coroutines.CoroutineScope
79
import kotlinx.coroutines.coroutineScope
810
import java.io.IOException
911

1012

1113
/**
12-
*
14+
*Repository层基类
1315
*/
1416
private const val TAG = "BaseRepository"
15-
open class BaseRepository() {
1617

18+
open class BaseRepository {
19+
20+
/**
21+
* repo 请求数据的公共方法,
22+
* 在不同状态下先设置 baseResp.dataState的值,最后将dataState 的状态通知给UI
23+
*/
24+
suspend fun <T : Any> executeResp(
25+
block: suspend () -> BaseResp<T>,
26+
stateLiveData: StateLiveData<T>
27+
) {
28+
var baseResp = BaseResp<T>()
29+
try {
30+
baseResp.dataState = DataState.STATE_LOADING
31+
//开始请求数据
32+
val invoke = block.invoke()
33+
//将结果复制给baseResp
34+
baseResp = invoke
35+
if (baseResp.errorCode == 0) {
36+
//请求成功,判断数据是否为空,
37+
//因为数据有多种类型,需要自己设置类型进行判断
38+
if (baseResp.data == null || baseResp.data is List<*> && (baseResp.data as List<*>).size == 0) {
39+
//TODO: 数据为空,结构变化时需要修改判空条件
40+
baseResp.dataState = DataState.STATE_EMPTY
41+
} else {
42+
//请求成功并且数据为空的情况下,为STATE_SUCCESS
43+
baseResp.dataState = DataState.STATE_SUCCESS
44+
}
45+
46+
} else {
47+
//服务器请求错误
48+
baseResp.dataState = DataState.STATE_FAILED
49+
}
50+
} catch (e: Exception) {
51+
//非后台返回错误,捕获到的异常
52+
baseResp.dataState = DataState.STATE_ERROR
53+
baseResp.error = e
54+
} finally {
55+
stateLiveData.postValue(baseResp)
56+
}
57+
}
58+
59+
60+
/**
61+
* @deprecated Use {@link executeResp}
62+
* instead.
63+
*/
1764
suspend fun <T : Any> executeResp(
18-
resp: BaseResp<T>, successBlock: (suspend CoroutineScope.() -> Unit)? = null,
65+
resp: BaseResp<T>,
66+
successBlock: (suspend CoroutineScope.() -> Unit)? = null,
1967
errorBlock: (suspend CoroutineScope.() -> Unit)? = null
2068
): ResState<T> {
2169
return coroutineScope {
2270
if (resp.errorCode == 0) {
2371
successBlock?.let { it() }
24-
ResState.Success(resp.data)
72+
ResState.Success(resp.data!!)
2573
} else {
2674
Log.d(TAG, "executeResp: error")
2775
errorBlock?.let { it() }

common/src/main/java/com/fuusy/common/base/BaseViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import android.util.Log
44
import androidx.lifecycle.MutableLiveData
55
import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
7+
import com.fuusy.common.network.BaseResp
8+
import com.fuusy.common.network.DataState
79
import com.fuusy.common.support.SingleLiveData
810
import kotlinx.coroutines.*
911
import java.lang.Exception
@@ -33,4 +35,5 @@ open class BaseViewModel : ViewModel() {
3335
}
3436

3537

38+
3639
}

common/src/main/java/com/fuusy/common/loadsir/EmptyCallback.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fuusy.common.loadsir
2+
3+
import android.content.Context
4+
import android.view.View
5+
import com.fuusy.common.R
6+
import com.kingja.loadsir.callback.Callback
7+
8+
class EmptyCallback : Callback() {
9+
override fun onCreateView(): Int {
10+
return R.layout.base_layout_empty
11+
}
12+
13+
//当前Callback的点击事件,如果返回true则覆盖注册时的onReload(),如果返回false则两者都执行,先执行onReloadEvent()。
14+
override fun onReloadEvent(
15+
context: Context,
16+
view: View
17+
): Boolean {
18+
return false
19+
}
20+
}

common/src/main/java/com/fuusy/common/loadsir/ErrorCallback.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.fuusy.common.loadsir
2+
3+
import com.fuusy.common.R
4+
import com.kingja.loadsir.callback.Callback
5+
6+
class ErrorCallback : Callback() {
7+
override fun onCreateView(): Int {
8+
return R.layout.base_layout_error
9+
}
10+
}

common/src/main/java/com/fuusy/common/loadsir/LoadingCallback.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)