Simplify the provider used to get WithContext.

This commit is contained in:
Dain Nilsson 2022-04-28 15:23:04 +02:00
parent 13ad504808
commit 9850b4ba29
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
4 changed files with 18 additions and 16 deletions

View File

@ -58,8 +58,7 @@ Future<Widget> initialize() async {
ref.read(androidSubPageProvider);
/// initializes global handler for dialogs
FDialogApi.setup(
FDialogApiImpl(ref.watch(contextProvider.notifier).withContext));
FDialogApi.setup(FDialogApiImpl(ref.watch(withContextProvider)));
return const MainPage();
},
)),

View File

@ -112,19 +112,12 @@ final qrScannerProvider = Provider<QrScanner?>(
(ref) => null,
);
final contextProvider =
StateNotifierProvider<ContextProvider, Function(BuildContext)?>(
(ref) => ContextProvider());
final contextConsumer =
StateNotifierProvider<ContextConsumer, Function(BuildContext)?>(
(ref) => ContextConsumer());
/// Signature for the callback to [ContextProvider.withContext].
///
/// The callback will be invoked with a [BuildContext] that can be used to open
/// dialogs, show Snackbars, etc.
typedef WithContext = Future<T> Function<T>(
Future<T> Function(BuildContext context) action);
class ContextProvider extends StateNotifier<Function(BuildContext)?> {
ContextProvider() : super(null);
class ContextConsumer extends StateNotifier<Function(BuildContext)?> {
ContextConsumer() : super(null);
Future<T> withContext<T>(Future<T> Function(BuildContext context) action) {
final completer = Completer<T>();
@ -138,3 +131,13 @@ class ContextProvider extends StateNotifier<Function(BuildContext)?> {
return completer.future;
}
}
/// 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);

View File

@ -15,7 +15,7 @@ class MainPage extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
ref.listen<Function(BuildContext)?>(
contextProvider,
contextConsumer,
(previous, next) {
next?.call(context);
},

View File

@ -182,7 +182,7 @@ final desktopOathCredentialListProvider = StateNotifierProvider.autoDispose
.family<OathCredentialListNotifier, List<OathPair>?, DevicePath>(
(ref, devicePath) {
var notifier = _DesktopCredentialListNotifier(
ref.watch(contextProvider.notifier).withContext,
ref.watch(withContextProvider),
ref.watch(_sessionProvider(devicePath)),
ref.watch(oathStateProvider(devicePath)
.select((r) => r.whenOrNull(data: (state) => state.locked) ?? true)),