7.mvp中协程的使用

view代码

class MvpActivity : BaseActivity(), MvpContract.View {
    //    private val myPresenter:MvpPresenter by lifecycleScope.inject()
    override fun initData(savedInstanceState: Bundle?) {
        mPresenter = lifecycleScope.get()
        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(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'

你可能感兴趣的:(7.mvp中协程的使用)