show more days of forecast, enable reload of forecasts

This commit is contained in:
Konstantin Kollar
2022-08-31 14:56:00 +02:00
parent bc3dcf575c
commit 7c4320f4b6
4 changed files with 76 additions and 22 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ class _CompactWeatherDataState extends State<CompactWeatherData> {
child: Column(
children: [
Text(
"${DateFormat('EEEE').format(DateTime.now().toLocal())}, ${DateTime.now().toLocal().day}.${DateTime.now().toLocal().month}.",
"${DateFormat('EEEE').format(widget.dwm.dateOfDay())}, ${widget.dwm.dateOfDay().day}.${widget.dwm.dateOfDay().month}.",
style: Theme.of(context).textTheme.headline4,
),
const SizedBox(height: 20),
+36 -20
View File
@@ -13,12 +13,13 @@ class MyApp extends StatefulWidget {
}
class _MyAppState extends State<MyApp> {
late Future<DailyWeatherModel> futureAlbum;
late Future<List<DailyWeatherModel>> dailyWeather;
@override
void initState() {
super.initState();
futureAlbum = BrightSkyAPI.fetchForecast(DateTime.now());
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
DateTime.now(), DateTime.now().add(const Duration(days: 7)));
}
@override
@@ -34,25 +35,40 @@ class _MyAppState extends State<MyApp> {
),
),
home: Scaffold(
appBar: AppBar(
title: const Text('Wetter heute in Harburg'),
),
body: Center(
child: FutureBuilder<DailyWeatherModel>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return CompactWeatherData(snapshot.data!);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
appBar: AppBar(
title: const Text('Wetter in Harburg'),
),
),
),
body: RefreshIndicator(
onRefresh: () {
return Future(() {
setState(() {
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
DateTime.now(),
DateTime.now().add(const Duration(days: 7)));
});
});
},
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Center(
child: FutureBuilder<List<DailyWeatherModel>>(
future: dailyWeather,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: snapshot.data!
.map((e) => CompactWeatherData(e))
.toList());
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
))),
);
}
}
+4
View File
@@ -4,6 +4,10 @@ import 'dart:math';
class DailyWeatherModel {
late List<HourlyWeatherModel> hourlyForecasts;
DateTime dateOfDay() {
return hourlyForecasts[0].timestamp.toLocal();
}
DailyWeatherModel.fromJson(Map<String, dynamic> json) {
List dataPoints = json['weather'];
hourlyForecasts = [];
+35 -1
View File
@@ -3,7 +3,7 @@ import 'package:http/http.dart' as http;
import 'package:wetter/models/daily_weather_model.dart';
class BrightSkyAPI {
static Future<DailyWeatherModel> fetchForecast(DateTime d) async {
static Future<DailyWeatherModel> fetchForecastOfSingleDay(DateTime d) async {
String dt = "${d.year}-${d.month}-${d.day}";
final response = await http.get(
Uri.parse(
@@ -15,4 +15,38 @@ class BrightSkyAPI {
throw Exception('Failed to load weather data');
}
}
static Future<List<DailyWeatherModel>> fetchForecastForTimeRange(
DateTime from, DateTime until) async {
String fromDay = "${from.year}-${from.month}-${from.day}";
String untilDay = "${until.year}-${until.month}-${until.day}";
final response = await http.get(
Uri.parse(
'https://api.brightsky.dev/weather?lat=53.43&lon=10&date=$fromDay&last_date=$untilDay&tz=Europe/Berlin'),
headers: {"Accept": "application/json"});
final Map<String, dynamic> responseAsJson = jsonDecode(response.body);
if (response.statusCode == 200) {
List<DailyWeatherModel> data = splitRangeToDaily(responseAsJson);
return data;
} else {
throw Exception('Failed to load weather data');
}
}
static List<DailyWeatherModel> splitRangeToDaily(Map<String, dynamic> json) {
List dataPoints = json['weather'];
List<DailyWeatherModel> response = [];
Map<String, dynamic> currPoint = {"weather": []};
String currDay = dataPoints[0]['timestamp'].substring(0, 10);
for (var point in dataPoints) {
if (currDay != point['timestamp'].substring(0, 10)) {
currDay = point['timestamp'].substring(0, 10);
response.add(DailyWeatherModel.fromJson(currPoint));
currPoint = {"weather": []};
} else {
currPoint['weather'].add(point);
}
}
return response;
}
}