Skip to content

Commit

Permalink
implement popup notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloose committed Oct 5, 2024
1 parent dc2c670 commit 7c4e4ff
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
56 changes: 56 additions & 0 deletions lib/features/app_updates/wrappers/popup_notifications_wrapper.dart
Original file line number Diff line number Diff line change
@@ -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<ConsumerStatefulWidget> createState() {
return PopupNotificationsWrapperState();
}
}

class PopupNotificationsWrapperState
extends ConsumerState<PopupNotificationsWrapper> {
@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;
}
}
2 changes: 1 addition & 1 deletion lib/features/core/alerts/simple_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NotificationsStateNotifier
extends StateNotifier<AsyncValue<List<NotificationAd>>> {
Ref ref;
bool firstTime = true;
FutureOr<List<NotificationAd>>? _initialFuture;

NotificationsStateNotifier(this.ref) : super(const AsyncValue.loading()) {
_fetchNotifications();
Expand All @@ -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);
Expand All @@ -63,6 +65,10 @@ class NotificationsStateNotifier
}
}

Future<List<NotificationAd>> getFuture() async {
return _initialFuture ??= getNotifications();
}

Set<int> getSawIds() {
final seen = localStorage.getItem(kSeenNotificationsStorageKey) ?? '';
return seen
Expand Down
7 changes: 5 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(),
),
),
),
),
Expand Down

0 comments on commit 7c4e4ff

Please sign in to comment.