diff --git a/web-angular/package.json b/web-angular/package.json index e1d40e05..15cdd4a7 100644 --- a/web-angular/package.json +++ b/web-angular/package.json @@ -5,6 +5,7 @@ "scripts": { "ng": "ng", "start": "ng serve", + "start:forecast": "ng serve weather-forecast-today", "build": "ng build", "build:forecast": "ng build --prod weather-forecast-today && npm run concat:forecast", "concat:forecast": "node ./ng-element-concat.js weather-forecast-today", diff --git a/web-angular/projects/weather-forecast-today/src/app/web-component/weather-forecast-today.component.ts b/web-angular/projects/weather-forecast-today/src/app/web-component/weather-forecast-today.component.ts index df8a4593..d8dfb44e 100644 --- a/web-angular/projects/weather-forecast-today/src/app/web-component/weather-forecast-today.component.ts +++ b/web-angular/projects/weather-forecast-today/src/app/web-component/weather-forecast-today.component.ts @@ -1,21 +1,22 @@ import { + AfterViewChecked, Component, - OnInit, - Input, - OnDestroy, - ViewEncapsulation, - ViewChild, ElementRef, HostListener, - AfterViewChecked + Input, + OnDestroy, + OnInit, + ViewChild, + ViewEncapsulation } from '@angular/core'; -import { Subscription, BehaviorSubject } from 'rxjs'; -import { ResourceService } from '../../../../../src/app/services/resource.service'; +import {BehaviorSubject, Subscription} from 'rxjs'; +import {ResourceService} from '../../../../../src/app/services/resource.service'; import { CityForecastService, + ForecastPeriod, WeatherConditionSummary } from '../../../../../src/app/services/city-forecast.service'; -import { DefaultService, City } from '../../../../../src/app/api'; +import {City, DefaultService} from '../../../../../src/app/api'; /** * Shows the current day forecast for a particular city. The main use is @@ -32,6 +33,7 @@ export class WeatherForecastTodayComponent implements OnInit, AfterViewChecked, @Input() cityId: number; @Input() langCode: 'de' | 'en'; + @Input() tomorrow: boolean; @Input() dayTime?: boolean; @Input() noHeader?: boolean; @Input() noFooter?: boolean; @@ -77,7 +79,9 @@ export class WeatherForecastTodayComponent implements OnInit, AfterViewChecked, (city: City) => { if (!this.noHeader) { this.setHeaderInfos(city); } const date = new Date(); - const { dayForecasts, daytimeForecasts, error } = this.cityForecastService.fetchForecasts(city, date, true); + // if (this.tomorrow) { date.setDate(date.getDate() + 1); } + const period = this.tomorrow ? ForecastPeriod.TOMORROW : ForecastPeriod.TODAY; + const { dayForecasts, daytimeForecasts, error } = this.cityForecastService.fetchForecasts(city, date, period); this.forecast$ = this.dayTime ? daytimeForecasts : dayForecasts; this.forecastReqErrorSubscr = error.subscribe( (forecastErr: any) => this.handleErrorMsg(forecastErr, 'An Error occured while fetching forecast information.') @@ -106,6 +110,7 @@ export class WeatherForecastTodayComponent implements OnInit, AfterViewChecked, private evalInputProperties(): void { if (!this.langCode) { this.langCode = 'de'; } + this.tomorrow = this.tomorrow !== undefined; this.dayTime = this.dayTime !== undefined; this.noHeader = this.noHeader !== undefined; this.noFooter = this.noFooter !== undefined; @@ -140,6 +145,7 @@ export class WeatherForecastTodayComponent implements OnInit, AfterViewChecked, const outerDiv: HTMLDivElement = this.outer.nativeElement; const innerDiv: HTMLDivElement = this.inner.nativeElement; if (this.dayTime) { + // Needs to check container size due to multiple tiles. if (this.outerBrkPt2 < outerDiv.clientWidth && outerDiv.clientWidth <= this.outerBrkPt1) { this.setHeaderTextSize(this.innerHeadMd); if (innerDiv.style.maxWidth !== this.innerMedium) { @@ -157,6 +163,7 @@ export class WeatherForecastTodayComponent implements OnInit, AfterViewChecked, } } } else { + // Container size always set to small because there is only one tile. this.setHeaderTextSize(this.innerHeadSm); if (innerDiv.style.maxWidth !== this.innerSmall) { innerDiv.style.maxWidth = this.innerSmall; diff --git a/web-angular/projects/weather-forecast-today/src/index.html b/web-angular/projects/weather-forecast-today/src/index.html index f745be74..8a8f01ff 100644 --- a/web-angular/projects/weather-forecast-today/src/index.html +++ b/web-angular/projects/weather-forecast-today/src/index.html @@ -7,6 +7,6 @@ - + diff --git a/web-angular/src/app/services/city-forecast.service.ts b/web-angular/src/app/services/city-forecast.service.ts index 632a2888..3d72e9a8 100644 --- a/web-angular/src/app/services/city-forecast.service.ts +++ b/web-angular/src/app/services/city-forecast.service.ts @@ -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, dayForecasts: BehaviorSubject>, daytimeForecasts: BehaviorSubject>, @@ -53,7 +59,7 @@ export class CityForecastService { const error = new BehaviorSubject(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>; - error: BehaviorSubject - } { - const subject = new BehaviorSubject>(null); - const error = new BehaviorSubject>(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 | null { + private computeDailySummary(forecast: WeatherForecast, city: City, date: Date): Array | null { let days: Array | 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(); 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) - : Array { + private computeTimeOfDaySummary(days: Array): Array { const times = new Array(); if (days) { days.forEach(