-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AhsanSarwar45
committed
Sep 10, 2024
1 parent
e238789
commit 4f24909
Showing
8 changed files
with
107 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'package:background_fetch/background_fetch.dart'; | ||
import 'package:clock_app/alarm/logic/update_alarms.dart'; | ||
import 'package:clock_app/debug/logic/logger.dart'; | ||
import 'package:clock_app/timer/logic/update_timers.dart'; | ||
|
||
Future<void> initBackgroundService() async { | ||
await BackgroundFetch.configure( | ||
BackgroundFetchConfig( | ||
minimumFetchInterval: 30, | ||
stopOnTerminate: false, | ||
enableHeadless: true, | ||
requiresBatteryNotLow: false, | ||
requiresCharging: false, | ||
requiresStorageNotLow: false, | ||
requiresDeviceIdle: false, | ||
requiredNetworkType: NetworkType.NONE), (String taskId) async { | ||
// <-- Event handler | ||
// This is the fetch-event callback. | ||
logger.i("[BackgroundFetch] Event received $taskId"); | ||
|
||
// await initializeIsolate(); | ||
|
||
await updateAlarms("initBackgroundService(): Update alarms in background service"); | ||
await updateTimers("initBackgroundService(): Update timers in background service"); | ||
// IMPORTANT: You must signal completion of your task or the OS can punish your app | ||
// for taking too long in the background. | ||
BackgroundFetch.finish(taskId); | ||
}, (String taskId) async { | ||
// <-- Task timeout handler. | ||
// This task has exceeded its allowed running-time. You must stop what you're doing and immediately .finish(taskId) | ||
logger.i("[BackgroundFetch] TASK TIMEOUT taskId: $taskId"); | ||
BackgroundFetch.finish(taskId); | ||
}); | ||
} | ||
|
||
// [Android-only] This "Headless Task" is run when the Android app is terminated with `enableHeadless: true` | ||
@pragma('vm:entry-point') | ||
void handleBackgroundServiceTask(HeadlessTask task) async { | ||
String taskId = task.taskId; | ||
bool isTimeout = task.timeout; | ||
if (isTimeout) { | ||
// This task has exceeded its allowed running-time. | ||
// You must stop what you're doing and immediately .finish(taskId) | ||
logger.i("[BackgroundFetch] Headless task timed-out: $taskId"); | ||
BackgroundFetch.finish(taskId); | ||
return; | ||
} | ||
logger.i('[BackgroundFetch] Headless event received.'); | ||
await updateAlarms("handleBackgroundServiceTask(): Update alarms in background service"); | ||
await updateTimers("handleBackgroundServiceTask(): Update timers in background service"); | ||
|
||
BackgroundFetch.finish(taskId); | ||
} | ||
|
||
void registerHeadlessBackgroundService() { | ||
BackgroundFetch.registerHeadlessTask(handleBackgroundServiceTask); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'dart:isolate'; | ||
import 'dart:ui'; | ||
|
||
import 'package:clock_app/alarm/logic/alarm_isolate.dart'; | ||
import 'package:clock_app/debug/logic/logger.dart'; | ||
import 'package:clock_app/settings/types/listener_manager.dart'; | ||
|
||
void initializeIsolatePorts(){ | ||
ReceivePort receivePort = ReceivePort(); | ||
IsolateNameServer.removePortNameMapping(updatePortName); | ||
IsolateNameServer.registerPortWithName(receivePort.sendPort, updatePortName); | ||
printIsolateInfo(); | ||
receivePort.listen((message) { | ||
if (message == "updateAlarms") { | ||
ListenerManager.notifyListeners("alarms"); | ||
} else if (message == "updateTimers") { | ||
ListenerManager.notifyListeners("timers"); | ||
} else if (message == "updateStopwatches") { | ||
ListenerManager.notifyListeners("stopwatch"); | ||
} | ||
}); | ||
} |