-
-
Save harryhan24/6a0a36871bfc6b6b666edc165ab50172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class BaseFragment<VB : ViewDataBinding, VM : BaseViewModel> : DaggerFragment() { | |
| abstract fun getViewModelClass(): Class<VM> | |
| abstract fun layoutId(): Int | |
| @Inject | |
| lateinit var viewModelFactory: ViewModelProvider.Factory | |
| protected lateinit var binding: VB | |
| protected lateinit var viewModel: VM | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| viewModel = ViewModelProviders.of(this, viewModelFactory)[getViewModelClass()] | |
| observe(viewModel.errorMessage) { it -> | |
| it.getContentIfNotHandled()?.let { | |
| (activity as BaseActivity).alert(it.messageStringRes) | |
| } | |
| } | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, | |
| container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View { | |
| val view = inflater.inflate(layoutId(), container, false) | |
| binding = DataBindingUtil.bind(view)!! | |
| binding.setLifecycleOwner(this) | |
| return view | |
| } | |
| fun navController() = findNavController(view!!) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment