148 lines
3.3 KiB
Dart
148 lines
3.3 KiB
Dart
// Local
|
|
import 'dart:convert';
|
|
|
|
import '/var/secrets.dart';
|
|
import '/var/config.dart';
|
|
import 'helpers/http.dart';
|
|
import 'helpers/json.dart';
|
|
|
|
// Flutter / Dart
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() {
|
|
runApp(const MainApp());
|
|
}
|
|
|
|
final String loadText = "Loading...";
|
|
|
|
class MainApp extends StatefulWidget {
|
|
const MainApp({super.key});
|
|
|
|
@override
|
|
State<MainApp> createState() => _MainAppState();
|
|
}
|
|
|
|
class _MainAppState extends State<MainApp> {
|
|
String weather = loadText;
|
|
bool keepLoading = false;
|
|
DateTime lastLoadTime = DateTime.now().subtract(
|
|
Duration(seconds: limitRefreshSeconds),
|
|
);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_refreshWeather();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Widget weatherLayout = Column(
|
|
children: [Text(weather), CircularProgressIndicator()],
|
|
);
|
|
|
|
if (weather == loadText) {
|
|
Future.delayed(Duration(seconds: 1), () {
|
|
setState(() {
|
|
keepLoading = true;
|
|
});
|
|
});
|
|
} else {
|
|
weatherLayout = Column(
|
|
children: [
|
|
TextButton.icon(
|
|
label: Text("Reload Weather"),
|
|
onPressed: () {
|
|
setState(() {
|
|
_refreshWeather();
|
|
});
|
|
},
|
|
),
|
|
Text(weather),
|
|
],
|
|
);
|
|
}
|
|
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
body: Center(
|
|
child: Column(children: [Text('Weather Today!'), weatherLayout]),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// Convert the Future value into a usable value.
|
|
pullWeather({
|
|
String? country,
|
|
String? zip,
|
|
String? city,
|
|
String? lat,
|
|
String? long,
|
|
}) async {
|
|
country ??= "US";
|
|
|
|
if (lat != null && long != null) {
|
|
weather = "Lat / Long Not Yet Implemented";
|
|
} else if (zip != null) {
|
|
weather = await hitOpenWeatherZip(zip, country);
|
|
} else if (city != null) {
|
|
weather = "City Weather Not Yet Implemented";
|
|
} else {
|
|
weather = "Please enter a location.";
|
|
}
|
|
|
|
if (weather.toString().contains("Sorry!")) {
|
|
return weather;
|
|
}
|
|
|
|
if (weather != loadText) {
|
|
if (debug) {
|
|
debugPrint("DEBUG: Formatting text.");
|
|
}
|
|
var weatherObject = jsonDecode(weather);
|
|
|
|
String weatherString = formatOpenWeatherData(weatherObject);
|
|
|
|
weather = weatherString;
|
|
if (debug) {
|
|
debugPrint("DEBUG: Set to formatted weather string.");
|
|
}
|
|
} else {
|
|
if (debug) {
|
|
debugPrint("DEBUG: Skipping text formatting.");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Call the API and put the desired information into the screen variable.
|
|
_refreshWeather() {
|
|
var lastReloadSeconds = DateTime.now().difference(lastLoadTime).inSeconds;
|
|
if (debug) {
|
|
debugPrint("DEBUG: Refresh was '$lastReloadSeconds' seconds ago.");
|
|
}
|
|
|
|
if (lastReloadSeconds < limitRefreshSeconds) {
|
|
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;
|
|
}
|
|
|
|
if (debug) {
|
|
debugPrint("DEBUG: Pulling weather...");
|
|
}
|
|
weather = loadText;
|
|
pullWeather(country: "US", zip: "47630");
|
|
lastLoadTime = DateTime.now();
|
|
if (debug) {
|
|
debugPrint("DEBUG: Weather pulled and date is set.");
|
|
}
|
|
}
|
|
}
|