2020-10-27 02:58:29 +07:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-12-13 11:56:24 +09:00
|
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
2020-10-27 02:58:29 +07:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class StorageService {
|
2021-12-13 11:56:24 +09:00
|
|
|
constructor(private router: Router, private route: ActivatedRoute) {
|
2022-01-06 19:59:33 +09:00
|
|
|
this.setDefaultValueIfNeeded('graphWindowPreference', '2h');
|
|
|
|
this.setDefaultValueIfNeeded('poolsWindowPreference', '1d');
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultValueIfNeeded(key: string, defaultValue: string) {
|
|
|
|
let graphWindowPreference: string = this.getValue(key);
|
2021-12-13 11:56:24 +09:00
|
|
|
if (graphWindowPreference === null) { // First visit to mempool.space
|
2022-01-06 19:59:33 +09:00
|
|
|
if (this.router.url.includes("graphs") || this.router.url.includes("pools")) {
|
|
|
|
this.setValue(key, this.route.snapshot.fragment ? this.route.snapshot.fragment : defaultValue);
|
2021-12-13 11:56:24 +09:00
|
|
|
} else {
|
2022-01-06 19:59:33 +09:00
|
|
|
this.setValue(key, defaultValue);
|
2021-12-13 11:56:24 +09:00
|
|
|
}
|
2022-01-06 19:59:33 +09:00
|
|
|
} else if (this.router.url.includes("graphs") || this.router.url.includes("pools")) { // Visit a different graphs#fragment from last visit
|
|
|
|
if (this.route.snapshot.fragment !== null && graphWindowPreference !== this.route.snapshot.fragment) {
|
|
|
|
this.setValue(key, this.route.snapshot.fragment);
|
2021-12-13 11:56:24 +09:00
|
|
|
}
|
2022-01-06 19:59:33 +09:00
|
|
|
}
|
2021-12-13 11:56:24 +09:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:58:29 +07:00
|
|
|
getValue(key: string): string {
|
|
|
|
try {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2020-11-16 19:27:06 +07:00
|
|
|
return '';
|
2020-10-27 02:58:29 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(key: string, value: any): void {
|
|
|
|
try {
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|