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> { class _MainAppState extends State<MainApp> {
String weather = loadText; String weather = loadText;
bool keepLoading = false; bool keepLoading = false;
DateTime lastLoadTime = DateTime.now().subtract(Duration(seconds: 5));
@override @override
void initState() { void initState() {
@ -90,10 +91,10 @@ class _MainAppState extends State<MainApp> {
if (response.statusCode == 200) { if (response.statusCode == 200) {
data = response.body; data = response.body;
var decodedData = jsonDecode(data); var decodedData = jsonDecode(data);
if (debug) debugPrint(decodedData.toString()); //if (debug) debugPrint(decodedData.toString());
data = decodedData.toString(); data = decodedData.toString();
} else { } else {
debugPrint(response.statusCode.toString()); if (debug) debugPrint(response.statusCode.toString());
} }
return data; return data;
@ -104,7 +105,18 @@ class _MainAppState extends State<MainApp> {
} }
_refreshWeather() { _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; weather = loadText;
_callOpenWeather(); _callOpenWeather();
lastLoadTime = DateTime.now();
} }
} }