Skip to content

Commit cc565ba

Browse files
committed
refactor(settings): clean up code and improve formatting
1 parent a8023f9 commit cc565ba

1 file changed

Lines changed: 84 additions & 36 deletions

File tree

src-tauri/src/settings.rs

Lines changed: 84 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use serde::{Deserialize, Serialize};
2+
use serde_json::Value;
23
use std::fs;
34
use std::path::PathBuf;
45
use tauri_plugin_autostart::ManagerExt;
56
use tauri_plugin_notification::{NotificationExt, PermissionState};
6-
use serde_json::Value;
77

88
#[derive(Serialize, Deserialize, Clone, Debug)]
99
pub struct AppSettings {
@@ -29,9 +29,7 @@ pub struct NotificationSettings {
2929
impl Default for AppSettings {
3030
fn default() -> Self {
3131
AppSettings {
32-
general: GeneralSettings {
33-
auto_start: false,
34-
},
32+
general: GeneralSettings { auto_start: false },
3533
notifications: NotificationSettings {
3634
show_notifications: true,
3735
notification_duration: 3000,
@@ -47,7 +45,7 @@ impl Default for AppSettings {
4745
impl AppSettings {
4846
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
4947
let settings_path = get_settings_path();
50-
48+
5149
if settings_path.exists() {
5250
let content = fs::read_to_string(&settings_path)?;
5351
let mut value: Value = serde_json::from_str(&content)?;
@@ -76,7 +74,7 @@ impl AppSettings {
7674
let autostart_manager = app.autolaunch();
7775
let current_autostart = autostart_manager.is_enabled().unwrap_or(false);
7876
let new_autostart = self.general.auto_start;
79-
77+
8078
if current_autostart != new_autostart {
8179
if new_autostart {
8280
autostart_manager.enable()?;
@@ -85,25 +83,36 @@ impl AppSettings {
8583
}
8684
}
8785

88-
8986
// Применяем настройки уведомлений
9087
// (эти настройки будут использоваться в других частях приложения)
91-
88+
9289
// Применяем настройки языка
9390
// (можно добавить логику для смены языка интерфейса)
94-
91+
9592
Ok(())
9693
}
9794

98-
99-
100-
pub fn show_notification(&self, app: &tauri::AppHandle, title: &str, body: &str) -> Result<(), Box<dyn std::error::Error>> {
95+
pub fn show_notification(
96+
&self,
97+
app: &tauri::AppHandle,
98+
title: &str,
99+
body: &str,
100+
) -> Result<(), Box<dyn std::error::Error>> {
101101
self.show_notification_with_icon(app, title, body, None)
102102
}
103103

104-
fn show_notification_with_icon(&self, app: &tauri::AppHandle, title: &str, body: &str, icon: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
105-
println!("[Settings] Attempting to show notification: title='{}', body='{}', icon='{:?}'", title, body, icon);
106-
104+
fn show_notification_with_icon(
105+
&self,
106+
app: &tauri::AppHandle,
107+
title: &str,
108+
body: &str,
109+
icon: Option<&str>,
110+
) -> Result<(), Box<dyn std::error::Error>> {
111+
println!(
112+
"[Settings] Attempting to show notification: title='{}', body='{}', icon='{:?}'",
113+
title, body, icon
114+
);
115+
107116
if !self.notifications.show_notifications {
108117
println!("[Settings] Notifications are disabled in settings");
109118
return Ok(());
@@ -112,7 +121,7 @@ impl AppSettings {
112121
// Проверяем права на уведомления
113122
let permission_state = app.notification().permission_state();
114123
println!("[Settings] Permission state: {:?}", permission_state);
115-
124+
116125
match permission_state {
117126
Ok(state) if state == PermissionState::Granted => {
118127
println!("[Settings] Permission granted, proceeding with notification");
@@ -123,22 +132,27 @@ impl AppSettings {
123132
if let Err(e) = app.notification().request_permission() {
124133
println!("[Settings] Failed to request permission: {}", e);
125134
// Показываем уведомление об ошибке вместо вывода в консоль
126-
let _ = self.show_error_notification(app, "Ошибка разрешений", &format!("Не удалось запросить разрешение на уведомления: {}", e));
135+
let _ = self.show_error_notification(
136+
app,
137+
"Ошибка разрешений",
138+
&format!("Не удалось запросить разрешение на уведомления: {}", e),
139+
);
127140
}
128141
return Ok(());
129142
}
130143
}
131144

132-
let mut builder = app.notification().builder()
133-
.title(title)
134-
.body(body);
145+
let mut builder = app.notification().builder().title(title).body(body);
135146

136147
if let Some(icon) = icon {
137148
builder = builder.icon(icon);
138149
}
139150

140-
println!("[Settings] Building notification with title='{}', body='{}'", title, body);
141-
151+
println!(
152+
"[Settings] Building notification with title='{}', body='{}'",
153+
title, body
154+
);
155+
142156
match builder.show() {
143157
Ok(_) => {
144158
println!("[Settings] Notification shown successfully");
@@ -151,9 +165,17 @@ impl AppSettings {
151165
}
152166
}
153167

154-
pub fn show_success_notification(&self, app: &tauri::AppHandle, title: &str, body: &str) -> Result<(), Box<dyn std::error::Error>> {
155-
println!("[Settings] show_success_notification called: title='{}', body='{}'", title, body);
156-
168+
pub fn show_success_notification(
169+
&self,
170+
app: &tauri::AppHandle,
171+
title: &str,
172+
body: &str,
173+
) -> Result<(), Box<dyn std::error::Error>> {
174+
println!(
175+
"[Settings] show_success_notification called: title='{}', body='{}'",
176+
title, body
177+
);
178+
157179
if !self.notifications.success_notifications {
158180
println!("[Settings] Success notifications are disabled in settings");
159181
return Ok(());
@@ -163,9 +185,17 @@ impl AppSettings {
163185
self.show_notification_with_icon(app, title, body, Some("✅"))
164186
}
165187

166-
pub fn show_error_notification(&self, app: &tauri::AppHandle, title: &str, body: &str) -> Result<(), Box<dyn std::error::Error>> {
167-
println!("[Settings] show_error_notification called: title='{}', body='{}'", title, body);
168-
188+
pub fn show_error_notification(
189+
&self,
190+
app: &tauri::AppHandle,
191+
title: &str,
192+
body: &str,
193+
) -> Result<(), Box<dyn std::error::Error>> {
194+
println!(
195+
"[Settings] show_error_notification called: title='{}', body='{}'",
196+
title, body
197+
);
198+
169199
if !self.notifications.error_notifications {
170200
println!("[Settings] Error notifications are disabled in settings");
171201
return Ok(());
@@ -175,9 +205,17 @@ impl AppSettings {
175205
self.show_notification_with_icon(app, title, body, Some("❌"))
176206
}
177207

178-
pub fn show_info_notification(&self, app: &tauri::AppHandle, title: &str, body: &str) -> Result<(), Box<dyn std::error::Error>> {
179-
println!("[Settings] show_info_notification called: title='{}', body='{}'", title, body);
180-
208+
pub fn show_info_notification(
209+
&self,
210+
app: &tauri::AppHandle,
211+
title: &str,
212+
body: &str,
213+
) -> Result<(), Box<dyn std::error::Error>> {
214+
println!(
215+
"[Settings] show_info_notification called: title='{}', body='{}'",
216+
title, body
217+
);
218+
181219
if !self.notifications.info_notifications {
182220
println!("[Settings] Info notifications are disabled in settings");
183221
return Ok(());
@@ -187,9 +225,17 @@ impl AppSettings {
187225
self.show_notification_with_icon(app, title, body, Some("ℹ️"))
188226
}
189227

190-
pub fn show_warning_notification(&self, app: &tauri::AppHandle, title: &str, body: &str) -> Result<(), Box<dyn std::error::Error>> {
191-
println!("[Settings] show_warning_notification called: title='{}', body='{}'", title, body);
192-
228+
pub fn show_warning_notification(
229+
&self,
230+
app: &tauri::AppHandle,
231+
title: &str,
232+
body: &str,
233+
) -> Result<(), Box<dyn std::error::Error>> {
234+
println!(
235+
"[Settings] show_warning_notification called: title='{}', body='{}'",
236+
title, body
237+
);
238+
193239
if !self.notifications.warning_notifications {
194240
println!("[Settings] Warning notifications are disabled in settings");
195241
return Ok(());
@@ -286,7 +332,9 @@ fn merge_defaults(current: &mut Value, default: &Value) {
286332
for (k, v) in def_map {
287333
match cur_map.get_mut(k) {
288334
Some(cur_v) => merge_defaults(cur_v, v),
289-
None => { cur_map.insert(k.clone(), v.clone()); },
335+
None => {
336+
cur_map.insert(k.clone(), v.clone());
337+
}
290338
}
291339
}
292340
}
@@ -305,4 +353,4 @@ fn remove_extra_fields(current: &mut Value, default: &Value) {
305353
}
306354
}
307355
}
308-
}
356+
}

0 commit comments

Comments
 (0)