Skip to content

Instantly share code, notes, and snippets.

@hieuwu
Created August 1, 2023 14:37
Show Gist options
  • Save hieuwu/8c9e98e37eef33148284bb8d87799d1c to your computer and use it in GitHub Desktop.
Save hieuwu/8c9e98e37eef33148284bb8d87799d1c to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun SignInScreen(
modifier: Modifier = Modifier,
navController: NavController,
viewModel: SignInViewModel = hiltViewModel()
) {
val snackBarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()
Scaffold(
snackbarHost = { androidx.compose.material.SnackbarHost(snackBarHostState) },
topBar = {
TopAppBar(
navigationIcon = {
IconButton(onClick = {
navController.navigateUp()
}) {
Icon(
imageVector = Icons.Filled.ArrowBack,
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimary
)
}
},
backgroundColor = MaterialTheme.colorScheme.primary,
title = {
Text(
text = "Login",
color = MaterialTheme.colorScheme.onPrimary,
)
},
)
}
) { paddingValues ->
Column(
modifier = modifier
.padding(paddingValues)
.padding(20.dp)
) {
val email = viewModel.email.collectAsState(initial = "")
val password = viewModel.password.collectAsState()
androidx.compose.material.OutlinedTextField(
label = {
Text(
text = "Email",
color = MaterialTheme.colorScheme.primary,
style = MaterialTheme.typography.titleMedium
)
},
maxLines = 1,
shape = RoundedCornerShape(32),
modifier = modifier.fillMaxWidth(),
value = email.value,
onValueChange = {
viewModel.onEmailChange(it)
},
)
androidx.compose.material.OutlinedTextField(
label = {
Text(
text = "Password",
color = MaterialTheme.colorScheme.primary,
style = MaterialTheme.typography.titleMedium
)
},
maxLines = 1,
shape = RoundedCornerShape(32),
modifier = modifier
.fillMaxWidth()
.padding(top = 12.dp),
value = password.value,
onValueChange = {
viewModel.onPasswordChange(it)
},
)
val localSoftwareKeyboardController = LocalSoftwareKeyboardController.current
Button(modifier = modifier
.fillMaxWidth()
.padding(top = 12.dp),
onClick = {
localSoftwareKeyboardController?.hide()
viewModel.onGoogleSignIn()
}) {
Text("Sign in with Google")
}
Button(modifier = modifier
.fillMaxWidth()
.padding(top = 12.dp),
onClick = {
localSoftwareKeyboardController?.hide()
viewModel.onSignIn()
coroutineScope.launch {
snackBarHostState.showSnackbar(
message = "Sign in successfully !",
duration = SnackbarDuration.Long
)
}
}) {
Text("Sign in")
}
OutlinedButton(modifier = modifier
.fillMaxWidth()
.padding(top = 12.dp), onClick = {
navController.navigate(SignUpDestination.route)
}) {
Text("Sign up")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment