From 7a48d137e0e266d7a0461a355c0df32b33890324 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 25 Apr 2025 08:34:19 -0700 Subject: [PATCH] Do not allow the weather to be refreshed very quickly. --- lib/main.dart | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 552d32d..6adb694 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -23,6 +23,7 @@ class MainApp extends StatefulWidget { class _MainAppState extends State { String weather = loadText; bool keepLoading = false; + DateTime lastLoadTime = DateTime.now().subtract(Duration(seconds: 5)); @override void initState() { @@ -90,10 +91,10 @@ class _MainAppState extends State { if (response.statusCode == 200) { data = response.body; var decodedData = jsonDecode(data); - if (debug) debugPrint(decodedData.toString()); + //if (debug) debugPrint(decodedData.toString()); data = decodedData.toString(); } else { - debugPrint(response.statusCode.toString()); + if (debug) debugPrint(response.statusCode.toString()); } return data; @@ -104,7 +105,18 @@ class _MainAppState extends State { } _refreshWeather() { + var lastReloadSeconds = DateTime.now().difference(lastLoadTime).inSeconds; + if (debug) debugPrint("DEBUG: Refresh was $lastReloadSeconds seconds ago."); + + if (lastReloadSeconds < 5) { + debugPrint("DEBUG: Skipping reload."); + // TODO / TBD: Show a toast / scaffold snackbar that it cannot reload yet, + // or change the button text to "Please wait X seconds!". + return; + } + weather = loadText; _callOpenWeather(); + lastLoadTime = DateTime.now(); } }