synpad 0.1.0

A full-featured Matrix chat client built with Dioxus
use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::client::config::ClientConfig;
use crate::theme::colors::Theme;

/// Serializable application preferences.
#[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,
    // Notification preferences
    #[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(),
        }
    }
}

/// Save application preferences to disk.
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(())
}

/// Load application preferences from disk.
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)
}