From 7c4e4ff2db36370a186f8eba019ef352a0d1cf62 Mon Sep 17 00:00:00 2001 From: Paolo Flores Date: Sat, 5 Oct 2024 14:10:38 -0500 Subject: [PATCH] implement popup notifications --- .../wrappers/popup_notifications_wrapper.dart | 56 +++++++++++++++++++ lib/features/core/alerts/simple_dialog.dart | 2 +- .../provider/notifications_provider.dart | 8 ++- lib/main.dart | 7 ++- 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 lib/features/app_updates/wrappers/popup_notifications_wrapper.dart diff --git a/lib/features/app_updates/wrappers/popup_notifications_wrapper.dart b/lib/features/app_updates/wrappers/popup_notifications_wrapper.dart new file mode 100644 index 0000000..85e9a75 --- /dev/null +++ b/lib/features/app_updates/wrappers/popup_notifications_wrapper.dart @@ -0,0 +1,56 @@ +import 'package:burrito/features/core/alerts/simple_dialog.dart'; +import 'package:burrito/features/notifications/provider/notifications_provider.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +class PopupNotificationsWrapper extends ConsumerStatefulWidget { + final Widget child; + + const PopupNotificationsWrapper({super.key, required this.child}); + + @override + ConsumerState createState() { + return PopupNotificationsWrapperState(); + } +} + +class PopupNotificationsWrapperState + extends ConsumerState { + @override + void initState() { + super.initState(); + + final notifications = ref.read(notificationsProvider.notifier).getFuture(); + + notifications.then((notifications) async { + final popups = notifications.where((n) => n.isPopup).toList(); + if (popups.isEmpty) return; + + if (!mounted) return; + + for (final popup in popups) { + final content = popup.content; + if (popup.title == null || popup.content == null) return; + + await showDialog( + context: context, + barrierDismissible: true, + builder: (context) { + return SimpleAppDialog( + title: popup.title!, + content: content!, + showAcceptButton: true, + ); + }, + ); + } + }).catchError((e, st) { + debugPrint('Error getting popup notifications: $e\n$st'); + }); + } + + @override + Widget build(BuildContext context) { + return widget.child; + } +} diff --git a/lib/features/core/alerts/simple_dialog.dart b/lib/features/core/alerts/simple_dialog.dart index c49cde7..cc7f6a7 100644 --- a/lib/features/core/alerts/simple_dialog.dart +++ b/lib/features/core/alerts/simple_dialog.dart @@ -27,7 +27,7 @@ class SimpleAppDialog extends ConsumerWidget { return ConstrainedBox( constraints: BoxConstraints( minHeight: constraints.maxHeight * 0.20, - maxHeight: 200, + maxHeight: 230, minWidth: double.infinity, ), child: Column( diff --git a/lib/features/notifications/provider/notifications_provider.dart b/lib/features/notifications/provider/notifications_provider.dart index 19278a1..9a2ce37 100644 --- a/lib/features/notifications/provider/notifications_provider.dart +++ b/lib/features/notifications/provider/notifications_provider.dart @@ -21,6 +21,7 @@ class NotificationsStateNotifier extends StateNotifier>> { Ref ref; bool firstTime = true; + FutureOr>? _initialFuture; NotificationsStateNotifier(this.ref) : super(const AsyncValue.loading()) { _fetchNotifications(); @@ -36,7 +37,8 @@ class NotificationsStateNotifier void _fetchNotifications([_]) async { try { - final notifications = await getNotifications(); + _initialFuture = getNotifications(); + final notifications = await _initialFuture!; if (firstTime) { if (notifications.isEmpty) { closeModalBottomSheet(ref); @@ -63,6 +65,10 @@ class NotificationsStateNotifier } } + Future> getFuture() async { + return _initialFuture ??= getNotifications(); + } + Set getSawIds() { final seen = localStorage.getItem(kSeenNotificationsStorageKey) ?? ''; return seen diff --git a/lib/main.dart b/lib/main.dart index 7a53471..e9b7f22 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:burrito/features/app_updates/wrappers/popup_notifications_wrapper.dart'; import 'package:universal_io/io.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -52,8 +53,10 @@ void main() async { theme: BurritoMobileTheme.theme, debugShowCheckedModeBanner: false, home: const SafeArea( - child: PendingUpdatesWrapper( - child: BurritoApp(), + child: PopupNotificationsWrapper( + child: PendingUpdatesWrapper( + child: BurritoApp(), + ), ), ), ),