2018-04-19 17:06:08 +02:00
|
|
|
import { Injectable, OnDestroy } from '@angular/core';
|
|
|
|
|
import { Subject } from 'rxjs/Subject';
|
|
|
|
|
import { SettingsService } from './settings.service';
|
|
|
|
|
import { SelfUpdaterService } from './self-updater.service';
|
|
|
|
|
import { TimerObservable } from 'rxjs/observable/TimerObservable';
|
|
|
|
|
import { Subscription } from 'rxjs/Subscription';
|
2018-04-23 11:34:51 +02:00
|
|
|
import { ActivationStart, Router } from '@angular/router';
|
2018-04-19 17:06:08 +02:00
|
|
|
import { SlideShowService } from '../../display/slide-show.service';
|
|
|
|
|
import 'rxjs/add/operator/filter';
|
|
|
|
|
import 'rxjs/add/operator/switchMap';
|
|
|
|
|
|
|
|
|
|
const TIMER_UPDATE_POLL_INTERVAL = 30000;
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class TimerService implements OnDestroy {
|
|
|
|
|
private autoSwitch = false;
|
|
|
|
|
private slideShowTimer: Subscription;
|
|
|
|
|
private selfUpdateCheckerTimer: Subscription;
|
|
|
|
|
private slideTimerSubject: Subject<number> = new Subject<number>();
|
|
|
|
|
private slideIntervalSubscription: Subscription;
|
|
|
|
|
|
|
|
|
|
constructor(private settings: SettingsService,
|
|
|
|
|
private selfUpdaterService: SelfUpdaterService,
|
|
|
|
|
private router: Router,
|
|
|
|
|
private slideShowService: SlideShowService) {
|
|
|
|
|
|
|
|
|
|
const timerSUC = TimerObservable.create(TIMER_UPDATE_POLL_INTERVAL, TIMER_UPDATE_POLL_INTERVAL);
|
|
|
|
|
this.selfUpdateCheckerTimer = timerSUC.subscribe(() => {
|
|
|
|
|
this.selfUpdaterService.checkAndReloadIfNecessary();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.slideShowTimer = this.slideTimerSubject.switchMap((period: number) => TimerObservable.create(period))
|
|
|
|
|
.subscribe(() => this.changeSlide());
|
|
|
|
|
this.setSlideTimer(this.settings.slideInterval);
|
|
|
|
|
|
|
|
|
|
this.autoSwitch = false;
|
|
|
|
|
this.router.events
|
2018-04-23 11:34:51 +02:00
|
|
|
.filter(event => event instanceof ActivationStart)
|
|
|
|
|
.subscribe((event: ActivationStart) => this.autoSwitch = !!event.snapshot.data.autoSwitchable);
|
2018-04-19 17:06:08 +02:00
|
|
|
|
|
|
|
|
this.slideIntervalSubscription = this.settings.slideIntervalChanged.subscribe(
|
|
|
|
|
interval => this.setSlideTimer(interval)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
|
|
|
|
this.slideShowTimer.unsubscribe();
|
|
|
|
|
this.selfUpdateCheckerTimer.unsubscribe();
|
|
|
|
|
this.slideIntervalSubscription.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private changeSlide() {
|
|
|
|
|
if (this.autoSwitch) {
|
|
|
|
|
this.slideShowService.nextSlide();
|
|
|
|
|
}
|
|
|
|
|
this.setSlideTimer(this.settings.slideInterval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setSlideTimer(delay: number) {
|
|
|
|
|
this.slideTimerSubject.next(delay);
|
|
|
|
|
}
|
|
|
|
|
}
|