package com.lukouguoji.module_base.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import kotlin.properties.Delegates

abstract class BaseBindingFragment<T : ViewDataBinding, VM : ViewModel> : Fragment() {

    protected var binding: T by Delegates.notNull()

    protected val viewModel: VM by lazy {
        ViewModelProvider(this).get(viewModelClass())
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, layoutId(), container, false)

        binding.lifecycleOwner = this

        return binding.root
    }

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


    /**
     * Fragment布局ID
     */
    abstract fun layoutId(): Int

    /**
     * ViewModel类
     */
    abstract fun viewModelClass(): Class<VM>

    /**
     * 初始化Fragment内容
     */
    abstract fun initOnViewCreated()

}