preferences.service.ts 1.11 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

Lukas Burgey's avatar
Lukas Burgey committed
4
5
import { IdP } from '../types/types.module';

Lukas Burgey's avatar
Lukas Burgey committed
6
7
8
9
export const prefsKey = "feudalPrefs";

export interface Prefs {
  showEmptyVOs?: boolean;
Lukas Burgey's avatar
Lukas Burgey committed
10
  preferredIdP?: IdP;
Lukas Burgey's avatar
Lukas Burgey committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
};

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

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

  constructor() {
    this.load();
  }

  public save(prefs: Prefs): void {
    localStorage.setItem(prefsKey, JSON.stringify(prefs));
Lukas Burgey's avatar
Lukas Burgey committed
27
    console.log("Saved preferences:", prefs);
Lukas Burgey's avatar
Lukas Burgey committed
28
29
30
  }

  public load(): void {
Lukas Burgey's avatar
Lukas Burgey committed
31
32
33
34
35
36
37
    const loaded = JSON.parse(localStorage.getItem(prefsKey));
    if (loaded != null) {
      this.prefs = loaded;
      this.prefs$.next(loaded);
      console.log("Loaded preferences:", this.prefs);
    } else {
      console.log("No preferences to load");
Lukas Burgey's avatar
Lukas Burgey committed
38
    }
Lukas Burgey's avatar
Lukas Burgey committed
39
40
41
42
43
44
  }

  public setPreferredIdP(idp: IdP): void {
    this.prefs.preferredIdP = idp;
    this.save(this.prefs);
    this.prefs$.next(this.prefs);
Lukas Burgey's avatar
Lukas Burgey committed
45
46
47
48
49
50
  }

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