-
Notifications
You must be signed in to change notification settings - Fork 0
BindingFragment
kaloglu edited this page Jul 12, 2020
·
3 revisions
Create an base fragment as a application specific
abstract class ATBaseFragment<VDB, VM>(resourceLayoutId: Int) : BindingFragment<VDB, VM>(resourceLayoutId)
where VDB : ViewDataBinding, VM : BindableViewModel<*, *> {
override val activity by lazy { getActivity() as ATBaseActivity }
override val application by lazy { activity.application }
internal val navController by lazy { activity.navController }
val loginViewModel: LoginViewModel by activityViewModels { LoginViewModelFactory(lifecycle) }
override fun getBindingVariable() = BR.viewModel
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TwitterAuthConfig.DEFAULT_AUTH_REQUEST_CODE)
twitterLogin?.onActivityResult(requestCode, resultCode, data)
}
}Extend application specific base fragment
@ExperimentalCoroutinesApi
@InternalCoroutinesApi
class TweetListFragment : ATBaseFragment<TweetListFragmentBinding, TweetListViewModel>(R.layout.tweet_list_fragment) {
override val viewModel: TweetListViewModel by navGraphViewModels { TweetListViewModelFactory(lifecycle) }
override fun initUserInterface(savedInstanceState: Bundle?) {
viewDataBinding.adapter = TimelineAdapter() // if you prefer to use DataBoundRecyclerAdapter
loginViewModel.stateLiveData.observe(viewLifecycleOwner, Observer { loginState : LoginState ->
when (loginState) {
is LoginState.UnAuthenticated -> findNavController().navigate(R.id.loginDialogFragment)
is LoginState.Authenticated -> viewModel.postEvent(TweetListEvent.GetTweetList(loginState.user.id))
}
})
}
//region ***OPTIONAL*** UI action for NETWORK states. Loading progress, Empty message, etc...
override fun onStateLoading(state: State.Loading) {
showToast("Loading!!!!!")
Log.i(viewTag, state.javaClass.simpleName)
}
override fun onStateEmpty(state: State.Empty) {
showToast("Empty!!!!!")
Log.i(viewTag, state.javaClass.simpleName)
}
override fun onStateSuccess(state: State.Success) {
showToast("Success!!!!!")
Log.i(viewTag, state.javaClass.simpleName)
}
//endregion
}Full sample for databinding4vm : https://github.com/kaloglu/androtweet