Andrews notes - I package base class (activity / fragment / listener)

Just encapsulates some of the features I use often in the development process, this article only for the record, I used development experience.
baseActivity code is as follows

/**
 *@author Chord.p
 *@time 2019-12-30 14:34
 *speak softly love
 */
open class BaseActivity : AppCompatActivity() {
    val mMessageDialog : MessageDialog by lazy(::MessageDialog)

    override fun onBackPressed() { commonBack() }

    open fun initView() {}

    open fun initViewModel() {}

    open fun bindListener() {}

    open fun commonBack() { finish() }

    open fun toast(content : String , time : Int = Toast.LENGTH_SHORT) { Toast.makeText(this,content,time).show() }

    open fun navigateTo(intent: Intent) { startActivity(intent) }

    open fun handleErrorData(data : BaseData<Any>) {
        when(data.code) {
            //用来处理网络请求返回error的数据的情况
        }
    }

    open fun <T> navigateTo(clz: Class<T>) { startActivity(Intent(this,clz)) }

    open fun waitDialog(isShow : Boolean) {
        if (isShow) {
            WaitDialog.show(this,"加载中,请稍等...")
        }else{
            WaitDialog.dismiss()
        }
    }

baseFragment code is as follows

/**
 *@author Chord.p
 *@time 2020-01-02 18:11
 *speak softly love
 */
open class BaseFragment : Fragment() {

    open fun initViewModel() {}

    open fun initView() {}

    open fun bindView() {}

    open fun toast(content : String, time : Int = Toast.LENGTH_SHORT) { Toast.makeText(context,content,time).show() }

    open fun navigateTo(intent: Intent) { startActivity(intent) }

    open fun handleErrorData(data : BaseData<Any>) {
        when(data.code) {
            //用来处理网络请求返回error的数据的情况
        }
    }

    open fun <T> navigateTo(clz: Class<T>) { startActivity(Intent(context,clz)) }

    open fun waitDialog(isShow : Boolean) {
        if (isShow) {
            WaitDialog.show(activity as AppCompatActivity,"加载中,请稍等...")
        }else{
            WaitDialog.dismiss()
        }
    }
}

Listener code is as follows

/**
 *@author Chord.p
 *@time 2019-12-19 10:56
 *speak softly love
 */
class BaseListener<T> (onNotice : (value : T) -> Unit) {
    var onNotice = onNotice
}
Released six original articles · won praise 0 · Views 80

Guess you like

Origin blog.csdn.net/u011997929/article/details/105141506