|
|
|
@ -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<Slot> {
|
|
|
|
@ -28,6 +28,8 @@ export class ScheduleDataSource implements DataSource<Slot> {
|
|
|
|
|
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<Slot> {
|
|
|
|
|
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<Slot[]> {
|
|
|
|
|
console.log('Connecting data source');
|
|
|
|
|
return this.scheduleSubject.asObservable();
|
|
|
|
|