show more days of forecast, enable reload of forecasts
This commit is contained in:
@@ -21,7 +21,7 @@ class _CompactWeatherDataState extends State<CompactWeatherData> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
Text(
|
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,
|
style: Theme.of(context).textTheme.headline4,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
|
|||||||
+36
-20
@@ -13,12 +13,13 @@ class MyApp extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyAppState extends State<MyApp> {
|
class _MyAppState extends State<MyApp> {
|
||||||
late Future<DailyWeatherModel> futureAlbum;
|
late Future<List<DailyWeatherModel>> dailyWeather;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
futureAlbum = BrightSkyAPI.fetchForecast(DateTime.now());
|
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||||
|
DateTime.now(), DateTime.now().add(const Duration(days: 7)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -34,25 +35,40 @@ class _MyAppState extends State<MyApp> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
home: Scaffold(
|
home: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Wetter heute in Harburg'),
|
title: const Text('Wetter 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();
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
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,6 +4,10 @@ import 'dart:math';
|
|||||||
class DailyWeatherModel {
|
class DailyWeatherModel {
|
||||||
late List<HourlyWeatherModel> hourlyForecasts;
|
late List<HourlyWeatherModel> hourlyForecasts;
|
||||||
|
|
||||||
|
DateTime dateOfDay() {
|
||||||
|
return hourlyForecasts[0].timestamp.toLocal();
|
||||||
|
}
|
||||||
|
|
||||||
DailyWeatherModel.fromJson(Map<String, dynamic> json) {
|
DailyWeatherModel.fromJson(Map<String, dynamic> json) {
|
||||||
List dataPoints = json['weather'];
|
List dataPoints = json['weather'];
|
||||||
hourlyForecasts = [];
|
hourlyForecasts = [];
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import 'package:http/http.dart' as http;
|
|||||||
import 'package:wetter/models/daily_weather_model.dart';
|
import 'package:wetter/models/daily_weather_model.dart';
|
||||||
|
|
||||||
class BrightSkyAPI {
|
class BrightSkyAPI {
|
||||||
static Future<DailyWeatherModel> fetchForecast(DateTime d) async {
|
static Future<DailyWeatherModel> fetchForecastOfSingleDay(DateTime d) async {
|
||||||
String dt = "${d.year}-${d.month}-${d.day}";
|
String dt = "${d.year}-${d.month}-${d.day}";
|
||||||
final response = await http.get(
|
final response = await http.get(
|
||||||
Uri.parse(
|
Uri.parse(
|
||||||
@@ -15,4 +15,38 @@ class BrightSkyAPI {
|
|||||||
throw Exception('Failed to load weather data');
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user