From 31b6aed8a8820ef745b19ac6ba2ef6cbbf9b29d0 Mon Sep 17 00:00:00 2001 From: Thomas Andres Gomez Date: Sun, 9 May 2021 12:26:22 +0200 Subject: [PATCH] Refacor login screen composable. --- .../screen/LoginScreenComposable.kt | 154 ++++++++++-------- 1 file changed, 84 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/com/pixelized/biblib/ui/composable/screen/LoginScreenComposable.kt b/app/src/main/java/com/pixelized/biblib/ui/composable/screen/LoginScreenComposable.kt index 3348977..61c5790 100644 --- a/app/src/main/java/com/pixelized/biblib/ui/composable/screen/LoginScreenComposable.kt +++ b/app/src/main/java/com/pixelized/biblib/ui/composable/screen/LoginScreenComposable.kt @@ -72,7 +72,6 @@ fun LoginScreenComposable( private fun LoginScreenContentComposable( authentication: IAuthentication, ) { - val typography = MaterialTheme.typography Column( modifier = Modifier .fillMaxWidth() @@ -81,77 +80,11 @@ private fun LoginScreenContentComposable( .padding(16.dp) ) { Spacer(modifier = Modifier.weight(1f)) - - Text( - modifier = Modifier - .padding(vertical = 16.dp) - .align(alignment = Alignment.CenterHorizontally), - style = typography.h4, - text = stringResource(id = R.string.welcome_sign_in) - ) - + Title() Spacer(modifier = Modifier.weight(1f)) - - val focusRequester = remember { FocusRequester() } - val localFocus = LocalFocusManager.current - LoginField( - authentication = authentication, - modifier = Modifier - .fillMaxWidth() - .padding(bottom = 16.dp), - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next), - keyboardActions = KeyboardActions { focusRequester.requestFocus() } - ) - PasswordField( - authentication = authentication, - modifier = Modifier - .fillMaxWidth() - .padding(bottom = 16.dp) - .focusRequester(focusRequester), - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), - keyboardActions = KeyboardActions { localFocus.clearFocus() } - ) - CredentialRemember( - authentication = authentication, - modifier = Modifier - .height(48.dp) - .padding(bottom = 16.dp) - ) - Row( - modifier = Modifier - .padding(bottom = 16.dp) - .align(Alignment.End) - ) { - val context = LocalContext.current - Button( - modifier = Modifier.padding(end = 8.dp), - colors = outlinedButtonColors(), - onClick = { - context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(REGISTER_URL))) - }) { - Text(text = stringResource(id = R.string.action_register)) - } - Button(onClick = { - authentication.login() - }) { - Text(text = stringResource(id = R.string.action_login)) - } - } - + Form(authentication) Spacer(modifier = Modifier.weight(2f)) - - Button( - modifier = Modifier.fillMaxWidth(), - colors = outlinedButtonColors(), - onClick = { - authentication.loginWithGoogle() - }) { - Image( - modifier = Modifier.padding(end = 8.dp), - painter = painterResource(id = R.drawable.ic_google), contentDescription = "" - ) - Text(text = stringResource(id = R.string.action_google_sign_in)) - } + SignIn(authentication) } } @@ -220,6 +153,87 @@ private fun LoginScreenNavigationComposable( } } +@Composable +private fun ColumnScope.Title() { + val typography = MaterialTheme.typography + Text( + modifier = Modifier + .padding(vertical = 16.dp) + .align(alignment = Alignment.CenterHorizontally), + style = typography.h4, + text = stringResource(id = R.string.welcome_sign_in) + ) +} + +@Composable +private fun ColumnScope.Form( + authentication: IAuthentication, +) { + val focusRequester = remember { FocusRequester() } + val localFocus = LocalFocusManager.current + LoginField( + authentication = authentication, + modifier = Modifier + .fillMaxWidth() + .padding(bottom = 16.dp), + keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next), + keyboardActions = KeyboardActions { focusRequester.requestFocus() } + ) + PasswordField( + authentication = authentication, + modifier = Modifier + .fillMaxWidth() + .padding(bottom = 16.dp) + .focusRequester(focusRequester), + keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), + keyboardActions = KeyboardActions { localFocus.clearFocus() } + ) + CredentialRemember( + authentication = authentication, + modifier = Modifier + .height(48.dp) + .padding(bottom = 16.dp) + ) + Row( + modifier = Modifier + .padding(bottom = 16.dp) + .align(Alignment.End) + ) { + val context = LocalContext.current + Button( + modifier = Modifier.padding(end = 8.dp), + colors = outlinedButtonColors(), + onClick = { + context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(REGISTER_URL))) + }) { + Text(text = stringResource(id = R.string.action_register)) + } + Button(onClick = { + authentication.login() + }) { + Text(text = stringResource(id = R.string.action_login)) + } + } +} + +@Composable +private fun SignIn( + authentication: IAuthentication, +) { + Button( + modifier = Modifier.fillMaxWidth(), + colors = outlinedButtonColors(), + onClick = { + authentication.loginWithGoogle() + }) { + Image( + modifier = Modifier.padding(end = 8.dp), + painter = painterResource(id = R.drawable.ic_google), contentDescription = "" + ) + Text(text = stringResource(id = R.string.action_google_sign_in)) + } +} + @Composable private fun LoginField( authentication: IAuthentication,