Feat: Erstellt die Möglichkeit, Tasks als Hintergrundprozesse auszuführen
This commit is contained in:
59
lib/Controller/background_task_controller.dart
Normal file
59
lib/Controller/background_task_controller.dart
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:isolate';
|
||||||
|
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:workmanager/workmanager.dart';
|
||||||
|
|
||||||
|
import '../Tasks/generate_transactions_task.dart';
|
||||||
|
import 'port_controller.dart';
|
||||||
|
|
||||||
|
/// Erstellt Hintergrundtasks und führt diese aus
|
||||||
|
class BackgroundTaskController {
|
||||||
|
/// Erstellt eine neue Instanz dieser Klasse
|
||||||
|
BackgroundTaskController() {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
unawaited(Workmanager().initialize(callbackDispatcher));
|
||||||
|
unawaited(
|
||||||
|
Workmanager().registerPeriodicTask(
|
||||||
|
'generate-transactions',
|
||||||
|
'generate_transactions',
|
||||||
|
frequency: const Duration(minutes: 30),
|
||||||
|
initialDelay: const Duration(minutes: 1),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
unawaited(
|
||||||
|
Isolate.run(() async {
|
||||||
|
sleep(const Duration(minutes: 1));
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
final RootIsolateToken? rootIsolateToken = await PortController()
|
||||||
|
.getRootIsolateToken();
|
||||||
|
|
||||||
|
if (rootIsolateToken != null) {
|
||||||
|
BackgroundIsolateBinaryMessenger.ensureInitialized(
|
||||||
|
rootIsolateToken,
|
||||||
|
);
|
||||||
|
await GenerateTransactionsTask().execute();
|
||||||
|
}
|
||||||
|
sleep(const Duration(minutes: 30));
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Die Funktion wird von Hintergrundtasks ausgerufen, um diese auszuführen
|
||||||
|
@pragma('vm:entry-point')
|
||||||
|
void callbackDispatcher() {
|
||||||
|
Workmanager().executeTask((final task, final inputData) {
|
||||||
|
switch (task) {
|
||||||
|
case 'generate_transactions':
|
||||||
|
return GenerateTransactionsTask().execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Future.value(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
68
lib/Controller/port_controller.dart
Normal file
68
lib/Controller/port_controller.dart
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:isolate';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
|
/// Ein PortController mit verschiedenen FUnktionen zur KOmmunikations zwischen
|
||||||
|
/// main und anderen Isolates
|
||||||
|
class PortController {
|
||||||
|
/// Gibt eine Instanz dieser Klasse zurück
|
||||||
|
factory PortController() => _instance;
|
||||||
|
|
||||||
|
PortController._internal() {
|
||||||
|
if (ServicesBinding.rootIsolateToken != null) {
|
||||||
|
_registerRootIsolateTokenSender();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final PortController _instance = PortController._internal();
|
||||||
|
|
||||||
|
/// Fügt einen Port mit [name] zum NameServer hinzu
|
||||||
|
void addPort(final SendPort sendPort, final String name) {
|
||||||
|
IsolateNameServer.registerPortWithName(sendPort, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gibt einen Port mit [name] vom NameServer zurück, falls gefunden
|
||||||
|
SendPort? getPort(final String name) =>
|
||||||
|
IsolateNameServer.lookupPortByName(name);
|
||||||
|
|
||||||
|
/// Gibt das [RootIsolateToken] der main-Isolate zurück
|
||||||
|
Future<RootIsolateToken?> getRootIsolateToken() async {
|
||||||
|
final ReceivePort receivePort = ReceivePort();
|
||||||
|
|
||||||
|
final SendPort? rootPort = IsolateNameServer.lookupPortByName(
|
||||||
|
'root-isolate-token',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (rootPort == null) {
|
||||||
|
receivePort.close();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
rootPort.send(receivePort.sendPort);
|
||||||
|
|
||||||
|
try {
|
||||||
|
final dynamic message = await receivePort.first;
|
||||||
|
|
||||||
|
if (message is RootIsolateToken) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
receivePort.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _registerRootIsolateTokenSender() {
|
||||||
|
final ReceivePort receivePort = ReceivePort()
|
||||||
|
..listen((final value) {
|
||||||
|
if (value is SendPort) {
|
||||||
|
value.send(ServicesBinding.rootIsolateToken);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
addPort(receivePort.sendPort, 'root-isolate-token');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
import 'package:routemaster/routemaster.dart';
|
import 'package:routemaster/routemaster.dart';
|
||||||
|
|
||||||
|
import 'Controller/background_task_controller.dart';
|
||||||
import 'Services/navigation_service.dart';
|
import 'Services/navigation_service.dart';
|
||||||
import 'Services/router_service.dart';
|
import 'Services/router_service.dart';
|
||||||
import 'Services/theme_service.dart';
|
import 'Services/theme_service.dart';
|
||||||
@@ -9,6 +10,8 @@ import 'Services/theme_service.dart';
|
|||||||
void main() {
|
void main() {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
BackgroundTaskController();
|
||||||
|
|
||||||
runApp(
|
runApp(
|
||||||
MaterialApp.router(
|
MaterialApp.router(
|
||||||
routerDelegate: RoutemasterDelegate(
|
routerDelegate: RoutemasterDelegate(
|
||||||
@@ -22,9 +25,7 @@ void main() {
|
|||||||
GlobalWidgetsLocalizations.delegate,
|
GlobalWidgetsLocalizations.delegate,
|
||||||
GlobalCupertinoLocalizations.delegate,
|
GlobalCupertinoLocalizations.delegate,
|
||||||
],
|
],
|
||||||
supportedLocales: const [
|
supportedLocales: const [Locale('de')],
|
||||||
Locale('de'),
|
|
||||||
],
|
|
||||||
|
|
||||||
title: 'DragonLedger 🐉📒',
|
title: 'DragonLedger 🐉📒',
|
||||||
theme: ThemeService.getLightTheme(),
|
theme: ThemeService.getLightTheme(),
|
||||||
|
|||||||
Reference in New Issue
Block a user