Skip to content

Commit

Permalink
preliminary implementation of eye symbol in password field (currently…
Browse files Browse the repository at this point in the history
… with Text() because Icons are reworked), with constant placeholder or empty if empty, password confirmation
  • Loading branch information
hg42 committed Sep 29, 2022
1 parent 7d0fe0a commit 02c789a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 17 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/com/machiav3lli/backup/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.machiav3lli.backup.preferences.pref_biometricLock
import com.machiav3lli.backup.preferences.pref_deviceLock
import com.machiav3lli.backup.preferences.pref_encryption
import com.machiav3lli.backup.preferences.pref_password
import com.machiav3lli.backup.preferences.pref_passwordConfirmation
import com.machiav3lli.backup.preferences.pref_pauseApps
import com.machiav3lli.backup.preferences.pref_pmSuspend
import com.machiav3lli.backup.preferences.pref_shadowRootFile
Expand Down Expand Up @@ -65,7 +64,6 @@ val Context.PrefsDependencies: Map<Pref, Boolean>
pref_biometricLock to (isBiometricLockAvailable() && isDeviceLockEnabled()),
pref_deviceLock to isDeviceLockAvailable(),
pref_password to pref_encryption.value,
pref_passwordConfirmation to pref_encryption.value,
pref_pmSuspend to pref_pauseApps.value,
pref_shadowRootFile to pref_allowShadowingDefault.value
)
Expand Down
93 changes: 85 additions & 8 deletions app/src/main/java/com/machiav3lli/backup/dialogs/ComposeDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
Expand Down Expand Up @@ -237,6 +238,7 @@ fun ListDialogUI(
fun StringDialogUI(
pref: StringPref,
isPrivate: Boolean = false,
confirm: Boolean = false,
openDialogCustom: MutableState<Boolean>,
onChanged: (() -> Unit) = {}
) {
Expand All @@ -248,16 +250,21 @@ fun StringDialogUI(
delay(100)
textFieldFocusRequester.requestFocus()
}
var savedValue by remember {
mutableStateOf(if(isPrivate) "" else pref.value)
}
var savedValue by remember { mutableStateOf(if(isPrivate) "" else pref.value) }
var savedValueConfirm by remember { mutableStateOf("") }
var isEdited by remember { mutableStateOf(false) }

val textColor = if(isPrivate) { if(savedValue != savedValueConfirm) Color.Red else Color.Green } else Color.Unspecified

Card(
shape = RoundedCornerShape(8.dp),
modifier = Modifier.padding(8.dp),
elevation = CardDefaults.elevatedCardElevation(8.dp),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background)
) {
// from https://stackoverflow.com/questions/65304229/toggle-password-field-jetpack-compose
var isPasswordVisible by remember { mutableStateOf(!isPrivate) } // rememberSavable?

Column(
modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
Expand All @@ -272,20 +279,90 @@ fun StringDialogUI(
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
textColor = textColor
),
shape = MaterialTheme.shapes.medium,
singleLine = true,
onValueChange = { savedValue = it },
visualTransformation = if (isPrivate)
PasswordVisualTransformation()
onValueChange = {
isEdited = true
savedValue = it
},
visualTransformation = if (isPasswordVisible)
VisualTransformation.None
else
VisualTransformation.None,
PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done,
keyboardType = if(isPrivate) KeyboardType.Password else KeyboardType.Text
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
trailingIcon = {
//val image = if (passwordVisible)
// Icons.Filled.Visibility
//else
// Icons.Filled.VisibilityOff

// Please provide localized description for accessibility services
//val description = if (passwordVisible) "Hide password" else "Show password" //???

IconButton(onClick = {isPasswordVisible = !isPasswordVisible}) {
//Icon(imageVector = image, description = description)
Text(if(isPasswordVisible) "<O>" else "<=>")
}
},
placeholder = {
if(isPrivate) {
if (pref.value.isNotEmpty() and !isEdited) {
if (isPasswordVisible)
Text(pref.value)
else
Text("**********")
}
}
}
)
if(isPrivate && confirm) {
TextField(
modifier = Modifier
.fillMaxWidth()
, //.focusRequester(textFieldFocusRequester),
value = savedValueConfirm,
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
textColor = textColor
),
shape = MaterialTheme.shapes.medium,
singleLine = true,
onValueChange = {
isEdited = true
savedValueConfirm = it
},
visualTransformation = if (isPasswordVisible)
VisualTransformation.None
else
PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done,
keyboardType = if(isPrivate) KeyboardType.Password else KeyboardType.Text
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
trailingIcon = {
//val image = if (passwordVisible)
// Icons.Filled.Visibility
//else
// Icons.Filled.VisibilityOff

// Please provide localized description for accessibility services
//val description = if (passwordVisible) "Hide password" else "Show password" //???

IconButton(onClick = {isPasswordVisible = !isPasswordVisible}) {
//Icon(imageVector = image, description = description)
Text(if(isPasswordVisible) "<O>" else "<=>")
}
}
)
}

Row(
Modifier.fillMaxWidth()
Expand All @@ -295,7 +372,7 @@ fun StringDialogUI(
}
Spacer(Modifier.weight(1f))
ElevatedActionButton(text = stringResource(id = R.string.dialogSave)) {
if (pref.value != savedValue) {
if ((pref.value != savedValue) and (!confirm or (savedValue == savedValueConfirm))) {
pref.value = savedValue
onChanged()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fun ServicePrefsPage() {
is PasswordPref -> StringDialogUI(
pref = dialogsPref as PasswordPref,
isPrivate = true,
confirm = true,
openDialogCustom = openDialog
)
is StringPref -> StringDialogUI(
Expand Down Expand Up @@ -97,13 +98,6 @@ val pref_password = PasswordPref(
defaultValue = ""
)

val pref_passwordConfirmation = PasswordPref( // TODO smart summary
key = "srv.passwordConfirmation",
titleId = R.string.prefs_passwordconfirmation,
iconId = R.drawable.ic_password,
defaultValue = ""
)

val pref_backupDeviceProtectedData = BooleanPref(
key = "srv.backupDeviceProtectedData",
titleId = R.string.prefs_deviceprotecteddata,
Expand Down

0 comments on commit 02c789a

Please sign in to comment.