KotlinMall实战之注册部分MVP架构配置

包目录如下:

 

 

 

 

①BaseView部分:基本的回调

interface BaseView {
fun showLoading()
fun hideLoading()
fun onError()
}

②BasePresenter部分:用泛型的方式使持有BaseView的引用

open class BasePresenter<T:BaseView> {
lateinit var mView:T
}

③BaseActivity部分:所有的Activity都应该继承它

open class BaseActivity:AppCompatActivity() {
}

④BaseMvpActivity部分:需要继承BaseActivity,用泛型的方式使持有BasePresenter的引用,需要实现BaseView

open class BaseMvpActivity<T:BasePresenter<*>>:BaseActivity() ,BaseView{
override fun showLoading() {
}

override fun hideLoading() {
}

override fun onError() {
}
lateinit var mPresenter:T

⑤RegisterView部分:需要实现BaseView

interface RegisterView:BaseView{
fun onRegisterResult(result:Boolean)
}

⑥RegisterPresenter部分:继承BasePresenter

open class RegisterPresenter: BasePresenter<RegisterView>() {
fun register(moblie:String,verifyCode:String){
/*
业务逻辑:请求网络做注册等等
*/
mView.onRegisterResult(true)
}
}

⑥RegisterActivity部分:继承BaseMvpActivity,需要实现RegisterView

class RegisterActivity :BaseMvpActivity<RegisterPresenter>(),RegisterView{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mPresenter= RegisterPresenter()
mPresenter.mView=this
mregisterBtn.setOnClickListener {
mPresenter.register("","")
}
}

override fun onRegisterResult(result: Boolean) {
toast("注册成功")
}
}
posted @ 2021-01-30 20:47  虞美人体重90  阅读(65)  评论(0)    收藏  举报