kotlin实战---MVP模式实现登录,实现Base层封装,秀出天际

class LoginActivity : BaseActivity<LoginPresenter>(),LoginView {


    private lateinit var mMediaPlayer:MediaPlayer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
       user_login_bt.setOnClickListener {

           doLogin()
       }
    }

    private fun doLogin() {
        val userName=user_phone_et.text.toString()
        val pwd=user_password_et.text.toString()
        //只关心P层
        presenter.loginAction(this@LoginActivity,userName,pwd)
//            .
    }
    override fun getLayoutID(): Int = R.layout.activity_login

    override fun createP(): LoginPresenter =LoginPresenterImpl(this)

    override fun recycle() {
        presenter.unAttachView()
    }

    override fun loginSuccess(loginBean: LoginResponse?) {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        Toast.makeText(this@LoginActivity,"登陆成功嘿嘿~",Toast.LENGTH_SHORT).show()
    }

    override fun loginFialure(errorMsg: String?) {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        Toast.makeText(this@LoginActivity,errorMsg,Toast.LENGTH_SHORT).show()
    }
} 

2.2、Presenter层的实现

Presenter类的完全把Model层和View层进行隔离,Model和View要产生联系,所以Presenter必须拿到Model层和View层 IBasePrensenter

interface IBasePresenter {
//    fun attachView()
    //视图离开了
    fun  unAttachView()
} 

LoginPresenter

//Presenter 层
interface LoginPresenter:IBasePresenter {
    //登录
    fun loginAction(context:Context,userName:String,password:String)

    //监听回调
    interface  OnLoginListener{
        fun loginSuccess(loginBean: LoginResponse?)
        fun loginFialure(errrMsg:String?)
    }
} 

LoginPresenterImpl

//P 层是需要拿到 Model和View两边的
class LoginPresenterImpl(var loginView: LoginView?) :LoginPresenter,LoginPresenter.OnLoginListener {
    
    //Model  请求服务器
    private val loginModel=LoginModelImpl()
    //view 去更新UI

    override fun loginAction(context: Context, userName: String, password: String) {
        //        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        //做很多事情 校验 啊
        // TODO 调用模型层
        loginModel.login(context,userName,password,this)
    }

    //接收Model的结果
    override fun loginSuccess(loginBean: LoginResponse?) {
     //   TODO("not implemented")校验 结果集
        //...
        //回调到View层 更新UI
        loginView?.loginSuccess(loginBean)
    }

    override fun loginFialure(errrMsg: String?) {
        //   TODO("not implemented")校验 结果集
        //...
        //回调到View层 更新UI
        loginView?.loginFialure(errrMsg)
    }
    override fun unAttachView() {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        loginView= null
        loginModel.cancelRequest()
    }
} 

2.3、Model层实现

LoginModel

interface LoginModel {
    fun cancelRequest()
    //登录
    fun login(context: Context,userName:String,password:String,
              //把结果回调 给P层
    onLoginListener: LoginPresenter.OnLoginListener)


} 

LoginModelImpl 在此进行网络请求,并将结果回调给Presenter

class LoginModelImpl :LoginModel{
    override fun cancelRequest() {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun login(
        context: Context,
        userName: String,
        password: String,
        onLoginListener: LoginPresenter.OnLoginListener
    ) =
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        APIClient.instance.instanceRetrofit(WanAndroidAPI::class.java)
            .loginAction(userName,password)
            .subscribeOn(Schedulers.io())//在IO线程进行网络请求
            .observeOn(AndroidSchedulers.mainThread())//在主线程更新UI
            .subscribe(object: APIResponse<LoginResponse>(context){
                override fun onSuccess(data: LoginResponse?) {

                    onLoginListener.loginSuccess(data)
                }

                override fun onFailure(errorMsg: String?) {
//                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
//                    Toast.makeText(this@LoginActivity,errorMsg,Toast.LENGTH_SHORT).show()
                    //回调给P层
                    onLoginListener.loginFialure(errorMsg)
                }

            })

### 尾声

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

* **思维脑图**
  ![](https://img-blog.csdnimg.cn/img_convert/0ec7e6c3031ca43f1cac9c5219b29a5f.png)
* **性能优化学习笔记**
  ![](https://img-blog.csdnimg.cn/img_convert/25cea18222c912440a1645003ff6f5bb.png)
  ![](https://img-blog.csdnimg.cn/img_convert/ed34f9a3995760d7653e2f0e545b821d.png)

![](https://img-blog.csdnimg.cn/img_convert/9fa6ad983d037ae8621f870c50e6df78.png)
![](https://img-blog.csdnimg.cn/img_convert/32af3478fc42adcf76c30f3d190af475.png)


* **性能优化视频**
  ![](https://img-blog.csdnimg.cn/img_convert/15bc703fddc604e833a00a33383b7a00.png)
  当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
中...(img-oocPABzq-1630580282436)]
[外链图片转存中...(img-jzM8mFyU-1630580282437)]


* **性能优化视频**
  [外链图片转存中...(img-IswTpaWA-1630580282439)]
  当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
  **[CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》](https://codechina.csdn.net/m0_60958482/android_p7)**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值