2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2023-11-10 17:24:53 +03:00
|
|
|
* Copyright (C) 2022-2023 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-03-22 16:23:12 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-12 16:46:43 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-03-22 16:23:12 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../../app/models.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../desktop/models.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../../widgets/app_text_form_field.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2022-07-06 16:22:15 +03:00
|
|
|
import '../../widgets/utf8_utils.dart';
|
2022-03-22 16:23:12 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
|
|
|
|
class RenameFingerprintDialog extends ConsumerStatefulWidget {
|
2022-03-28 13:58:45 +03:00
|
|
|
final DevicePath devicePath;
|
2022-03-22 16:23:12 +03:00
|
|
|
final Fingerprint fingerprint;
|
2022-05-12 10:56:55 +03:00
|
|
|
const RenameFingerprintDialog(this.devicePath, this.fingerprint, {super.key});
|
2022-03-22 16:23:12 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_RenameAccountDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RenameAccountDialogState extends ConsumerState<RenameFingerprintDialog> {
|
|
|
|
late String _label;
|
|
|
|
_RenameAccountDialogState();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-04-04 12:15:38 +03:00
|
|
|
_label = widget.fingerprint.name ?? '';
|
2022-03-22 16:23:12 +03:00
|
|
|
}
|
|
|
|
|
2022-04-06 14:58:09 +03:00
|
|
|
_submit() async {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-06-13 17:45:26 +03:00
|
|
|
try {
|
|
|
|
final renamed = await ref
|
|
|
|
.read(fingerprintProvider(widget.devicePath).notifier)
|
|
|
|
.renameFingerprint(widget.fingerprint, _label);
|
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.of(context).pop(renamed);
|
2023-03-02 14:45:55 +03:00
|
|
|
showMessage(context, l10n.s_fingerprint_renamed);
|
2022-06-13 17:45:26 +03:00
|
|
|
} catch (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,
|
2023-03-01 12:30:32 +03:00
|
|
|
l10n.l_rename_fp_failed(errorMessage),
|
2022-06-13 17:45:26 +03:00
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
|
|
|
}
|
2022-04-06 14:58:09 +03:00
|
|
|
}
|
|
|
|
|
2022-03-22 16:23:12 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-03-22 16:23:12 +03:00
|
|
|
return ResponsiveDialog(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_rename_fp),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: _label.isNotEmpty ? _submit : null,
|
2023-03-02 14:45:55 +03:00
|
|
|
child: Text(l10n.s_save),
|
2022-05-12 09:34:51 +03:00
|
|
|
),
|
|
|
|
],
|
2022-09-07 11:25:22 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-02-28 21:05:46 +03:00
|
|
|
Text(l10n.q_rename_target(widget.fingerprint.label)),
|
|
|
|
Text(l10n.p_will_change_label_fp),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextFormField(
|
2022-09-07 11:25:22 +03:00
|
|
|
initialValue: _label,
|
|
|
|
maxLength: 15,
|
|
|
|
inputFormatters: [limitBytesLength(15)],
|
|
|
|
buildCounter: buildByteCounterFor(_label),
|
2022-09-12 16:46:43 +03:00
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_label,
|
2022-09-12 16:46:43 +03:00
|
|
|
prefixIcon: const Icon(Icons.fingerprint_outlined),
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_label = value.trim();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onFieldSubmitted: (_) {
|
|
|
|
if (_label.isNotEmpty) {
|
|
|
|
_submit();
|
|
|
|
}
|
|
|
|
},
|
2022-03-22 16:23:12 +03:00
|
|
|
),
|
2022-09-07 11:25:22 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
2022-03-22 16:23:12 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|