-
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.
Merge pull request #5815 from gitbutlerapp/settings-v2
Settings v2
- Loading branch information
Showing
18 changed files
with
796 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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; | ||
} |
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 |
---|---|---|
|
@@ -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] |
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,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" | ||
} | ||
} |
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,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() | ||
} | ||
} |
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,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, | ||
} |
Oops, something went wrong.