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

Commit

Permalink
refactor: Rename to UpdateLocationHistory.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 12, 2023
1 parent efb029e commit 1feb7ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ 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});
class UpdateLocationHistory extends StatefulWidget {
const UpdateLocationHistory({super.key});

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

class _LocationHistoryUpdaterState extends State<LocationHistoryUpdater> {
class _UpdateLocationHistoryState extends State<UpdateLocationHistory> {
late final CurrentLocationService _currentLocation;
late final StreamSubscription _subscription;
late final LocationHistory _locationHistory;
Expand All @@ -25,11 +24,7 @@ class _LocationHistoryUpdaterState extends State<LocationHistoryUpdater> {
super.initState();

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

_locationHistory.add(location);
});
_subscription = _currentLocation.stream.listen(_locationHistory.add);
}

@override
Expand Down
8 changes: 4 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:locus/App.dart';
import 'package:locus/app_wrappers/CheckViewAlarmsLive.dart';
import 'package:locus/app_wrappers/HandleNotifications.dart';
import 'package:locus/app_wrappers/InitCurrentLocationFromSettings.dart';
import 'package:locus/app_wrappers/LocationHistoryUpdater.dart';
import 'package:locus/app_wrappers/UpdateLocationHistory.dart';
import 'package:locus/app_wrappers/PublishTaskPositionsOnUpdate.dart';
import 'package:locus/app_wrappers/RegisterBackgroundListeners.dart';
import 'package:locus/app_wrappers/ManageQuickActions.dart';
Expand All @@ -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 @@ -107,7 +107,7 @@ void main() async {
],
child: const Stack(
children: [
LocationHistoryUpdater(),
UpdateLocationHistory(),
UniLinksHandler(),
UpdateLastLocationToSettings(),
RegisterBackgroundListeners(),
Expand Down
28 changes: 17 additions & 11 deletions lib/services/location_history_service/location_history.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import 'package:flutter/cupertino.dart';
import 'package:geolocator/geolocator.dart';

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

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

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

factory LocationHistory.fromJSON(final Map<String, dynamic> data) =>
LocationHistory(
data["locations"] != null
? List<LocationPointService>.from(
? List<Position>.from(
data["locations"].map(
(location) => LocationPointService.fromJSON(location),
(location) =>
Position.fromMap(location as Map<String, dynamic>),
),
)
: null,
Expand All @@ -33,24 +35,28 @@ class LocationHistory extends ChangeNotifier {

// To avoid too many crumbled locations, we only save locations that are at
// least one minute apart
bool _canAdd(final LocationPointService location) {
bool _canAdd(final Position position) {
if (position.timestamp == null) {
return false;
}

if (locations.isEmpty) {
return true;
}

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

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

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

Expand All @@ -60,6 +66,6 @@ class LocationHistory extends ChangeNotifier {
}

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

0 comments on commit 1feb7ad

Please sign in to comment.