2023-01-02 12:26:10 -06:00
|
|
|
import { Injectable } from '@angular/core';
|
2024-04-05 18:36:32 +09:00
|
|
|
import { Subject } from 'rxjs';
|
2023-01-02 12:26:10 -06:00
|
|
|
import { defaultMempoolFeeColors, contrastMempoolFeeColors } from '../app.constants';
|
|
|
|
import { StorageService } from './storage.service';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ThemeService {
|
|
|
|
style: HTMLLinkElement;
|
|
|
|
theme: string = 'default';
|
|
|
|
themeChanged$: Subject<string> = new Subject();
|
|
|
|
mempoolFeeColors: string[] = defaultMempoolFeeColors;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private storageService: StorageService,
|
|
|
|
) {
|
|
|
|
const theme = this.storageService.getValue('theme-preference') || 'default';
|
|
|
|
this.apply(theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(theme) {
|
|
|
|
this.theme = theme;
|
|
|
|
if (theme !== 'default') {
|
2024-04-09 16:15:05 +09:00
|
|
|
theme === 'contrast' ? this.mempoolFeeColors = contrastMempoolFeeColors : this.mempoolFeeColors = defaultMempoolFeeColors;
|
2023-01-03 05:24:14 -06:00
|
|
|
try {
|
|
|
|
if (!this.style) {
|
|
|
|
this.style = document.createElement('link');
|
|
|
|
this.style.rel = 'stylesheet';
|
|
|
|
this.style.href = `${theme}.css`;
|
|
|
|
document.head.appendChild(this.style);
|
|
|
|
} else {
|
|
|
|
this.style.href = `${theme}.css`;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log('failed to apply theme stylesheet: ', err);
|
2023-01-02 12:26:10 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.mempoolFeeColors = defaultMempoolFeeColors;
|
|
|
|
if (this.style) {
|
|
|
|
this.style.remove();
|
|
|
|
this.style = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.storageService.setValue('theme-preference', theme);
|
|
|
|
this.themeChanged$.next(this.theme);
|
|
|
|
}
|
|
|
|
}
|