Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
feat: Update location history on background task
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 12, 2023
1 parent 1feb7ad commit 10503af
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import 'package:provider/provider.dart';
const storage = FlutterSecureStorage();

final StreamController<NotificationResponse> selectedNotificationsStream =
StreamController.broadcast();
StreamController.broadcast();

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -59,7 +59,7 @@ void main() async {
);

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
FlutterLocalNotificationsPlugin();
const initializationSettings = InitializationSettings(
android: AndroidInitializationSettings("ic_launcher_foreground"),
iOS: DarwinInitializationSettings(),
Expand Down Expand Up @@ -105,7 +105,7 @@ void main() async {
create: (_) => CurrentLocationService()),
ChangeNotifierProvider<LocationHistory>(create: (_) => locationHistory),
],
child: const Stack(
child: const Column(
children: [
UpdateLocationHistory(),
UniLinksHandler(),
Expand Down
11 changes: 10 additions & 1 deletion lib/services/location_history_service/location_history.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:geolocator/geolocator.dart';

Expand Down Expand Up @@ -65,7 +67,14 @@ class LocationHistory extends ChangeNotifier {
notifyListeners();
}

void toJSON() => {
Map<String, dynamic> toJSON() => {
"locations": locations.map((location) => location.toJson()).toList(),
};

Future<void> save() async {
await storage.write(
key: KEY,
value: jsonEncode(toJSON()),
);
}
}
48 changes: 46 additions & 2 deletions lib/services/manager_service/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:geolocator/geolocator.dart';
import 'package:locus/constants/notifications.dart';
import 'package:locus/constants/values.dart';
import 'package:locus/services/location_history_service/index.dart';
import 'package:locus/services/location_point_service.dart';
import 'package:locus/services/manager_service/helpers.dart';
import 'package:locus/services/settings_service/index.dart';
Expand Down Expand Up @@ -40,6 +42,38 @@ void _showPermissionMissingNotification({
);
}

void _updateLocation(final Position position) async {
FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Updating Location History; Restoring...",
);

final locationHistory = await LocationHistory.restore();

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Updating Location History; Adding position.",
);

locationHistory.add(position);

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Updating Location History; Saving...",
);

await locationHistory.save();

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
"Updating Location History; Done!",
);
}

Future<void> runBackgroundTask({
final LocationPointService? locationData,
final bool force = false,
Expand Down Expand Up @@ -75,6 +109,18 @@ Future<void> runBackgroundTask({
return;
}

final location = locationData ?? await getLocationData();

try {
_updateLocation(location.asPosition());
} catch (error) {
FlutterLogs.logError(
LOG_TAG,
"Headless Task",
"Error while updating location history: $error",
);
}

if (!force) {
FlutterLogs.logInfo(
LOG_TAG,
Expand Down Expand Up @@ -108,8 +154,6 @@ Future<void> runBackgroundTask({
"Executing headless task now.",
);

final location = locationData ?? await getLocationData();

FlutterLogs.logInfo(
LOG_TAG,
"Headless Task",
Expand Down

0 comments on commit 10503af

Please sign in to comment.