Keep Pinned status when account is renamed.

This commit is contained in:
Dain Nilsson 2022-09-06 08:50:42 +02:00
parent 07f9bab181
commit 19cb2e49b2
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
2 changed files with 48 additions and 30 deletions

View File

@ -150,6 +150,13 @@ class FavoritesNotifier extends StateNotifier<List<String>> {
}
_prefs.setStringList(_key, state);
}
renameCredential(String oldCredentialId, String newCredentialId) {
if (state.contains(oldCredentialId)) {
state = [newCredentialId, ...state.toList()..remove(oldCredentialId)];
_prefs.setStringList(_key, state);
}
}
}
final filteredCredentialsProvider = StateNotifierProvider.autoDispose

View File

@ -39,6 +39,41 @@ class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
_account = widget.credential.name.trim();
}
void _submit() async {
try {
// Rename credentials
final renamed = await ref
.read(credentialListProvider(widget.device.path).notifier)
.renameAccount(
widget.credential, _issuer.isNotEmpty ? _issuer : null, _account);
// Update favorite
ref
.read(favoritesProvider.notifier)
.renameCredential(widget.credential.id, renamed.id);
if (!mounted) return;
Navigator.of(context).pop(renamed);
showMessage(context, 'Account renamed');
} on CancellationException catch (_) {
// ignored
} 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),
);
}
}
@override
Widget build(BuildContext context) {
final credential = widget.credential;
@ -79,36 +114,7 @@ class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
title: const Text('Rename account'),
actions: [
TextButton(
onPressed: didChange && isValid
? () async {
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');
} on CancellationException catch (_) {
// ignored
} 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),
);
}
}
: null,
onPressed: didChange && isValid ? _submit : null,
child: const Text('Save'),
),
],
@ -160,6 +166,11 @@ class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
_account = value.trim();
});
},
onFieldSubmitted: (_) {
if (didChange && isValid) {
_submit();
}
},
),
]
.map((e) => Padding(