better background tasks

This commit is contained in:
brausjonas
2024-09-22 15:10:47 +02:00
parent 54eb6814da
commit 7bd8ae8373
4 changed files with 47 additions and 21 deletions
+27 -20
View File
@@ -10,6 +10,7 @@ import 'package:disable_battery_optimization/disable_battery_optimization.dart';
import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'dart:math'; import 'dart:math';
import 'package:workmanager/workmanager.dart';
String formatMinutes(int minutes) { String formatMinutes(int minutes) {
int hours = minutes ~/ 60; int hours = minutes ~/ 60;
@@ -29,6 +30,8 @@ String formatMinutes(int minutes) {
@pragma('vm:entry-point') @pragma('vm:entry-point')
void callbackDispatcher() async { void callbackDispatcher() async {
WidgetsFlutterBinding.ensureInitialized();
Map content = await FileHandler.fileContent; Map content = await FileHandler.fileContent;
int id = 15; int id = 15;
for (String key in content.keys) { for (String key in content.keys) {
@@ -62,26 +65,26 @@ void callbackDispatcher() async {
} }
} }
@pragma('vm:entry-point') // @pragma('vm:entry-point')
void onStart(ServiceInstance service) { // void onStart(ServiceInstance service) {
Timer.periodic(const Duration(minutes: 15), (timer) { // Timer.periodic(const Duration(minutes: 15), (timer) {
callbackDispatcher(); // callbackDispatcher();
}); // });
} // }
Future<void> initService() async { // Future<void> initService() async {
final service = FlutterBackgroundService(); // final service = FlutterBackgroundService();
//
await service.configure( // await service.configure(
iosConfiguration: IosConfiguration(), // iosConfiguration: IosConfiguration(),
androidConfiguration: AndroidConfiguration( // androidConfiguration: AndroidConfiguration(
onStart: onStart, // onStart: onStart,
isForegroundMode: false, // isForegroundMode: false,
autoStart: true, // autoStart: true,
autoStartOnBoot: true)); // autoStartOnBoot: true));
//
service.startService(); // service.startService();
} // }
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@@ -95,7 +98,11 @@ void main() async {
) )
]); ]);
await initService();
Workmanager().initialize(callbackDispatcher);
Workmanager().registerPeriodicTask("1", "notifyTask", frequency: Duration(minutes: 15));
// await initService();
runApp(const MyApp()); runApp(const MyApp());
} }
+2 -1
View File
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:todoapp/modals/addlistmodal.dart'; import 'package:todoapp/modals/addlistmodal.dart';
import 'package:todoapp/providers/todoprovider.dart'; import 'package:todoapp/providers/todoprovider.dart';
import 'package:restart_app/restart_app.dart';
class TodoDrawer extends StatefulWidget { class TodoDrawer extends StatefulWidget {
const TodoDrawer({super.key}); const TodoDrawer({super.key});
@@ -44,7 +45,7 @@ class _TodoDrawerState extends State<TodoDrawer> {
], ],
), ),
) )
) ),
], ],
), ),
), ),
+16
View File
@@ -304,6 +304,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.1.2" version: "6.1.2"
restart_app:
dependency: "direct dev"
description:
name: restart_app
sha256: "00d5ec3e9de871cedbe552fc41e615b042b5ec654385e090e0983f6d02f655ed"
url: "https://pub.dev"
source: hosted
version: "1.3.2"
shared_preferences: shared_preferences:
dependency: "direct dev" dependency: "direct dev"
description: description:
@@ -437,6 +445,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.0" version: "1.0.0"
workmanager:
dependency: "direct dev"
description:
name: workmanager
sha256: ed13530cccd28c5c9959ad42d657cd0666274ca74c56dea0ca183ddd527d3a00
url: "https://pub.dev"
source: hosted
version: "0.5.2"
xdg_directories: xdg_directories:
dependency: transitive dependency: transitive
description: description:
+2
View File
@@ -46,6 +46,8 @@ dev_dependencies:
android_intent_plus: android_intent_plus:
disable_battery_optimization: disable_battery_optimization:
shared_preferences: shared_preferences:
restart_app:
workmanager:
# The "flutter_lints" package below contains a set of recommended lints to # The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is # encourage good coding practices. The lint set provided by the package is