Some Improvements
This commit is contained in:
@@ -21,6 +21,13 @@ class FileHandler {
|
||||
return js;
|
||||
}
|
||||
|
||||
static Future<void> saveNotified(String listName, String todoName, String notifyType, bool value) async {
|
||||
Map content = await fileContent;
|
||||
content[listName][todoName][notifyType] = value;
|
||||
|
||||
await saveFile(content);
|
||||
}
|
||||
|
||||
static Future<void> saveFile(js) async {
|
||||
File f = await localFile;
|
||||
String content = json.encode(js);
|
||||
|
||||
+3
-1
@@ -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;
|
||||
}
|
||||
+30
-9
@@ -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<void> initService() async {
|
||||
onStart: onStart,
|
||||
isForegroundMode: false,
|
||||
autoStart: true,
|
||||
autoStartOnBoot: true
|
||||
)
|
||||
);
|
||||
autoStartOnBoot: true));
|
||||
|
||||
service.startService();
|
||||
}
|
||||
|
||||
@@ -30,8 +30,11 @@ class _TodoWidgetState extends State<TodoWidget> {
|
||||
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<int, String> monthList = {
|
||||
@@ -49,6 +52,8 @@ class _TodoWidgetState extends State<TodoWidget> {
|
||||
12: "Dez"
|
||||
};
|
||||
|
||||
print(diff);
|
||||
|
||||
if (diff < 0) {
|
||||
return [
|
||||
"Over",
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user