127 lines
3.6 KiB
Dart
127 lines
3.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:todoapp/elems/filehandler.dart';
|
|
import 'package:todoapp/pages/Home.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:todoapp/providers/todoprovider.dart';
|
|
import 'package:awesome_notifications/awesome_notifications.dart';
|
|
import 'package:disable_battery_optimization/disable_battery_optimization.dart';
|
|
import 'package:flutter_background_service/flutter_background_service.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'dart:math';
|
|
import 'package:workmanager/workmanager.dart';
|
|
|
|
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 {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
Map content = await FileHandler.fileContent;
|
|
int id = 15;
|
|
for (String key in content.keys) {
|
|
Map list = content[key];
|
|
for (String title in list.keys) {
|
|
Map current = list[title];
|
|
DateTime dueTime = DateTime.parse(current["dueTime"]);
|
|
bool checked = current["checked"];
|
|
DateTime currentTime = DateTime.now();
|
|
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 ${formatMinutes(timeDiff)} !"));
|
|
}
|
|
id++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// @pragma('vm:entry-point')
|
|
// void onStart(ServiceInstance service) {
|
|
// Timer.periodic(const Duration(minutes: 15), (timer) {
|
|
// callbackDispatcher();
|
|
// });
|
|
// }
|
|
|
|
// Future<void> initService() async {
|
|
// final service = FlutterBackgroundService();
|
|
//
|
|
// await service.configure(
|
|
// iosConfiguration: IosConfiguration(),
|
|
// androidConfiguration: AndroidConfiguration(
|
|
// onStart: onStart,
|
|
// isForegroundMode: false,
|
|
// autoStart: true,
|
|
// autoStartOnBoot: true));
|
|
//
|
|
// service.startService();
|
|
// }
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
await AwesomeNotifications().initialize(null, [
|
|
NotificationChannel(
|
|
channelKey: "reminder_channel",
|
|
channelName: "Reminders",
|
|
channelDescription: "Reminder for ToDo",
|
|
importance: NotificationImportance.High,
|
|
)
|
|
]);
|
|
|
|
|
|
Workmanager().initialize(callbackDispatcher);
|
|
Workmanager().registerPeriodicTask("1", "notifyTask", frequency: Duration(minutes: 15));
|
|
|
|
// await initService();
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MultiProvider(
|
|
providers: [ChangeNotifierProvider(create: (context) => TodoProvider())],
|
|
child: MaterialApp(
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(
|
|
seedColor: Colors.blue, brightness: Brightness.dark),
|
|
useMaterial3: true,
|
|
),
|
|
home: const Home(),
|
|
),
|
|
);
|
|
}
|
|
}
|