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

252 lines
8.1 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
2024-09-11 09:07:00 +03:00
* Copyright (C) 2022-2024 Yubico.
2022-10-04 13:12:54 +03:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-01-24 12:59:09 +03:00
import 'package:flutter/material.dart';
2022-09-06 15:30:18 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-01-24 12:59:09 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-06-13 17:45:26 +03:00
import 'package:logging/logging.dart';
2024-03-08 11:30:47 +03:00
import 'package:material_symbols_icons/symbols.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';
import '../../app/models.dart';
2023-08-18 15:54:55 +03:00
import '../../app/state.dart';
2022-06-13 17:45:26 +03:00
import '../../desktop/models.dart';
2023-11-10 17:24:53 +03:00
import '../../exception/cancellation_exception.dart';
import '../../widgets/app_input_decoration.dart';
import '../../widgets/app_text_form_field.dart';
import '../../widgets/responsive_dialog.dart';
2022-07-06 16:22:15 +03:00
import '../../widgets/utf8_utils.dart';
2023-11-10 17:24:53 +03:00
import '../keys.dart' as keys;
2022-01-24 12:59:09 +03:00
import '../models.dart';
import '../state.dart';
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 DevicePath devicePath;
2023-08-18 15:54:55 +03:00
final String? issuer;
final String name;
final OathType oathType;
final int period;
final List<(String? issuer, String name)> existing;
final Future<dynamic> Function(String? issuer, String name) rename;
const RenameAccountDialog({
required this.devicePath,
2023-08-18 15:54:55 +03:00
required this.issuer,
required this.name,
required this.oathType,
this.period = defaultPeriod,
this.existing = const [],
required this.rename,
super.key,
});
2022-01-24 12:59:09 +03:00
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_RenameAccountDialogState();
2023-08-18 15:54:55 +03:00
factory RenameAccountDialog.forOathCredential(
WidgetRef ref,
DevicePath devicePath,
2023-08-18 15:54:55 +03:00
OathCredential credential,
List<(String? issuer, String name)> existing) {
return RenameAccountDialog(
devicePath: devicePath,
2023-08-18 15:54:55 +03:00
issuer: credential.issuer,
name: credential.name,
oathType: credential.oathType,
period: credential.period,
existing: existing,
rename: (issuer, name) async {
final withContext = ref.read(withContextProvider);
try {
// Rename credentials
final renamed = await ref
.read(credentialListProvider(devicePath).notifier)
2023-08-18 15:54:55 +03:00
.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();
2023-08-18 15:54:55 +03:00
} catch (e) {
2024-08-31 11:45:33 +03:00
_log.error('Failed to rename account', e);
2023-08-18 15:54:55 +03:00
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)!
2024-08-31 11:45:33 +03:00
.l_rename_account_failed(errorMessage),
2023-08-18 15:54:55 +03:00
duration: const Duration(seconds: 4),
));
return null;
}
},
);
}
2022-01-24 12:59:09 +03:00
}
class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
late String _issuer;
2023-08-18 15:54:55 +03:00
late String _name;
2022-01-24 12:59:09 +03:00
2024-08-29 11:17:18 +03:00
final _issuerFocus = FocusNode();
final _nameFocus = FocusNode();
2024-08-29 10:56:58 +03:00
2022-01-24 12:59:09 +03:00
@override
void initState() {
super.initState();
2023-08-18 15:54:55 +03:00
_issuer = widget.issuer?.trim() ?? '';
_name = widget.name.trim();
2022-01-24 12:59:09 +03:00
}
2024-08-29 11:17:18 +03:00
@override
void dispose() {
_issuerFocus.dispose();
_nameFocus.dispose();
super.dispose();
}
void _submit() async {
2024-08-29 11:17:18 +03:00
_issuerFocus.unfocus();
_nameFocus.unfocus();
2023-08-18 15:54:55 +03:00
final nav = Navigator.of(context);
final renamed =
await widget.rename(_issuer.isNotEmpty ? _issuer : null, _name);
if (renamed is! CancellationException) {
nav.pop(renamed);
}
}
2022-01-24 12:59:09 +03:00
@override
Widget build(BuildContext context) {
2023-02-28 21:05:46 +03:00
final l10n = AppLocalizations.of(context)!;
2022-01-24 12:59:09 +03:00
2023-05-22 12:20:30 +03:00
final (issuerRemaining, nameRemaining) = getRemainingKeySpace(
2023-08-18 15:54:55 +03:00
oathType: widget.oathType,
period: widget.period,
issuer: _issuer,
2023-08-18 15:54:55 +03:00
name: _name,
);
2022-09-02 19:24:55 +03:00
2023-08-18 15:54:55 +03:00
// are the name/issuer values different from original
final didChange = (widget.issuer ?? '') != _issuer || widget.name != _name;
// is this credentials name/issuer pair different from all other, or initial value?
final isUnique = !widget.existing.contains((_issuer, _name)) || !didChange;
2022-09-02 19:24:55 +03:00
// is this credential name/issuer of valid format
2023-08-18 15:54:55 +03:00
final nameNotEmpty = _name.isNotEmpty;
2022-09-02 19:24:55 +03:00
// can we rename with the new values
2023-08-18 15:54:55 +03:00
final isValid = isUnique && nameNotEmpty;
2022-01-25 16:41:41 +03:00
2022-03-15 20:04:26 +03:00
return ResponsiveDialog(
title: Text(l10n.s_rename_account),
2022-05-12 09:34:51 +03:00
actions: [
TextButton(
onPressed: didChange && isValid ? _submit : null,
2022-09-12 13:58:17 +03:00
key: keys.saveButton,
child: Text(l10n.s_save),
2022-05-12 09:34:51 +03:00
),
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2023-08-18 15:54:55 +03:00
Text(l10n.q_rename_target(widget.issuer != null
? '${widget.issuer} (${widget.name})'
: widget.name)),
2023-02-28 21:05:46 +03:00
Text(l10n.p_rename_will_change_account_displayed),
2023-11-10 17:24:53 +03:00
AppTextFormField(
initialValue: _issuer,
enabled: issuerRemaining > 0,
maxLength: issuerRemaining > 0 ? issuerRemaining : null,
buildCounter: buildByteCounterFor(_issuer),
inputFormatters: [limitBytesLength(issuerRemaining)],
2022-09-12 13:58:17 +03:00
key: keys.issuerField,
decoration: AppInputDecoration(
border: const OutlineInputBorder(),
labelText: l10n.s_issuer_optional,
helperText: '', // Prevents dialog resizing when disabled
2024-03-08 11:30:47 +03:00
prefixIcon: const Icon(Symbols.business),
),
textInputAction: TextInputAction.next,
2024-08-29 11:17:18 +03:00
focusNode: _issuerFocus,
2024-09-04 14:34:00 +03:00
autofocus: true,
onChanged: (value) {
setState(() {
_issuer = value.trim();
});
},
).init(),
2023-11-10 17:24:53 +03:00
AppTextFormField(
2023-08-18 15:54:55 +03:00
initialValue: _name,
maxLength: nameRemaining,
inputFormatters: [limitBytesLength(nameRemaining)],
2023-08-18 15:54:55 +03:00
buildCounter: buildByteCounterFor(_name),
2022-09-12 13:58:17 +03:00
key: keys.nameField,
decoration: AppInputDecoration(
border: const OutlineInputBorder(),
labelText: l10n.s_account_name,
2024-09-09 12:15:52 +03:00
helperText: '', // Prevents dialog resizing when disabled
2023-08-18 15:54:55 +03:00
errorText: !nameNotEmpty
2023-02-28 21:05:46 +03:00
? l10n.l_account_name_required
: !isUnique
2023-02-28 21:05:46 +03:00
? l10n.l_name_already_exists
: null,
2024-03-08 11:30:47 +03:00
prefixIcon: const Icon(Symbols.people_alt),
),
textInputAction: TextInputAction.done,
2024-08-29 11:17:18 +03:00
focusNode: _nameFocus,
onChanged: (value) {
setState(() {
2023-08-18 15:54:55 +03:00
_name = value.trim();
});
},
onFieldSubmitted: (_) {
if (didChange && isValid) {
_submit();
}
},
).init(),
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
2022-01-24 12:59:09 +03:00
),
);
}
}