-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add LocationHistoryUpdater.dart
- Loading branch information
Showing
5 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export "./location_history.dart"; |
65 changes: 65 additions & 0 deletions
65
lib/services/location_history_service/location_history.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
} |