33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
|
import {Inject, Injectable, PLATFORM_ID} from "@angular/core";
|
||
|
import {BehaviorSubject, Observable} from "rxjs";
|
||
|
import {isPlatformBrowser} from "@angular/common";
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: "root"
|
||
|
})
|
||
|
export class LocalStorageService {
|
||
|
|
||
|
private communityDirectLink: BehaviorSubject<boolean>;
|
||
|
|
||
|
constructor(@Inject(PLATFORM_ID) private platformId: string) {
|
||
|
if(isPlatformBrowser(this.platformId)) {
|
||
|
let item = localStorage.getItem('communityDirectLink');
|
||
|
if(item !== null) {
|
||
|
this.communityDirectLink = new BehaviorSubject<boolean>(item == 'true');
|
||
|
} else {
|
||
|
this.communityDirectLink = new BehaviorSubject<boolean>(false);
|
||
|
}
|
||
|
} else this.communityDirectLink = new BehaviorSubject<boolean>(true);
|
||
|
}
|
||
|
|
||
|
public setCommunityDirectLink(value: string) {
|
||
|
localStorage.setItem('communityDirectLink', value);
|
||
|
this.communityDirectLink.next(Boolean(value).valueOf());
|
||
|
}
|
||
|
|
||
|
public get(): Observable<boolean> {
|
||
|
console.log(this.communityDirectLink.getValue());
|
||
|
return this.communityDirectLink.asObservable();
|
||
|
}
|
||
|
}
|