Fix storeState overrides user theme

This commit is contained in:
Wendelin 2025-02-13 09:51:39 +01:00
parent 4f9ca3b173
commit cd1ca72e45
No known key found for this signature in database
4 changed files with 28 additions and 22 deletions

View File

@ -12,12 +12,11 @@ import "../../components/ha-svg-icon";
import "../../layouts/hass-tabs-subpage"; import "../../layouts/hass-tabs-subpage";
import { profileSections } from "./ha-panel-profile"; import { profileSections } from "./ha-panel-profile";
import { isExternal } from "../../data/external"; import { isExternal } from "../../data/external";
import { SELECTED_THEME_KEY } from "../../data/ws-themes";
import type { CoreFrontendUserData } from "../../data/frontend"; import type { CoreFrontendUserData } from "../../data/frontend";
import { getOptimisticFrontendUserDataCollection } from "../../data/frontend"; import { getOptimisticFrontendUserDataCollection } from "../../data/frontend";
import { showConfirmationDialog } from "../../dialogs/generic/show-dialog-box"; import { showConfirmationDialog } from "../../dialogs/generic/show-dialog-box";
import { haStyle } from "../../resources/styles"; import { haStyle } from "../../resources/styles";
import type { HomeAssistant, Route, ThemeSettings } from "../../types"; import type { HomeAssistant, Route } from "../../types";
import "./ha-advanced-mode-row"; import "./ha-advanced-mode-row";
import "./ha-enable-shortcuts-row"; import "./ha-enable-shortcuts-row";
import "./ha-force-narrow-row"; import "./ha-force-narrow-row";
@ -32,7 +31,6 @@ import "./ha-pick-time-zone-row";
import "./ha-push-notifications-row"; import "./ha-push-notifications-row";
import "./ha-set-suspend-row"; import "./ha-set-suspend-row";
import "./ha-set-vibrate-row"; import "./ha-set-vibrate-row";
import { storage } from "../../common/decorators/storage";
import type { HaSwitch } from "../../components/ha-switch"; import type { HaSwitch } from "../../components/ha-switch";
@customElement("ha-profile-section-general") @customElement("ha-profile-section-general")
@ -45,13 +43,6 @@ class HaProfileSectionGeneral extends LitElement {
@property({ attribute: false }) public route!: Route; @property({ attribute: false }) public route!: Route;
@storage({
key: SELECTED_THEME_KEY,
state: true,
subscribe: true,
})
private _browserThemeSettings?: ThemeSettings;
@state() private _browserThemeActivated = false; @state() private _browserThemeActivated = false;
private _unsubCoreData?: UnsubscribeFunc; private _unsubCoreData?: UnsubscribeFunc;
@ -144,7 +135,7 @@ class HaProfileSectionGeneral extends LitElement {
.narrow=${this.narrow} .narrow=${this.narrow}
.hass=${this.hass} .hass=${this.hass}
></ha-pick-first-weekday-row> ></ha-pick-first-weekday-row>
${this._browserThemeSettings || this._browserThemeActivated ${this.hass.browserThemeEnabled || this._browserThemeActivated
? html` ? html`
<ha-settings-row .narrow=${this.narrow}> <ha-settings-row .narrow=${this.narrow}>
<span slot="heading"> <span slot="heading">
@ -198,12 +189,12 @@ class HaProfileSectionGeneral extends LitElement {
)} )}
</span> </span>
<ha-switch <ha-switch
.checked=${!!this._browserThemeSettings || .checked=${this.hass.browserThemeEnabled ||
this._browserThemeActivated} this._browserThemeActivated}
@change=${this._toggleBrowserTheme} @change=${this._toggleBrowserTheme}
></ha-switch> ></ha-switch>
</ha-settings-row> </ha-settings-row>
${this._browserThemeSettings || this._browserThemeActivated ${this.hass.browserThemeEnabled || this._browserThemeActivated
? html` ? html`
<ha-expansion-panel <ha-expansion-panel
outlined outlined
@ -298,7 +289,7 @@ class HaProfileSectionGeneral extends LitElement {
const enabled = switchElement.checked; const enabled = switchElement.checked;
if (!enabled) { if (!enabled) {
if (!this._browserThemeSettings && this._browserThemeActivated) { if (!this.hass.browserThemeEnabled && this._browserThemeActivated) {
// no changed have made, disable without confirmation // no changed have made, disable without confirmation
this._browserThemeActivated = false; this._browserThemeActivated = false;
} else { } else {
@ -315,7 +306,6 @@ class HaProfileSectionGeneral extends LitElement {
if (confirm) { if (confirm) {
this._browserThemeActivated = false; this._browserThemeActivated = false;
this._browserThemeSettings = undefined;
fireEvent(this, "resetBrowserTheme"); fireEvent(this, "resetBrowserTheme");
} else { } else {
// revert switch // revert switch

View File

@ -51,6 +51,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
}; };
this._updateHass({ this._updateHass({
selectedTheme, selectedTheme,
browserThemeEnabled: ev.detail.storageLocation === "browser",
}); });
this._applyTheme(mql.matches); this._applyTheme(mql.matches);
@ -67,6 +68,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
const selectedTheme = await fetchSelectedTheme(this.hass!); const selectedTheme = await fetchSelectedTheme(this.hass!);
this._updateHass({ this._updateHass({
selectedTheme, selectedTheme,
browserThemeEnabled: false,
}); });
this._applyTheme(mql.matches); this._applyTheme(mql.matches);
}); });
@ -108,7 +110,10 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
selectedTheme selectedTheme
) { ) {
this._themeApplied = true; this._themeApplied = true;
this._updateHass({ selectedTheme }); this._updateHass({
selectedTheme,
browserThemeEnabled: false,
});
this._applyTheme(mql.matches); this._applyTheme(mql.matches);
} }
} }

View File

@ -223,6 +223,7 @@ export interface HomeAssistant {
config: HassConfig; config: HassConfig;
themes: Themes; themes: Themes;
selectedTheme: ThemeSettings | null; selectedTheme: ThemeSettings | null;
browserThemeEnabled?: boolean;
panels: Panels; panels: Panels;
panelUrl: string; panelUrl: string;
// i18n // i18n

View File

@ -1,8 +1,9 @@
import { SELECTED_THEME_KEY } from "../data/ws-themes";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
const STORED_STATE = [ const STORED_STATE = [
"dockedSidebar", "dockedSidebar",
"selectedTheme", SELECTED_THEME_KEY,
"selectedLanguage", "selectedLanguage",
"vibrate", "vibrate",
"debugConnection", "debugConnection",
@ -11,11 +12,17 @@ const STORED_STATE = [
"defaultPanel", "defaultPanel",
]; ];
const CLEARABLE_STATE = ["selectedTheme"]; const CLEARABLE_STATE = [SELECTED_THEME_KEY];
export function storeState(hass: HomeAssistant) { export function storeState(hass: HomeAssistant) {
try { try {
STORED_STATE.forEach((key) => { const states = [...STORED_STATE];
if (!hass.browserThemeEnabled) {
states.splice(states.indexOf(SELECTED_THEME_KEY), 1);
}
states.forEach((key) => {
const value = hass[key]; const value = hass[key];
window.localStorage.setItem( window.localStorage.setItem(
key, key,
@ -34,15 +41,18 @@ export function storeState(hass: HomeAssistant) {
} }
export function getState() { export function getState() {
const state = {}; const state: Partial<HomeAssistant> = {};
STORED_STATE.forEach((key) => { STORED_STATE.forEach((key) => {
const storageItem = window.localStorage.getItem(key); const storageItem = window.localStorage.getItem(key);
if (storageItem !== null) { if (storageItem !== null) {
let value = JSON.parse(storageItem); let value = JSON.parse(storageItem);
// selectedTheme went from string to object on 20200718 // selectedTheme went from string to object on 20200718
if (key === "selectedTheme" && typeof value === "string") { if (key === SELECTED_THEME_KEY) {
value = { theme: value }; if (typeof value === "string") {
value = { theme: value };
}
state.browserThemeEnabled = true;
} }
// dockedSidebar went from boolean to enum on 20190720 // dockedSidebar went from boolean to enum on 20190720
if (key === "dockedSidebar" && typeof value === "boolean") { if (key === "dockedSidebar" && typeof value === "boolean") {