7.mvp中协程的使用

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'

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。