From 37101a3db9c8f17a3b5fb3f149d6fcf0bc71136c Mon Sep 17 00:00:00 2001 From: aritra Date: Thu, 14 Mar 2024 00:17:56 +0530 Subject: [PATCH] Fixed PasswordGeneratorScreen --- .../presentation/account/PasswordGenerator.kt | 124 +++++++++++------- 1 file changed, 76 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/geekymusketeers/uncrack/presentation/account/PasswordGenerator.kt b/app/src/main/java/com/geekymusketeers/uncrack/presentation/account/PasswordGenerator.kt index 4518a0ad..a1f2059a 100644 --- a/app/src/main/java/com/geekymusketeers/uncrack/presentation/account/PasswordGenerator.kt +++ b/app/src/main/java/com/geekymusketeers/uncrack/presentation/account/PasswordGenerator.kt @@ -16,17 +16,22 @@ import androidx.compose.material3.Slider import androidx.compose.material3.SliderDefaults import androidx.compose.material3.Switch import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalClipboardManager import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.SpanStyle +import androidx.compose.ui.text.buildAnnotatedString +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.text.withStyle +import androidx.compose.ui.unit.TextUnit +import androidx.compose.ui.unit.TextUnitType import androidx.compose.ui.unit.dp import androidx.navigation.NavHostController import com.geekymusketeers.uncrack.R @@ -35,9 +40,13 @@ import com.geekymusketeers.uncrack.components.UCTopAppBar import com.geekymusketeers.uncrack.ui.theme.BackgroundLight import com.geekymusketeers.uncrack.ui.theme.OnPrimaryContainerLight import com.geekymusketeers.uncrack.ui.theme.PrimaryLight -import com.geekymusketeers.uncrack.ui.theme.bold20 +import com.geekymusketeers.uncrack.ui.theme.SurfaceLight import com.geekymusketeers.uncrack.ui.theme.medium24 +import com.geekymusketeers.uncrack.ui.theme.medium30 import com.geekymusketeers.uncrack.ui.theme.normal16 +import com.geekymusketeers.uncrack.util.Constants.sliderStepRange +import com.geekymusketeers.uncrack.util.Constants.sliderSteps +import timber.log.Timber @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -48,8 +57,8 @@ fun PasswordGenerator( ) { val context = LocalContext.current val clipboardManager = LocalClipboardManager.current - val password by passwordGeneratorViewModel.password.observeAsState() - val passwordLength by passwordGeneratorViewModel.passwordLength.observeAsState() + val password by passwordGeneratorViewModel.password.observeAsState("") + val passwordLength by passwordGeneratorViewModel.passwordLength.observeAsState(0.0f) val includeUppercase by passwordGeneratorViewModel.includeUppercase.observeAsState(true) val includeLowercase by passwordGeneratorViewModel.includeLowercase.observeAsState(true) val includeNumbers by passwordGeneratorViewModel.includeNumbers.observeAsState(true) @@ -59,8 +68,7 @@ fun PasswordGenerator( topBar = { UCTopAppBar( modifier = modifier.fillMaxWidth(), - "Password Generator", - colors = TopAppBarDefaults.topAppBarColors(BackgroundLight), + title = "Password Generator", onBackPress = { navController.popBackStack() } ) } @@ -71,44 +79,62 @@ fun PasswordGenerator( .padding(paddingValues) .padding(16.dp) .background(BackgroundLight), - verticalArrangement = Arrangement.spacedBy(16.dp) + verticalArrangement = Arrangement.Center ) { Text( - text = password ?: "", - style = bold20.copy(OnPrimaryContainerLight) + modifier = Modifier.fillMaxWidth(), + text = buildAnnotatedString { + password.forEach { + val textColor = when { + it.isDigit() -> Color.Blue + it.isLetterOrDigit().not() -> Color.Magenta + else -> OnPrimaryContainerLight + } + withStyle(style = SpanStyle(color = textColor)) { + append(it.toString()) + } + } + }, + style = medium30, + textAlign = TextAlign.Center, + letterSpacing = TextUnit(1F, TextUnitType.Sp) ) - Spacer(modifier = Modifier.height(20.dp)) + Spacer(modifier = Modifier.height(90.dp)) Text( - text = stringResource(id = R.string.password_length), + text = stringResource(R.string.password_generated_length, passwordLength.toInt()), style = medium24.copy(OnPrimaryContainerLight) ) - Spacer(modifier = Modifier.height(5.dp)) - - passwordLength?.let { - Slider( - modifier = Modifier.fillMaxWidth(), - value = it.toFloat(), - onValueChange = { password -> - passwordGeneratorViewModel.updatePasswordLength(password.toInt()) - }, - valueRange = 0f..32f, - colors = SliderDefaults.colors( - activeTrackColor = PrimaryLight - ) + Spacer(modifier = Modifier.height(10.dp)) + + Slider( + modifier = Modifier.fillMaxWidth(), + value = passwordLength, + onValueChange = { newPasswordLength -> + passwordGeneratorViewModel.updatePasswordLength(newPasswordLength) + }, + steps = sliderSteps, + valueRange = sliderStepRange, + colors = SliderDefaults.colors( + thumbColor = OnPrimaryContainerLight, + activeTrackColor = PrimaryLight, + inactiveTrackColor = SurfaceLight, + activeTickColor = Color.Transparent, + inactiveTickColor = Color.Transparent ) - } + ) - Spacer(modifier = Modifier.height(20.dp)) + Spacer(modifier = Modifier.height(30.dp)) Text( - text = stringResource(R.string.include), + text = stringResource(R.string.include_following), style = medium24.copy(OnPrimaryContainerLight) ) + Spacer(modifier = Modifier.height(15.dp)) SwitchItem( label = stringResource(R.string.numbers), @@ -135,34 +161,36 @@ fun PasswordGenerator( passwordGeneratorViewModel.updateIncludeSpecialChars(it) } - Spacer(modifier = Modifier.weight(1f)) - Row( + UCButton( modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween - ) { - UCButton( - text = stringResource(R.string.generate), - onClick = { - passwordGeneratorViewModel.generatePassword() - }, - leadingIcon = painterResource(id = R.drawable.generate_password) - ) + text = stringResource(R.string.generate), + onClick = { + passwordGeneratorViewModel.generatePassword() + Timber.d("Password ${passwordGeneratorViewModel.generatePassword()}") + }, + leadingIcon = painterResource(id = R.drawable.generate_password) + ) - UCButton( - text = stringResource(R.string.copy), - onClick = { - clipboardManager.setText(AnnotatedString(password.toString())) - Toast.makeText(context, "Copied", Toast.LENGTH_SHORT).show() - }, - leadingIcon = painterResource(id = R.drawable.copy_password) - ) - } + Spacer(modifier = Modifier.height(10.dp)) + + UCButton( + modifier = Modifier.fillMaxWidth(), + text = stringResource(R.string.copy), + onClick = { + password.let { passwordToCopy -> +// clipboardManager.setText() //TODO: Need to check + } + Toast.makeText(context, "Copied", Toast.LENGTH_SHORT).show() + }, + leadingIcon = painterResource(id = R.drawable.copy_password) + ) } } } + @Composable fun SwitchItem( label: String,