Skip to content

Commit e43d28a

Browse files
authored
Merge pull request #502 from Klath123/fix/heatmap-race-conditon
Refactor ThemeService and fix duplicate Material theme generation
2 parents aa4aa5d + 5211d17 commit e43d28a

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/app/pages/circular-heatmap/circular-heatmap.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ export class CircularHeatmapComponent implements OnInit, OnDestroy {
7676
}
7777

7878
ngOnInit(): void {
79-
const savedTheme: string = this.themeService.getTheme() || 'light';
80-
this.themeService.setTheme(savedTheme); // sets .light-theme or .dark-theme
8179
requestAnimationFrame(() => {
8280
// Now the DOM has the correct class and CSS vars are live
8381
console.log(`${perfNow()}s: ngOnInit: Initial theme:`, this.theme);

src/app/service/theme.service.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
import { Injectable } from '@angular/core';
22
import { BehaviorSubject } from 'rxjs';
33

4+
export type AppTheme = 'light' | 'dark';
5+
46
@Injectable({ providedIn: 'root' })
57
export class ThemeService {
6-
private themeSubject = new BehaviorSubject<string>('light');
8+
private readonly STORAGE_KEY = 'theme';
9+
private readonly defaultTheme: AppTheme = 'light';
10+
11+
private themeSubject = new BehaviorSubject<AppTheme>(this.defaultTheme);
712
public readonly theme$ = this.themeSubject.asObservable();
813

914
constructor() {}
1015

1116
initTheme(): void {
12-
const saved = localStorage.getItem('theme') || 'light';
13-
this.setTheme(saved);
17+
const stored = localStorage.getItem(this.STORAGE_KEY);
18+
const theme: AppTheme = stored === 'dark' ? 'dark' : this.defaultTheme;
19+
this.setTheme(theme);
20+
}
21+
22+
setTheme(theme: AppTheme): void {
23+
if (this.themeSubject.value === theme) return;
24+
this.applyTheme(theme);
1425
}
1526

16-
setTheme(theme: string): void {
27+
private applyTheme(theme: AppTheme): void {
1728
document.body.classList.remove('light-theme', 'dark-theme');
1829
document.body.classList.add(`${theme}-theme`);
19-
localStorage.setItem('theme', theme);
30+
31+
localStorage.setItem(this.STORAGE_KEY, theme);
2032
this.themeSubject.next(theme);
2133
}
2234

23-
getTheme(): string {
35+
getTheme(): AppTheme {
2436
return this.themeSubject.value;
2537
}
38+
39+
toggleTheme(): void {
40+
this.setTheme(this.themeSubject.value === 'light' ? 'dark' : 'light');
41+
}
2642
}

src/custom-theme.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ body.dark-theme {
205205
}
206206
}
207207

208-
@include mat.all-component-themes($DSOMM-dark-theme);
209208

210209
.button-container {
211210
display: flex;

0 commit comments

Comments
 (0)