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

273 lines
9.0 KiB
Dart
Raw Normal View History

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.
*/
2023-12-22 18:46:29 +03:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2022-09-06 15:30:18 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-12-22 18:46:29 +03:00
import '../../app/message.dart';
import '../../app/models.dart';
2022-06-10 14:49:02 +03:00
import '../../app/shortcuts.dart';
2023-12-22 18:46:29 +03:00
import '../../app/state.dart';
2022-05-20 18:19:39 +03:00
import '../../app/views/app_failure_page.dart';
import '../../app/views/app_page.dart';
import '../../app/views/message_page.dart';
import '../../core/state.dart';
import '../../widgets/app_input_decoration.dart';
2023-11-10 17:24:53 +03:00
import '../../widgets/app_text_form_field.dart';
2024-01-08 16:11:32 +03:00
import '../../widgets/file_drop_overlay.dart';
import '../features.dart' as features;
2022-09-12 13:58:17 +03:00
import '../keys.dart' as keys;
import '../models.dart';
import '../state.dart';
import 'account_list.dart';
import 'key_actions.dart';
2022-09-14 14:19:39 +03:00
import 'unlock_form.dart';
2023-12-22 18:46:29 +03:00
import 'utils.dart';
class OathScreen extends ConsumerWidget {
final DevicePath devicePath;
2022-10-18 12:52:02 +03:00
2022-05-12 10:56:55 +03:00
const OathScreen(this.devicePath, {super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-02-28 21:05:46 +03:00
final l10n = AppLocalizations.of(context)!;
return ref.watch(oathStateProvider(devicePath)).when(
loading: () => MessagePage(
title: Text(l10n.s_authenticator),
graphic: const CircularProgressIndicator(),
delayedContent: true,
),
error: (error, _) => AppFailurePage(
title: Text(l10n.s_authenticator),
cause: error,
),
data: (oathState) => oathState.locked
? _LockedView(devicePath, oathState)
: _UnlockedView(devicePath, oathState),
);
2022-03-31 12:41:28 +03:00
}
}
class _LockedView extends ConsumerWidget {
final DevicePath devicePath;
final OathState oathState;
const _LockedView(this.devicePath, this.oathState);
2022-03-31 12:41:28 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
final hasActions = ref.watch(featureProvider)(features.actions);
return AppPage(
title: Text(AppLocalizations.of(context)!.s_authenticator),
keyActionsBuilder: hasActions
? (context) => oathBuildActions(context, devicePath, oathState, ref)
: null,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 18),
child: UnlockForm(
devicePath,
keystore: oathState.keystore,
),
),
);
}
2022-03-31 12:41:28 +03:00
}
class _UnlockedView extends ConsumerStatefulWidget {
2022-03-31 12:41:28 +03:00
final DevicePath devicePath;
final OathState oathState;
const _UnlockedView(this.devicePath, this.oathState);
2022-03-31 12:41:28 +03:00
@override
ConsumerState<ConsumerStatefulWidget> createState() => _UnlockedViewState();
}
class _UnlockedViewState extends ConsumerState<_UnlockedView> {
late FocusNode searchFocus;
late TextEditingController searchController;
@override
void initState() {
super.initState();
searchFocus = FocusNode();
searchController = TextEditingController(text: ref.read(searchProvider));
2023-12-20 17:09:31 +03:00
searchFocus.addListener(_onFocusChange);
}
@override
void dispose() {
searchFocus.dispose();
searchController.dispose();
super.dispose();
}
2023-12-20 17:09:31 +03:00
void _onFocusChange() {
setState(() {});
}
@override
Widget build(BuildContext context) {
2023-02-28 21:05:46 +03:00
final l10n = AppLocalizations.of(context)!;
// ONLY rebuild if the number of credentials changes.
final numCreds = ref.watch(credentialListProvider(widget.devicePath)
.select((value) => value?.length));
final hasActions = ref.watch(featureProvider)(features.actions);
Future<void> onFileDropped(List<int> fileData) async {
final qrScanner = ref.read(qrScannerProvider);
if (qrScanner != null) {
final b64Image = base64Encode(fileData);
final qrData = await qrScanner.scanQr(b64Image);
final withContext = ref.read(withContextProvider);
await withContext(
(context) async {
if (qrData != null) {
final credentials = ref.read(credentialsProvider);
await handleUri(context, credentials, qrData, widget.devicePath,
widget.oathState, l10n);
} else {
showMessage(context, l10n.l_qr_not_found);
}
},
);
}
}
if (numCreds == 0) {
return MessagePage(
title: Text(l10n.s_authenticator),
2022-09-12 13:58:17 +03:00
key: keys.noAccountsView,
2023-12-20 17:09:31 +03:00
graphic: Icon(Icons.people,
size: 96, color: Theme.of(context).colorScheme.primary),
header: l10n.s_no_accounts,
keyActionsBuilder: hasActions
? (context) => oathBuildActions(
context, widget.devicePath, widget.oathState, ref,
used: 0)
: null,
onFileDropped: onFileDropped,
2024-01-08 16:11:32 +03:00
fileDropOverlay: FileDropOverlay(
title: l10n.s_add_account,
subtitle: l10n.l_drop_qr_description,
2024-01-08 16:11:32 +03:00
),
2022-03-31 12:41:28 +03:00
);
}
2022-06-10 14:49:02 +03:00
return Actions(
actions: {
2023-02-24 16:19:34 +03:00
SearchIntent: CallbackAction<SearchIntent>(onInvoke: (_) {
searchController.selection = TextSelection(
baseOffset: 0, extentOffset: searchController.text.length);
searchFocus.requestFocus();
2022-06-10 14:49:02 +03:00
return null;
}),
},
2022-07-07 13:40:54 +03:00
child: AppPage(
title: Focus(
canRequestFocus: false,
onKeyEvent: (node, event) {
if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
node.focusInDirection(TraversalDirection.down);
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
child: Builder(builder: (context) {
2022-09-06 16:02:24 +03:00
final textTheme = Theme.of(context).textTheme;
2023-11-10 17:24:53 +03:00
return AppTextFormField(
2022-09-12 13:58:17 +03:00
key: keys.searchAccountsField,
2022-07-07 13:40:54 +03:00
controller: searchController,
focusNode: searchFocus,
2022-09-06 16:02:24 +03:00
// Use the default style, but with a smaller font size:
style: textTheme.titleMedium
2022-09-06 16:02:24 +03:00
?.copyWith(fontSize: textTheme.titleSmall?.fontSize),
decoration: AppInputDecoration(
2023-12-20 17:09:31 +03:00
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32),
borderSide: BorderSide(
width: 0,
style: searchFocus.hasFocus
? BorderStyle.solid
: BorderStyle.none,
),
),
2023-12-20 17:09:31 +03:00
contentPadding: const EdgeInsets.all(16),
fillColor: Theme.of(context).hoverColor,
filled: true,
hintText: l10n.s_search_accounts,
2022-07-07 13:40:54 +03:00
isDense: true,
2023-12-20 17:09:31 +03:00
prefixIcon: const Padding(
padding: EdgeInsetsDirectional.only(start: 8.0),
child: Icon(Icons.search_outlined),
),
2023-12-20 17:09:31 +03:00
suffixIcon: searchController.text.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear),
iconSize: 16,
onPressed: () {
searchController.clear();
ref.read(searchProvider.notifier).setFilter('');
setState(() {});
},
)
: null,
2022-07-07 13:40:54 +03:00
),
onChanged: (value) {
ref.read(searchProvider.notifier).setFilter(value);
2023-12-20 17:09:31 +03:00
setState(() {});
2022-07-07 13:40:54 +03:00
},
textInputAction: TextInputAction.next,
onFieldSubmitted: (value) {
Focus.of(context).focusInDirection(TraversalDirection.down);
},
);
}),
),
keyActionsBuilder: hasActions
? (context) => oathBuildActions(
context,
widget.devicePath,
widget.oathState,
ref,
used: numCreds ?? 0,
)
: null,
onFileDropped: onFileDropped,
2024-01-08 16:11:32 +03:00
fileDropOverlay: FileDropOverlay(
title: l10n.s_add_account,
subtitle: l10n.l_drop_qr_description,
2024-01-08 16:11:32 +03:00
),
centered: numCreds == null,
delayedContent: numCreds == null,
child: numCreds != null
? Consumer(
builder: (context, ref, _) {
return AccountList(
ref.watch(credentialListProvider(widget.devicePath)) ?? [],
);
},
)
: const CircularProgressIndicator(),
),
);
}
}