package com.yidian.bcommon.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.viewbinding.ViewBinding
import com.yidian.commoncomponent.BaseFragment

/**
 * 所有Fragment的基类
 */
abstract class BaseFragment<VB : ViewBinding> : BaseFragment() {

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

  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    _binding = createViewBinding(inflater, container)
    return _binding?.root
  }

  abstract fun createViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB

  override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    init(savedInstanceState)
  }

  protected open fun init(savedInstanceState: Bundle?) {}

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

  fun showToast(msg: String) {
    Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
  }
}
