BaseFragment.kt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.mrozon.utils.base
  2. import android.os.Bundle
  3. import android.view.LayoutInflater
  4. import android.view.View
  5. import android.view.ViewGroup
  6. import androidx.annotation.LayoutRes
  7. import androidx.databinding.DataBindingUtil
  8. import androidx.databinding.ViewDataBinding
  9. import androidx.fragment.app.Fragment
  10. import com.google.android.material.snackbar.Snackbar
  11. abstract class BaseFragment<T : ViewDataBinding>: Fragment()//,
  12. {
  13. // ViewTreeObserver.OnGlobalLayoutListener {
  14. // var rootView: View? = null
  15. var binding: T? = null
  16. @LayoutRes
  17. abstract fun getLayoutId(): Int
  18. abstract fun subscribeUi()
  19. // override fun onGlobalLayout() {
  20. // rootView!!.viewTreeObserver.removeOnGlobalLayoutListener(this)
  21. // }
  22. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  23. val layoutId = getLayoutId()
  24. // if (rootView != null) {
  25. // val parent = rootView!!.parent as ViewGroup
  26. // parent.removeView(rootView)
  27. // } else {
  28. // try {
  29. // binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
  30. // rootView = if (binding != null) {
  31. // binding!!.root
  32. // } else {
  33. // inflater.inflate(layoutId, container, false)
  34. // }
  35. // rootView!!.viewTreeObserver.addOnGlobalLayoutListener(this)
  36. // binding!!.executePendingBindings()
  37. //
  38. // } catch (e: InflateException) {
  39. // e.printStackTrace()
  40. // }
  41. // }
  42. binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
  43. subscribeUi()
  44. return binding!!.root
  45. }
  46. fun showError(message: String, action:()->Unit) {
  47. MyInfoDialog.newInstance(MyInfoDialogType.ERROR,message, action)
  48. .show(requireActivity().supportFragmentManager, MyInfoDialog.TAG)
  49. }
  50. fun showError(message: String) {
  51. MyInfoDialog.newInstance(MyInfoDialogType.ERROR,message)
  52. .show(requireActivity().supportFragmentManager, MyInfoDialog.TAG)
  53. }
  54. fun showInfo(message: String, action:()->Unit) {
  55. MyInfoDialog.newInstance(MyInfoDialogType.INFO,message, action)
  56. .show(requireActivity().supportFragmentManager, MyInfoDialog.TAG)
  57. }
  58. fun show(message: String) {
  59. val snackbar = Snackbar.make(binding?.root!!,message,Snackbar.LENGTH_LONG)
  60. snackbar.show()
  61. }
  62. }