Skip to content

Commit

Permalink
Fixed ocassional API error on first refresh, moved networking out of …
Browse files Browse the repository at this point in the history
…weather class
  • Loading branch information
Aimireal committed Aug 18, 2021
1 parent c563bbb commit d70dfb3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 55 deletions.
4 changes: 4 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
String openWeatherURL = "https://api.openweathermap.org/data/2.5/onecall";
String reverseGeocoding = "http://api.openweathermap.org/geo/1.0/reverse";

//Future settings switch
String appLang = "en";
String appUnits = "metric";

//App Icons
Icon kLightningIcon = Icon(
FontAwesomeIcons.bolt,
Expand Down
54 changes: 23 additions & 31 deletions lib/models/weather.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import 'package:http/http.dart';

import 'package:weatherapp/constants.dart';
import 'package:weatherapp/credentials.dart';
import 'package:weatherapp/utils/location.dart';
import 'package:weatherapp/models/dailyweather.dart';
import 'package:weatherapp/utils/location.dart';
import 'package:weatherapp/utils/daynight.dart';
import 'package:weatherapp/utils/network.dart';
import 'package:weatherapp/widgets/daily_weather_display.dart';

//Setting icon and background image
class WeatherDisplayData{
Expand Down Expand Up @@ -40,18 +42,13 @@ class WeatherData{
//Daily weather values
dynamic maxTemp;
dynamic minTemp;

//Future settings switch
String appLang = "en";
String appUnits = "metric";
dynamic weatherResponse;

Future<void> getCurrentTemperature() async{
Response response = await get(
//One call API. Returns more data than the standard data
Uri.parse(
'$openWeatherURL?lat=${locationData.latitude}&lon=${locationData.longitude}&exclude=&appid=$apiKey&units=$appUnits&lang=$appLang'
)
);
Response response = await
NetworkService().getCurrentTemperature(locationData);

weatherResponse = response;

//Return the weather values
if(response.statusCode == 200){
Expand All @@ -74,26 +71,21 @@ class WeatherData{
}

//Generating a DailyWeather instance for daily weather cards
void getDailyWeather(dynamic weatherData) {
print("weather: getDailyWeather");
if(weatherData['daily'] != null){
print("weather:not null");
List<dynamic> jsonDays = weatherData['daily'];
jsonDays.forEach((day) {
dailyWeatherCards.add(
DailyWeather(
weekday: kWeekdays[
DateTime.fromMillisecondsSinceEpoch(day['dt'] * 1000).weekday]?? '',
//conditionWeather: day['weather'][0]['id'],
maxTemp:
day['temp']['max'].round(),
minTemp: day['temp']['min'].round(),
),
);
});
}else{
print("weather: Daily null");
}
//ToDo: I wonder if it doesn't work since I use weatherData, instead of the response object
void getDailyWeather(weatherResponse) {
List<dynamic> jsonDays = weatherResponse['daily'];
jsonDays.forEach((day) {
dailyWeatherCards.add(
DailyWeather(
weekday: kWeekdays[
DateTime.fromMillisecondsSinceEpoch(day['dt'] * 1000).weekday]?? '',
//conditionWeather: day['weather'][0]['id'],
maxTemp:
day['temp']['max'].round(),
minTemp: day['temp']['min'].round(),
),
);
});
}

//Icon changing based on weather
Expand Down
11 changes: 4 additions & 7 deletions lib/screens/main_display.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart';

import 'package:weatherapp/utils/daynight.dart';
import 'package:weatherapp/utils/string_formatter.dart';
import 'package:weatherapp/utils/location.dart';
import 'package:weatherapp/utils/network.dart';
import 'package:weatherapp/models/geocoding.dart';
import 'package:weatherapp/models/weather.dart';
import 'package:weatherapp/models/dailyweather.dart';
Expand Down Expand Up @@ -61,13 +63,10 @@ class _MainDisplayState extends State<MainDisplay> {
weatherData.getWeatherDisplayData();
backgroundImage = weatherDisplayData.weatherImage;
weatherDisplayIcon = weatherDisplayData.weatherIcon;

//Daily forecast
//weatherData.getDailyWeather(weatherData);
});
}

void refreshInfo() async{
Future<void> refreshInfo() async{
//Refresh Location, Update Geodata and Refresh Weather
newLocation = LocationHelper();
await newLocation.getCurrentLocation();
Expand All @@ -77,8 +76,6 @@ class _MainDisplayState extends State<MainDisplay> {

newWeather = WeatherData(locationData: newLocation);
await newWeather.getCurrentTemperature();

newWeather.getDailyWeather(newWeather);
}

@override
Expand Down Expand Up @@ -157,7 +154,7 @@ class _MainDisplayState extends State<MainDisplay> {
floatingActionButton: FloatingActionButton(
onPressed: () async {
//Refresh Location, Update Geodata and Refresh Weather
refreshInfo();
await refreshInfo();

print("Updated UI");

Expand Down
27 changes: 11 additions & 16 deletions lib/utils/network.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import 'dart:convert' as convert;

import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:weatherapp/credentials.dart';
import 'package:weatherapp/constants.dart';

String key = apiKey;

//ToDo: Currently unused due to it needing required, and the URL in the method that evokes it
class NetworkService{
final String url;
NetworkService(String s, {required this.url});

Future fetchAPI(String url) async{
final response = await http.get(url as Uri);
String key = apiKey;

if(response.statusCode == 200){
return convert.jsonDecode(response.body);
} else{
throw Exception('Failed to fetch API');
}
Future<Response> getCurrentTemperature(locationData) async{
Response response = await get(
//One call API. Returns more data than the standard data
Uri.parse(
'$openWeatherURL?lat=${locationData.latitude}&lon=${locationData.longitude}&exclude=&appid=$apiKey&units=$appUnits&lang=$appLang'
)
);
return response;
}
}
1 change: 0 additions & 1 deletion lib/widgets/daily_weather_display.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:weatherapp/models/weather.dart';
import 'package:weatherapp/utils/daynight.dart';
import 'package:weatherapp/constants.dart';

Expand Down

0 comments on commit d70dfb3

Please sign in to comment.