This repository was archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#228): move theme switcher to root
- Loading branch information
1 parent
22d4d30
commit 36981aa
Showing
8 changed files
with
163 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
remote/src/app/modals/app-remote-dark-mode-switch/app-remote-dark-mode-switch.scss
This file was deleted.
Oops, something went wrong.
67 changes: 0 additions & 67 deletions
67
remote/src/app/modals/app-remote-dark-mode-switch/app-remote-dark-mode-switch.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
div.social-links { | ||
padding-top: 32px; | ||
padding-bottom: 32px; | ||
|
||
a { | ||
font-size: 3rem; | ||
|
||
&:not(last-child) { | ||
padding-right: 8px; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {Component, h, State} from '@stencil/core'; | ||
|
||
import {take} from 'rxjs/operators'; | ||
|
||
import {ThemeService} from '../../services/theme/theme.service'; | ||
|
||
@Component({ | ||
tag: 'app-settings', | ||
styleUrl: 'app-settings.scss' | ||
}) | ||
export class AppSettings { | ||
|
||
private themeService: ThemeService; | ||
|
||
@State() | ||
private darkTheme: boolean; | ||
|
||
constructor() { | ||
this.themeService = ThemeService.getInstance(); | ||
} | ||
|
||
componentWillLoad() { | ||
this.themeService.watch().pipe(take(1)).subscribe((dark: boolean) => { | ||
this.darkTheme = dark; | ||
}); | ||
} | ||
|
||
async toggleTheme() { | ||
this.darkTheme = !this.darkTheme; | ||
await this.themeService.switch(this.darkTheme); | ||
} | ||
|
||
render() { | ||
return [ | ||
<ion-header> | ||
<ion-toolbar color="primary"> | ||
<ion-buttons slot="start"> | ||
<ion-menu-toggle> | ||
<ion-button> | ||
<ion-icon slot="icon-only" name="menu"></ion-icon> | ||
</ion-button> | ||
</ion-menu-toggle> | ||
</ion-buttons> | ||
|
||
<ion-title class="ion-text-uppercase">DeckDeckGo</ion-title> | ||
</ion-toolbar> | ||
</ion-header>, | ||
|
||
<ion-content class="ion-padding"> | ||
<ion-toggle checked={this.darkTheme} mode="md" color="switcher" onIonChange={() => this.toggleTheme()}></ion-toggle> | ||
</ion-content> | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {Observable, Subject} from 'rxjs'; | ||
|
||
import {get, set} from 'idb-keyval'; | ||
|
||
export class ThemeService { | ||
|
||
private static instance: ThemeService; | ||
|
||
private darkTheme: Subject<boolean> = new Subject<boolean>(); | ||
|
||
private constructor() { | ||
// Private constructor, singleton | ||
} | ||
|
||
static getInstance() { | ||
if (!ThemeService.instance) { | ||
ThemeService.instance = new ThemeService(); | ||
} | ||
return ThemeService.instance; | ||
} | ||
|
||
watch(): Observable<boolean> { | ||
return this.darkTheme.asObservable(); | ||
} | ||
|
||
async switch(dark: boolean) { | ||
this.darkTheme.next(dark); | ||
|
||
try { | ||
await set('deckdeckgo_dark_mode', dark); | ||
} catch (err) { | ||
// We ignore this error. In worst case scenario, the application will be displayed in another theme after next refresh. | ||
} | ||
} | ||
|
||
async initDarkModePreference(): Promise<void> { | ||
try { | ||
const savedDarkModePreference: boolean = await get('deckdeckgo_dark_mode'); | ||
|
||
// If user already specified once a preference, we use that as default | ||
if (savedDarkModePreference !== undefined) { | ||
this.switch(savedDarkModePreference); | ||
return; | ||
} | ||
} catch (err) { | ||
this.switch(false); | ||
return; | ||
} | ||
|
||
// Otherwise we check the prefers-color-scheme of the OS | ||
const darkModePreferenceFromMedia: MediaQueryList = window.matchMedia('(prefers-color-scheme: dark)'); | ||
|
||
this.switch(darkModePreferenceFromMedia.matches); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters