yubioath-flutter/lib/app/state.dart

176 lines
5.3 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.
*/
import 'dart:async';
import 'package:flutter/material.dart';
2021-11-19 17:05:57 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
2021-11-23 16:51:36 +03:00
import 'package:shared_preferences/shared_preferences.dart';
2022-05-03 12:24:25 +03:00
import 'package:yubico_authenticator/app/logging.dart';
2021-11-19 17:05:57 +03:00
import '../core/state.dart';
2021-11-19 17:05:57 +03:00
import 'models.dart';
final _log = Logger('app.state');
// Override this to alter the set of supported apps.
final supportedAppsProvider =
Provider<List<Application>>((ref) => Application.values);
// Default implementation is always focused, override with platform specific version.
final windowStateProvider = Provider<WindowState>(
(ref) => WindowState(focused: true, visible: true, active: true),
);
2022-09-23 11:17:28 +03:00
final supportedThemesProvider = StateProvider<List<ThemeMode>>(
(ref) => throw UnimplementedError(),
);
final themeModeProvider = StateNotifierProvider<ThemeModeNotifier, ThemeMode>(
(ref) => ThemeModeNotifier(
ref.watch(prefProvider), ref.read(supportedThemesProvider)),
);
class ThemeModeNotifier extends StateNotifier<ThemeMode> {
static const String _key = 'APP_STATE_THEME';
final SharedPreferences _prefs;
2022-09-22 18:48:06 +03:00
ThemeModeNotifier(this._prefs, List<ThemeMode> supportedThemes)
: super(_fromName(_prefs.getString(_key), supportedThemes));
void setThemeMode(ThemeMode mode) {
2022-05-03 12:24:25 +03:00
_log.debug('Set theme to $mode');
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);
}
// Override with platform implementation
2022-03-07 11:57:29 +03:00
final attachedDevicesProvider =
NotifierProvider<AttachedDevicesNotifier, List<DeviceNode>>(
() => throw UnimplementedError(),
);
2022-01-12 14:49:04 +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() {}
}
// Override with platform implementation
final currentDeviceDataProvider = Provider<AsyncValue<YubiKeyData>>(
(ref) => throw UnimplementedError(),
);
2022-01-12 14:49:04 +03:00
// Override with platform implementation
2021-11-19 17:05:57 +03:00
final currentDeviceProvider =
NotifierProvider<CurrentDeviceNotifier, DeviceNode?>(
() => throw UnimplementedError());
abstract class CurrentDeviceNotifier extends Notifier<DeviceNode?> {
setCurrentDevice(DeviceNode? device);
2021-11-19 17:05:57 +03:00
}
final currentAppProvider =
StateNotifierProvider<CurrentAppNotifier, Application>((ref) {
final notifier = CurrentAppNotifier(ref.watch(supportedAppsProvider));
ref.listen<AsyncValue<YubiKeyData>>(currentDeviceDataProvider, (_, data) {
notifier._notifyDeviceChanged(data.whenOrNull(data: ((data) => data)));
}, fireImmediately: true);
return notifier;
});
class CurrentAppNotifier extends StateNotifier<Application> {
final List<Application> _supportedApps;
CurrentAppNotifier(this._supportedApps) : super(_supportedApps.first);
2021-11-19 17:05:57 +03:00
void setCurrentApp(Application app) {
state = app;
2021-11-19 17:05:57 +03:00
}
void _notifyDeviceChanged(YubiKeyData? data) {
if (data == null ||
state.getAvailability(data) != Availability.unsupported) {
// Keep current app
return;
}
state = _supportedApps.firstWhere(
(app) => app.getAvailability(data) == Availability.enabled,
orElse: () => _supportedApps.first,
);
}
}
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,
);
final contextConsumer =
StateNotifierProvider<ContextConsumer, Function(BuildContext)?>(
(ref) => ContextConsumer());
class ContextConsumer extends StateNotifier<Function(BuildContext)?> {
ContextConsumer() : super(null);
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;
}
}
abstract class AppClipboard {
const AppClipboard();
Future<void> setText(String toClipboard, {bool isSensitive = false});
bool platformGivesFeedback();
}
final clipboardProvider = Provider<AppClipboard>(
(ref) => throw UnimplementedError(),
);
/// 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);