Files
wetterApp/lib/models/daily_weather_model.dart
T
2022-08-31 14:56:00 +02:00

58 lines
1.3 KiB
Dart

import 'package:wetter/models/hourly_weather_model.dart';
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 = [];
for (var dataPoint in dataPoints) {
hourlyForecasts.add(HourlyWeatherModel.fromJson(dataPoint));
}
}
String dailyAverageTemp() {
double tempSum = 0;
for (var forecast in hourlyForecasts) {
tempSum = tempSum + forecast.temperature;
}
return (tempSum / hourlyForecasts.length).round().toString();
}
List<int> tempList() {
List<int> temps = [];
for (HourlyWeatherModel hf in hourlyForecasts) {
temps.add(hf.temperature.round());
}
return temps;
}
String dailyMaxTemp() {
return tempList().reduce(max).toString();
}
String dailyMinTemp() {
return tempList().reduce(min).toString();
}
String? cumulatedSunshineMinutes() {
bool dataCorrect = true;
int csm = 0;
for (var hf in hourlyForecasts) {
if (hf.sunshine == null) {
return null;
} else {
csm = csm + hf.sunshine!;
}
}
if (dataCorrect) {
return csm.toString();
}
}
}