-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First stab at adding a frontend service subscribing to settings changes
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { listen, invoke } from '$lib/backend/ipc'; | ||
import { plainToInstance } from 'class-transformer'; | ||
import { writable } from 'svelte/store'; | ||
|
||
export class SettingsService { | ||
readonly settings = writable<AppSettings | undefined>(undefined, () => { | ||
this.refresh(); | ||
const unsubscribe = this.subscribe(async (settings) => await this.handlePayload(settings)); | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}); | ||
|
||
private async handlePayload(settings: AppSettings) { | ||
// TODO: Remove this log | ||
console.log(settings); | ||
this.settings.set(settings); | ||
} | ||
|
||
private async refresh() { | ||
const response = await invoke<any>('get_app_settings'); | ||
const settings = plainToInstance(AppSettings, response); | ||
this.handlePayload(settings); | ||
} | ||
|
||
private subscribe(callback: (settings: AppSettings) => void) { | ||
return listen<any>(`settings://update`, (event) => | ||
callback(plainToInstance(AppSettings, event.payload)) | ||
); | ||
} | ||
|
||
public async updateOnboardingComplete(update: boolean) { | ||
await invoke('update_onboarding_complete', { update }); | ||
} | ||
|
||
public async updateTelemetry(update: TelemetryUpdate) { | ||
await invoke('update_telemetry', { update }); | ||
} | ||
} | ||
|
||
export class AppSettings { | ||
/** Whether the user has passed the onboarding flow. */ | ||
onboardingComplete!: boolean; | ||
/** Telemetry settings */ | ||
telemetry!: TelemetrySettings; | ||
} | ||
|
||
export class TelemetrySettings { | ||
/** Whether the anonymous metrics are enabled. */ | ||
appMetricsEnabled!: boolean; | ||
/** Whether anonymous error reporting is enabled. */ | ||
appErrorReportingEnabled!: boolean; | ||
/** Whether non-anonymous metrics are enabled. */ | ||
appNonAnonMetricsEnabled!: boolean; | ||
} | ||
|
||
/** Request updating the TelemetrySettings. Only the fields that are set are updated */ | ||
export class TelemetryUpdate { | ||
/** Whether the anonymous metrics are enabled. */ | ||
appMetricsEnabled?: boolean | undefined; | ||
/** Whether anonymous error reporting is enabled. */ | ||
appErrorReportingEnabled?: boolean | undefined; | ||
/** Whether non-anonymous metrics are enabled. */ | ||
appNonAnonMetricsEnabled?: boolean | undefined; | ||
} |