import { Injectable } from "@angular/core" ; import { UserSettingsModel , UserZonesModel , ZoneModel } from "@elevate/shared/models" ; import { UserSettingsDao } from "../../dao/user-settings/user-settings.dao" ; import { ZoneDefinitionModel } from "../../models/zone-definition.model" ; import { userSettingsData } from "@elevate/shared/data" ; import { LoggerService } from "../logging/logger.service" ; @ Injectable ( ) export class UserSettingsService { public static readonly MARK_LOCAL_STORAGE_CLEAR : string = "localStorageMustBeCleared" ; constructor ( public userSettingsDao , public logger ) { } public fetch ( ) { return ( < Promise < UserSettingsModel > > this . userSettingsDao . fetch ( ) ) ; } param path param value public saveProperty < V > ( path , value ) { return this . userSettingsDao . upsertProperty < V > ( path , value ) ; } returns { Promise < UserSettingsModel > } public clearLocalStorageOnNextLoad ( ) { return this . saveProperty ( UserSettingsService . MARK_LOCAL_STORAGE_CLEAR , true ) . then ( ( ) => { this . logger . info ( "LocalStorage is marked to be cleared on next core load" ) ; return Promise . resolve ( ) ; } ) ; } param { ZoneDefinitionModel } zoneDefinition param { ZoneModel [ ] } zones returns { Promise < ZoneModel [ ] > } public saveZones ( zoneDefinition , zones ) { const path = [ "zones" , zoneDefinition . value ] ; return this . saveProperty < number [ ] > ( path , UserZonesModel . serialize ( zones ) ) . then ( ( userSettingsModel ) => { return Promise . resolve ( UserZonesModel . deserialize ( userSettingsModel . zones [ zoneDefinition . value ] ) ) ; } ) ; } returns { Promise < UserSettingsModel > } public reset ( ) { return ( < Promise < UserSettingsModel > > this . userSettingsDao . save ( userSettingsData ) ) ; } returns { Promise < UserSettingsModel > } public resetZones ( ) { const defaultZones = ( < UserSettingsModel > this . userSettingsDao . getDefaultStorageValue ( ) ) . zones ; return this . saveProperty < UserZonesModel > ( [ "zones" ] , defaultZones ) ; } }