Added tomorrow switch to the weather-forecast-today web component.
This commit is contained in:
@@ -13,6 +13,12 @@ export interface WeatherConditionSummary {
|
||||
conditions: WeatherCondition[]; // the conditions to aggregate
|
||||
}
|
||||
|
||||
export enum ForecastPeriod {
|
||||
TODAY,
|
||||
TOMORROW,
|
||||
ALL
|
||||
}
|
||||
|
||||
/**
|
||||
* Service which fetches the forecast of a particular city.
|
||||
*/
|
||||
@@ -36,12 +42,12 @@ export class CityForecastService {
|
||||
*
|
||||
* @param city The city to retrieve the weather forecast from.
|
||||
* @param date The start date.
|
||||
* @param onlyStart Determines if only the start date is of interest.
|
||||
* @param period Specifies what period of forecasts is of interest.
|
||||
* @returns An object which includes the raw forecast, the daily and the daytime
|
||||
* data which is split into night, morning, afternoon and evening.
|
||||
* Additionally an error object is returned if the http call fails.
|
||||
*/
|
||||
fetchForecasts(city: City, date: Date, onlyStart: boolean = false): {
|
||||
fetchForecasts(city: City, date: Date, period: ForecastPeriod = ForecastPeriod.ALL): {
|
||||
forecast: BehaviorSubject<WeatherForecast>,
|
||||
dayForecasts: BehaviorSubject<Array<WeatherConditionSummary>>,
|
||||
daytimeForecasts: BehaviorSubject<Array<WeatherConditionSummary>>,
|
||||
@@ -53,7 +59,7 @@ export class CityForecastService {
|
||||
const error = new BehaviorSubject<any>(null);
|
||||
const subscription = this.apiClientService.getCityForecast(city.id).subscribe(
|
||||
(f: WeatherForecast) => {
|
||||
const prepForecast = this.preprocessForecast(f, city, date, onlyStart);
|
||||
const prepForecast = this.preprocessForecast(f, city, date, period);
|
||||
forecast.next(prepForecast);
|
||||
dayForecasts.next(this.computeDailySummary(prepForecast, city, date));
|
||||
daytimeForecasts.next(this.computeTimeOfDaySummary(dayForecasts.value));
|
||||
@@ -64,58 +70,31 @@ export class CityForecastService {
|
||||
return { forecast, dayForecasts, daytimeForecasts, error };
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the weather forecast for a particular city and summarizes the retrieved
|
||||
* data into daily chunks which in turn are split into night, morning, noon
|
||||
* and evening chunks for a particular day.
|
||||
*
|
||||
* @param city The city to retrieve the weather forecast from.
|
||||
* @param date The start date.
|
||||
* @param onlyToday Determines if only the start date is of interest.
|
||||
* @returns A object which inlcudes a BehaviorSubject that contain the fetched
|
||||
* result and an other BehaviorSubject which contains the error
|
||||
* response if the http call fails.
|
||||
*/
|
||||
fetchTimeOfDayForcasts(city: City, date: Date, onlyToday: boolean = false): {
|
||||
subject: BehaviorSubject<Array<WeatherConditionSummary>>;
|
||||
error: BehaviorSubject<any>
|
||||
} {
|
||||
const subject = new BehaviorSubject<Array<WeatherConditionSummary>>(null);
|
||||
const error = new BehaviorSubject<Array<WeatherConditionSummary>>(null);
|
||||
const subscription = this.apiClientService.getCityForecast(city.id).subscribe(
|
||||
(forecast: WeatherForecast) => {
|
||||
forecast.conditions.sort((a, b) => a.time - b.time);
|
||||
const days = this.computeDailySummary(forecast, city, date);
|
||||
subject.next(this.computeTimeOfDaySummary(days));
|
||||
subscription.unsubscribe();
|
||||
},
|
||||
(err: any) => error.next(err)
|
||||
);
|
||||
return { subject, error };
|
||||
}
|
||||
|
||||
private preprocessForecast(forecast: WeatherForecast, city: City, date: Date, onlyStart: boolean)
|
||||
: WeatherForecast {
|
||||
private preprocessForecast(forecast: WeatherForecast, city: City, date: Date, period: ForecastPeriod): WeatherForecast {
|
||||
forecast.conditions.sort((a, b) => a.time - b.time);
|
||||
if (onlyStart) {
|
||||
// const startDate = new Date(date);
|
||||
// startDate.setHours(0, 0, 0, 0);
|
||||
// const startMilli = startDate.getTime();
|
||||
const today = this.localDate.transform(date, city.timezone);
|
||||
// const start = this.localDate.transform(c.time, city.timezone);
|
||||
// const endMilli = startMilli + 86_400_000 - 1;
|
||||
forecast.conditions = forecast.conditions
|
||||
// .filter(c => startMilli <= c.time && c.time <= endMilli);
|
||||
.filter(c => today === this.localDate.transform(c.time, city.timezone));
|
||||
switch (period) {
|
||||
case ForecastPeriod.TODAY:
|
||||
const today = this.localDate.transform(date, city.timezone);
|
||||
forecast.conditions = forecast.conditions
|
||||
.filter(c => today === this.localDate.transform(c.time, city.timezone));
|
||||
break;
|
||||
case ForecastPeriod.TOMORROW:
|
||||
const dateTomorrow = new Date(date);
|
||||
dateTomorrow.setDate(dateTomorrow.getDate() + 1)
|
||||
const tomorrow = this.localDate.transform(dateTomorrow, city.timezone);
|
||||
forecast.conditions = forecast.conditions
|
||||
.filter(c => tomorrow === this.localDate.transform(c.time, city.timezone));
|
||||
break;
|
||||
}
|
||||
return forecast;
|
||||
}
|
||||
|
||||
private computeDailySummary(forecast: WeatherForecast, city: City, date: Date)
|
||||
: Array<WeatherConditionSummary> | null {
|
||||
private computeDailySummary(forecast: WeatherForecast, city: City, date: Date): Array<WeatherConditionSummary> | null {
|
||||
let days: Array<WeatherConditionSummary> | null;
|
||||
if (forecast != null) {
|
||||
const yesterday = this.localDate.transform(new Date(date).setDate(date.getDate() - 1), city.timezone);
|
||||
const today = this.localDate.transform(date, city.timezone);
|
||||
const tomorrow = this.localDate.transform(new Date(date).setDate(date.getDate() + 1), city.timezone);
|
||||
days = new Array<WeatherConditionSummary>();
|
||||
|
||||
forecast.conditions.forEach((c: WeatherCondition) => {
|
||||
@@ -144,25 +123,21 @@ export class CityForecastService {
|
||||
});
|
||||
|
||||
days.forEach((value, index) => {
|
||||
if (value.title === today) {
|
||||
if (value.title === yesterday) {
|
||||
value.title = this.res.strings('label_yesterday');
|
||||
} else if (value.title === today) {
|
||||
value.title = this.res.strings('label_today');
|
||||
if (index > 0) {
|
||||
days[index - 1].title = this.res.strings('label_yesterday');
|
||||
}
|
||||
if (days.length > index + 1) {
|
||||
days[index + 1].title = this.res.strings('label_tomorrow');
|
||||
}
|
||||
} else if (value.title === tomorrow) {
|
||||
value.title = this.res.strings('label_tomorrow');
|
||||
}
|
||||
});
|
||||
// remove days with very litte predictions
|
||||
// at the end of the prediction period
|
||||
// remove days with very little predictions at the end of the prediction period
|
||||
days = days.filter(wcs => wcs.conditions.length >= 3);
|
||||
}
|
||||
return days;
|
||||
}
|
||||
|
||||
private computeTimeOfDaySummary(days: Array<WeatherConditionSummary>)
|
||||
: Array<WeatherConditionSummary> {
|
||||
private computeTimeOfDaySummary(days: Array<WeatherConditionSummary>): Array<WeatherConditionSummary> {
|
||||
const times = new Array<WeatherConditionSummary>();
|
||||
if (days) {
|
||||
days.forEach(
|
||||
|
||||
Reference in New Issue
Block a user