Android MVP+Kotlin+ViewBinding+Retrofit+RxJava基类封装

这篇文章主要介绍一下MVP的封装

其中封装ViewBinding涉及到郭霖大神的一篇文章

优雅地封装和使用 ViewBinding
https://blog.csdn.net/c10WTiybQ1Ye3/article/details/112690188

整个框架如图

Android MVP+Kotlin+ViewBinding+Retrofit+RxJava基类封装_第1张图片

首先在build.gradle中添加

  android{
    viewBinding {
        enabled = true
    }
  }
dependencies{
	implementation 'com.dylanc:viewbinding-ktx:1.0.0'
	
	implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.14'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
}

为了方便管理,将MVP中 View 与 Present 的接口统一放在一个BaseContract接口中

/**
 * @author ludoven
 */
interface BaseContract {
    interface BasePresenter {
        fun attachView(view: BaseView)
        fun detachView()
        fun cancelAll()
    }

    interface BaseView {
        /**
         * @param flag 用于标记对应接口
         * @param e 错误信息
         */
        fun showError( e: Throwable?)
    }
}

实现BasePresenter的基类

open class BasePresenter<V: BaseContract.BaseView> : BaseContract.BasePresenter {
    protected var mView: V? = null
    private var weakView: WeakReference<BaseContract.BaseView>? = null
    protected var listReqs: MutableList<Disposable> =
        ArrayList()

    override fun attachView(view: BaseContract.BaseView) {
        weakView = WeakReference(view)
        mView = weakView!!.get() as V?
    }

    override fun detachView() {
        if (mView != null) {
            mView = null
            weakView!!.clear()
            weakView = null
        }
    }

    override fun cancelAll() {
        for (disposable in listReqs) {
            disposable.dispose()
        }
    }

    protected fun addReqs(disposable: Disposable) {
        listReqs.add(disposable)
    }
}

BaseActivity的封装

abstract class BaseActivity<VB : ViewBinding, P : BaseContract.BasePresenter> : AppCompatActivity(),
    BaseContract.BaseView {
    protected var mPresenter: P? = null
    protected lateinit var binding: VB


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mPresenter = getPresenter()
        mPresenter?.attachView(this)
        binding = this.inflateBindingWithGeneric(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    abstract fun getPresenter(): P
    
    protected abstract fun initView()
    
    override fun showError(e: Throwable?) {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (mPresenter != null) {
            mPresenter!!.cancelAll()
            mPresenter!!.detachView()
        }
        System.gc()
    }
}

BaseFragment的封装

abstract class BaseFragment<VB : ViewBinding,V, P : BaseContract.BasePresenter> :
    Fragment(), BaseContract.BaseView {
    protected var mPresenter: P? = null

    private var _binding: VB? = null
    val binding:VB get() = _binding!!

    override fun showError(e: Throwable?) {

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = inflateBindingWithGeneric(layoutInflater)

        return _binding!!.root
    }


    protected abstract fun initView()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mPresenter = getPresenter()
        if (mPresenter != null) {
            mPresenter?.attachView(this)
        }

    }

    override fun onViewCreated(
        view: View,
        savedInstanceState: Bundle?
    ) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    protected abstract fun getPresenter(): P

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mPresenter != null) {
            mPresenter?.cancelAll()
            mPresenter?.detachView()
        }
        System.gc()
    }
}

到这MVP框架已经封装完成了,网络请求以及完整的代码在下方

Android MVP+Kotlin+ViewBinding+Retrofit+RxJava基类封装 完整代码

你可能感兴趣的:(2021,android,mvp,retrofit,封装,viewbinding)