Skip to content

Commit

Permalink
Merge pull request #5815 from gitbutlerapp/settings-v2
Browse files Browse the repository at this point in the history
Settings v2
  • Loading branch information
krlvi authored Dec 19, 2024
2 parents 19b12dd + f8eeb48 commit 8397b1a
Show file tree
Hide file tree
Showing 18 changed files with 796 additions and 37 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}
14 changes: 14 additions & 0 deletions crates/gitbutler-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ authors = ["GitButler <[email protected]>"]
publish = false

[dependencies]
anyhow = "1.0.93"
serde = { workspace = true, features = ["std"] }
serde_json = { version = "1.0", features = ["std", "arbitrary_precision"] }
serde_json_lenient = "0.2.3"
gitbutler-fs.workspace = true
notify = { version = "6.0.1" }
tracing.workspace = true
tokio = { workspace = true, features = ["macros", "rt"] }

[[test]]
name = "settings"
path = "tests/mod.rs"

[dev-dependencies]
16 changes: 16 additions & 0 deletions crates/gitbutler-settings/assets/defaults.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
/// Whether the user has passed the onboarding flow.
"onboardingComplete": false,
"telemetry": {
/// Whether the anonymous metrics are enabled.
"appMetricsEnabled": true,
/// Whether anonymous error reporting is enabled.
"appErrorReportingEnabled": true,
/// Whether non-anonymous metrics are enabled.
"appNonAnonMetricsEnabled": false
},
"githubOauthApp": {
/// Client ID for the GitHub OAuth application. Set this to use custom (non-GitButler) OAuth application.
"oauthClientId": "cd51880daa675d9e6452"
}
}
35 changes: 35 additions & 0 deletions crates/gitbutler-settings/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::AppSettingsWithDiskSync;
use anyhow::Result;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
/// Update request for [`crate::app_settings::TelemetrySettings`].
pub struct TelemetryUpdate {
pub app_metrics_enabled: Option<bool>,
pub app_error_reporting_enabled: Option<bool>,
pub app_non_anon_metrics_enabled: Option<bool>,
}

/// Mutation, immediately followed by writing everything to disk.
impl AppSettingsWithDiskSync {
pub fn update_onboarding_complete(&self, update: bool) -> Result<()> {
let mut settings = self.get_mut_enforce_save()?;
settings.onboarding_complete = update;
settings.save()
}

pub fn update_telemetry(&self, update: TelemetryUpdate) -> Result<()> {
let mut settings = self.get_mut_enforce_save()?;
if let Some(app_metrics_enabled) = update.app_metrics_enabled {
settings.telemetry.app_metrics_enabled = app_metrics_enabled;
}
if let Some(app_error_reporting_enabled) = update.app_error_reporting_enabled {
settings.telemetry.app_error_reporting_enabled = app_error_reporting_enabled;
}
if let Some(app_non_anon_metrics_enabled) = update.app_non_anon_metrics_enabled {
settings.telemetry.app_non_anon_metrics_enabled = app_non_anon_metrics_enabled;
}
settings.save()
}
}
19 changes: 19 additions & 0 deletions crates/gitbutler-settings/src/app_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct TelemetrySettings {
/// Whether the anonymous metrics are enabled.
pub app_metrics_enabled: bool,
/// Whether anonymous error reporting is enabled.
pub app_error_reporting_enabled: bool,
/// Whether non-anonymous metrics are enabled.
pub app_non_anon_metrics_enabled: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GitHubOAuthAppSettings {
/// Client ID for the GitHub OAuth application. Set this to use custom (non-GitButler) OAuth application.
pub oauth_client_id: String,
}
Loading

0 comments on commit 8397b1a

Please sign in to comment.