Fix schedule app

master
Nikola Forró 6 years ago
parent 2c57699107
commit c9d3c0fc10

@ -8,9 +8,3 @@ export interface Slot {
minutes: number; minutes: number;
offset: number; offset: number;
} }
export interface Schedule {
schedule: {
scheduleSlots: Slot[];
};
}

@ -18,6 +18,8 @@ export class DaysHoursMinutesPipe implements PipeTransform {
let hours = hrs % 24; let hours = hrs % 24;
let days = (hrs - hours) / 24 >> 0; let days = (hrs - hours) / 24 >> 0;
let result = minutes.toString() + ' minutes'; let result = minutes.toString() + ' minutes';
if (minutes == 1)
result = minutes.toString() + ' minute';
if (hours == 1) if (hours == 1)
result = hours.toString() + ' hour, ' + result; result = hours.toString() + ' hour, ' + result;
else if (hours > 1) else if (hours > 1)

@ -3,7 +3,7 @@ import {
PipeTransform PipeTransform
} from '@angular/core'; } from '@angular/core';
import { Slot } from '../models/schedule'; import { Slot } from '../models/slot';
@Pipe({ @Pipe({
@ -13,15 +13,26 @@ export class GetDateTimePipe implements PipeTransform {
transform(slot: Slot): Date { transform(slot: Slot): Date {
let result = new Date(); let result = new Date();
let weekdays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; let day = WEEKDAYS.indexOf(slot.label.toLowerCase());
let day = weekdays.indexOf(slot.label.toLowerCase()); result.setDate(result.getDate() + day - result.getDay());
let offset = day - result.getDay();
if (offset < 0)
offset += 7;
result.setDate(result.getDate() + offset)
let remainder = slot.offset % 60; let remainder = slot.offset % 60;
result.setUTCHours(slot.hours - (slot.offset - remainder) / 60 >> 0); result.setUTCHours(slot.hours - (slot.offset - remainder) / 60 >> 0);
result.setUTCMinutes(slot.minutes - remainder); result.setUTCMinutes(slot.minutes - remainder);
result.setUTCSeconds(0);
result.setUTCMilliseconds(0);
if (result.getTime() < Date.now()) {
result.setDate(result.getDate() + 7);
}
return result; return result;
} }
} }
const WEEKDAYS = [
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday'
];

@ -18,7 +18,21 @@
<mat-card-title>Next scheduled livestream</mat-card-title> <mat-card-title>Next scheduled livestream</mat-card-title>
<mat-card-content>In {{dataSource.countdown$ | async | dayshoursminutes}}</mat-card-content> <mat-card-content>
<span *ngIf="dataSource.countdown$ | async as countdown">
<span [ngStyle]="{color: calcColor(countdown)}">
<span *ngIf="countdown / 60000 >= 1">In {{countdown | dayshoursminutes}}</span>
<span *ngIf="countdown / 60000 < 1">Right now!</span>
</span>
</span>
</mat-card-content>
</mat-card> </mat-card>

@ -39,4 +39,9 @@ export class ScheduleComponent implements OnInit {
changeTimeZone(value) { changeTimeZone(value) {
this.timeZone = value; this.timeZone = value;
} }
calcColor(value) {
let x = Math.min(Math.exp(value / 600000), 255);
return `rgb(255, ${x}, ${x})`;
}
} }

@ -17,7 +17,7 @@ import { GetDateTimePipe } from '../pipes/getdatetime.pipe';
import { ScheduleService } from './schedule.service'; import { ScheduleService } from './schedule.service';
import { Slot } from '../models/schedule'; import { Slot } from '../models/slot';
export class ScheduleDataSource implements DataSource<Slot> { export class ScheduleDataSource implements DataSource<Slot> {
@ -28,6 +28,8 @@ export class ScheduleDataSource implements DataSource<Slot> {
public loading$ = this.loadingSubject.asObservable(); public loading$ = this.loadingSubject.asObservable();
public countdown$ = this.countdownSubject.asObservable(); public countdown$ = this.countdownSubject.asObservable();
private lastCountdown = 0;
constructor(private scheduleService: ScheduleService) { } constructor(private scheduleService: ScheduleService) { }
loadSchedule(disabled: boolean) { loadSchedule(disabled: boolean) {
@ -38,35 +40,52 @@ export class ScheduleDataSource implements DataSource<Slot> {
catchError(() => of([])), catchError(() => of([])),
finalize(() => this.loadingSubject.next(false)) finalize(() => this.loadingSubject.next(false))
) )
.subscribe(schedule => { .subscribe((schedule: Slot[]) => {
schedule = this.sorted(schedule); schedule = this.sorted(schedule);
this.scheduleSubject.next(schedule); this.scheduleSubject.next(schedule);
timer(0, 1000).subscribe(() => timer(0, 1000).subscribe(() => {
this.countdownSubject.next(this.countdown(schedule)) 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) { private sorted(schedule) {
schedule.sort(function(a, b) { schedule.sort(function(a, b) {
a = new GetDateTimePipe().transform(a); a = (new GetDateTimePipe()).transform(a);
b = new GetDateTimePipe().transform(b); b = (new GetDateTimePipe()).transform(b);
return a.getTime() - b.getTime(); return a.getTime() - b.getTime();
}); });
return schedule; 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<Slot[]> { connect(collectionViewer: CollectionViewer): Observable<Slot[]> {
console.log('Connecting data source'); console.log('Connecting data source');
return this.scheduleSubject.asObservable(); return this.scheduleSubject.asObservable();

@ -9,10 +9,7 @@ import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { import { Slot } from '../models/slot';
Schedule,
Slot
} from '../models/schedule';
@Injectable() @Injectable()
@ -21,9 +18,11 @@ export class ScheduleService {
getSchedule(disabled = false): Observable<Slot[]> { getSchedule(disabled = false): Observable<Slot[]> {
return this.http.get<Schedule>('https://schedule.twitch.serpent.ai/schedule/92737529') return this.http.get('https://schedule.twitch.serpent.ai/schedule/92737529')
.pipe( .pipe(
map(res => res.schedule.scheduleSlots.filter(slot => disabled || slot.isEnabled)) map((res: any) => res.schedule.scheduleSlots.filter(
(slot: Slot) => disabled || slot.isEnabled
))
); );
} }
} }

Loading…
Cancel
Save