Skip to content

Commit

Permalink
refactor: combine handler for same event
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Feb 16, 2024
1 parent 31bb832 commit 1396964
Showing 1 changed file with 44 additions and 51 deletions.
95 changes: 44 additions & 51 deletions packages/ui-services/src/Theme/ThemeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
StorageValueModes,
FeatureStatus,
PreferenceServiceInterface,
PreferencesServiceEvent,
ComponentManagerInterface,
} from '@standardnotes/services'
import { NativeFeatureIdentifier, FindNativeTheme, ThemeFeatureDescription } from '@standardnotes/features'
Expand Down Expand Up @@ -75,56 +74,6 @@ export class ThemeManager extends AbstractUIService {
}),
)
}

this.eventDisposers.push(
this.preferences.addEventObserver(async (event) => {
if (event !== PreferencesServiceEvent.LocalPreferencesChanged) {
return
}

this.toggleTranslucentUIColors()

let hasChange = false

const { features, uuids } = this.components.getActiveThemesIdentifiers()

const featuresList = new ActiveThemeList(this.application.items, features)
const uuidsList = new ActiveThemeList(this.application.items, uuids)

for (const active of this.themesActiveInTheUI.getList()) {
if (!featuresList.has(active) && !uuidsList.has(active)) {
this.deactivateThemeInTheUI(active)
hasChange = true
}
}

for (const feature of features) {
if (!this.themesActiveInTheUI.has(feature)) {
const theme = FindNativeTheme(feature.value)
if (theme) {
const uiFeature = new UIFeature<ThemeFeatureDescription>(theme)
this.activateTheme(uiFeature)
hasChange = true
}
}
}

for (const uuid of uuids) {
if (!this.themesActiveInTheUI.has(uuid)) {
const theme = this.application.items.findItem<ComponentInterface>(uuid.value)
if (theme) {
const uiFeature = new UIFeature<ThemeFeatureDescription>(theme)
this.activateTheme(uiFeature)
hasChange = true
}
}
}

if (hasChange) {
this.cacheThemeState().catch(console.error)
}
}),
)
}

override async onAppEvent(event: ApplicationEvent) {
Expand Down Expand Up @@ -170,7 +119,51 @@ export class ThemeManager extends AbstractUIService {
}
}

private handleThemeStateChange() {
let hasChange = false

const { features, uuids } = this.components.getActiveThemesIdentifiers()

const featuresList = new ActiveThemeList(this.application.items, features)
const uuidsList = new ActiveThemeList(this.application.items, uuids)

for (const active of this.themesActiveInTheUI.getList()) {
if (!featuresList.has(active) && !uuidsList.has(active)) {
this.deactivateThemeInTheUI(active)
hasChange = true
}
}

for (const feature of features) {
if (!this.themesActiveInTheUI.has(feature)) {
const theme = FindNativeTheme(feature.value)
if (theme) {
const uiFeature = new UIFeature<ThemeFeatureDescription>(theme)
this.activateTheme(uiFeature)
hasChange = true
}
}
}

for (const uuid of uuids) {
if (!this.themesActiveInTheUI.has(uuid)) {
const theme = this.application.items.findItem<ComponentInterface>(uuid.value)
if (theme) {
const uiFeature = new UIFeature<ThemeFeatureDescription>(theme)
this.activateTheme(uiFeature)
hasChange = true
}
}
}

if (hasChange) {
this.cacheThemeState().catch(console.error)
}
}

private async handlePreferencesChangeEvent() {
this.handleThemeStateChange()

this.toggleTranslucentUIColors()

const useDeviceThemeSettings = this.application.getLocalPreference(LocalPrefKey.UseSystemColorScheme, false)
Expand Down

0 comments on commit 1396964

Please sign in to comment.