diff --git a/lib/helpers/http.dart b/lib/helpers/http.dart index 61f50fe..127090e 100644 --- a/lib/helpers/http.dart +++ b/lib/helpers/http.dart @@ -10,26 +10,30 @@ import 'dart:convert'; Future hitAPI(String url) async { if (debug) debugPrint("DEBUG: URL is '$url'."); - http.Response response = await http.get(Uri.parse(url)); + try { + http.Response response = await http.get(Uri.parse(url)); - String data = ""; - if (response.statusCode == 200) { - data = response.body; - var decodedData = jsonDecode(data); - //data = decodedData.toString(); - //data = jsonEncode(decodedData); - if (debugVerbose) { - debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n"); - } - } else { - if (debug) { - debugPrint( - "DEBUG: Response failed with code '${response.statusCode.toString()}'", - ); + String data = ""; + if (response.statusCode == 200) { + data = response.body; + var decodedData = jsonDecode(data); + //data = decodedData.toString(); + //data = jsonEncode(decodedData); + if (debugVerbose) { + debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n"); + } + } else { + if (debug) { + debugPrint( + "DEBUG: Response failed with code '${response.statusCode.toString()}'", + ); + } } + + return data; + } catch (e) { + return "Sorry! It seems we have an issue: '${e.toString()}'."; } - - return data; } // How to guide: https://openweathermap.org/current#zip diff --git a/lib/helpers/json.dart b/lib/helpers/json.dart index 32ea39c..dafbf07 100644 --- a/lib/helpers/json.dart +++ b/lib/helpers/json.dart @@ -11,7 +11,8 @@ String formatOpenWeatherData(var data) { windSpeed = pullOpenWeatherWind(data), humidity = pullOpenWeatherHumidity(data); - String windChill = getWindChill(temp, windSpeed), heatIndex = ""; + String windChill = getWindChill(temp, windSpeed), + heatIndex = getHeatIndex(temp, humidity); String comfort = ""; @@ -19,7 +20,8 @@ String formatOpenWeatherData(var data) { "$location is $temp$tempUnits and $conditions" " with a wind speed of $windSpeed$windUnits" " and humidity of $humidity$humidityUnits." - " $windChill$heatIndex" + " $windChill" + " $heatIndex" " $comfort"; final String doubleSpace = " ", singleSpace = " "; @@ -89,10 +91,54 @@ String getWindChill(String temp, String windSpeed) { double temperature = double.parse(temp); double wind = double.parse(windSpeed); - if (temperature < 50 && wind > 5) { + if ((temperature < 50 && wind > 5) || debug) { double windChill = calcWindChill((temperature), (wind)); - return "My guess is that's a wind chill of $windChill$tempUnits"; + return "My guess is that's a wind chill of $windChill$tempUnits."; + } + return ""; +} + +double calcHeatIndex(double temp, double humidity) { + // ## Heat Index ## + // # Official formula: https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml + double heatIndex = + -42.379 + + 2.04901523 * temp + + 10.14333127 * humidity - + 0.22475541 * temp * humidity - + 0.00683783 * temp * temp - + 0.05481717 * humidity * humidity + + 0.00122874 * temp * temp * humidity + + 0.00085282 * temp * humidity * humidity - + 0.00000199 * temp * temp * humidity * humidity; + + if (humidity < 13 && temp >= 80 && temp <= 112) { + heatIndex -= ((13 - humidity) / 4) * sqrt((17 - (temp - 95.0).abs()) / 17); + } + + if (humidity > 85 && temp >= 80 && temp <= 87) { + heatIndex += ((humidity - 85) / 10) * ((87 - temp) / 5); + } + + if (heatIndex < 80) { + heatIndex = + 0.5 * (temp + 61.0 + ((temp - 68.0) * 1.2) + (humidity * 0.094)); + heatIndex = (heatIndex + temp) / 2; + } + + if (debug) { + debugPrint("DEBUG: heatIndex = '$heatIndex'"); + } + return heatIndex; +} + +String getHeatIndex(String temp, String humidity) { + double temperature = double.parse(temp); + double humid = double.parse(humidity); + + if (temperature > 80 || debug) { + double heatIndex = calcHeatIndex((temperature), (humid)); + return "My guess is that's a heat index of $heatIndex$tempUnits."; } - return ""; } diff --git a/lib/main.dart b/lib/main.dart index 8e9c07f..d5b64e4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -97,6 +97,10 @@ class _MainAppState extends State { weather = "Please enter a location."; } + if (weather.toString().contains("Sorry!")) { + return weather; + } + if (weather != loadText) { if (debug) { debugPrint("DEBUG: Formatting text.");