Skip to content

Commit

Permalink
refactor: Refactoring manager service
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 5, 2023
1 parent 51499cb commit f53884f
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 228 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:locus/App.dart';
import 'package:locus/services/app_update_service.dart';
import 'package:locus/services/log_service.dart';
import 'package:locus/services/manager_service.dart';
import 'package:locus/services/manager_service/background_fetch.dart';
import 'package:locus/services/settings_service/index.dart';
import 'package:locus/services/task_service.dart';
import 'package:locus/services/view_service.dart';
Expand Down
3 changes: 2 additions & 1 deletion lib/screens/LocationsOverviewScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:locus/screens/locations_overview_screen_widgets/OutOfBoundMarker
import 'package:locus/screens/locations_overview_screen_widgets/ShareLocationSheet.dart';
import 'package:locus/screens/locations_overview_screen_widgets/ViewLocationPopup.dart';
import 'package:locus/screens/locations_overview_screen_widgets/view_location_fetcher.dart';
import 'package:locus/services/manager_service/helpers.dart';
import 'package:locus/services/settings_service/SettingsMapLocation.dart';
import 'package:locus/services/task_service.dart';
import 'package:locus/services/view_service.dart';
Expand Down Expand Up @@ -53,7 +54,7 @@ import '../main.dart';
import '../services/app_update_service.dart';
import '../services/location_point_service.dart';
import '../services/log_service.dart';
import '../services/manager_service.dart';
import '../services/manager_service/background_fetch.dart';
import 'package:locus/services/settings_service/index.dart';
import '../utils/PageRoute.dart';
import '../utils/color.dart';
Expand Down
107 changes: 107 additions & 0 deletions lib/services/manager_service/background_fetch.dart
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;
}
}
Loading

0 comments on commit f53884f

Please sign in to comment.