2022-01-24 12:59:09 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../app/logging.dart';
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-06-10 13:23:52 +03:00
|
|
|
import '../../app/models.dart';
|
2022-09-02 19:24:55 +03:00
|
|
|
import '../../cancellation_exception.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../desktop/models.dart';
|
2022-03-31 12:50:40 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2022-07-06 16:22:15 +03:00
|
|
|
import '../../widgets/utf8_utils.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
2022-01-31 13:02:34 +03:00
|
|
|
import 'utils.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
|
2022-06-13 17:45:26 +03:00
|
|
|
final _log = Logger('oath.view.rename_account_dialog');
|
|
|
|
|
2022-01-24 12:59:09 +03:00
|
|
|
class RenameAccountDialog extends ConsumerStatefulWidget {
|
|
|
|
final DeviceNode device;
|
|
|
|
final OathCredential credential;
|
2022-09-02 19:24:55 +03:00
|
|
|
final List<OathCredential>? credentials;
|
|
|
|
|
|
|
|
const RenameAccountDialog(this.device, this.credential, this.credentials,
|
|
|
|
{super.key});
|
2022-01-24 12:59:09 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_RenameAccountDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
|
2022-01-31 13:02:34 +03:00
|
|
|
late String _issuer;
|
|
|
|
late String _account;
|
2022-01-24 12:59:09 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-09-05 16:02:36 +03:00
|
|
|
_issuer = widget.credential.issuer?.trim() ?? '';
|
|
|
|
_account = widget.credential.name.trim();
|
2022-01-24 12:59:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final credential = widget.credential;
|
|
|
|
|
|
|
|
final label = credential.issuer != null
|
|
|
|
? '${credential.issuer} (${credential.name})'
|
|
|
|
: credential.name;
|
|
|
|
|
2022-01-31 13:02:34 +03:00
|
|
|
final remaining = getRemainingKeySpace(
|
|
|
|
oathType: credential.oathType,
|
|
|
|
period: credential.period,
|
|
|
|
issuer: _issuer,
|
|
|
|
name: _account,
|
|
|
|
);
|
|
|
|
final issuerRemaining = remaining.first;
|
|
|
|
final nameRemaining = remaining.second;
|
2022-09-02 19:24:55 +03:00
|
|
|
|
|
|
|
// is this credentials name/issuer pair different from all other?
|
|
|
|
final isUnique = widget.credentials
|
|
|
|
?.where((element) =>
|
|
|
|
element != credential &&
|
|
|
|
element.name == _account &&
|
|
|
|
(element.issuer ?? '') == _issuer)
|
|
|
|
.isEmpty ??
|
|
|
|
false;
|
|
|
|
|
|
|
|
// is this credential name/issuer of valid format
|
|
|
|
final isValidFormat = _account.isNotEmpty;
|
|
|
|
|
|
|
|
// are the name/issuer values different from original
|
2022-09-05 16:02:36 +03:00
|
|
|
final didChange = (widget.credential.issuer ?? '') != _issuer ||
|
|
|
|
widget.credential.name != _account;
|
2022-09-02 19:24:55 +03:00
|
|
|
|
|
|
|
// can we rename with the new values
|
|
|
|
final isValid = isUnique && isValidFormat;
|
2022-01-25 16:41:41 +03:00
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
return ResponsiveDialog(
|
|
|
|
title: const Text('Rename account'),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
2022-09-02 19:24:55 +03:00
|
|
|
onPressed: didChange && isValid
|
2022-05-12 09:34:51 +03:00
|
|
|
? () async {
|
2022-06-10 13:23:52 +03:00
|
|
|
try {
|
|
|
|
final renamed = await ref
|
|
|
|
.read(
|
|
|
|
credentialListProvider(widget.device.path).notifier)
|
|
|
|
.renameAccount(credential,
|
|
|
|
_issuer.isNotEmpty ? _issuer : null, _account);
|
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.of(context).pop(renamed);
|
|
|
|
showMessage(context, 'Account renamed');
|
2022-06-20 10:47:34 +03:00
|
|
|
} on CancellationException catch (_) {
|
2022-06-10 13:23:52 +03:00
|
|
|
// ignored
|
2022-06-13 17:45:26 +03:00
|
|
|
} catch (e) {
|
|
|
|
_log.error('Failed to add account', e);
|
|
|
|
final String errorMessage;
|
|
|
|
// TODO: Make this cleaner than importing desktop specific RpcError.
|
|
|
|
if (e is RpcError) {
|
|
|
|
errorMessage = e.message;
|
|
|
|
} else {
|
|
|
|
errorMessage = e.toString();
|
|
|
|
}
|
|
|
|
showMessage(
|
|
|
|
context,
|
|
|
|
'Failed adding account: $errorMessage',
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
2022-06-10 13:23:52 +03:00
|
|
|
}
|
2022-05-12 09:34:51 +03:00
|
|
|
}
|
|
|
|
: null,
|
|
|
|
child: const Text('Save'),
|
|
|
|
),
|
|
|
|
],
|
2022-03-15 20:04:26 +03:00
|
|
|
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.'),
|
2022-01-31 13:02:34 +03:00
|
|
|
TextFormField(
|
|
|
|
initialValue: _issuer,
|
2022-01-25 16:41:41 +03:00
|
|
|
enabled: issuerRemaining > 0,
|
|
|
|
maxLength: issuerRemaining > 0 ? issuerRemaining : null,
|
2022-07-06 16:22:15 +03:00
|
|
|
buildCounter: buildByteCounterFor(_issuer),
|
2022-07-05 15:53:21 +03:00
|
|
|
inputFormatters: [limitBytesLength(issuerRemaining)],
|
2022-09-05 16:02:36 +03:00
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
2022-01-31 13:02:34 +03:00
|
|
|
labelText: 'Issuer (optional)',
|
2022-09-05 16:02:36 +03:00
|
|
|
helperText: '', // Prevents dialog resizing when enabled = false
|
|
|
|
prefixIcon: Icon(Icons.business_outlined),
|
2022-01-25 16:41:41 +03:00
|
|
|
),
|
2022-08-12 10:54:47 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
2022-01-25 16:41:41 +03:00
|
|
|
onChanged: (value) {
|
2022-01-31 13:02:34 +03:00
|
|
|
setState(() {
|
|
|
|
_issuer = value.trim();
|
|
|
|
});
|
2022-01-25 16:41:41 +03:00
|
|
|
},
|
2022-01-24 12:59:09 +03:00
|
|
|
),
|
2022-01-31 13:02:34 +03:00
|
|
|
TextFormField(
|
|
|
|
initialValue: _account,
|
|
|
|
maxLength: nameRemaining,
|
2022-07-05 15:53:21 +03:00
|
|
|
inputFormatters: [limitBytesLength(nameRemaining)],
|
2022-07-06 16:22:15 +03:00
|
|
|
buildCounter: buildByteCounterFor(_account),
|
2022-01-25 16:41:41 +03:00
|
|
|
decoration: InputDecoration(
|
2022-03-15 20:04:26 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2022-01-31 13:02:34 +03:00
|
|
|
labelText: 'Account name',
|
2022-09-02 19:24:55 +03:00
|
|
|
helperText: '',
|
|
|
|
// Prevents dialog resizing when enabled = false
|
|
|
|
errorText: !isValidFormat
|
|
|
|
? 'Your account must have a name'
|
2022-09-05 16:02:36 +03:00
|
|
|
: !isUnique
|
|
|
|
? 'This name already exists for the Issuer'
|
|
|
|
: null,
|
2022-06-09 12:23:34 +03:00
|
|
|
prefixIcon: const Icon(Icons.people_alt_outlined),
|
2022-01-24 12:59:09 +03:00
|
|
|
),
|
2022-08-12 10:54:47 +03:00
|
|
|
textInputAction: TextInputAction.done,
|
2022-01-24 12:59:09 +03:00
|
|
|
onChanged: (value) {
|
2022-01-31 13:02:34 +03:00
|
|
|
setState(() {
|
|
|
|
_account = value.trim();
|
|
|
|
});
|
2022-01-24 12:59:09 +03:00
|
|
|
},
|
|
|
|
),
|
2022-03-15 20:04:26 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
2022-05-12 09:34:51 +03:00
|
|
|
child: e,
|
2022-03-15 20:04:26 +03:00
|
|
|
))
|
|
|
|
.toList(),
|
2022-01-24 12:59:09 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|