import { Inject , Injectable , InjectionToken } from '@angular/core' ; import { NavigationStart , Router } from '@angular/router' ; import { Platform } from '@angular/cdk/platform' ; import { BehaviorSubject , Observable } from 'rxjs' ; import { filter } from 'rxjs/operators' ; import * as _ from 'lodash' ; import { NgForage } from 'ngforage' ; import { NotaddConfig } from '@notadd/types/notadd-config' ; export const NOTADD_CONFIG = new InjectionToken ( 'notaddCustomConfig' ) ; @ Injectable ( { providedIn : 'root' } ) export class NotaddConfigService { private configSubject : BehaviorSubject < any > ; readonly defaultConfig : any ; constructor ( private platform , private router , @ Inject ( NOTADD_CONFIG ) private customConfig , private ngForage ) { this . defaultConfig = customConfig ; this . init ( ) ; } set config ( value ) { let config = this . configSubject . getValue ( ) ; config = _ . merge ( { } , config , value ) ; this . configSubject . next ( config ) ; this . setStorageConfig ( config ) ; } get config ( ) : any | Observable < any > { return this . configSubject . asObservable ( ) ; } private init ( ) { if ( this . platform . ANDROID || this . platform . IOS ) { this . defaultConfig . customScrollbars = false ; } this . configSubject = new BehaviorSubject ( _ . cloneDeep ( this . defaultConfig ) ) ; } private async setStorageConfig ( config ) { const storageConfig = await this . ngForage . getItem ( 'NOTADD_CONFIG' ) ; if ( ! _ . isEqual ( config . layout , storageConfig ) ) { this . ngForage . setItem ( 'NOTADD_CONFIG' , config ) ; } } setConfig ( value , opts = { emitEvent : true } ) { let config = this . configSubject . getValue ( ) ; config = _ . merge ( { } , config , value ) ; if ( opts . emitEvent === true ) { this . configSubject . next ( config ) ; } } getConfig ( ) { return this . configSubject . asObservable ( ) ; } resetToDefaults ( ) { this . configSubject . next ( _ . cloneDeep ( this . defaultConfig ) ) ; this . setStorageConfig ( _ . cloneDeep ( this . defaultConfig ) ) ; } }