From 7e53a2b4e2f4b6f4363bfb5c0a847c636808eb3b Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 25 Apr 2025 17:10:22 -0700 Subject: [PATCH] Successfully pulling a good weather report. Now to add the heat index and wind chill calculations. --- lib/helpers/http.dart | 20 ++++++------ lib/helpers/json.dart | 71 +++++++++++++++++++++++++++++++++++++++++++ lib/main.dart | 56 ++++++++++++++++++++++++++++++++-- 3 files changed, 135 insertions(+), 12 deletions(-) diff --git a/lib/helpers/http.dart b/lib/helpers/http.dart index f807a50..61f50fe 100644 --- a/lib/helpers/http.dart +++ b/lib/helpers/http.dart @@ -8,7 +8,7 @@ import 'dart:convert'; // Generic method to hit a GET request and return the response. Future hitAPI(String url) async { - if (debug) debugPrint(url); + if (debug) debugPrint("DEBUG: URL is '$url'."); http.Response response = await http.get(Uri.parse(url)); @@ -16,10 +16,17 @@ Future hitAPI(String url) async { if (response.statusCode == 200) { data = response.body; var decodedData = jsonDecode(data); - if (debugVerbose) debugPrint(decodedData.toString()); - data = decodedData.toString(); + //data = decodedData.toString(); + //data = jsonEncode(decodedData); + if (debugVerbose) { + debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n"); + } } else { - if (debug) debugPrint(response.statusCode.toString()); + if (debug) { + debugPrint( + "DEBUG: Response failed with code '${response.statusCode.toString()}'", + ); + } } return data; @@ -38,8 +45,3 @@ Future hitOpenWeatherZip(String zipCode, String? countryCode) async { String data = await hitAPI(url); return data; } - -// Convert the Future value into a usable value. -loadWeather() async { - return await hitOpenWeatherZip("47630", "US"); -} diff --git a/lib/helpers/json.dart b/lib/helpers/json.dart index 541589c..6227e59 100644 --- a/lib/helpers/json.dart +++ b/lib/helpers/json.dart @@ -1 +1,72 @@ // Functions related to parsing JSON strings and objects. + +import '/var/config.dart'; +import 'package:flutter/material.dart'; + +String formatOpenWeatherData(var data) { + String location = pullOpenWeatherCity(data), + temp = pullOpenWeatherTemp(data), + conditions = pullOpenWeatherConditions(data), + windSpeed = pullOpenWeatherWind(data), + humidity = pullOpenWeatherHumidity(data), + windChill = "", + heatIndex = "", + comfort = ""; + + // Always ask for Imperial units in API request + final String tempUnits = 'F', windUnits = 'mph', humidityUnits = '%'; + + String text = + "$location is $temp$tempUnits and $conditions" + " with a wind speed of $windSpeed$windUnits" + " and humidity of $humidity$humidityUnits." + " $windChill$heatIndex" + " $comfort"; + + final String doubleSpace = " ", singleSpace = " "; + while (text.contains(doubleSpace)) { + text = text.replaceAll(doubleSpace, singleSpace); + } + + return text; +} + +String pullOpenWeatherCity(var data) { + String location = "${data['city']['name']} (${data['city']['country']})"; + if (debug) { + debugPrint("DEBUG: location = '$location'"); + } + return location; +} + +String pullOpenWeatherTemp(var data) { + String temp = data['list'][0]['main']['temp'].toString(); + if (debug) { + debugPrint("DEBUG: temp = '$temp'"); + } + return temp; +} + +String pullOpenWeatherConditions(var data) { + String conditions = data['list'][0]['weather'][0]['description'].toString(); + if (debug) { + debugPrint("DEBUG: conditions = '$conditions'"); + } + return conditions; +} + +String pullOpenWeatherWind(var data) { + String wind = data['list'][0]['wind']['speed'].toString(); + if (debug) { + debugPrint("DEBUG: wind = '$wind'"); + } + return wind; +} + +String pullOpenWeatherHumidity(var data) { + String humidity = data['list'][0]['main']['humidity'].toString(); + if (debug) { + debugPrint("DEBUG: humidity = '$humidity'"); + } + return humidity; +} diff --git a/lib/main.dart b/lib/main.dart index ad98b46..8e9c07f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,10 @@ // 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'; @@ -74,10 +77,51 @@ class _MainAppState extends State { ); } - // Call the + // 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 != 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 (debug) { + debugPrint("DEBUG: Refresh was '$lastReloadSeconds' seconds ago."); + } if (lastReloadSeconds < limitRefreshSeconds) { debugPrint("DEBUG: Skipping reload."); @@ -86,8 +130,14 @@ class _MainAppState extends State { return; } + if (debug) { + debugPrint("DEBUG: Pulling weather..."); + } weather = loadText; - weather = loadWeather(); + pullWeather(country: "US", zip: "47630"); lastLoadTime = DateTime.now(); + if (debug) { + debugPrint("DEBUG: Weather pulled and date is set."); + } } }