Created
March 21, 2022 11:56
-
-
Save Ahmedgadein/4167e0301429cc76d5f75381b9cf62a2 to your computer and use it in GitHub Desktop.
This file contains 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
class LoginFragment : Fragment(){ | |
private val viewModel: LoginViewModel by viewModels() | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View { | |
val binding = LoginFragmentBinding.inflate(inflater, container, false) | |
// ... | |
binding.loginButton.setOnClickListener { | |
val user: User = ... | |
viewModel.onLoginAttempt(user) | |
} | |
lifecycleScope.launchWhenCreated { | |
viewModel.loginUiState.collect { | |
// Show snackbar when an error occurs | |
it.errorMessages.firstOrNull()?.let { message -> | |
showSnackbar(message) | |
viewModel.userMessageShown() | |
} | |
if(it.navigateToHome){ | |
// Navigate using navigation components | |
navigateToHomeFragment() | |
} | |
if(it.navigateToRegistration){ | |
// Navigate using navigation components | |
navigateToRegistrationFragment() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment