src/app/services/app-settings.service.ts
Properties |
|
Methods |
constructor()
|
|
Defined in src/app/services/app-settings.service.ts:9
|
| Public defaultSettings |
Type : AppSettings
|
Default value : {
persistSelectedTheme: true,
selectedTheme: AppThemes.light
}
|
|
Defined in src/app/services/app-settings.service.ts:5
|
| Public settings |
Type : unknown
|
Default value : new BehaviorSubject<AppSettings>(this.defaultSettings)
|
|
Defined in src/app/services/app-settings.service.ts:9
|
| restoreAppSettings |
restoreAppSettings()
|
|
Defined in src/app/services/app-settings.service.ts:18
|
|
Restore app settings from the local storage
Returns :
void
|
| setAppSettings | ||||||||
setAppSettings(appSettings: Partial<AppSettings>)
|
||||||||
|
Defined in src/app/services/app-settings.service.ts:48
|
||||||||
|
Function used in the app to update global settings object
Parameters :
Returns :
void
|
| storeAppSettings |
storeAppSettings()
|
|
Defined in src/app/services/app-settings.service.ts:39
|
|
Save app settings to the local storage
Returns :
void
|
import { BehaviorSubject } from 'rxjs'
import { AppSettings, AppThemes } from '../models/AppSettings'
export class AppSettingsService {
public defaultSettings: AppSettings = {
persistSelectedTheme: true,
selectedTheme: AppThemes.light
}
public settings = new BehaviorSubject<AppSettings>(this.defaultSettings)
constructor() {
this.restoreAppSettings()
}
/**
* Restore app settings from the local storage
*/
restoreAppSettings() {
try {
const serializedSettings = localStorage.getItem('app-settings')
if (serializedSettings) {
const settings = JSON.parse(serializedSettings)
this.setAppSettings(settings)
} else {
console.info(
'No app settings stored in the localStorage, we will set to default values.'
)
}
} catch (err) {
console.warn('Error restoring settings from local storgae.', err)
}
}
/**
* Save app settings to the local storage
*/
storeAppSettings() {
localStorage.setItem('app-settings', JSON.stringify(this.settings.value))
}
/**
* Function used in the app to update global settings object
*
* @param appSettings contains properties that should be updated in settings
*/
setAppSettings(appSettings: Partial<AppSettings>) {
this.settings.next({
...this.settings.value,
...appSettings
})
this.storeAppSettings()
}
}