Skip to content

Commit

Permalink
First stab at adding a frontend service subscribing to settings changes
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvi committed Dec 19, 2024
1 parent 5ec9eaa commit 607c093
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions apps/desktop/src/lib/config/appSettingsV2.ts
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;
}

0 comments on commit 607c093

Please sign in to comment.