yubioath-flutter/lib/otp/views/otp_screen.dart

229 lines
9.3 KiB
Dart
Raw Normal View History

2023-11-09 16:09:59 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* 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.
*/
2024-01-10 13:26:37 +03:00
import 'dart:async';
2023-11-09 16:09:59 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2024-03-08 11:30:47 +03:00
import 'package:material_symbols_icons/symbols.dart';
2023-11-09 16:09:59 +03:00
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/shortcuts.dart';
2024-01-10 13:26:37 +03:00
import '../../app/views/action_list.dart';
2023-11-09 16:09:59 +03:00
import '../../app/views/app_failure_page.dart';
import '../../app/views/app_list_item.dart';
import '../../app/views/app_page.dart';
import '../../app/views/message_page.dart';
import '../../core/state.dart';
import '../../management/models.dart';
2023-11-09 16:09:59 +03:00
import '../../widgets/list_title.dart';
2023-12-13 21:35:17 +03:00
import '../features.dart' as features;
2024-02-08 16:21:05 +03:00
import '../keys.dart';
2023-11-09 16:09:59 +03:00
import '../models.dart';
import '../state.dart';
import 'actions.dart';
import 'key_actions.dart';
import 'slot_dialog.dart';
2024-01-10 22:47:54 +03:00
class OtpScreen extends ConsumerStatefulWidget {
2023-11-09 16:09:59 +03:00
final DevicePath devicePath;
2024-01-10 22:47:54 +03:00
OtpScreen(this.devicePath) : super(key: ObjectKey(devicePath));
2023-11-09 16:09:59 +03:00
@override
2024-01-10 22:47:54 +03:00
ConsumerState<ConsumerStatefulWidget> createState() => _OtpScreenState();
}
class _OtpScreenState extends ConsumerState<OtpScreen> {
SlotId? _selected;
@override
Widget build(BuildContext context) {
2023-11-09 16:09:59 +03:00
final l10n = AppLocalizations.of(context)!;
final hasFeature = ref.watch(featureProvider);
2024-01-10 22:47:54 +03:00
return ref.watch(otpStateProvider(widget.devicePath)).when(
loading: () => MessagePage(
title: l10n.s_slots,
capabilities: const [Capability.otp],
centered: true,
graphic: const CircularProgressIndicator(),
2023-11-09 16:09:59 +03:00
delayedContent: true,
),
2024-01-15 17:58:40 +03:00
error: (error, _) => AppFailurePage(cause: error),
2023-11-09 16:09:59 +03:00
data: (otpState) {
2024-01-10 22:47:54 +03:00
final selected = _selected != null
? otpState.slots.firstWhere((e) => e.slot == _selected)
: null;
return OtpActions(
devicePath: widget.devicePath,
builder: (context) => Actions(
actions: {
EscapeIntent:
CallbackAction<EscapeIntent>(onInvoke: (intent) {
if (selected != null) {
setState(() {
_selected = null;
});
} else {
Actions.invoke(context, intent);
}
return false;
}),
OpenIntent<OtpSlot>: CallbackAction<OpenIntent<OtpSlot>>(
onInvoke: (intent) async {
await showBlurDialog(
context: context,
barrierColor: Colors.transparent,
builder: (context) => SlotDialog(intent.target.slot),
);
return null;
}),
},
child: AppPage(
2024-01-15 17:58:40 +03:00
title: l10n.s_slots,
2024-01-26 13:59:37 +03:00
capabilities: const [Capability.otp],
detailViewBuilder: selected != null
? (context) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ListTitle(l10n.s_details),
Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Card(
elevation: 0.0,
color: Theme.of(context).hoverColor,
child: Padding(
padding: const EdgeInsets.all(16),
// TODO: Reuse from fingerprint_dialog
child: Column(
children: [
Text(
selected.slot
.getDisplayName(l10n),
style: Theme.of(context)
.textTheme
.headlineSmall,
softWrap: true,
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
const Icon(
2024-03-08 11:30:47 +03:00
Symbols.touch_app,
size: 100.0,
),
const SizedBox(height: 8),
Text(selected.isConfigured
? l10n.l_otp_slot_configured
: l10n.l_otp_slot_empty)
],
),
),
2024-01-10 19:52:27 +03:00
),
),
ActionListSection.fromMenuActions(
context,
l10n.s_setup,
actions: buildSlotActions(selected, l10n),
)
],
)
: null,
keyActionsBuilder: hasFeature(features.actions)
? (context) => otpBuildActions(
context, widget.devicePath, otpState, ref)
: null,
builder: (context, expanded) {
// De-select if window is resized to be non-expanded.
if (!expanded && _selected != null) {
Timer.run(() {
setState(() {
_selected = null;
});
});
}
return Actions(
2024-01-10 22:47:54 +03:00
actions: {
if (expanded)
OpenIntent<OtpSlot>:
CallbackAction<OpenIntent<OtpSlot>>(
onInvoke: (intent) async {
2024-01-10 22:47:54 +03:00
setState(() {
_selected = intent.target.slot;
2024-01-10 22:47:54 +03:00
});
return null;
}),
2024-01-10 22:47:54 +03:00
},
child: Column(children: [
...otpState.slots.map((e) => _SlotListItem(
e,
expanded: expanded,
selected: e == selected,
))
]),
);
},
),
));
2023-11-09 16:09:59 +03:00
});
}
}
class _SlotListItem extends ConsumerWidget {
final OtpSlot otpSlot;
2024-01-10 13:26:37 +03:00
final bool expanded;
2024-01-10 22:47:54 +03:00
final bool selected;
2024-01-10 13:26:37 +03:00
2024-01-10 22:47:54 +03:00
const _SlotListItem(this.otpSlot,
{required this.expanded, required this.selected});
2023-11-09 16:09:59 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
final slot = otpSlot.slot;
final l10n = AppLocalizations.of(context)!;
final colorScheme = Theme.of(context).colorScheme;
final isConfigured = otpSlot.isConfigured;
final hasFeature = ref.watch(featureProvider);
2024-01-09 16:26:52 +03:00
return AppListItem(
2024-02-08 16:21:05 +03:00
key: getAppListItemKey(slot),
otpSlot,
2024-01-10 13:26:37 +03:00
selected: selected,
2024-01-09 16:26:52 +03:00
leading: CircleAvatar(
foregroundColor: colorScheme.onSecondary,
backgroundColor: colorScheme.secondary,
child: Text(slot.numberId.toString())),
title: slot.getDisplayName(l10n),
subtitle:
isConfigured ? l10n.l_otp_slot_configured : l10n.l_otp_slot_empty,
2024-01-10 13:26:37 +03:00
trailing: expanded
? null
: OutlinedButton(
2024-02-08 16:21:05 +03:00
key: getOpenMenuButtonKey(slot),
onPressed: Actions.handler(context, OpenIntent(otpSlot)),
2024-03-08 11:30:47 +03:00
child: const Icon(Symbols.more_horiz),
2024-01-10 13:26:37 +03:00
),
tapIntent: isDesktop && !expanded ? null : OpenIntent(otpSlot),
doubleTapIntent: isDesktop && !expanded ? OpenIntent(otpSlot) : null,
2024-01-09 16:26:52 +03:00
buildPopupActions: hasFeature(features.slots)
? (context) => buildSlotActions(otpSlot, l10n)
2024-01-09 16:26:52 +03:00
: null,
);
2023-11-09 16:09:59 +03:00
}
}