Improve add OATH account.

- Add padding to match Period with Digits.
- Validate secret length on submit.
This commit is contained in:
Dain Nilsson 2022-02-01 08:24:29 +01:00
parent 40335e66da
commit 6963b69c10
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8

View File

@ -31,6 +31,7 @@ class _AddAccountFormState extends State<AddAccountForm> {
HashAlgorithm _hashAlgorithm = defaultHashAlgorithm; HashAlgorithm _hashAlgorithm = defaultHashAlgorithm;
int _period = defaultPeriod; int _period = defaultPeriod;
int _digits = defaultDigits; int _digits = defaultDigits;
bool _validateSecretLength = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -43,9 +44,8 @@ class _AddAccountFormState extends State<AddAccountForm> {
final issuerRemaining = remaining.first; final issuerRemaining = remaining.first;
final nameRemaining = remaining.second; final nameRemaining = remaining.second;
final secretValid = _secret.length * 5 % 8 < 5; final secretLengthValid = _secret.length * 5 % 8 < 5;
final isValid = final isValid = _account.isNotEmpty && _secret.isNotEmpty && _period > 0;
_account.isNotEmpty && _secret.isNotEmpty && secretValid && _period > 0;
return Column( return Column(
children: [ children: [
@ -86,10 +86,13 @@ class _AddAccountFormState extends State<AddAccountForm> {
], ],
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Secret key', labelText: 'Secret key',
errorText: secretValid ? null : 'Invalid value'), errorText: _validateSecretLength && !secretLengthValid
? 'Invalid length'
: null),
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_secret = value.replaceAll(' ', ''); _secret = value.replaceAll(' ', '');
_validateSecretLength = false;
}); });
}, },
), ),
@ -148,7 +151,7 @@ class _AddAccountFormState extends State<AddAccountForm> {
Expanded( Expanded(
child: DropdownButtonFormField<HashAlgorithm>( child: DropdownButtonFormField<HashAlgorithm>(
decoration: decoration:
const InputDecoration(label: Text('Algorithm')), const InputDecoration(labelText: 'Algorithm'),
value: _hashAlgorithm, value: _hashAlgorithm,
items: HashAlgorithm.values items: HashAlgorithm.values
.map((e) => DropdownMenuItem( .map((e) => DropdownMenuItem(
@ -178,7 +181,10 @@ class _AddAccountFormState extends State<AddAccountForm> {
FilteringTextInputFormatter.digitsOnly, FilteringTextInputFormatter.digitsOnly,
], ],
decoration: InputDecoration( decoration: InputDecoration(
label: const Text('Period'), contentPadding:
// Manual alignment to match digits-dropdown.
const EdgeInsets.fromLTRB(0, 12, 0, 15),
labelText: 'Period',
errorText: _period > 0 errorText: _period > 0
? null ? null
: 'Must be a positive number', : 'Must be a positive number',
@ -196,8 +202,7 @@ class _AddAccountFormState extends State<AddAccountForm> {
), ),
Expanded( Expanded(
child: DropdownButtonFormField<int>( child: DropdownButtonFormField<int>(
decoration: decoration: const InputDecoration(labelText: 'Digits'),
const InputDecoration(label: Text('Digits')),
value: _digits, value: _digits,
items: [6, 7, 8] items: [6, 7, 8]
.map((e) => DropdownMenuItem( .map((e) => DropdownMenuItem(
@ -223,18 +228,24 @@ class _AddAccountFormState extends State<AddAccountForm> {
child: ElevatedButton( child: ElevatedButton(
onPressed: isValid onPressed: isValid
? () { ? () {
widget.onSubmit( if (secretLengthValid) {
CredentialData( widget.onSubmit(
issuer: _issuer.isEmpty ? null : _issuer, CredentialData(
name: _account, issuer: _issuer.isEmpty ? null : _issuer,
secret: _secret, name: _account,
oathType: _oathType, secret: _secret,
hashAlgorithm: _hashAlgorithm, oathType: _oathType,
digits: _digits, hashAlgorithm: _hashAlgorithm,
period: _period, digits: _digits,
), period: _period,
_touch, ),
); _touch,
);
} else {
setState(() {
_validateSecretLength = true;
});
}
} }
: null, : null,
child: const Text('Add account'), child: const Text('Add account'),