Skip to content

Commit

Permalink
fix: todos notifying anoyingly after enabling notifications + updates
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Sep 24, 2024
1 parent 6937abd commit b114a87
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import 'package:get/get.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();

// await Workmanager().initialize(
// callbackDispatcher,
// isInDebugMode: true,
// );
await Workmanager().initialize(
callbackDispatcher,
isInDebugMode: true,
);

await LocalNotifierService().initialize();
await LocalNotificationStatusManager().initialize();
Expand Down
1 change: 1 addition & 0 deletions lib/storage/schemas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const schemas = <String, String>{
name TEXT NOT NULL,
sub_tasks INTEGER NOT NULL,
complete INTEGER NOT NULL,
notify INTEGER NOT NULL,
description TEXT NOT NULL,
due TEXT NOT NULL,
dateAdded TEXT NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions lib/tools/todo/controllers/todo_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TodoController extends GetxController {

Future<bool> addTask(Todo t) async {
int value = await TodoModelHelper().create(t.toJson());
t.id = value;
allTodos.add(t);
return value == 0 ? false : true;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/tools/todo/models/todo_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Todo {
int? id;
String name;
bool complete;
bool notify;
String description;
Map<String, bool>? subTasks;
DateTime due;
Expand All @@ -19,6 +20,7 @@ class Todo {
required this.due,
required this.dateAdded,
this.dateCompleted,
this.notify = false,
});
// Method to convert Todo object to JSON Map
Map<String, dynamic> toJson() {
Expand All @@ -27,6 +29,7 @@ class Todo {
'name': name,
'sub_tasks': json.encode(subTasks),
'complete': complete ? 1 : 0,
'notify': notify ? 1 : 0,
'description': description,
'due': due.toIso8601String(),
'dateAdded': dateAdded.toIso8601String(),
Expand All @@ -41,6 +44,7 @@ class Todo {
name: data['name'],
subTasks: json.decode(data["sub_tasks"]).cast<String, bool>(),
complete: data['complete'] == 1 ? true : false,
notify: data['notify'] == 1 ? true : false,
description: data['description'],
due: DateTime.parse(data['due']),
dateAdded: DateTime.parse(data['dateAdded']),
Expand Down
35 changes: 24 additions & 11 deletions lib/tools/todo/todo_view_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/notifier/local_notification_channel.dart';
import 'package:academia/notifier/notifier.dart';
import 'package:get/get.dart';

class TodoViewPage extends StatefulWidget {
Expand Down Expand Up @@ -48,6 +50,7 @@ class _TodoViewPageState extends State<TodoViewPage> {
descriptionController.text = widget.todo!.description;
duedateController.text = formatDateTime(widget.todo!.due);
dueDate = widget.todo!.due;
notify = widget.todo!.notify;
subtasks.addAll(widget.todo!.subTasks ?? {});
}
setState(() {});
Expand Down Expand Up @@ -255,24 +258,21 @@ class _TodoViewPageState extends State<TodoViewPage> {
description: descriptionController.text,
dateAdded: DateTime.now(),
due: dueDate,
notify: notify,
complete: false),
);

if (ok) {
if (!context.mounted) return;
if (notify) {
// notification logic
notificationsController.scheduleNotification(
DateTime.now().copyWith(
hour: 8,
month: null,
day: null,
),
"Todos",
"Todo $todoController needs your attention",
repeats: true,
channelKey: "reminders",
notificationLayout: NotificationLayout.Inbox,
LocalNotifierService().showNotification(
id: LocalNotificationStatusManager().getNextId(),
title: "Todos",
body:
"Todo ${titleController.text} has been scheduled successfully",
channelKey:
LocalNotificationChannelType.reminders.channelKey,
);
}
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
Expand Down Expand Up @@ -303,10 +303,23 @@ class _TodoViewPageState extends State<TodoViewPage> {
widget.todo?.description = descriptionController.text;
widget.todo?.subTasks = subtasks;
widget.todo?.due = dueDate;
widget.todo?.notify = notify;

final ok = await todoController.updateTodo(widget.todo!);
if (ok) {
if (!context.mounted) return;
if (notify) {
// notification logic
LocalNotifierService().showNotification(
id: LocalNotificationStatusManager().getNextId(),
title: "Todos",
body:
"Todo ${titleController.text} has been scheduled successfully",
channelKey:
LocalNotificationChannelType.reminders.channelKey,
);
}

ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text(
"Your task has been sucessfully updated",
Expand Down

0 comments on commit b114a87

Please sign in to comment.