2022-10-04 13:12:54 +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.
|
|
|
|
*/
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-03-25 18:24:15 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2022-09-06 15:30:18 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
|
|
|
import '../../app/models.dart';
|
2022-06-10 14:49:02 +03:00
|
|
|
import '../../app/shortcuts.dart';
|
2022-05-20 18:19:39 +03:00
|
|
|
import '../../app/views/app_failure_page.dart';
|
2022-03-25 18:24:15 +03:00
|
|
|
import '../../app/views/app_page.dart';
|
2022-05-18 15:11:17 +03:00
|
|
|
import '../../app/views/graphics.dart';
|
2022-04-05 12:46:22 +03:00
|
|
|
import '../../app/views/message_page.dart';
|
2022-09-12 13:58:17 +03:00
|
|
|
import '../keys.dart' as keys;
|
2022-03-25 18:24:15 +03:00
|
|
|
import '../models.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import '../state.dart';
|
|
|
|
import 'account_list.dart';
|
2023-01-12 10:56:08 +03:00
|
|
|
import 'key_actions.dart';
|
2022-09-14 14:19:39 +03:00
|
|
|
import 'unlock_form.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
class OathScreen extends ConsumerWidget {
|
2022-04-03 12:06:22 +03:00
|
|
|
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});
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-04-03 12:06:22 +03:00
|
|
|
return ref.watch(oathStateProvider(devicePath)).when(
|
2022-12-20 18:07:16 +03:00
|
|
|
loading: () => MessagePage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-12-20 18:07:16 +03:00
|
|
|
graphic: const CircularProgressIndicator(),
|
|
|
|
delayedContent: true,
|
2022-04-03 12:06:22 +03:00
|
|
|
),
|
2022-05-20 18:19:39 +03:00
|
|
|
error: (error, _) => AppFailurePage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-05-20 18:19:39 +03:00
|
|
|
cause: error,
|
2022-04-03 12:06:22 +03:00
|
|
|
),
|
2022-03-31 12:41:28 +03:00
|
|
|
data: (oathState) => oathState.locked
|
2022-04-03 12:06:22 +03:00
|
|
|
? _LockedView(devicePath, oathState)
|
|
|
|
: _UnlockedView(devicePath, oathState),
|
2022-03-31 12:41:28 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LockedView extends ConsumerWidget {
|
|
|
|
final DevicePath devicePath;
|
|
|
|
final OathState oathState;
|
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const _LockedView(this.devicePath, this.oathState);
|
2022-03-31 12:41:28 +03:00
|
|
|
|
|
|
|
@override
|
2023-01-12 10:56:08 +03:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
return AppPage(
|
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
|
|
|
keyActionsBuilder: (context) =>
|
|
|
|
oathBuildActions(context, devicePath, oathState, ref),
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 18),
|
|
|
|
child: UnlockForm(
|
|
|
|
devicePath,
|
|
|
|
keystore: oathState.keystore,
|
2022-04-03 12:06:22 +03:00
|
|
|
),
|
2023-01-12 10:56:08 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-03-31 12:41:28 +03:00
|
|
|
}
|
|
|
|
|
2022-06-09 12:52:54 +03:00
|
|
|
class _UnlockedView extends ConsumerStatefulWidget {
|
2022-03-31 12:41:28 +03:00
|
|
|
final DevicePath devicePath;
|
|
|
|
final OathState oathState;
|
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const _UnlockedView(this.devicePath, this.oathState);
|
2022-03-31 12:41:28 +03:00
|
|
|
|
|
|
|
@override
|
2022-06-09 12:52:54 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
searchFocus.dispose();
|
|
|
|
searchController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-09-29 15:30:20 +03:00
|
|
|
// ONLY rebuild if the number of credentials changes.
|
|
|
|
final numCreds = ref.watch(credentialListProvider(widget.devicePath)
|
|
|
|
.select((value) => value?.length));
|
|
|
|
if (numCreds == 0) {
|
2022-04-05 12:46:22 +03:00
|
|
|
return MessagePage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.noAccountsView,
|
2022-05-20 12:11:20 +03:00
|
|
|
graphic: noAccounts,
|
2022-09-06 15:30:18 +03:00
|
|
|
header: AppLocalizations.of(context)!.oath_no_accounts,
|
2023-01-12 10:56:08 +03:00
|
|
|
keyActionsBuilder: (context) => oathBuildActions(
|
|
|
|
context, widget.devicePath, widget.oathState, ref,
|
|
|
|
used: 0),
|
2022-03-31 12:41:28 +03:00
|
|
|
);
|
2022-04-05 12:46:22 +03:00
|
|
|
}
|
2022-06-10 14:49:02 +03:00
|
|
|
return Actions(
|
|
|
|
actions: {
|
|
|
|
SearchIntent: CallbackAction(onInvoke: (_) {
|
2022-06-09 12:52:54 +03:00
|
|
|
searchController.selection = TextSelection(
|
|
|
|
baseOffset: 0, extentOffset: searchController.text.length);
|
|
|
|
searchFocus.requestFocus();
|
2022-06-10 14:49:02 +03:00
|
|
|
return null;
|
|
|
|
}),
|
2022-06-09 12:52:54 +03:00
|
|
|
},
|
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;
|
2022-07-07 13:40:54 +03:00
|
|
|
return TextFormField(
|
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.subtitle1
|
|
|
|
?.copyWith(fontSize: textTheme.titleSmall?.fontSize),
|
2022-09-06 15:30:18 +03:00
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: AppLocalizations.of(context)!.oath_search_accounts,
|
2022-12-08 18:40:00 +03:00
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(32)),
|
|
|
|
),
|
|
|
|
contentPadding: EdgeInsets.zero,
|
2022-07-07 13:40:54 +03:00
|
|
|
isDense: true,
|
2022-09-06 15:30:18 +03:00
|
|
|
prefixIcon: const Icon(Icons.search_outlined),
|
2022-12-08 18:40:00 +03:00
|
|
|
prefixIconConstraints:
|
|
|
|
const BoxConstraints(minHeight: 30, minWidth: 30),
|
|
|
|
/*
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: const Icon(Icons.highlight_off),
|
|
|
|
iconSize: 16,
|
|
|
|
color: Colors.white54,
|
|
|
|
onPressed: searchController.clear,
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2022-12-08 18:40:00 +03:00
|
|
|
suffixIconConstraints:
|
|
|
|
const BoxConstraints(minHeight: 30, minWidth: 30),
|
|
|
|
*/
|
2022-07-07 13:40:54 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
ref.read(searchProvider.notifier).setFilter(value);
|
|
|
|
},
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onFieldSubmitted: (value) {
|
|
|
|
Focus.of(context).focusInDirection(TraversalDirection.down);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2023-01-12 10:56:08 +03:00
|
|
|
keyActionsBuilder: (context) => oathBuildActions(
|
2022-07-07 21:20:04 +03:00
|
|
|
context,
|
2023-01-12 10:56:08 +03:00
|
|
|
widget.devicePath,
|
|
|
|
widget.oathState,
|
2022-09-29 15:30:20 +03:00
|
|
|
ref,
|
|
|
|
used: numCreds ?? 0,
|
2022-07-07 21:20:04 +03:00
|
|
|
),
|
2022-09-29 15:30:20 +03:00
|
|
|
centered: numCreds == null,
|
2022-12-20 18:07:16 +03:00
|
|
|
delayedContent: numCreds == null,
|
2022-09-29 15:30:20 +03:00
|
|
|
child: numCreds != null
|
|
|
|
? Consumer(
|
|
|
|
builder: (context, ref, _) {
|
|
|
|
return AccountList(
|
2022-10-07 15:01:35 +03:00
|
|
|
ref.watch(credentialListProvider(widget.devicePath)) ?? [],
|
2022-09-29 15:30:20 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
2022-12-20 18:07:16 +03:00
|
|
|
: const CircularProgressIndicator(),
|
2022-04-05 12:46:22 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-11-22 11:49:52 +03:00
|
|
|
}
|