美文网首页
7.mvp中协程的使用

7.mvp中协程的使用

作者: 学吉他的袁先生 | 来源:发表于2020-08-10 09:00 被阅读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'

    相关文章

      网友评论

          本文标题:7.mvp中协程的使用

          本文链接:https://www.haomeiwen.com/subject/pkeddktx.html