refactor exception handling in rename dialog

This commit is contained in:
Adam Velebil 2024-09-13 08:37:57 +02:00
parent ce95119a73
commit 3f8e69a510
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10

View File

@ -67,49 +67,15 @@ class RenameAccountDialog extends ConsumerStatefulWidget {
OathCredential credential, OathCredential credential,
List<(String? issuer, String name)> existing) { List<(String? issuer, String name)> existing) {
return RenameAccountDialog( return RenameAccountDialog(
devicePath: devicePath, devicePath: devicePath,
issuer: credential.issuer, issuer: credential.issuer,
name: credential.name, name: credential.name,
oathType: credential.oathType, oathType: credential.oathType,
period: credential.period, period: credential.period,
existing: existing, existing: existing,
rename: (issuer, name) async { rename: (issuer, name) async => await ref
final withContext = ref.read(withContextProvider); .read(credentialListProvider(devicePath).notifier)
try { .renameAccount(credential, issuer, name));
// Rename credentials
final renamed = await ref
.read(credentialListProvider(devicePath).notifier)
.renameAccount(credential, issuer, name);
// Update favorite
ref
.read(favoritesProvider.notifier)
.renameCredential(credential.id, renamed.id);
await withContext((context) async => showMessage(
context, AppLocalizations.of(context)!.s_account_renamed));
return renamed;
} on CancellationException catch (_) {
return CancellationException();
} catch (e) {
_log.error('Failed to rename 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();
}
await withContext((context) async => showMessage(
context,
AppLocalizations.of(context)!
.l_rename_account_failed(errorMessage),
duration: const Duration(seconds: 4),
));
return null;
}
},
);
} }
} }
@ -138,10 +104,38 @@ class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
_issuerFocus.unfocus(); _issuerFocus.unfocus();
_nameFocus.unfocus(); _nameFocus.unfocus();
final nav = Navigator.of(context); final nav = Navigator.of(context);
final renamed = final withContext = ref.read(withContextProvider);
await widget.rename(_issuer.isNotEmpty ? _issuer : null, _name);
if (renamed is! CancellationException) { try {
// Rename credentials
final renamed =
await widget.rename(_issuer.isNotEmpty ? _issuer : null, _name);
// Update favorite
ref
.read(favoritesProvider.notifier)
.renameCredential(renamed.id, renamed.id);
await withContext((context) async => showMessage(
context, AppLocalizations.of(context)!.s_account_renamed));
nav.pop(renamed); nav.pop(renamed);
} on CancellationException catch (_) {
// ignored
} catch (e) {
_log.error('Failed to rename 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();
}
await withContext((context) async => showMessage(
context,
AppLocalizations.of(context)!.l_rename_account_failed(errorMessage),
duration: const Duration(seconds: 4),
));
} }
} }