|
1 | 1 | import { DOCUMENT } from '@angular/common';
|
2 |
| -import { Inject, Injectable } from '@angular/core'; |
| 2 | +import { Inject, Injectable, OnDestroy } from '@angular/core'; |
3 | 3 | import { getItem, setItem, StorageItem } from '@app/@core/utils';
|
4 |
| -import { BehaviorSubject } from 'rxjs'; |
5 |
| -import { ThemeList } from './theme.config'; |
| 4 | +import { fromEventPattern, Subject } from 'rxjs'; |
| 5 | +import { takeUntil } from 'rxjs/operators'; |
| 6 | +import { defaultBaseTheme, ThemeList } from './theme.config'; |
6 | 7 |
|
7 | 8 | @Injectable({
|
8 | 9 | providedIn: 'root',
|
9 | 10 | })
|
10 |
| -export class ThemeService { |
11 |
| - private currentTheme$ = new BehaviorSubject<ThemeList>( |
12 |
| - this.currentTheme || ThemeList.System, |
| 11 | +export class ThemeService implements OnDestroy { |
| 12 | + destroy$ = new Subject(); |
| 13 | + |
| 14 | + private readonly mediaQuery = window.matchMedia( |
| 15 | + '(prefers-color-scheme: dark)', |
13 | 16 | );
|
14 | 17 |
|
15 | 18 | constructor(@Inject(DOCUMENT) private document: Document) {}
|
16 | 19 |
|
17 |
| - get currentTheme(): ThemeList { |
| 20 | + get systemTheme(): ThemeList.Light | ThemeList.Dark { |
| 21 | + return this.mediaQuery.matches ? ThemeList.Dark : ThemeList.Light; |
| 22 | + } |
| 23 | + |
| 24 | + get currentAppTheme(): ThemeList { |
18 | 25 | return getItem(StorageItem.Theme) as ThemeList;
|
19 | 26 | }
|
20 | 27 |
|
| 28 | + /** |
| 29 | + * Makes initial check for system preferences and attach mediaQuery listener |
| 30 | + * |
| 31 | + */ |
21 | 32 | init(): void {
|
22 |
| - if (!this.currentTheme) { |
23 |
| - this.listenForMediaQuery(); |
| 33 | + if (this.currentAppTheme) { |
| 34 | + this.setTheme(this.currentAppTheme); |
| 35 | + } else if (this.currentAppTheme === ThemeList.System) { |
| 36 | + this.setTheme(this.systemTheme); |
| 37 | + } else { |
| 38 | + this.setTheme(defaultBaseTheme); |
24 | 39 | }
|
25 | 40 |
|
26 |
| - this.listenForThemeChanges(); |
| 41 | + this.listenForMediaQueryChanges(); |
27 | 42 | }
|
28 | 43 |
|
29 |
| - changeTheme(theme: ThemeList): void { |
30 |
| - this.currentTheme$.next(theme); |
31 |
| - } |
32 |
| - |
33 |
| - private listenForMediaQuery(): void { |
34 |
| - const colorScheme = window.matchMedia('(prefers-color-scheme: dark)'); |
35 |
| - |
36 |
| - colorScheme.addEventListener('change', (e: MediaQueryListEvent) => { |
37 |
| - const currentTheme = this.currentTheme; |
| 44 | + /** |
| 45 | + * Manually changes theme in BehaviorSubject, LocalStorage & HTML element |
| 46 | + * |
| 47 | + * @param theme new theme |
| 48 | + */ |
| 49 | + setTheme(theme: ThemeList): void { |
| 50 | + this.clearThemes(); |
| 51 | + setItem(StorageItem.Theme, theme); |
38 | 52 |
|
39 |
| - if (currentTheme === ThemeList.System) { |
40 |
| - const theme = e.matches ? ThemeList.Dark : ThemeList.Light; |
41 |
| - this.setTheme(theme); |
42 |
| - } |
43 |
| - }); |
44 |
| - } |
| 53 | + let bodyClass = theme; |
45 | 54 |
|
46 |
| - private listenForThemeChanges(): void { |
47 |
| - this.currentTheme$.subscribe((theme) => { |
48 |
| - this.setTheme(theme); |
49 |
| - }); |
| 55 | + if (theme === ThemeList.System) { |
| 56 | + bodyClass = this.systemTheme; |
| 57 | + } |
| 58 | + this.document.body.classList.add(bodyClass); |
50 | 59 | }
|
51 | 60 |
|
52 |
| - private setTheme(theme: ThemeList): void { |
53 |
| - this.clearThemes(); |
54 |
| - this.document.body.classList.add(theme); |
55 |
| - setItem(StorageItem.Theme, theme); |
| 61 | + /** |
| 62 | + * Handles system color preference changes |
| 63 | + * |
| 64 | + */ |
| 65 | + private listenForMediaQueryChanges(): void { |
| 66 | + fromEventPattern<MediaQueryListEvent>( |
| 67 | + this.mediaQuery.addListener.bind(this.mediaQuery), |
| 68 | + this.mediaQuery.removeListener.bind(this.mediaQuery), |
| 69 | + ) |
| 70 | + .pipe(takeUntil(this.destroy$)) |
| 71 | + .subscribe(() => { |
| 72 | + // Only applies changes when the current theme is "system" |
| 73 | + if (this.currentAppTheme === ThemeList.System) { |
| 74 | + this.setTheme(ThemeList.System); |
| 75 | + } |
| 76 | + }); |
56 | 77 | }
|
57 | 78 |
|
| 79 | + /** |
| 80 | + * Clears all themes in ThemeList enum from the HTML element |
| 81 | + * |
| 82 | + */ |
58 | 83 | private clearThemes(): void {
|
59 | 84 | for (const theme in ThemeList) {
|
60 | 85 | const key: ThemeList = ThemeList[theme as keyof typeof ThemeList];
|
61 | 86 | this.document.body.classList.remove(key);
|
62 | 87 | }
|
63 | 88 | }
|
| 89 | + |
| 90 | + ngOnDestroy(): void { |
| 91 | + this.destroy$.complete(); |
| 92 | + this.destroy$.unsubscribe(); |
| 93 | + } |
64 | 94 | }
|
0 commit comments