use anyhow::Result;
use serde::{Deserialize, Serialize};
use crate::client::config::ClientConfig;
use crate::theme::colors::Theme;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AppPreferences {
pub theme: Theme,
pub font_size: u8,
pub compact_mode: bool,
pub send_read_receipts: bool,
pub send_typing_notifications: bool,
pub show_join_leave_events: bool,
pub language: String,
#[serde(default = "default_true")]
pub desktop_notifications: bool,
#[serde(default = "default_true")]
pub notification_sounds: bool,
#[serde(default = "default_true")]
pub show_message_preview: bool,
#[serde(default)]
pub notification_keywords: Vec<String>,
}
fn default_true() -> bool { true }
impl Default for AppPreferences {
fn default() -> Self {
Self {
theme: Theme::Dark,
font_size: 14,
compact_mode: false,
send_read_receipts: true,
send_typing_notifications: true,
show_join_leave_events: true,
language: "en".to_string(),
desktop_notifications: true,
notification_sounds: true,
show_message_preview: true,
notification_keywords: Vec::new(),
}
}
}
pub async fn save_preferences(prefs: &AppPreferences) -> Result<()> {
let config = ClientConfig::default();
let prefs_file = config.data_dir().join("preferences.json");
let json = serde_json::to_string_pretty(prefs)?;
tokio::fs::write(&prefs_file, json).await?;
Ok(())
}
pub async fn load_preferences() -> Result<AppPreferences> {
let config = ClientConfig::default();
let prefs_file = config.data_dir().join("preferences.json");
if !prefs_file.exists() {
return Ok(AppPreferences::default());
}
let json = tokio::fs::read_to_string(&prefs_file).await?;
let prefs: AppPreferences = serde_json::from_str(&json)?;
Ok(prefs)
}