2022-08-03 16:21:39 +03:00
|
|
|
import 'dart:io';
|
|
|
|
|
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';
|
|
|
|
|
2021-12-02 13:44:17 +03:00
|
|
|
import '../../about_page.dart';
|
2022-08-03 16:21:39 +03:00
|
|
|
import '../../android/views/android_settings_page.dart';
|
|
|
|
import '../../management/views/management_screen.dart';
|
2022-02-22 13:15:15 +03:00
|
|
|
import '../../settings_page.dart';
|
2022-07-04 13:56:31 +03:00
|
|
|
import '../message.dart';
|
2021-11-24 10:44:28 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
2022-08-12 10:53:34 +03:00
|
|
|
import 'graphics.dart';
|
2021-11-24 10:44:28 +03:00
|
|
|
|
2022-03-14 13:48:39 +03:00
|
|
|
extension on Application {
|
|
|
|
IconData get _icon {
|
|
|
|
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;
|
|
|
|
}
|
2022-03-04 19:45:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-03-14 13:48:39 +03:00
|
|
|
final supportedApps = ref.watch(supportedAppsProvider);
|
2022-06-28 20:51:58 +03:00
|
|
|
final data =
|
|
|
|
ref.watch(currentDeviceDataProvider).whenOrNull(data: (data) => data);
|
2022-03-14 13:48:39 +03:00
|
|
|
final currentApp = ref.watch(currentAppProvider);
|
2022-02-24 17:19:57 +03:00
|
|
|
|
2022-05-20 15:16:50 +03:00
|
|
|
MediaQuery? mediaQuery =
|
|
|
|
context.findAncestorWidgetOfExactType<MediaQuery>();
|
|
|
|
final width = mediaQuery?.data.size.width ?? 400;
|
|
|
|
|
2022-08-12 10:53:34 +03:00
|
|
|
final color =
|
|
|
|
Theme.of(context).brightness == Brightness.dark ? 'white' : 'green';
|
|
|
|
|
2022-05-20 15:16:50 +03:00
|
|
|
return Drawer(
|
|
|
|
width: width < 357 ? 0.85 * width : null,
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.only(
|
|
|
|
topRight: Radius.circular(20.0),
|
|
|
|
bottomRight: Radius.circular(20.0))),
|
|
|
|
child: ListView(
|
|
|
|
primary: false, //Prevents conflict with the MainPage scroll view.
|
|
|
|
children: [
|
2022-08-12 10:53:34 +03:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const Divider(indent: 16.0, endIndent: 28.0),
|
2022-05-20 15:16:50 +03:00
|
|
|
if (data != null) ...[
|
|
|
|
// Normal YubiKey Applications
|
|
|
|
...supportedApps
|
|
|
|
.where((app) =>
|
|
|
|
app != Application.management &&
|
|
|
|
app.getAvailability(data) != Availability.unsupported)
|
|
|
|
.map((app) => ApplicationItem(
|
|
|
|
app: app,
|
|
|
|
available:
|
|
|
|
app.getAvailability(data) == Availability.enabled,
|
|
|
|
selected: app == currentApp,
|
|
|
|
onSelect: () {
|
|
|
|
if (shouldPop) Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
// Management app
|
|
|
|
if (supportedApps.contains(Application.management) &&
|
|
|
|
Application.management.getAvailability(data) ==
|
|
|
|
Availability.enabled) ...[
|
|
|
|
DrawerItem(
|
2022-07-27 13:00:31 +03:00
|
|
|
titleText:
|
|
|
|
AppLocalizations.of(context)!.mainDrawer_txt_applications,
|
2022-05-20 15:16:50 +03:00
|
|
|
icon: Icon(Application.management._icon),
|
|
|
|
onTap: () {
|
|
|
|
if (shouldPop) Navigator.of(context).pop();
|
2022-07-04 13:56:31 +03:00
|
|
|
showBlurDialog(
|
2022-05-18 12:52:50 +03:00
|
|
|
context: context,
|
2022-05-20 15:16:50 +03:00
|
|
|
builder: (context) => ManagementScreen(data),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2022-05-24 10:01:19 +03:00
|
|
|
const Divider(indent: 16.0, endIndent: 28.0),
|
2022-05-18 12:52:50 +03:00
|
|
|
],
|
2022-05-20 15:16:50 +03:00
|
|
|
// Non-YubiKey pages
|
|
|
|
DrawerItem(
|
2022-07-27 13:00:31 +03:00
|
|
|
titleText: AppLocalizations.of(context)!.mainDrawer_txt_settings,
|
2022-05-20 15:16:50 +03:00
|
|
|
icon: const Icon(Icons.settings),
|
|
|
|
onTap: () {
|
|
|
|
final nav = Navigator.of(context);
|
|
|
|
if (shouldPop) nav.pop();
|
2022-07-04 13:56:31 +03:00
|
|
|
showBlurDialog(
|
2022-06-10 17:02:07 +03:00
|
|
|
context: context,
|
2022-08-03 16:21:39 +03:00
|
|
|
builder: (context) => Platform.isAndroid
|
|
|
|
? const AndroidSettingsPage()
|
|
|
|
: const SettingsPage(),
|
2022-06-10 17:02:07 +03:00
|
|
|
routeSettings: const RouteSettings(name: 'settings'),
|
|
|
|
);
|
2022-05-20 15:16:50 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
DrawerItem(
|
2022-07-27 13:00:31 +03:00
|
|
|
titleText: AppLocalizations.of(context)!.mainDrawer_txt_help,
|
2022-05-20 15:16:50 +03:00
|
|
|
icon: const Icon(Icons.help),
|
|
|
|
onTap: () {
|
|
|
|
final nav = Navigator.of(context);
|
|
|
|
if (shouldPop) nav.pop();
|
2022-07-04 13:56:31 +03:00
|
|
|
showBlurDialog(
|
2022-06-10 17:02:07 +03:00
|
|
|
context: context,
|
|
|
|
builder: (context) => const AboutPage(),
|
|
|
|
routeSettings: const RouteSettings(name: 'about'),
|
|
|
|
);
|
2022-05-20 15:16:50 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2021-11-24 10:44:28 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 13:44:17 +03:00
|
|
|
|
2022-03-14 13:48:39 +03:00
|
|
|
class ApplicationItem extends ConsumerWidget {
|
|
|
|
final Application app;
|
2022-03-04 19:45:08 +03:00
|
|
|
final bool available;
|
|
|
|
final bool selected;
|
|
|
|
final Function onSelect;
|
2022-03-14 13:48:39 +03:00
|
|
|
const ApplicationItem({
|
|
|
|
required this.app,
|
2022-03-04 19:45:08 +03:00
|
|
|
required this.available,
|
|
|
|
required this.selected,
|
|
|
|
required this.onSelect,
|
2022-05-12 10:56:55 +03:00
|
|
|
super.key,
|
|
|
|
});
|
2022-03-04 19:45:08 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
return DrawerItem(
|
2022-03-14 13:48:39 +03:00
|
|
|
titleText: app.displayName,
|
|
|
|
icon: Icon(app._icon),
|
2022-03-04 19:45:08 +03:00
|
|
|
selected: selected,
|
|
|
|
enabled: available,
|
|
|
|
onTap: available & !selected
|
|
|
|
? () {
|
2022-03-14 13:48:39 +03:00
|
|
|
ref.read(currentAppProvider.notifier).setCurrentApp(app);
|
2022-03-04 19:45:08 +03:00
|
|
|
onSelect();
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:44:17 +03:00
|
|
|
class DrawerItem extends StatelessWidget {
|
2022-03-04 19:45:08 +03:00
|
|
|
final bool enabled;
|
2021-12-02 13:44:17 +03:00
|
|
|
final bool selected;
|
|
|
|
final String titleText;
|
|
|
|
final Icon icon;
|
|
|
|
final void Function()? onTap;
|
|
|
|
|
|
|
|
const DrawerItem({
|
|
|
|
required this.titleText,
|
|
|
|
required this.icon,
|
|
|
|
this.onTap,
|
|
|
|
this.selected = false,
|
2022-03-04 19:45:08 +03:00
|
|
|
this.enabled = true,
|
2022-05-12 10:56:55 +03:00
|
|
|
super.key,
|
|
|
|
});
|
2021-12-02 13:44:17 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
2022-05-18 12:52:50 +03:00
|
|
|
padding: const EdgeInsets.only(left: 12.0, right: 12.0),
|
2021-12-02 13:44:17 +03:00
|
|
|
child: ListTile(
|
2022-03-04 19:45:08 +03:00
|
|
|
enabled: enabled,
|
2021-12-02 13:44:17 +03:00
|
|
|
shape: const RoundedRectangleBorder(
|
2022-05-18 12:52:50 +03:00
|
|
|
borderRadius: BorderRadius.all(Radius.circular(30)),
|
2021-12-02 13:44:17 +03:00
|
|
|
),
|
|
|
|
dense: true,
|
2022-05-18 12:52:50 +03:00
|
|
|
minLeadingWidth: 24,
|
|
|
|
minVerticalPadding: 18,
|
2021-12-02 13:44:17 +03:00
|
|
|
selected: selected,
|
2022-05-20 15:10:17 +03:00
|
|
|
selectedColor: Theme.of(context).colorScheme.onPrimary,
|
|
|
|
selectedTileColor: Theme.of(context).colorScheme.primary,
|
2022-05-20 12:10:49 +03:00
|
|
|
leading: IconTheme.merge(
|
|
|
|
data: const IconThemeData(size: 24),
|
|
|
|
child: icon,
|
|
|
|
),
|
2021-12-02 22:29:40 +03:00
|
|
|
title: Text(titleText),
|
2021-12-02 13:44:17 +03:00
|
|
|
onTap: onTap,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|