-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactoring manager service
- Loading branch information
Showing
12 changed files
with
252 additions
and
228 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import 'package:background_fetch/background_fetch.dart'; | ||
import 'package:flutter_logs/flutter_logs.dart'; | ||
import 'package:locus/constants/values.dart'; | ||
import 'package:locus/services/manager_service/task.dart'; | ||
|
||
@pragma('vm:entry-point') | ||
void backgroundFetchHeadlessTask(HeadlessTask task) async { | ||
String taskId = task.taskId; | ||
bool isTimeout = task.timeout; | ||
|
||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Headless Task", | ||
"Running headless task with ID $taskId", | ||
); | ||
|
||
if (isTimeout) { | ||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Headless Task", | ||
"Task $taskId timed out.", | ||
); | ||
|
||
BackgroundFetch.finish(taskId); | ||
return; | ||
} | ||
|
||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Headless Task", | ||
"Starting headless task with ID $taskId now...", | ||
); | ||
|
||
await runBackgroundTask(); | ||
|
||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Headless Task", | ||
"Starting headless task with ID $taskId now... Done!", | ||
); | ||
|
||
BackgroundFetch.finish(taskId); | ||
} | ||
|
||
void registerBackgroundFetch() { | ||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Background Fetch", | ||
"Registering headless task...", | ||
); | ||
|
||
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask); | ||
|
||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Background Fetch", | ||
"Registering headless task... Done!", | ||
); | ||
} | ||
|
||
Future<void> configureBackgroundFetch() async { | ||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Background Fetch", | ||
"Configuring background fetch...", | ||
); | ||
|
||
try { | ||
BackgroundFetch.configure( | ||
BackgroundFetchConfig( | ||
minimumFetchInterval: 15, | ||
requiresCharging: false, | ||
enableHeadless: true, | ||
requiredNetworkType: NetworkType.ANY, | ||
requiresBatteryNotLow: false, | ||
requiresDeviceIdle: false, | ||
requiresStorageNotLow: false, | ||
startOnBoot: true, | ||
stopOnTerminate: false, | ||
), | ||
(taskId) async { | ||
// We only use one taskId to update the location for all tasks, | ||
// so we don't need to check the taskId. | ||
await runBackgroundTask(); | ||
|
||
BackgroundFetch.finish(taskId); | ||
}, | ||
(taskId) { | ||
// Timeout, we need to finish immediately. | ||
BackgroundFetch.finish(taskId); | ||
}, | ||
); | ||
|
||
FlutterLogs.logInfo( | ||
LOG_TAG, | ||
"Background Fetch", | ||
"Configuring background fetch. Configuring... Done!", | ||
); | ||
} catch (error) { | ||
FlutterLogs.logError( | ||
LOG_TAG, | ||
"Background Fetch", | ||
"Configuring background fetch. Configuring... Failed! $error", | ||
); | ||
return; | ||
} | ||
} |
Oops, something went wrong.