Skip to content

Commit

Permalink
feat: todo notifications that now work for real
Browse files Browse the repository at this point in the history
# Upcoming
Implement a way to ensure that todos will happen at 8AM on a daily basis
  • Loading branch information
IamMuuo committed Sep 24, 2024
1 parent b114a87 commit 294f595
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 21 deletions.
7 changes: 1 addition & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions lib/tools/todo/models/models.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'todo_model.dart';
export 'todo_model_helper.dart';
export 'services/todo_background_service.dart';
33 changes: 33 additions & 0 deletions lib/tools/todo/models/services/todo_background_service.dart
Original file line number Diff line number Diff line change
@@ -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,
);
}
});
});
}
}
4 changes: 2 additions & 2 deletions lib/tools/todo/todo_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class _TodoViewPageState extends State<TodoViewPage> {
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,
);
Expand Down Expand Up @@ -314,7 +314,7 @@ class _TodoViewPageState extends State<TodoViewPage> {
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,
);
Expand Down
51 changes: 39 additions & 12 deletions lib/workers/background_worker.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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),
);
}
}
}
2 changes: 1 addition & 1 deletion lib/workers/workers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit 294f595

Please sign in to comment.