WanAndroid-基础款

367 阅读1分钟

🥑 WanAndroid基础款(MVVM+Kotlin+Jetpack+组件化)


首页项目导航
w1.jpegw2.jpegw3.jpeg
收藏个人登录
w6.jpegw5.jpegw4.jpeg

🍓 项目介绍

项目采用组件化,架构如下:

------app Module------

用户Module

首页Module

项目Module

导航Module

个人Module

---

---

公共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
        }
    }
}

🥝 感谢

🍇 版本说明(持续更新...)

  • 待完成:页面状态统一UI(Loading-UI,Error-UI)

  • V1.1 - 2022-08-30
    bug fix ...

  • V1.0 - 2022-08-25
    项目上传,持续更新

结尾

项目Github地址
非常感谢鸿洋大神提供的API,以及众多大佬牛逼的开源项目!

:该项目是本人参考了众多大神级人物的mvvm架构项目+结合自身业务需求所编写,架构和代码都没有严谨遵守官方规则,仅供参考,酌情使用,当然也非常欢迎大家⭐️Star&Fork⭐️
参考项目:
github.com/android/arc…
github.com/fuusy/compo…
github.com/yechaoa/wan…