Skip to content

Commit

Permalink
Refresh map after slider changed
Browse files Browse the repository at this point in the history
  • Loading branch information
dchristl committed Oct 9, 2023
1 parent 626bf35 commit 07c73c7
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions headless_haystack/lib/history/accessory_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ class _AccessoryHistoryState extends State<AccessoryHistory> {

@override
Widget build(BuildContext context) {
var now = DateTime.now();
var filteredEntries = widget.accessory.locationHistory
.where(
(element) => element.end.isAfter(
now.subtract(Duration(days: numberOfDays.round())),
),
)
.toList();
List<Pair<dynamic, dynamic>> filteredEntries = filterHistoryEntries();
var historyLength = filteredEntries.length;
List<Polyline> polylines = [];

Expand Down Expand Up @@ -196,6 +189,9 @@ class _AccessoryHistoryState extends State<AccessoryHistory> {
onChanged: (double newValue) {
setState(() {
numberOfDays = newValue.toInt();
WidgetsBinding.instance.addPostFrameCallback((_) {
mapReady();
});
});
},
),
Expand All @@ -213,14 +209,26 @@ class _AccessoryHistoryState extends State<AccessoryHistory> {
}

mapReady() {
if (widget.accessory.locationHistory.isNotEmpty) {
var historicLocations = widget.accessory.locationHistory
.map((entry) => entry.location)
.toList();
List<Pair<dynamic, dynamic>> filteredEntries = filterHistoryEntries();
if (filteredEntries.isNotEmpty) {
var historicLocations =
filteredEntries.map((entry) => entry.location).toList();
var bounds = LatLngBounds.fromPoints(historicLocations);
_mapController
..fitBounds(bounds)
..move(_mapController.center, _mapController.zoom + 0.00001);
}
}

List<Pair<dynamic, dynamic>> filterHistoryEntries() {
var now = DateTime.now();
var filteredEntries = widget.accessory.locationHistory
.where(
(element) => element.end.isAfter(
now.subtract(Duration(days: numberOfDays.round())),
),
)
.toList();
return filteredEntries;
}
}

0 comments on commit 07c73c7

Please sign in to comment.