From 7ecf2a9c74dec91662c706fcbac503b3668383db Mon Sep 17 00:00:00 2001 From: danny Date: Mon, 17 Apr 2023 19:37:18 +0200 Subject: [PATCH] Added option to fetch only latest location --- headless_haystack/lib/dashboard/dashboard.dart | 2 +- headless_haystack/lib/history/accessory_history.dart | 12 +++++++++--- .../lib/preferences/preferences_page.dart | 3 ++- webserver/FindMy_proxy.py | 9 +++++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/headless_haystack/lib/dashboard/dashboard.dart b/headless_haystack/lib/dashboard/dashboard.dart index e3d3e13..b96227e 100644 --- a/headless_haystack/lib/dashboard/dashboard.dart +++ b/headless_haystack/lib/dashboard/dashboard.dart @@ -82,7 +82,7 @@ class _DashboardState extends State { SnackBar( backgroundColor: Theme.of(context).colorScheme.primary, content: Text( - 'Fetched $count locations', + 'Fetched $count location(s)', style: TextStyle( color: Theme.of(context).colorScheme.onPrimary, ), diff --git a/headless_haystack/lib/history/accessory_history.dart b/headless_haystack/lib/history/accessory_history.dart index 270e62e..b109d47 100644 --- a/headless_haystack/lib/history/accessory_history.dart +++ b/headless_haystack/lib/history/accessory_history.dart @@ -18,6 +18,7 @@ class AccessoryHistory extends StatefulWidget { Key? key, required this.accessory, }) : super(key: key); + @override State createState() { return _AccessoryHistoryState(); @@ -30,12 +31,16 @@ class _AccessoryHistoryState extends State { bool showPopup = false; Pair? popupEntry; - double numberOfDays = Settings.getValue(numberOfDaysToFetch, defaultValue: 7)!.toDouble(); + double numberOfDays = + Settings.getValue(numberOfDaysToFetch, defaultValue: 7)!.toDouble(); @override void initState() { super.initState(); _mapController = MapController(); + if (numberOfDays < 1) { + numberOfDays = 7; + } WidgetsBinding.instance.addPostFrameCallback((_) { mapReady(); }); @@ -55,8 +60,9 @@ class _AccessoryHistoryState extends State { .toList(); return Scaffold( - appBar: AppBar( - title: Text("${widget.accessory.name} (${widget.accessory.locationHistory.length} history reports)"), + appBar: AppBar( + title: Text( + "${widget.accessory.name} (${widget.accessory.locationHistory.length} history reports)"), ), body: SafeArea( child: Column( diff --git a/headless_haystack/lib/preferences/preferences_page.dart b/headless_haystack/lib/preferences/preferences_page.dart index b77f4e0..2888be3 100644 --- a/headless_haystack/lib/preferences/preferences_page.dart +++ b/headless_haystack/lib/preferences/preferences_page.dart @@ -56,13 +56,14 @@ class _PreferencesPageState extends State { title: 'Number of days to fetch location', settingKey: numberOfDaysToFetch, values: const { + 0: "latest location only", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", - 7: "7" + 7: "7", }, selected: 7, ); diff --git a/webserver/FindMy_proxy.py b/webserver/FindMy_proxy.py index e2f7c16..3f0116e 100755 --- a/webserver/FindMy_proxy.py +++ b/webserver/FindMy_proxy.py @@ -62,16 +62,21 @@ def do_POST(self): res = conn.getresponse() result = json.loads(res.read()) - results = result["results"] newResults = [] + latestEntry = None for idx, entry in enumerate(results): if (int(entry["datePublished"]) > startdate): newResults.append(entry) + if latestEntry is None: + latestEntry = entry + elif latestEntry["datePublished"] < entry["datePublished"]: + latestEntry = entry - + if days < 1 and latestEntry is not None: + newResults.append(latestEntry) result["results"] = newResults self.send_response(200) # send response headers