yubioath-flutter/lib/oath/views/rename_account_dialog.dart

120 lines
3.7 KiB
Dart
Raw Normal View History

2022-01-24 12:59:09 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-03-25 17:43:32 +03:00
import '../../app/message.dart';
import '../../widgets/responsive_dialog.dart';
2022-01-24 12:59:09 +03:00
import '../models.dart';
import '../state.dart';
import '../../app/models.dart';
import '../../app/state.dart';
import 'utils.dart';
2022-01-24 12:59:09 +03:00
class RenameAccountDialog extends ConsumerStatefulWidget {
final DeviceNode device;
final OathCredential credential;
const RenameAccountDialog(this.device, this.credential, {Key? key})
: super(key: key);
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_RenameAccountDialogState();
}
class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
late String _issuer;
late String _account;
2022-01-24 12:59:09 +03:00
@override
void initState() {
super.initState();
_issuer = widget.credential.issuer ?? '';
_account = widget.credential.name;
2022-01-24 12:59:09 +03:00
}
@override
Widget build(BuildContext context) {
// If current device changes, we need to pop back to the main Page.
ref.listen<DeviceNode?>(currentDeviceProvider, (previous, next) {
Navigator.of(context).pop();
});
final credential = widget.credential;
final label = credential.issuer != null
? '${credential.issuer} (${credential.name})'
: credential.name;
final remaining = getRemainingKeySpace(
oathType: credential.oathType,
period: credential.period,
issuer: _issuer,
name: _account,
);
final issuerRemaining = remaining.first;
final nameRemaining = remaining.second;
final isValid = _account.isNotEmpty;
2022-01-25 16:41:41 +03:00
2022-03-15 20:04:26 +03:00
return ResponsiveDialog(
title: const Text('Rename account'),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2022-01-24 12:59:09 +03:00
children: [
2022-03-15 20:04:26 +03:00
Text('Rename $label?'),
2022-01-24 12:59:09 +03:00
const Text(
'This will change how the account is displayed in the list.'),
TextFormField(
initialValue: _issuer,
2022-01-25 16:41:41 +03:00
enabled: issuerRemaining > 0,
maxLength: issuerRemaining > 0 ? issuerRemaining : null,
decoration: const InputDecoration(
2022-03-15 20:04:26 +03:00
border: OutlineInputBorder(),
labelText: 'Issuer (optional)',
2022-01-25 17:00:42 +03:00
helperText: '', // Prevents dialog resizing when enabled = false
2022-01-25 16:41:41 +03:00
),
onChanged: (value) {
setState(() {
_issuer = value.trim();
});
2022-01-25 16:41:41 +03:00
},
2022-01-24 12:59:09 +03:00
),
TextFormField(
initialValue: _account,
maxLength: nameRemaining,
2022-01-25 16:41:41 +03:00
decoration: InputDecoration(
2022-03-15 20:04:26 +03:00
border: const OutlineInputBorder(),
labelText: 'Account name',
2022-01-25 17:00:42 +03:00
helperText: '', // Prevents dialog resizing when enabled = false
2022-01-25 16:41:41 +03:00
errorText: isValid ? null : 'Your account must have a name',
2022-01-24 12:59:09 +03:00
),
onChanged: (value) {
setState(() {
_account = value.trim();
});
2022-01-24 12:59:09 +03:00
},
),
2022-03-15 20:04:26 +03:00
]
.map((e) => Padding(
child: e,
padding: const EdgeInsets.symmetric(vertical: 8.0),
))
.toList(),
2022-01-24 12:59:09 +03:00
),
actions: [
2022-03-15 20:04:26 +03:00
TextButton(
2022-01-25 16:41:41 +03:00
onPressed: isValid
2022-01-24 12:59:09 +03:00
? () async {
final renamed = await ref
2022-01-24 12:59:09 +03:00
.read(credentialListProvider(widget.device.path).notifier)
.renameAccount(credential,
_issuer.isNotEmpty ? _issuer : null, _account);
Navigator.of(context).pop(renamed);
2022-03-25 17:43:32 +03:00
showMessage(context, 'Account renamed');
2022-01-24 12:59:09 +03:00
}
: null,
2022-03-15 20:04:26 +03:00
child: const Text('Save'),
2022-01-24 12:59:09 +03:00
),
],
);
}
}