From c9d3c0fc10a244358d5f8d099ba7aa07c2848bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Fri, 15 Jun 2018 17:16:22 +0200 Subject: [PATCH] Fix schedule app --- .../src/app/models/{schedule.ts => slot.ts} | 6 --- .../src/app/pipes/dayshoursminutes.pipe.ts | 2 + .../src/app/pipes/getdatetime.pipe.ts | 25 +++++++--- .../src/app/schedule/schedule.component.html | 16 +++++- .../src/app/schedule/schedule.component.ts | 5 ++ .../src/app/services/schedule.datasource.ts | 49 +++++++++++++------ .../src/app/services/schedule.service.ts | 11 ++--- 7 files changed, 79 insertions(+), 35 deletions(-) rename apps/schedule/src/app/models/{schedule.ts => slot.ts} (70%) diff --git a/apps/schedule/src/app/models/schedule.ts b/apps/schedule/src/app/models/slot.ts similarity index 70% rename from apps/schedule/src/app/models/schedule.ts rename to apps/schedule/src/app/models/slot.ts index d57c839..832ef71 100644 --- a/apps/schedule/src/app/models/schedule.ts +++ b/apps/schedule/src/app/models/slot.ts @@ -8,9 +8,3 @@ export interface Slot { minutes: number; offset: number; } - -export interface Schedule { - schedule: { - scheduleSlots: Slot[]; - }; -} diff --git a/apps/schedule/src/app/pipes/dayshoursminutes.pipe.ts b/apps/schedule/src/app/pipes/dayshoursminutes.pipe.ts index dac1fbf..7d455c8 100644 --- a/apps/schedule/src/app/pipes/dayshoursminutes.pipe.ts +++ b/apps/schedule/src/app/pipes/dayshoursminutes.pipe.ts @@ -18,6 +18,8 @@ export class DaysHoursMinutesPipe implements PipeTransform { let hours = hrs % 24; let days = (hrs - hours) / 24 >> 0; let result = minutes.toString() + ' minutes'; + if (minutes == 1) + result = minutes.toString() + ' minute'; if (hours == 1) result = hours.toString() + ' hour, ' + result; else if (hours > 1) diff --git a/apps/schedule/src/app/pipes/getdatetime.pipe.ts b/apps/schedule/src/app/pipes/getdatetime.pipe.ts index 2448306..703ea05 100644 --- a/apps/schedule/src/app/pipes/getdatetime.pipe.ts +++ b/apps/schedule/src/app/pipes/getdatetime.pipe.ts @@ -3,7 +3,7 @@ import { PipeTransform } from '@angular/core'; -import { Slot } from '../models/schedule'; +import { Slot } from '../models/slot'; @Pipe({ @@ -13,15 +13,26 @@ export class GetDateTimePipe implements PipeTransform { transform(slot: Slot): Date { let result = new Date(); - let weekdays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; - let day = weekdays.indexOf(slot.label.toLowerCase()); - let offset = day - result.getDay(); - if (offset < 0) - offset += 7; - result.setDate(result.getDate() + offset) + let day = WEEKDAYS.indexOf(slot.label.toLowerCase()); + result.setDate(result.getDate() + day - result.getDay()); let remainder = slot.offset % 60; result.setUTCHours(slot.hours - (slot.offset - remainder) / 60 >> 0); result.setUTCMinutes(slot.minutes - remainder); + result.setUTCSeconds(0); + result.setUTCMilliseconds(0); + if (result.getTime() < Date.now()) { + result.setDate(result.getDate() + 7); + } return result; } } + +const WEEKDAYS = [ + 'sunday', + 'monday', + 'tuesday', + 'wednesday', + 'thursday', + 'friday', + 'saturday' +]; diff --git a/apps/schedule/src/app/schedule/schedule.component.html b/apps/schedule/src/app/schedule/schedule.component.html index 8a6a6b5..32b5cbc 100644 --- a/apps/schedule/src/app/schedule/schedule.component.html +++ b/apps/schedule/src/app/schedule/schedule.component.html @@ -18,7 +18,21 @@ Next scheduled livestream - In {{dataSource.countdown$ | async | dayshoursminutes}} + + + + + + + In {{countdown | dayshoursminutes}} + + Right now! + + + + + + diff --git a/apps/schedule/src/app/schedule/schedule.component.ts b/apps/schedule/src/app/schedule/schedule.component.ts index 3f21f12..fde5b1c 100644 --- a/apps/schedule/src/app/schedule/schedule.component.ts +++ b/apps/schedule/src/app/schedule/schedule.component.ts @@ -39,4 +39,9 @@ export class ScheduleComponent implements OnInit { changeTimeZone(value) { this.timeZone = value; } + + calcColor(value) { + let x = Math.min(Math.exp(value / 600000), 255); + return `rgb(255, ${x}, ${x})`; + } } diff --git a/apps/schedule/src/app/services/schedule.datasource.ts b/apps/schedule/src/app/services/schedule.datasource.ts index 05b4a86..d50a58e 100644 --- a/apps/schedule/src/app/services/schedule.datasource.ts +++ b/apps/schedule/src/app/services/schedule.datasource.ts @@ -17,7 +17,7 @@ import { GetDateTimePipe } from '../pipes/getdatetime.pipe'; import { ScheduleService } from './schedule.service'; -import { Slot } from '../models/schedule'; +import { Slot } from '../models/slot'; export class ScheduleDataSource implements DataSource { @@ -28,6 +28,8 @@ export class ScheduleDataSource implements DataSource { public loading$ = this.loadingSubject.asObservable(); public countdown$ = this.countdownSubject.asObservable(); + private lastCountdown = 0; + constructor(private scheduleService: ScheduleService) { } loadSchedule(disabled: boolean) { @@ -38,35 +40,52 @@ export class ScheduleDataSource implements DataSource { catchError(() => of([])), finalize(() => this.loadingSubject.next(false)) ) - .subscribe(schedule => { + .subscribe((schedule: Slot[]) => { schedule = this.sorted(schedule); this.scheduleSubject.next(schedule); - timer(0, 1000).subscribe(() => - this.countdownSubject.next(this.countdown(schedule)) - ); + timer(0, 1000).subscribe(() => { + if (schedule.length == 0) { + return; + } + + let nearest = (new GetDateTimePipe()).transform(schedule[0]); + + if (schedule.length > 1) { + let nearest2 = (new GetDateTimePipe()).transform(schedule[1]); + + if (nearest.getTime() > nearest2.getTime()) { + schedule = this.sorted(schedule); + this.scheduleSubject.next(schedule); + return; + } + } + + let countdown = nearest.getTime() - Date.now(); + let h = countdown - this.lastCountdown; + + if (h > 0 && h < 2000) { + countdown = this.lastCountdown; + } + + this.lastCountdown = countdown; + + this.countdownSubject.next(countdown); + }); }); } private sorted(schedule) { schedule.sort(function(a, b) { - a = new GetDateTimePipe().transform(a); - b = new GetDateTimePipe().transform(b); + a = (new GetDateTimePipe()).transform(a); + b = (new GetDateTimePipe()).transform(b); return a.getTime() - b.getTime(); }); return schedule; } - private countdown(schedule) { - if (schedule.length == 0) - return undefined; - - let next = new GetDateTimePipe().transform(schedule[0]); - return next.getTime() - Date.now(); - } - connect(collectionViewer: CollectionViewer): Observable { console.log('Connecting data source'); return this.scheduleSubject.asObservable(); diff --git a/apps/schedule/src/app/services/schedule.service.ts b/apps/schedule/src/app/services/schedule.service.ts index 8772938..6b27de3 100644 --- a/apps/schedule/src/app/services/schedule.service.ts +++ b/apps/schedule/src/app/services/schedule.service.ts @@ -9,10 +9,7 @@ import { Observable } from 'rxjs/Observable'; import { map } from 'rxjs/operators'; -import { - Schedule, - Slot -} from '../models/schedule'; +import { Slot } from '../models/slot'; @Injectable() @@ -21,9 +18,11 @@ export class ScheduleService { getSchedule(disabled = false): Observable { - return this.http.get('https://schedule.twitch.serpent.ai/schedule/92737529') + return this.http.get('https://schedule.twitch.serpent.ai/schedule/92737529') .pipe( - map(res => res.schedule.scheduleSlots.filter(slot => disabled || slot.isEnabled)) + map((res: any) => res.schedule.scheduleSlots.filter( + (slot: Slot) => disabled || slot.isEnabled + )) ); } }