import { Injectable } from '@angular/core' ; import { NavigationEnd , NavigationStart , Router } from '@angular/router' ; import { BehaviorSubject , Observable } from 'rxjs' ; import { filter } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class NotaddProgressBarService { private bufferValueSubject : BehaviorSubject < number > ; private modeSubject : BehaviorSubject < string > ; private valueSubject : BehaviorSubject < number > ; private visibleSubject : BehaviorSubject < boolean > ; constructor ( private router ) { this . bufferValueSubject = new BehaviorSubject ( 0 ) ; this . modeSubject = new BehaviorSubject ( 'indeterminate' ) ; this . valueSubject = new BehaviorSubject ( 0 ) ; this . visibleSubject = new BehaviorSubject ( false ) ; this . subscribeRouterEvents ( ) ; } get bufferValue ( ) : Observable < any > { return this . bufferValueSubject . asObservable ( ) ; } setBufferValue ( value ) { this . bufferValueSubject . next ( value ) ; } get mode ( ) : Observable < any > { return this . modeSubject . asObservable ( ) ; } setMode ( value ) { this . modeSubject . next ( value ) ; } get value ( ) : Observable < any > { return this . valueSubject . asObservable ( ) ; } setValue ( value ) { this . valueSubject . next ( value ) ; } get visible ( ) : Observable < any > { return this . visibleSubject . asObservable ( ) ; } private subscribeRouterEvents ( ) { this . router . events . pipe ( filter ( ( event ) => event instanceof NavigationStart ) ) . subscribe ( ( ) => { this . setVisible ( true ) ; } ) ; this . router . events . pipe ( filter ( ( event ) => event instanceof NavigationEnd ) ) . subscribe ( ( ) => { this . setVisible ( false ) ; } ) ; } setVisible ( visible ) { this . visibleSubject . next ( visible ) ; } }