import { Injectable } from '@angular/core' ; import { STORAGE_KEYS , StorageService } from '../storage/storage.service' ; import { Settings } from '../../../shared/models/settings.model' ; import { UtilsService } from '../utils/utils.service' ; import { API_URL , HttpService } from '../../http/http.service' ; import { NotificationService } from '../notification/notification.service' ; import { BehaviorSubject , Observable } from 'rxjs' ; @ Injectable ( { providedIn : 'root' } ) export class SettingsService { public static readonly LANGUAGES = [ 'en' , 'fr' , 'de' , 'it' , 'zh-tw' , 'zh-cn' , 'es' , 'pt-br' ] ; public settings : Observable < Settings | null > ; private settingsSubject : BehaviorSubject < Settings | null > ; constructor ( private storageService , private notificationService , private httpService ) { this . settingsSubject = new BehaviorSubject ( null ) ; this . settings = this . settingsSubject . asObservable ( ) ; } /**
     * Initialize settings with the default or cached values and return them.
     */ public async init ( ) { const defaultSettings = await this . getDefaultSettings ( ) ; const settings = await this . storageService . get ( STORAGE_KEYS . SETTINGS ) ; if ( settings !== null && UtilsService . isSameJSONStructure ( defaultSettings , settings ) ) { this . settingsSubject . next ( settings ) ; return { settings , isFirstTime : false } ; } await this . storageService . set ( STORAGE_KEYS . SETTINGS , defaultSettings ) ; this . settingsSubject . next ( defaultSettings ) ; return { settings : defaultSettings , isFirstTime : true } ; } /**
     * Update the settings in the storage.
     */ public async updateCachedSettings ( settings ) { await this . storageService . set ( STORAGE_KEYS . SETTINGS , settings ) ; this . settingsSubject . next ( settings ) ; } /**
     * Return the current settings.
     */ public getCachedSettings ( ) { return this . settingsSubject . getValue ( ) ; } /**
     * Return the default settings.
     */ private getDefaultSettings ( ) { return this . httpService . get ( API_URL . LOCAL_ASSETS , 'settings.json' ) ; } }