2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2023-10-10 09:54:25 +03:00
|
|
|
* Copyright (C) 2022,2024 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.
|
|
|
|
*/
|
|
|
|
|
2022-03-25 10:59:02 +03:00
|
|
|
import 'dart:async';
|
2024-03-06 23:06:48 +03:00
|
|
|
import 'dart:convert';
|
2023-08-30 09:53:17 +03:00
|
|
|
import 'dart:io';
|
2023-02-24 16:20:17 +03:00
|
|
|
import 'dart:ui';
|
2022-03-25 10:59:02 +03:00
|
|
|
|
2021-12-02 13:44:17 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2021-11-19 17:05:57 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2021-11-23 15:02:05 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2021-11-23 16:51:36 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-11-19 17:05:57 +03:00
|
|
|
|
2021-12-02 13:44:17 +03:00
|
|
|
import '../core/state.dart';
|
2023-10-10 09:54:25 +03:00
|
|
|
import '../theme.dart';
|
2023-09-29 15:12:11 +03:00
|
|
|
import 'features.dart' as features;
|
2023-11-27 13:41:05 +03:00
|
|
|
import 'logging.dart';
|
|
|
|
import 'models.dart';
|
2021-11-19 17:05:57 +03:00
|
|
|
|
2022-02-21 11:38:09 +03:00
|
|
|
final _log = Logger('app.state');
|
2021-11-23 15:02:05 +03:00
|
|
|
|
2023-03-02 11:34:52 +03:00
|
|
|
// Officially supported translations
|
|
|
|
const officialLocales = [
|
|
|
|
Locale('en', ''),
|
2024-04-11 15:24:50 +03:00
|
|
|
Locale('fr', ''),
|
|
|
|
Locale('ja', ''),
|
2023-03-02 11:34:52 +03:00
|
|
|
];
|
|
|
|
|
2024-03-08 19:35:50 +03:00
|
|
|
extension on Section {
|
2023-09-29 15:12:11 +03:00
|
|
|
Feature get _feature => switch (this) {
|
2024-03-08 19:35:50 +03:00
|
|
|
Section.home => features.home,
|
|
|
|
Section.accounts => features.oath,
|
|
|
|
Section.securityKey => features.fido,
|
|
|
|
Section.passkeys => features.fido,
|
|
|
|
Section.fingerprints => features.fingerprints,
|
|
|
|
Section.slots => features.otp,
|
|
|
|
Section.certificates => features.piv,
|
2023-09-29 15:12:11 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-08 19:35:50 +03:00
|
|
|
final supportedSectionsProvider = Provider<List<Section>>(
|
2024-02-03 18:12:51 +03:00
|
|
|
(ref) {
|
|
|
|
final hasFeature = ref.watch(featureProvider);
|
2024-03-08 19:35:50 +03:00
|
|
|
return Section.values
|
|
|
|
.where((section) => hasFeature(section._feature))
|
|
|
|
.toList();
|
2024-02-03 18:12:51 +03:00
|
|
|
},
|
|
|
|
);
|
2022-03-14 13:48:39 +03:00
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
// Default implementation is always focused, override with platform specific version.
|
|
|
|
final windowStateProvider = Provider<WindowState>(
|
|
|
|
(ref) => WindowState(focused: true, visible: true, active: true),
|
|
|
|
);
|
2021-12-03 12:27:29 +03:00
|
|
|
|
2022-09-23 11:17:28 +03:00
|
|
|
final supportedThemesProvider = StateProvider<List<ThemeMode>>(
|
|
|
|
(ref) => throw UnimplementedError(),
|
|
|
|
);
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2023-03-02 11:34:52 +03:00
|
|
|
final communityTranslationsProvider =
|
|
|
|
StateNotifierProvider<CommunityTranslationsNotifier, bool>(
|
|
|
|
(ref) => CommunityTranslationsNotifier(ref.watch(prefProvider)));
|
2023-02-24 16:20:17 +03:00
|
|
|
|
2023-03-02 11:34:52 +03:00
|
|
|
class CommunityTranslationsNotifier extends StateNotifier<bool> {
|
|
|
|
static const String _key = 'APP_STATE_ENABLE_COMMUNITY_TRANSLATIONS';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
|
|
|
|
CommunityTranslationsNotifier(this._prefs)
|
|
|
|
: super(_prefs.getBool(_key) == true);
|
2023-02-24 16:20:17 +03:00
|
|
|
|
2023-03-02 11:34:52 +03:00
|
|
|
void setEnableCommunityTranslations(bool value) {
|
|
|
|
state = value;
|
|
|
|
_prefs.setBool(_key, value);
|
2023-03-01 12:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 09:53:17 +03:00
|
|
|
final supportedLocalesProvider = Provider<List<Locale>>((ref) {
|
|
|
|
final locales = [...officialLocales];
|
|
|
|
final localeStr = Platform.environment['_YA_LOCALE'];
|
|
|
|
if (localeStr != null) {
|
|
|
|
// Force locale
|
|
|
|
final locale = Locale(localeStr, '');
|
|
|
|
locales.add(locale);
|
|
|
|
}
|
|
|
|
return ref.watch(communityTranslationsProvider)
|
|
|
|
? AppLocalizations.supportedLocales
|
|
|
|
: locales;
|
|
|
|
});
|
2023-02-24 16:20:17 +03:00
|
|
|
|
2023-03-02 11:34:52 +03:00
|
|
|
final currentLocaleProvider = Provider<Locale>(
|
2023-08-30 09:53:17 +03:00
|
|
|
(ref) {
|
|
|
|
final localeStr = Platform.environment['_YA_LOCALE'];
|
|
|
|
if (localeStr != null) {
|
|
|
|
// Force locale
|
|
|
|
final locale = Locale(localeStr, '');
|
|
|
|
return basicLocaleListResolution(
|
|
|
|
[locale], AppLocalizations.supportedLocales);
|
|
|
|
}
|
|
|
|
// Choose from supported
|
|
|
|
return basicLocaleListResolution(PlatformDispatcher.instance.locales,
|
|
|
|
ref.watch(supportedLocalesProvider));
|
|
|
|
},
|
2023-03-02 11:34:52 +03:00
|
|
|
);
|
2023-02-24 16:20:17 +03:00
|
|
|
|
2023-03-02 11:34:52 +03:00
|
|
|
final l10nProvider = Provider<AppLocalizations>(
|
|
|
|
(ref) => lookupAppLocalizations(ref.watch(currentLocaleProvider)),
|
|
|
|
);
|
2023-02-24 16:20:17 +03:00
|
|
|
|
2021-12-02 13:44:17 +03:00
|
|
|
final themeModeProvider = StateNotifierProvider<ThemeModeNotifier, ThemeMode>(
|
2024-01-11 18:56:27 +03:00
|
|
|
(ref) {
|
|
|
|
// initialize the keyCustomizationManager
|
|
|
|
ref.read(keyCustomizationManagerProvider);
|
|
|
|
return ThemeModeNotifier(
|
|
|
|
ref.watch(prefProvider), ref.read(supportedThemesProvider));
|
|
|
|
},
|
2022-09-21 16:29:34 +03:00
|
|
|
);
|
2021-12-02 13:44:17 +03:00
|
|
|
|
|
|
|
class ThemeModeNotifier extends StateNotifier<ThemeMode> {
|
|
|
|
static const String _key = 'APP_STATE_THEME';
|
|
|
|
final SharedPreferences _prefs;
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2022-09-22 18:48:06 +03:00
|
|
|
ThemeModeNotifier(this._prefs, List<ThemeMode> supportedThemes)
|
|
|
|
: super(_fromName(_prefs.getString(_key), supportedThemes));
|
2021-12-02 13:44:17 +03:00
|
|
|
|
|
|
|
void setThemeMode(ThemeMode mode) {
|
2022-05-03 12:24:25 +03:00
|
|
|
_log.debug('Set theme to $mode');
|
2021-12-02 13:44:17 +03:00
|
|
|
state = mode;
|
|
|
|
_prefs.setString(_key, mode.name);
|
|
|
|
}
|
|
|
|
|
2022-09-22 18:48:06 +03:00
|
|
|
static ThemeMode _fromName(String? name, List<ThemeMode> supportedThemes) =>
|
|
|
|
supportedThemes.firstWhere((element) => element.name == name,
|
|
|
|
orElse: () => supportedThemes.first);
|
2021-12-02 13:44:17 +03:00
|
|
|
}
|
|
|
|
|
2024-01-24 19:13:03 +03:00
|
|
|
final defaultColorProvider = Provider<Color>((ref) => defaultPrimaryColor);
|
|
|
|
|
|
|
|
final primaryColorProvider = Provider<Color>((ref) {
|
|
|
|
const prefLastUsedColor = 'LAST_USED_COLOR';
|
|
|
|
final prefs = ref.watch(prefProvider);
|
2024-01-24 21:21:45 +03:00
|
|
|
final data = ref.watch(currentDeviceDataProvider).valueOrNull;
|
|
|
|
final defaultColor = ref.watch(defaultColorProvider);
|
|
|
|
if (data != null) {
|
|
|
|
final serial = data.info.serial;
|
|
|
|
if (serial != null) {
|
|
|
|
final customization = ref.watch(keyCustomizationManagerProvider)[serial];
|
|
|
|
final deviceColor = customization?.color;
|
|
|
|
if (deviceColor != null) {
|
|
|
|
prefs.setInt(prefLastUsedColor, deviceColor.value);
|
|
|
|
return deviceColor;
|
|
|
|
} else {
|
|
|
|
prefs.remove(prefLastUsedColor);
|
|
|
|
return defaultColor;
|
|
|
|
}
|
|
|
|
}
|
2024-01-11 18:56:27 +03:00
|
|
|
}
|
2023-10-10 09:54:25 +03:00
|
|
|
|
2024-04-03 15:22:24 +03:00
|
|
|
final lastUsedColor = prefs.getInt(prefLastUsedColor);
|
|
|
|
return lastUsedColor != null ? Color(lastUsedColor) : defaultColor;
|
2024-01-24 19:13:03 +03:00
|
|
|
});
|
2024-01-11 18:56:27 +03:00
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
// Override with platform implementation
|
2022-03-07 11:57:29 +03:00
|
|
|
final attachedDevicesProvider =
|
2022-11-30 17:27:32 +03:00
|
|
|
NotifierProvider<AttachedDevicesNotifier, List<DeviceNode>>(
|
|
|
|
() => throw UnimplementedError(),
|
2022-01-27 14:34:29 +03:00
|
|
|
);
|
2022-01-12 14:49:04 +03:00
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
abstract class AttachedDevicesNotifier extends Notifier<List<DeviceNode>> {
|
2022-03-07 11:57:29 +03:00
|
|
|
/// Force a refresh of all device data.
|
|
|
|
void refresh() {}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
// Override with platform implementation
|
2022-06-28 20:51:58 +03:00
|
|
|
final currentDeviceDataProvider = Provider<AsyncValue<YubiKeyData>>(
|
2022-03-04 19:45:08 +03:00
|
|
|
(ref) => throw UnimplementedError(),
|
2022-01-27 14:34:29 +03:00
|
|
|
);
|
2022-01-12 14:49:04 +03:00
|
|
|
|
2022-03-21 11:34:45 +03:00
|
|
|
// Override with platform implementation
|
2021-11-19 17:05:57 +03:00
|
|
|
final currentDeviceProvider =
|
2022-11-30 17:27:32 +03:00
|
|
|
NotifierProvider<CurrentDeviceNotifier, DeviceNode?>(
|
|
|
|
() => throw UnimplementedError());
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
abstract class CurrentDeviceNotifier extends Notifier<DeviceNode?> {
|
2022-06-07 16:13:11 +03:00
|
|
|
setCurrentDevice(DeviceNode? device);
|
2021-11-19 17:05:57 +03:00
|
|
|
}
|
|
|
|
|
2024-03-08 19:35:50 +03:00
|
|
|
final currentSectionProvider =
|
2024-03-14 16:31:11 +03:00
|
|
|
StateNotifierProvider<CurrentSectionNotifier, Section>(
|
|
|
|
(ref) => throw UnimplementedError());
|
2024-03-07 14:43:06 +03:00
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
abstract class CurrentSectionNotifier extends StateNotifier<Section> {
|
|
|
|
CurrentSectionNotifier(super.initial);
|
2024-03-07 14:43:06 +03:00
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
setCurrentSection(Section section);
|
2022-03-14 12:57:00 +03:00
|
|
|
}
|
2022-03-04 19:45:08 +03:00
|
|
|
|
2022-02-10 17:24:28 +03:00
|
|
|
abstract class QrScanner {
|
2022-06-13 17:45:26 +03:00
|
|
|
/// Scans (or searches the given image) for a QR code, and decodes it.
|
|
|
|
///
|
|
|
|
/// The contained data is returned as a String, or null, if no QR code is
|
|
|
|
/// found.
|
|
|
|
Future<String?> scanQr([String? imageData]);
|
2022-02-10 17:24:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
final qrScannerProvider = Provider<QrScanner?>(
|
|
|
|
(ref) => null,
|
|
|
|
);
|
2022-03-25 10:59:02 +03:00
|
|
|
|
2022-04-28 16:23:04 +03:00
|
|
|
final contextConsumer =
|
|
|
|
StateNotifierProvider<ContextConsumer, Function(BuildContext)?>(
|
|
|
|
(ref) => ContextConsumer());
|
2022-03-25 10:59:02 +03:00
|
|
|
|
2022-04-28 16:23:04 +03:00
|
|
|
class ContextConsumer extends StateNotifier<Function(BuildContext)?> {
|
|
|
|
ContextConsumer() : super(null);
|
2022-03-25 10:59:02 +03:00
|
|
|
|
|
|
|
Future<T> withContext<T>(Future<T> Function(BuildContext context) action) {
|
|
|
|
final completer = Completer<T>();
|
|
|
|
if (mounted) {
|
|
|
|
state = (context) async {
|
|
|
|
completer.complete(await action(context));
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
completer.completeError('Not attached');
|
|
|
|
}
|
|
|
|
return completer.future;
|
|
|
|
}
|
|
|
|
}
|
2022-04-28 16:23:04 +03:00
|
|
|
|
2022-09-21 16:29:34 +03:00
|
|
|
abstract class AppClipboard {
|
|
|
|
const AppClipboard();
|
|
|
|
|
|
|
|
Future<void> setText(String toClipboard, {bool isSensitive = false});
|
|
|
|
|
|
|
|
bool platformGivesFeedback();
|
|
|
|
}
|
|
|
|
|
|
|
|
final clipboardProvider = Provider<AppClipboard>(
|
|
|
|
(ref) => throw UnimplementedError(),
|
|
|
|
);
|
|
|
|
|
2022-04-28 16:23:04 +03:00
|
|
|
/// A callback which will be invoked with a [BuildContext] that can be used to
|
|
|
|
/// open dialogs, show Snackbars, etc.
|
|
|
|
///
|
|
|
|
/// Used with the [withContextProvider] provider.
|
|
|
|
typedef WithContext = Future<T> Function<T>(
|
|
|
|
Future<T> Function(BuildContext context) action);
|
|
|
|
|
|
|
|
final withContextProvider = Provider<WithContext>(
|
|
|
|
(ref) => ref.watch(contextConsumer.notifier).withContext);
|
2024-03-06 23:06:48 +03:00
|
|
|
|
|
|
|
final keyCustomizationManagerProvider =
|
|
|
|
StateNotifierProvider<KeyCustomizationNotifier, Map<int, KeyCustomization>>(
|
|
|
|
(ref) => KeyCustomizationNotifier(ref.watch(prefProvider)));
|
|
|
|
|
|
|
|
class KeyCustomizationNotifier
|
|
|
|
extends StateNotifier<Map<int, KeyCustomization>> {
|
|
|
|
static const _prefKeyCustomizations = 'KEY_CUSTOMIZATIONS';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
|
|
|
|
KeyCustomizationNotifier(this._prefs)
|
|
|
|
: super(_readCustomizations(_prefs.getString(_prefKeyCustomizations)));
|
|
|
|
|
|
|
|
static Map<int, KeyCustomization> _readCustomizations(String? pref) {
|
|
|
|
if (pref == null) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final retval = <int, KeyCustomization>{};
|
|
|
|
for (var element in json.decode(pref)) {
|
|
|
|
final keyCustomization = KeyCustomization.fromJson(element);
|
|
|
|
retval[keyCustomization.serial] = keyCustomization;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
} catch (e) {
|
|
|
|
_log.error('Failure reading customizations: $e');
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyCustomization? get(int serial) {
|
|
|
|
_log.debug('Getting key customization for $serial');
|
|
|
|
return state[serial];
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> set({required int serial, String? name, Color? color}) async {
|
|
|
|
_log.debug('Setting key customization for $serial: $name, $color');
|
|
|
|
if (name == null && color == null) {
|
|
|
|
// remove this customization
|
|
|
|
state = {...state..remove(serial)};
|
|
|
|
} else {
|
|
|
|
state = {
|
|
|
|
...state
|
|
|
|
..[serial] =
|
|
|
|
KeyCustomization(serial: serial, name: name, color: color)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
await _prefs.setString(
|
|
|
|
_prefKeyCustomizations, json.encode(state.values.toList()));
|
|
|
|
}
|
|
|
|
}
|