Skip to content

Commit

Permalink
improve account screen forms
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Mar 16, 2023
1 parent b44efe7 commit 9bf868b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class DefaultAccountRepository(

override suspend fun putAccount(domainAccountInfo: DomainAccountInfo) {
val entityAccount = domainAccountInfo.toEntityAccount()
rtdataDao.insertCountData(EntityCountData(entityAccount.id, domainAccountInfo.counter))
accountsDao.insert(entityAccount)
rtdataDao.upsertCountData(EntityCountData(entityAccount.id, domainAccountInfo.counter.toInt()))
accountsDao.upsert(entityAccount)
}

override suspend fun incrementAccountCounter(id: UUID) {
Expand Down Expand Up @@ -86,9 +86,9 @@ class DefaultAccountRepository(
secret = secret,
algorithm = algorithm,
type = type,
digits = digits,
period = period,
counter = counter
digits = digits.toString(),
period = period.toString(),
counter = counter.toString()
)
}

Expand All @@ -101,8 +101,8 @@ class DefaultAccountRepository(
issuer = issuer,
algorithm = algorithm,
type = type,
digits = digits,
period = period
digits = digits.toInt(),
period = period.toInt()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ data class DomainAccountInfo(
val secret: String,
val algorithm: OtpDigest,
val type: OtpType,
val digits: Int,
val counter: Int,
val period: Int,
val digits: String,
val counter: String,
val period: String,
) : Parcelable {

companion object {
Expand All @@ -32,9 +32,9 @@ data class DomainAccountInfo(
secret = "",
algorithm = OtpDigest.Sha1,
type = OtpType.Totp,
digits = 6,
counter = 0,
period = 30
digits = "6",
counter = "0",
period = "30"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class DefaultOtpRepository(
secret = parseResult.data.secret,
algorithm = parseResult.data.algorithm,
type = parseResult.data.type,
digits = parseResult.data.digits,
counter = parseResult.data.counter ?: DomainAccountInfo.DEFAULT.counter,
period = parseResult.data.period ?: DomainAccountInfo.DEFAULT.period,
digits = parseResult.data.digits.toString(),
counter = parseResult.data.counter?.toString() ?: DomainAccountInfo.DEFAULT.counter,
period = parseResult.data.period?.toString() ?: DomainAccountInfo.DEFAULT.period,
)
}
is OtpUriParserResult.Failure -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ fun AccountScreen(
topBar = {
TopAppBar(
actions = {
val enabled by remember(accountInfo) {
derivedStateOf {
accountInfo != null &&
accountInfo!!.label.isNotEmpty() &&
accountInfo!!.secret.isNotEmpty() &&
accountInfo!!.digits.toIntOrNull() != null &&
accountInfo!!.counter.toIntOrNull() != null &&
accountInfo!!.period.toIntOrNull() != null
}
}
TextButton(
onClick = {
onSave(accountInfo!!)
},
enabled = accountInfo != null
onClick = { onSave(accountInfo!!) },
enabled = enabled
) {
Text(stringResource(R.string.account_actions_save))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ fun AccountScreenSuccess(
onTypeChange: (OtpType) -> Unit,
digest: OtpDigest,
onDigestChange: (OtpDigest) -> Unit,
digits: Int,
onDigitsChange: (Int) -> Unit,
counter: Int,
onCounterChange: (Int) -> Unit,
period: Int,
onPeriodChange: (Int) -> Unit,
digits: String,
onDigitsChange: (String) -> Unit,
counter: String,
onCounterChange: (String) -> Unit,
period: String,
onPeriodChange: (String) -> Unit,
) {
LazyVerticalGrid(
modifier = Modifier.fillMaxSize(),
Expand Down Expand Up @@ -100,10 +100,9 @@ fun AccountScreenSuccess(
}
}
singleItem {
OutlinedTextField(
DataField(
value = label,
onValueChange = onLabelChange,
singleLine = true,
label = {
Text(stringResource(R.string.account_data_label))
},
Expand All @@ -113,13 +112,13 @@ fun AccountScreenSuccess(
contentDescription = null
)
},
required = true
)
}
singleItem {
OutlinedTextField(
DataField(
value = issuer,
onValueChange = onIssuerChange,
singleLine = true,
label = {
Text(stringResource(R.string.account_data_issuer))
},
Expand All @@ -133,10 +132,9 @@ fun AccountScreenSuccess(
}
singleItem {
var secretShown by remember { mutableStateOf(false) }
OutlinedTextField(
DataField(
value = secret,
onValueChange = onSecretChange,
singleLine = true,
label = {
Text(stringResource(R.string.account_data_secret))
},
Expand All @@ -161,7 +159,8 @@ fun AccountScreenSuccess(
PasswordVisualTransformation()
}
},
keyboardOptions = remember { KeyboardOptions(keyboardType = KeyboardType.Password) }
keyboardOptions = remember { KeyboardOptions(keyboardType = KeyboardType.Password) },
required = true
)
}
item {
Expand Down Expand Up @@ -245,22 +244,47 @@ private fun <S> SlideAnimatable(
}
}

@Composable
private fun DataField(
value: String,
onValueChange: (String) -> Unit,
required: Boolean = false,
label: (@Composable () -> Unit)? = null,
leadingIcon: (@Composable () -> Unit)? = null,
trailingIcon: (@Composable () -> Unit)? = null,
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
) {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
singleLine = true,
label = label,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
supportingText = if (required) { ->
Text(stringResource(R.string.account_data_status_required))
} else null,
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
)
}

@Composable
private fun NumberField(
value: Int,
onValueChange: (Int) -> Unit,
value: String,
onValueChange: (String) -> Unit,
label: (@Composable () -> Unit)? = null,
) {
OutlinedTextField(
value = value.toString(),
onValueChange = { newValue ->
onValueChange(newValue.filter { it.isDigit() }.toInt())
},
value = value,
onValueChange = onValueChange,
singleLine = true,
keyboardOptions = remember {
KeyboardOptions(keyboardType = KeyboardType.Number)
},
label = label
label = label,
isError = value.toIntOrNull() == null
)
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="account_discard_buttons_discard">Discard</string>
<string name="account_discard_buttons_cancel">Cancel</string>
<string name="account_error">An error occurred while loading the account</string>
<string name="account_data_status_required">required</string>

<string name="settings_title">Settings</string>
<string name="settings_prefs_securemode">Secure mode</string>
Expand Down

0 comments on commit 9bf868b

Please sign in to comment.