preferences.service.ts 855 Bytes
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

export const prefsKey = "feudalPrefs";

export interface Prefs {
  showEmptyVOs?: boolean;
};

@Injectable({
  providedIn: 'root'
})
export class PreferencesService {

  private prefs: Prefs = {};
  private prefs$ = new BehaviorSubject<Prefs>(this.prefs);

  constructor() {
    this.load();
  }

  public save(prefs: Prefs): void {
    console.log("Saved prefs:", prefs);
    localStorage.setItem(prefsKey, JSON.stringify(prefs));
  }

  public load(): void {
    this.prefs = JSON.parse(localStorage.getItem(prefsKey));
    if (this.prefs != null &&  this.prefs != undefined) {
      this.prefs$.next(this.prefs);
    }
    console.log("Loaded prefs:", this.prefs);
  }

  public connect(): Observable<Prefs> {
    return this.prefs$.asObservable();
  }
}