WanAndroid基础款(MVVM+Kotlin+Jetpack+组件化)
stewForAni 2022-10-16 20:07 收藏
:avocado: WanAndroid基础款(MVVM+Kotlin+Jetpack+组件化)
首页 | 项目 | 导航 |
---|---|---|
登录 | 个人 | 收藏 |
---|---|---|
:strawberry: 项目介绍
项目采用组件化,架构如下:
- | - | app module | - | - |
---|---|---|---|---|
用户 kb_user module | 首页 kb_home module | 项目 kb_project module | 导航 kb_navigation module | 个人 kb_me module |
- | - | 公共 kb_common module | - | - |
BaseViewModel.kt
typealias vmBLOCK = suspend () -> Unit
open class BaseViewModel : ViewModel() {
protected fun launch(block: vmBLOCK) {
viewModelScope.launch {
try {
block.invoke()
} catch (e: Exception) {
onError(e)
}
}
}
private fun onError(e: Exception) {
Log.d("onError", "onError: $e")
}
}
BaseRepository.kt
open class BaseRepository {
suspend fun <T> dealResp(
block: suspend () -> BaseResp<T>,liveData: RespStateData<T>,) {
var result = BaseResp<T>()
result.responseState = BaseResp.ResponseState.REQUEST_START
liveData.value = result
try {
result = block.invoke()
when (result.errorCode) {
Constants.HTTP_SUCCESS -> {
result.responseState = BaseResp.ResponseState.REQUEST_SUCCESS
}
Constants.HTTP_AUTH_INVALID -> {
result.responseState = BaseResp.ResponseState.REQUEST_FAILED
ToastUtil.showMsg("认证过期,请重新登录!")
ARouter.getInstance().build(Constants.PATH_LOGIN).navigation()
}
else -> {
result.responseState = BaseResp.ResponseState.REQUEST_FAILED
ToastUtil.showMsg("code:" + result.errorCode.toString() + " / msg:" + result.errorMsg)
}
}
} catch (e: Exception) {
when (e) {
is UnknownHostException,
is HttpException,
is ConnectException,
-> {
//网络error
ToastUtil.showMsg("网络错误!")
}
else -> {
ToastUtil.showMsg("未知错误!")
}
}
result.responseState = BaseResp.ResponseState.REQUEST_ERROR
} finally {
liveData.value = result
}
}
}
:kiwifruit: 感谢
:grapes: 版本说明(持续更新...)
待完成:页面状态统一UI(Loading-UI,Error-UI)
V1.1 - 2022-08-30
bug fix ...
V1.0 - 2022-08-25
项目上传,持续更新
:lemon: License
Copyright [2022] [stew]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.