Skip to content

Commit

Permalink
feat: Register background locator on use realtime and use background …
Browse files Browse the repository at this point in the history
…fetch otherwise
  • Loading branch information
Myzel394 committed Sep 5, 2023
1 parent 941254a commit 5c84e6f
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 42 deletions.
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@
"@backgroundLocationFetch_text": {
"description": "Keep this very short"
},
"backgroundLocator_title": "Updating location in real-time",
"backgroundLocator_text": "Locus is updating your location in real-time.",
"logs_createdAt": "{date}",
"@logs_createdAt": {
"placeholders": {
Expand Down
66 changes: 44 additions & 22 deletions 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/background_locator.dart';
import 'package:locus/services/manager_service/helpers.dart';
import 'package:locus/services/settings_service/SettingsMapLocation.dart';
import 'package:locus/services/task_service.dart';
Expand Down Expand Up @@ -139,41 +140,48 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
void initState() {
super.initState();

final taskService = context.read<TaskService>();
final viewService = context.read<ViewService>();
final logService = context.read<LogService>();
final settings = context.read<SettingsService>();
final appUpdateService = context.read<AppUpdateService>();

rotationController =
AnimationController(vsync: this, duration: Duration.zero);
rotationAnimation = Tween<double>(
begin: 0,
end: 2 * pi,
).animate(rotationController);

_createLocationFetcher();
_handleViewAlarmChecker();
_handleNotifications();

settings.addListener(_updateBackgroundListeners);
taskService.addListener(_updateBackgroundListeners);

WidgetsBinding.instance
..addObserver(this)
..addPostFrameCallback((_) async {
_setLocationFromSettings();
configureBackgroundFetch();

final taskService = context.read<TaskService>();
final viewService = context.read<ViewService>();
final logService = context.read<LogService>();
final appUpdateService = context.read<AppUpdateService>();
_fetchers.addListener(_rebuild);
appUpdateService.addListener(_rebuild);

_updateBackgroundListeners();
initQuickActions(context);
_initUniLinks();
_updateLocaleToSettings();
_showUpdateDialogIfRequired();

taskService.checkup(logService);

_fetchers.addListener(_rebuild);
appUpdateService.addListener(_rebuild);
viewService.addListener(_handleViewServiceChange);
hasGrantedLocationPermission().then((hasGranted) {
if (hasGranted) {
_initLiveLocationUpdate();
}
});

viewService.addListener(_handleViewServiceChange);
});

_handleViewAlarmChecker();
_handleNotifications();

final settings = context.read<SettingsService>();
if (settings.getMapProvider() == MapProvider.openStreetMap) {
flutterMapController = MapController();
flutterMapController!.mapEventStream.listen((event) {
Expand Down Expand Up @@ -202,13 +210,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>

flutterMapPopupController = PopupController();
}

rotationController =
AnimationController(vsync: this, duration: Duration.zero);
rotationAnimation = Tween<double>(
begin: 0,
end: 2 * pi,
).animate(rotationController);
}

@override
Expand Down Expand Up @@ -353,6 +354,27 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
await settings.save();
}

void _updateBackgroundListeners() async {
final settings = context.read<SettingsService>();
final taskService = context.read<TaskService>();

if (taskService.tasks.isEmpty) {
// Nothing needs to be updated
return;
}

if (settings.useRealtimeUpdates) {
removeBackgroundFetch();

await configureBackgroundLocator();
await initializeBackgroundLocator(context);
} else {
await configureBackgroundFetch();

registerBackgroundFetch();
}
}

void _initLiveLocationUpdate() {
if (_positionStream != null) {
return;
Expand Down
56 changes: 36 additions & 20 deletions lib/services/manager_service/background_fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ void backgroundFetchHeadlessTask(HeadlessTask task) async {

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Background Fetch",
"Running headless task with ID $taskId",
);

if (isTimeout) {
FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Background Fetch",
"Task $taskId timed out.",
);

Expand All @@ -27,37 +27,21 @@ void backgroundFetchHeadlessTask(HeadlessTask task) async {

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Background Fetch",
"Starting headless task with ID $taskId now...",
);

await runBackgroundTask();

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Background Fetch",
"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,
Expand Down Expand Up @@ -105,3 +89,35 @@ Future<void> configureBackgroundFetch() async {
return;
}
}

void registerBackgroundFetch() {
FlutterLogs.logInfo(
LOG_TAG,
"Background Fetch",
"Registering headless task...",
);

BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);

FlutterLogs.logInfo(
LOG_TAG,
"Background Fetch",
"Registering headless task... Done!",
);
}

void removeBackgroundFetch() {
FlutterLogs.logInfo(
LOG_TAG,
"Background Fetch",
"Removing headless task...",
);

BackgroundFetch.stop();

FlutterLogs.logInfo(
LOG_TAG,
"Background Fetch",
"Removing headless task... Done!",
);
}
65 changes: 65 additions & 0 deletions lib/services/manager_service/background_locator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:background_locator_2/background_locator.dart';
import 'package:background_locator_2/location_dto.dart';
import 'package:background_locator_2/settings/android_settings.dart';
import 'package:background_locator_2/settings/ios_settings.dart';
import 'package:background_locator_2/settings/locator_settings.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:locus/constants/app.dart';
import 'package:locus/constants/values.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

@pragma('vm:entry-point')
void runBackgroundLocatorTask(final LocationDto location,) {}

Future<void> configureBackgroundLocator() {
FlutterLogs.logInfo(
LOG_TAG,
"Background Locator",
"Initializing background locator.",
);

return BackgroundLocator.initialize();
}

Future<void> initializeBackgroundLocator(final BuildContext context,) {
final l10n = AppLocalizations.of(context);

FlutterLogs.logInfo(
LOG_TAG,
"Background Locator",
"Registering background locator.",
);

return BackgroundLocator.registerLocationUpdate(
runBackgroundLocatorTask,
autoStop: false,
androidSettings: AndroidSettings(
accuracy: LocationAccuracy.HIGH,
distanceFilter:
BACKGROUND_LOCATION_UPDATES_MINIMUM_DISTANCE_FILTER.toDouble(),
client: isGMSFlavor ? LocationClient.google : LocationClient.android,
androidNotificationSettings: AndroidNotificationSettings(
notificationTitle: l10n.backgroundLocator_title,
notificationMsg: l10n.backgroundLocator_text,
),
),
iosSettings: IOSSettings(
distanceFilter:
BACKGROUND_LOCATION_UPDATES_MINIMUM_DISTANCE_FILTER.toDouble(),
accuracy: LocationAccuracy.HIGH,
showsBackgroundLocationIndicator: true,
stopWithTerminate: false,
),
);
}

Future<void> removeBackgroundLocator() {
FlutterLogs.logInfo(
LOG_TAG,
"Background Locator",
"Removing background locator.",
);

return BackgroundLocator.unRegisterLocationUpdate();
}

0 comments on commit 5c84e6f

Please sign in to comment.