From 0f97dddcd8603bf4ace7d3e5ad4b7ec48ca7ee1d Mon Sep 17 00:00:00 2001 From: hihihaha <71922530+Monke14@users.noreply.github.com> Date: Sat, 2 Mar 2024 19:11:43 +0100 Subject: [PATCH] add logic to avoid notification spam --- .../settings/notifications_screen.dart | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/refilc_mobile_ui/lib/screens/settings/notifications_screen.dart b/refilc_mobile_ui/lib/screens/settings/notifications_screen.dart index 6066691..c79e85e 100644 --- a/refilc_mobile_ui/lib/screens/settings/notifications_screen.dart +++ b/refilc_mobile_ui/lib/screens/settings/notifications_screen.dart @@ -1,4 +1,8 @@ +import 'package:refilc/api/providers/database_provider.dart'; +import 'package:refilc/api/providers/user_provider.dart'; +import 'package:refilc/helpers/notification_helper.dart'; import 'package:refilc/models/settings.dart'; +import 'package:refilc/models/user.dart'; import 'package:refilc/theme/colors/colors.dart'; import 'package:refilc_mobile_ui/common/beta_chip.dart'; import 'package:refilc_mobile_ui/common/panel/panel.dart'; @@ -53,6 +57,17 @@ class MenuNotifications extends StatelessWidget { class NotificationsScreen extends StatelessWidget { const NotificationsScreen({super.key}); + void setAllAsSeen(BuildContext context) { + // Set all notification categories as seen to avoid spamming the user with notifications when they turn on notifications + DatabaseProvider database = Provider.of(context); + User? user = Provider.of(context).user; + if(user != null) { + for(LastSeenCategory category in LastSeenCategory.values) { + database.userStore.storeLastSeen(DateTime.now(), userId: user.id, category: category); + } + } + } + @override Widget build(BuildContext context) { SettingsProvider settings = Provider.of(context); @@ -78,8 +93,10 @@ class NotificationsScreen extends StatelessWidget { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12.0)), value: settings.notificationsGradesEnabled, - onChanged: (v) => - settings.update(notificationsGradesEnabled: v), + onChanged: (v) { + settings.update(notificationsGradesEnabled: v); + setAllAsSeen(context); + }, title: Row( children: [ Icon( @@ -113,8 +130,10 @@ class NotificationsScreen extends StatelessWidget { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12.0)), value: settings.notificationsAbsencesEnabled, - onChanged: (v) => - settings.update(notificationsAbsencesEnabled: v), + onChanged: (v) { + settings.update(notificationsAbsencesEnabled: v); + setAllAsSeen(context); + }, title: Row( children: [ Icon( @@ -148,8 +167,10 @@ class NotificationsScreen extends StatelessWidget { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12.0)), value: settings.notificationsMessagesEnabled, - onChanged: (v) => - settings.update(notificationsMessagesEnabled: v), + onChanged: (v) { + settings.update(notificationsMessagesEnabled: v); + setAllAsSeen(context); + }, title: Row( children: [ Icon( @@ -183,8 +204,10 @@ class NotificationsScreen extends StatelessWidget { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12.0)), value: settings.notificationsLessonsEnabled, - onChanged: (v) => - settings.update(notificationsLessonsEnabled: v), + onChanged: (v) { + settings.update(notificationsLessonsEnabled: v); + setAllAsSeen(context); + }, title: Row( children: [ Icon(