yubioath-flutter/lib/app/views/main_drawer.dart

166 lines
5.2 KiB
Dart
Raw Normal View History

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-24 10:44:28 +03:00
import 'package:flutter/material.dart';
2022-07-27 13:00:31 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2021-11-24 10:44:28 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../management/views/management_screen.dart';
2022-07-04 13:56:31 +03:00
import '../message.dart';
2021-11-24 10:44:28 +03:00
import '../models.dart';
import '../shortcuts.dart';
2021-11-24 10:44:28 +03:00
import '../state.dart';
2022-10-03 16:34:06 +03:00
import 'keys.dart';
2021-11-24 10:44:28 +03:00
extension on Application {
IconData get _icon {
2023-02-07 18:31:53 +03:00
switch (this) {
case Application.oath:
return Icons.supervisor_account_outlined;
case Application.fido:
return Icons.security_outlined;
case Application.otp:
return Icons.password_outlined;
case Application.piv:
return Icons.approval_outlined;
case Application.management:
return Icons.construction_outlined;
case Application.openpgp:
return Icons.key_outlined;
case Application.hsmauth:
return Icons.key_outlined;
}
}
IconData get _filledIcon {
switch (this) {
case Application.oath:
return Icons.supervisor_account;
case Application.fido:
return Icons.security;
case Application.otp:
return Icons.password;
case Application.piv:
return Icons.approval;
case Application.management:
return Icons.construction;
case Application.openpgp:
return Icons.key;
case Application.hsmauth:
return Icons.key;
}
}
}
2021-11-24 10:44:28 +03:00
class MainPageDrawer extends ConsumerWidget {
2022-03-03 19:09:03 +03:00
final bool shouldPop;
2022-05-12 10:56:55 +03:00
const MainPageDrawer({this.shouldPop = true, super.key});
2021-11-24 10:44:28 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-02-28 13:34:29 +03:00
final l10n = AppLocalizations.of(context)!;
final supportedApps = ref.watch(supportedAppsProvider);
final data = ref.watch(currentDeviceDataProvider).valueOrNull;
2022-08-12 10:53:34 +03:00
final color =
Theme.of(context).brightness == Brightness.dark ? 'white' : 'green';
2023-02-07 18:31:53 +03:00
final availableApps = data != null
? supportedApps
.where(
(app) => app.getAvailability(data) != Availability.unsupported)
.toList()
: <Application>[];
final hasManagement = availableApps.remove(Application.management);
return NavigationDrawer(
selectedIndex: availableApps.indexOf(ref.watch(currentAppProvider)),
onDestinationSelected: (index) {
if (shouldPop) Navigator.of(context).pop();
if (index < availableApps.length) {
// Switch to selected app
final app = availableApps[index];
ref.read(currentAppProvider.notifier).setCurrentApp(app);
} else {
// Handle action
index -= availableApps.length;
if (!hasManagement) {
index++;
}
switch (index) {
case 0:
showBlurDialog(
context: context,
// data must be non-null when index == 0
builder: (context) => ManagementScreen(data!),
);
2023-02-07 18:31:53 +03:00
break;
case 1:
Actions.maybeInvoke(context, const SettingsIntent());
2023-02-07 18:31:53 +03:00
break;
case 2:
Actions.maybeInvoke(context, const AboutIntent());
2023-02-07 18:31:53 +03:00
break;
}
}
},
2023-02-07 16:01:17 +03:00
children: [
Padding(
padding: const EdgeInsets.only(top: 19.0, left: 30.0, bottom: 12.0),
child: Image.asset(
'assets/graphics/yubico-$color.png',
alignment: Alignment.centerLeft,
height: 28,
filterQuality: FilterQuality.medium,
2022-08-12 10:53:34 +03:00
),
2023-02-07 16:01:17 +03:00
),
const Divider(indent: 16.0, endIndent: 28.0),
if (data != null) ...[
// Normal YubiKey Applications
2023-02-07 18:31:53 +03:00
...availableApps.map((app) => NavigationDrawerDestination(
2023-02-28 13:34:29 +03:00
label: Text(app.getDisplayName(l10n)),
2023-02-07 18:31:53 +03:00
icon: Icon(app._icon),
selectedIcon: Icon(app._filledIcon),
)),
2023-02-07 16:01:17 +03:00
// Management app
2023-02-07 18:31:53 +03:00
if (hasManagement) ...[
NavigationDrawerDestination(
2023-02-07 16:01:17 +03:00
key: managementAppDrawer,
2023-02-07 18:31:53 +03:00
label: Text(
l10n.s_toggle_applications,
2023-02-07 18:31:53 +03:00
),
icon: Icon(Application.management._icon),
selectedIcon: Icon(Application.management._filledIcon),
2023-02-07 16:01:17 +03:00
),
2022-05-18 12:52:50 +03:00
],
2023-02-07 16:01:17 +03:00
const Divider(indent: 16.0, endIndent: 28.0),
2022-05-20 15:16:50 +03:00
],
2023-02-07 16:01:17 +03:00
// Non-YubiKey pages
2023-02-07 18:31:53 +03:00
NavigationDrawerDestination(
label: Text(l10n.s_settings),
2023-02-07 18:31:53 +03:00
icon: const Icon(Icons.settings_outlined),
2023-02-07 16:01:17 +03:00
),
2023-02-07 18:31:53 +03:00
NavigationDrawerDestination(
label: Text(l10n.s_help_and_about),
2023-02-07 18:31:53 +03:00
icon: const Icon(Icons.help_outline),
2023-02-07 16:01:17 +03:00
),
],
2021-11-24 10:44:28 +03:00
);
}
}