diff --git a/devtools_options.yaml b/devtools_options.yaml new file mode 100644 index 0000000..fa0b357 --- /dev/null +++ b/devtools_options.yaml @@ -0,0 +1,3 @@ +description: This file stores settings for Dart & Flutter DevTools. +documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states +extensions: diff --git a/lib/elems/filehandler.dart b/lib/elems/filehandler.dart index fe25db8..dea41cd 100644 --- a/lib/elems/filehandler.dart +++ b/lib/elems/filehandler.dart @@ -21,6 +21,13 @@ class FileHandler { return js; } + static Future saveNotified(String listName, String todoName, String notifyType, bool value) async { + Map content = await fileContent; + content[listName][todoName][notifyType] = value; + + await saveFile(content); + } + static Future saveFile(js) async { File f = await localFile; String content = json.encode(js); diff --git a/lib/elems/todo.dart b/lib/elems/todo.dart index b2dc9a5..bc62884 100644 --- a/lib/elems/todo.dart +++ b/lib/elems/todo.dart @@ -1,10 +1,12 @@ import 'package:flutter/material.dart'; class Todo { - Todo({required this.title, required this.widget, required this.checked, required this.dueTime}); + Todo({required this.title, required this.widget, required this.checked, required this.dueTime, required this.notifiedDay, required this.notifiedHour}); String title; Widget widget; DateTime dueTime; bool checked; + bool notifiedHour; + bool notifiedDay; } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 4034650..d600f77 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -11,9 +11,24 @@ import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:math'; +String formatMinutes(int minutes) { + int hours = minutes ~/ 60; + int remainingMinutes = minutes % 60; + + String hoursString = ""; + String minutesString = ""; + + if (hours > 0) { + hoursString = "$hours h"; + } + + minutesString = "$remainingMinutes min"; + + return "$hoursString $minutesString"; +} + @pragma('vm:entry-point') void callbackDispatcher() async { - Map content = await FileHandler.fileContent; int id = 15; for (String key in content.keys) { @@ -23,16 +38,24 @@ void callbackDispatcher() async { DateTime dueTime = DateTime.parse(current["dueTime"]); bool checked = current["checked"]; DateTime currentTime = DateTime.now(); - int timeDiff = dueTime - .difference(currentTime) - .inMinutes; - if (timeDiff <= 45 && timeDiff >= 0 && !checked) { + bool notifiedDay = current["notifiedDay"]; + bool notifiedHour = current["notifiedHour"]; + int timeDiff = dueTime.difference(currentTime).inMinutes; + if ((timeDiff <= 90 && !notifiedHour || + timeDiff > 23*60 && timeDiff <= 25 * 60 && !notifiedDay) && + timeDiff >= 0 && + !checked) { + String notifyType = timeDiff <= 90 ? "notifiedHour" : "notifiedDay"; + + await FileHandler.saveNotified(key, title, notifyType, true); + AwesomeNotifications().createNotification( content: NotificationContent( id: id, channelKey: "reminder_channel", title: "Task due!", - body: "Task $title due in $timeDiff minutes!")); + body: + "Task $title due in ${formatMinutes(timeDiff)} !")); } id++; } @@ -55,9 +78,7 @@ Future initService() async { onStart: onStart, isForegroundMode: false, autoStart: true, - autoStartOnBoot: true - ) - ); + autoStartOnBoot: true)); service.startService(); } diff --git a/lib/modules/todowidget.dart b/lib/modules/todowidget.dart index e3147a2..fdbd620 100644 --- a/lib/modules/todowidget.dart +++ b/lib/modules/todowidget.dart @@ -30,8 +30,11 @@ class _TodoWidgetState extends State { DateTime today = DateTime.now(); DateTime due = widget.dueTime; - DateTime nowDay = DateTime(2000, 1, today.day); - DateTime dueDay = DateTime(2000, 1, due.day); + print(today); + print(due); + + DateTime nowDay = DateTime(today.year, today.month, today.day); + DateTime dueDay = DateTime(due.year, due.month, due.day); int diff = dueDay.difference(nowDay).inDays; Map monthList = { @@ -49,6 +52,8 @@ class _TodoWidgetState extends State { 12: "Dez" }; + print(diff); + if (diff < 0) { return [ "Over", diff --git a/lib/providers/todoprovider.dart b/lib/providers/todoprovider.dart index 39154c0..fa759da 100644 --- a/lib/providers/todoprovider.dart +++ b/lib/providers/todoprovider.dart @@ -24,11 +24,16 @@ class TodoProvider extends ChangeNotifier { String title = key2; bool checked = decode[key2]["checked"]; DateTime dueTime = DateTime.parse(decode[key2]["dueTime"]); + bool notifiedDay = decode[key2]["notifiedDay"] ?? false; + bool notifiedHour = decode[key2]["notifiedHour"] ?? false; todoLists[key]!.todos.add(Todo( - title: title, - widget: buildTodoWidget(title, checked, dueTime), - checked: checked, - dueTime: dueTime)); + title: title, + widget: buildTodoWidget(title, checked, dueTime), + checked: checked, + dueTime: dueTime, + notifiedDay: notifiedDay, + notifiedHour: notifiedHour, + )); } } @@ -91,7 +96,9 @@ class TodoProvider extends ChangeNotifier { for (Todo t in todos) { js["${t.title}"] = { "checked": t.checked, - "dueTime": t.dueTime.toString() + "dueTime": t.dueTime.toString(), + "notifiedDay": t.notifiedDay, + "notifiedHour": t.notifiedHour }; } await FileHandler.saveList(js, selectedList); @@ -99,7 +106,12 @@ class TodoProvider extends ChangeNotifier { void addTodo(String title, DateTime dueTime) async { todos.add(Todo( - title: title, checked: false, widget: buildTodoWidget(title, false, dueTime), dueTime: dueTime)); + title: title, + checked: false, + widget: buildTodoWidget(title, false, dueTime), + dueTime: dueTime, + notifiedHour: false, + notifiedDay: false)); await writeTodoChanges(); notifyListeners(); @@ -117,6 +129,8 @@ class TodoProvider extends ChangeNotifier { Todo todo = todoByTitle(oldTitle); todo.title = newTitle; todo.dueTime = dueTime; + todo.notifiedDay = false; + todo.notifiedHour = false; todo.widget = buildTodoWidget(newTitle, todo.checked, dueTime); @@ -152,6 +166,9 @@ class TodoProvider extends ChangeNotifier { title: "None", widget: const SizedBox(), checked: false, - dueTime: DateTime.now()); + dueTime: DateTime.now(), + notifiedDay: false, + notifiedHour: false, + ); } }