diff --git a/lib/notification.dart b/lib/notification.dart index 35794ca..9b3cd72 100644 --- a/lib/notification.dart +++ b/lib/notification.dart @@ -1,4 +1,7 @@ +// ignore_for_file: avoid_print + import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:timezone/data/latest.dart'; import 'package:timezone/timezone.dart'; @@ -50,6 +53,7 @@ void scheduleNotification() async { if (times.isEmpty) return; + final SharedPreferences prefs = await SharedPreferences.getInstance(); var currentTime = TZDateTime.now(local); // Map the time entries to TZDateTime objects @@ -73,16 +77,26 @@ void scheduleNotification() async { // Sort the array so we can find the closest time notificationTimes.sort((a, b) => a.compareTo(b)); + var notificationTime = notificationTimes[0]; - print("Scheduling notification for ${notificationTimes[0].toString()}"); + if (prefs.getString('scheduled_notification') == + notificationTime.toIso8601String()) { + print("Notification already scheduled for " + "${notificationTime.toIso8601String()}, not re-scheduling"); + return; + } + + print("Scheduling notification for ${notificationTime.toIso8601String()}"); await FlutterLocalNotificationsPlugin().zonedSchedule( 1, null, null, - notificationTimes[0], + notificationTime, details, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime, ); + await prefs.setString( + 'scheduled_notification', notificationTime.toIso8601String()); } diff --git a/lib/tasks.dart b/lib/tasks.dart index 9830630..8efe9c4 100644 --- a/lib/tasks.dart +++ b/lib/tasks.dart @@ -31,16 +31,8 @@ void setupBackgroundTasks() async { const InitializationSettings( android: AndroidInitializationSettings('estrogen'), ), - // onSelectNotification: handleNotificationClick, ); - var launchDetails = - await FlutterLocalNotificationsPlugin().getNotificationAppLaunchDetails(); - - // if (launchDetails?.payload != null && launchDetails!.payload!.isNotEmpty) { - // handleNotificationClick(launchDetails.payload); - // } - // Runs every 15 minutes await Workmanager().registerPeriodicTask( 'schedule_notifications', diff --git a/pubspec.lock b/pubspec.lock index ce36d80..35a2af1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -89,6 +89,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + file: + dependency: transitive + description: + name: file + sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" + url: "https://pub.dev" + source: hosted + version: "6.1.4" flutter: dependency: "direct main" description: flutter @@ -131,6 +139,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" lints: dependency: transitive description: @@ -171,6 +184,30 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "94b1e0dd80970c1ce43d5d4e050a9918fce4f4a775e6142424c30a29a363265c" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170" + url: "https://pub.dev" + source: hosted + version: "2.2.1" permission_handler: dependency: "direct main" description: @@ -219,6 +256,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.0.1" + platform: + dependency: transitive + description: + name: platform + sha256: ae68c7bfcd7383af3629daafb32fb4e8681c7154428da4febcff06200585f102 + url: "https://pub.dev" + source: hosted + version: "3.1.2" plugin_platform_interface: dependency: transitive description: @@ -227,6 +272,62 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.6" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: b7f41bad7e521d205998772545de63ff4e6c97714775902c199353f8bf1511ac + url: "https://pub.dev" + source: hosted + version: "2.2.1" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06" + url: "https://pub.dev" + source: hosted + version: "2.2.1" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "7bf53a9f2d007329ee6f3df7268fd498f8373602f943c975598bbb34649b62a7" + url: "https://pub.dev" + source: hosted + version: "2.3.4" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: c2eb5bf57a2fe9ad6988121609e47d3e07bb3bdca5b6f8444e4cf302428a128a + url: "https://pub.dev" + source: hosted + version: "2.3.1" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: d4ec5fc9ebb2f2e056c617112aa75dcf92fc2e4faaf2ae999caa297473f75d8a + url: "https://pub.dev" + source: hosted + version: "2.3.1" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: d762709c2bbe80626ecc819143013cc820fa49ca5e363620ee20a8b15a3e3daf + url: "https://pub.dev" + source: hosted + version: "2.2.1" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: f763a101313bd3be87edffe0560037500967de9c394a714cd598d945517f694f + url: "https://pub.dev" + source: hosted + version: "2.3.1" sky_engine: dependency: transitive description: flutter @@ -304,6 +405,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.1.4-beta" + win32: + dependency: transitive + description: + name: win32 + sha256: "350a11abd2d1d97e0cc7a28a81b781c08002aa2864d9e3f192ca0ffa18b06ed3" + url: "https://pub.dev" + source: hosted + version: "5.0.9" workmanager: dependency: "direct main" description: @@ -330,4 +439,4 @@ packages: version: "6.4.2" sdks: dart: ">=3.2.0-134.1.beta <4.0.0" - flutter: ">=3.4.0-17.0.pre" + flutter: ">=3.7.0" diff --git a/pubspec.yaml b/pubspec.yaml index 2cca27f..f632d61 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: permission_handler: ^11.0.1 workmanager: ^0.5.2 timezone: ^0.9.2 + shared_preferences: ^2.2.1 dev_dependencies: flutter_test: