From 294f5959149546e5d099183ae09a5b17e825ce49 Mon Sep 17 00:00:00 2001 From: Erick Date: Tue, 24 Sep 2024 11:00:55 +0300 Subject: [PATCH] feat: todo notifications that now work for real # Upcoming Implement a way to ensure that todos will happen at 8AM on a daily basis --- lib/main.dart | 7 +-- lib/tools/todo/models/models.dart | 1 + .../services/todo_background_service.dart | 33 ++++++++++++ lib/tools/todo/todo_view_page.dart | 4 +- lib/workers/background_worker.dart | 51 ++++++++++++++----- lib/workers/workers.dart | 2 +- 6 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 lib/tools/todo/models/services/todo_background_service.dart diff --git a/lib/main.dart b/lib/main.dart index 6277d36..d9cafed 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,14 +4,9 @@ import 'package:get/get.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); - - await Workmanager().initialize( - callbackDispatcher, - isInDebugMode: true, - ); - await LocalNotifierService().initialize(); await LocalNotificationStatusManager().initialize(); + await BackgroundWorker().initialize(); runApp( GetMaterialApp( diff --git a/lib/tools/todo/models/models.dart b/lib/tools/todo/models/models.dart index 8b773ec..4c91d40 100644 --- a/lib/tools/todo/models/models.dart +++ b/lib/tools/todo/models/models.dart @@ -1,2 +1,3 @@ export 'todo_model.dart'; export 'todo_model_helper.dart'; +export 'services/todo_background_service.dart'; diff --git a/lib/tools/todo/models/services/todo_background_service.dart b/lib/tools/todo/models/services/todo_background_service.dart new file mode 100644 index 0000000..e7aaf83 --- /dev/null +++ b/lib/tools/todo/models/services/todo_background_service.dart @@ -0,0 +1,33 @@ +import 'package:academia/exports/barrel.dart'; +import 'package:academia/notifier/local_notification_channel.dart'; +import 'package:academia/notifier/local_notification_status_manager.dart'; +import 'package:academia/notifier/local_notifier_service.dart'; + +class TodoBackgroundService { + static final TodoBackgroundService _instance = + TodoBackgroundService._internal(); + + factory TodoBackgroundService() { + return _instance; + } + + /// Private named constructor that prevents external instantiation. + TodoBackgroundService._internal(); + + void notifyPendingTasks() { + TodoModelHelper().queryAll().then((value) { + value.map((e) { + final todo = Todo.fromJson(e); + + if ((!todo.complete) && todo.notify == true) { + LocalNotifierService().showNotification( + id: LocalNotificationStatusManager().getNextId(), + title: "Todos ${Emojis.smile_skull}", + body: "Todo ${todo.name} requires your attention", + channelKey: LocalNotificationChannelType.reminders.channelKey, + ); + } + }); + }); + } +} diff --git a/lib/tools/todo/todo_view_page.dart b/lib/tools/todo/todo_view_page.dart index dd0987e..0c37cb8 100644 --- a/lib/tools/todo/todo_view_page.dart +++ b/lib/tools/todo/todo_view_page.dart @@ -270,7 +270,7 @@ class _TodoViewPageState extends State { id: LocalNotificationStatusManager().getNextId(), title: "Todos", body: - "Todo ${titleController.text} has been scheduled successfully", + "Todo ${titleController.text} has been scheduled successfully ${Emojis.smile_clown_face}", channelKey: LocalNotificationChannelType.reminders.channelKey, ); @@ -314,7 +314,7 @@ class _TodoViewPageState extends State { id: LocalNotificationStatusManager().getNextId(), title: "Todos", body: - "Todo ${titleController.text} has been scheduled successfully", + "Todo ${titleController.text} has been scheduled successfully ${Emojis.smile_clown_face}", channelKey: LocalNotificationChannelType.reminders.channelKey, ); diff --git a/lib/workers/background_worker.dart b/lib/workers/background_worker.dart index de82c81..5093f78 100644 --- a/lib/workers/background_worker.dart +++ b/lib/workers/background_worker.dart @@ -1,25 +1,52 @@ import 'package:academia/exports/barrel.dart'; +import 'package:academia/notifier/local_notification_channel.dart'; +import 'package:academia/notifier/local_notification_status_manager.dart'; +import 'package:academia/notifier/local_notifier_service.dart'; // background services @pragma('vm:entry-point') void callbackDispatcher() { Workmanager().executeTask((task, inputData) { switch (task) { - case BackgroundConfig.refresh: - debugPrint("hey"); + case BackgroundConfig.todosIDentifier: + TodoBackgroundService().notifyPendingTasks(); break; default: } - // notificationsController.createInstantNotification( - // "Hello there", - // "Hello again son", - // ); - // notificationsController.scheduleNotification( - // DateTime.now().add(Duration(minutes: 1)), - // "Test", - // "This is a mF big text notification", - // notificationLayout: NotificationLayout.BigText, - // ); + LocalNotifierService().showNotification( + id: LocalNotificationStatusManager().getNextId(), + title: "Dick", + body: "How are you doing pussy", + channelKey: LocalNotificationChannelType.general.channelKey, + ); return Future.value(true); }); } + +class BackgroundWorker { + static final BackgroundWorker _instance = BackgroundWorker._internal(); + + factory BackgroundWorker() { + return _instance; + } + + /// Private named constructor that prevents external instantiation. + BackgroundWorker._internal(); + + Future initialize() async { + if (Platform.isAndroid || Platform.isIOS) { + await Workmanager().initialize( + callbackDispatcher, + isInDebugMode: true, + ); + + /// Todos task that is suppossed to run after every 24 hours at 8 am + Workmanager().registerPeriodicTask( + BackgroundConfig.todosIDentifier, + BackgroundConfig.todosIDentifier, + initialDelay: const Duration(seconds: 10), + frequency: const Duration(minutes: 15), + ); + } + } +} diff --git a/lib/workers/workers.dart b/lib/workers/workers.dart index 37435b6..c5cf1cf 100644 --- a/lib/workers/workers.dart +++ b/lib/workers/workers.dart @@ -3,5 +3,5 @@ export 'background_worker.dart'; // Tasks Define class BackgroundConfig { - static const String refresh = "com.dita.academia.task.test"; + static const String todosIDentifier = "com.dita.academia.todos"; }