99 lines
2.8 KiB
Dart
99 lines
2.8 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';
|
|
|
|
@pragma('vm:entry-point')
|
|
void callbackDispatcher() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
SharedPreferences.getInstance();
|
|
print("test1");
|
|
Map content = await FileHandler.fileContent;
|
|
for (String key in content.keys) {
|
|
Map list = content[key];
|
|
for (String title in list.keys) {
|
|
print("test2");
|
|
Map current = list[title];
|
|
DateTime dueTime = DateTime.parse(current["dueTime"]);
|
|
bool checked = current["checked"];
|
|
DateTime currentTime = DateTime.now();
|
|
int timeDiff = dueTime
|
|
.difference(currentTime)
|
|
.inMinutes;
|
|
if (timeDiff <= 30 && timeDiff >= 0 && !checked) {
|
|
AwesomeNotifications().createNotification(
|
|
content: NotificationContent(
|
|
id: 10,
|
|
channelKey: "reminder_channel",
|
|
title: "Task due!",
|
|
body: "Task $title due in $timeDiff minutes!"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@pragma('vm:entry-point')
|
|
void onStart(ServiceInstance service) {
|
|
Timer.periodic(const Duration(seconds: 10), (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,
|
|
)
|
|
]);
|
|
|
|
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(),
|
|
),
|
|
);
|
|
}
|
|
}
|