import { ServerService } from '@app/core/server'
import { environment } from '../../../environments/environment'
import { PluginService } from '@app/core/plugins/plugin.service'
-import { ServerConfigTheme } from '@shared/models'
-import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
+import { ServerConfig, ServerConfigTheme } from '@shared/models'
+import { first } from 'rxjs/operators'
+import { User } from '@app/shared/users/user.model'
+import { UserService } from '@app/shared/users/user.service'
+import { LocalStorageService } from '@app/shared/misc/storage.service'
@Injectable()
export class ThemeService {
- private static KEYS = {
- LAST_ACTIVE_THEME: 'last_active_theme'
- }
-
private oldThemeName: string
private themes: ServerConfigTheme[] = []
private themeFromLocalStorage: ServerConfigTheme
private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
+ private serverConfig: ServerConfig
+
constructor (
private auth: AuthService,
+ private userService: UserService,
private pluginService: PluginService,
- private server: ServerService
+ private server: ServerService,
+ private localStorageService: LocalStorageService
) {}
initialize () {
// Try to load from local storage first, so we don't have to wait network requests
this.loadAndSetFromLocalStorage()
- this.server.configLoaded
- .subscribe(() => {
- const themes = this.server.getConfig().theme.registered
+ this.serverConfig = this.server.getTmpConfig()
+ this.server.getConfig()
+ .subscribe(config => {
+ this.serverConfig = config
+
+ const themes = this.serverConfig.theme.registered
this.removeThemeFromLocalStorageIfNeeded(themes)
this.injectThemes(themes)
private getCurrentTheme () {
if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name
- if (this.auth.isLoggedIn()) {
- const theme = this.auth.getUser().theme
- if (theme !== 'instance-default') return theme
- }
+ const theme = this.auth.isLoggedIn()
+ ? this.auth.getUser().theme
+ : this.userService.getAnonymousUser().theme
- return this.server.getConfig().theme.default
+ if (theme !== 'instance-default') return theme
+ return this.serverConfig.theme.default
}
private loadTheme (name: string) {
this.pluginService.reloadLoadedScopes()
- peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme))
+ this.localStorageService.setItem(User.KEYS.THEME, JSON.stringify(theme), false)
} else {
- peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
+ this.localStorageService.removeItem(User.KEYS.THEME, false)
}
this.oldThemeName = currentTheme
if (!this.auth.isLoggedIn()) {
this.updateCurrentTheme()
+
+ this.localStorageService.watch([User.KEYS.THEME]).subscribe(
+ () => this.updateCurrentTheme()
+ )
}
this.auth.userInformationLoaded
+ .pipe(first())
.subscribe(() => this.updateCurrentTheme())
}
private loadAndSetFromLocalStorage () {
- const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME)
+ const lastActiveThemeString = this.localStorageService.getItem(User.KEYS.THEME)
if (!lastActiveThemeString) return
try {