Do not allow the weather to be refreshed very quickly.

This commit is contained in:
Hyperling 2025-04-25 08:34:19 -07:00
parent 0f475773de
commit 7a48d137e0

View File

@ -23,6 +23,7 @@ class MainApp extends StatefulWidget {
class _MainAppState extends State<MainApp> {
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<MainApp> {
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<MainApp> {
}
_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();
}
}