particles setting

This commit is contained in:
Ben Wilson
2023-07-24 22:20:55 -04:00
parent 1cfb2dbb25
commit 898141a8fe
13 changed files with 140 additions and 10 deletions
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { LocalStorageService } from './local-storage.service';
describe('LocalStorageService', () => {
let service: LocalStorageService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(LocalStorageService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+44
View File
@@ -0,0 +1,44 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, shareReplay } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LocalStorageService {
private PARTICLES = 'PARTICLES';
private _particles$: BehaviorSubject<boolean>;
public particles$: Observable<boolean>;
constructor() {
this._particles$ = new BehaviorSubject<boolean>(this.getParticles());
this.particles$ = this._particles$.asObservable().pipe(shareReplay({ refCount: true, bufferSize: 1 }));
}
private get(key: string): string | null {
return localStorage.getItem(key);
}
private set(key: string, value: string) {
localStorage.setItem(key, value);
}
private remove(key: string): void {
localStorage.removeItem(key);
}
public getParticles(): boolean {
const result = this.get(this.PARTICLES);
return result == null || JSON.parse(result)?.particles === true;
}
public setParticles(particles: boolean) {
this.set(this.PARTICLES, JSON.stringify({ particles }));
this._particles$.next(particles);
}
}