package com.rdiot.yx485.base

import android.os.Bundle
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.rdiot.yx485.util.hideKeyboard
import com.rdiot.yx485.util.isShouldHideKeyboard


/**
 *
 * @author mR2hao
 * @date 2021/8/2
 */
abstract class BaseActivity<TBinding : ViewDataBinding> : AppCompatActivity() {
//    @Inject
//    lateinit var appData: AppData

    protected abstract fun getLayoutId(): Int

    protected abstract fun initView(savedInstanceState: Bundle?)

    protected lateinit var binding: TBinding

    /** 是否前台显示 */
    var isFront = false

    override fun onCreate(savedInstanceState: Bundle?) {
        AppManager().addActivity(this)
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, getLayoutId())
        initView(savedInstanceState)
    }

    override fun onDestroy() {
        AppManager().finishActivity(this)
        super.onDestroy()
    }

    /** 拦截点击事件，当输入法弹出时，点击非输入框位置就隐藏输入法 */
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus ?: return super.dispatchTouchEvent(ev)
            if (v.isShouldHideKeyboard(ev)) {
                val res = hideKeyboard(v.windowToken)
                if (res) return true
            }
        }
        return super.dispatchTouchEvent(ev)
    }


    override fun onResume() {
        super.onResume()
        isFront = true
    }

    override fun onStop() {
        super.onStop()
        isFront = false
    }

}