diff --git a/lib/components/compact_weather_data.dart b/lib/components/compact_weather_data.dart index fc26802..24b813a 100644 --- a/lib/components/compact_weather_data.dart +++ b/lib/components/compact_weather_data.dart @@ -21,7 +21,7 @@ class _CompactWeatherDataState extends State { 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), diff --git a/lib/main.dart b/lib/main.dart index 2fb7c13..661e651 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -13,12 +13,13 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - late Future futureAlbum; + late Future> 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 { ), ), home: Scaffold( - appBar: AppBar( - title: const Text('Wetter heute in Harburg'), - ), - body: Center( - child: FutureBuilder( - 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>( + 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(); + }, + ), + ), + ))), ); } } diff --git a/lib/models/daily_weather_model.dart b/lib/models/daily_weather_model.dart index 1b3018d..749def7 100644 --- a/lib/models/daily_weather_model.dart +++ b/lib/models/daily_weather_model.dart @@ -4,6 +4,10 @@ import 'dart:math'; class DailyWeatherModel { late List hourlyForecasts; + DateTime dateOfDay() { + return hourlyForecasts[0].timestamp.toLocal(); + } + DailyWeatherModel.fromJson(Map json) { List dataPoints = json['weather']; hourlyForecasts = []; diff --git a/lib/services/brightsky_api_service.dart b/lib/services/brightsky_api_service.dart index c899efa..7f79ad2 100644 --- a/lib/services/brightsky_api_service.dart +++ b/lib/services/brightsky_api_service.dart @@ -3,7 +3,7 @@ import 'package:http/http.dart' as http; import 'package:wetter/models/daily_weather_model.dart'; class BrightSkyAPI { - static Future fetchForecast(DateTime d) async { + static Future 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> 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 responseAsJson = jsonDecode(response.body); + if (response.statusCode == 200) { + List data = splitRangeToDaily(responseAsJson); + return data; + } else { + throw Exception('Failed to load weather data'); + } + } + + static List splitRangeToDaily(Map json) { + List dataPoints = json['weather']; + List response = []; + Map 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; + } }