7.mvp中协程的使用

2020-08-10  本文已影响0人  学吉他的袁先生

view代码

class MvpActivity : BaseActivity<MvpPresenter>(), MvpContract.View {
    //    private val myPresenter:MvpPresenter by lifecycleScope.inject()
    override fun initData(savedInstanceState: Bundle?) {
        mPresenter = lifecycleScope.get<MvpPresenter>()
        mPresenter?.getArticle()
    }

    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_mvp
    override fun showArticleBean(articleBean: ArticleBean) {
        tvResult.text = articleBean.toString()
        LogUtils.i(articleBean.errorCode)
    }
}

presenter代码

class MvpPresenter(
    view: MvpContract.View,
    model: MvpContract.Model
) :
    BasePresenter<MvpContract.View, MvpContract.Model>(view, model) {

    fun getArticle() {
        launch {
            mRootView.showLoading()
            val articleBean = mModel.getArticleBean()
            mRootView.showArticleBean(articleBean)
        }
    }
}

model代码

class MvpModel(context: Context) : BaseModel(context), MvpContract.Model {
    override suspend fun getArticleBean(): ArticleBean {
        return apiService.getArticle()
    }
}

需要额外引用框架
implementation 'com.yuanwanli:easymvp:0.0.1'

上一篇下一篇

猜你喜欢

热点阅读