Skip to content

Commit

Permalink
feat: Add LocationHistoryUpdater.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 10, 2023
1 parent 146f111 commit 7f64d4b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
46 changes: 46 additions & 0 deletions lib/app_wrappers/LocationHistoryUpdater.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:locus/services/current_location_service.dart';
import 'package:locus/services/location_history_service/index.dart';
import 'package:locus/services/location_point_service.dart';
import 'package:provider/provider.dart';

/// Makes sure that the [LocationHistory] is updated with the current location
/// from the [CurrentLocationService].
class LocationHistoryUpdater extends StatefulWidget {
const LocationHistoryUpdater({super.key});

@override
State<LocationHistoryUpdater> createState() => _LocationHistoryUpdaterState();
}

class _LocationHistoryUpdaterState extends State<LocationHistoryUpdater> {
late final CurrentLocationService _currentLocation;
late final StreamSubscription _subscription;
late final LocationHistory _locationHistory;

@override
void initState() {
super.initState();

_currentLocation = context.read<CurrentLocationService>();
_subscription = _currentLocation.stream.listen((position) async {
final location = await LocationPointService.fromPosition(position);

_locationHistory.add(location);
});
}

@override
void dispose() {
_subscription.cancel();

super.dispose();
}

@override
Widget build(BuildContext context) {
return const SizedBox.shrink();
}
}
14 changes: 11 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:locus/App.dart';
import 'package:locus/api/get-relays-meta.dart';
import 'package:locus/app_wrappers/LocationHistoryUpdater.dart';
import 'package:locus/screens/locations_overview_screen_widgets/LocationFetchers.dart';
import 'package:locus/services/app_update_service.dart';
import 'package:locus/services/current_location_service.dart';
import 'package:locus/services/location_history_service/index.dart';
import 'package:locus/services/log_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/index.dart';
import 'package:locus/services/view_service/index.dart';
Expand Down Expand Up @@ -67,12 +67,14 @@ void main() async {
SettingsService.restore(),
LogService.restore(),
AppUpdateService.restore(),
LocationHistory.restore(),
]);
final TaskService taskService = futures[0];
final ViewService viewService = futures[1];
final SettingsService settingsService = futures[2];
final LogService logService = futures[3];
final AppUpdateService appUpdateService = futures[4];
final LocationHistory locationHistory = futures[5];

await logService.deleteOldLogs();

Expand All @@ -91,8 +93,14 @@ void main() async {
create: (_) => LocationFetchers(viewService.views)),
ChangeNotifierProvider<CurrentLocationService>(
create: (_) => CurrentLocationService()),
ChangeNotifierProvider<LocationHistory>(create: (_) => locationHistory),
],
child: const App(),
child: const Stack(
children: [
LocationHistoryUpdater(),
App(),
],
),
),
);
}
4 changes: 4 additions & 0 deletions lib/services/location_history_service/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

const storage = FlutterSecureStorage();
const KEY = "_locus_own_location_history_v1";
1 change: 1 addition & 0 deletions lib/services/location_history_service/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export "./location_history.dart";
65 changes: 65 additions & 0 deletions lib/services/location_history_service/location_history.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:flutter/cupertino.dart';

import "./constants.dart";
import '../location_point_service.dart';

class LocationHistory extends ChangeNotifier {
late final List<LocationPointService> locations;

LocationHistory(
final List<LocationPointService>? locations,
) : locations = locations ?? [];

factory LocationHistory.fromJSON(final Map<String, dynamic> data) =>
LocationHistory(
data["locations"] != null
? List<LocationPointService>.from(
data["locations"].map(
(location) => LocationPointService.fromJSON(location),
),
)
: null,
);

static Future<LocationHistory> restore() async {
final data = await storage.read(key: KEY);

if (data == null) {
return LocationHistory(null);
}

return LocationHistory.fromJSON(data as Map<String, dynamic>);
}

// To avoid too many crumbled locations, we only save locations that are at
// least one minute apart
bool _canAdd(final LocationPointService location) {
if (locations.isEmpty) {
return true;
}

return locations.last.createdAt
.difference(location.createdAt)
.inMinutes
.abs() >
1;
}

void add(final LocationPointService location) {
if (!_canAdd(location)) {
return;
}

locations.add(location);
notifyListeners();
}

void clear() {
locations.clear();
notifyListeners();
}

void toJSON() => {
"locations": locations.map((location) => location.toJSON()).toList(),
};
}

0 comments on commit 7f64d4b

Please sign in to comment.