synpad 0.1.0

A full-featured Matrix chat client built with Dioxus
use std::collections::HashMap;
use std::sync::Mutex;

/// Thumbnail cache keyed by (mxc_uri, size).
pub struct ThumbnailCache {
    entries: Mutex<HashMap<String, Vec<u8>>>,
}

impl ThumbnailCache {
    pub fn new() -> Self {
        Self {
            entries: Mutex::new(HashMap::new()),
        }
    }

    /// Generate a cache key from URI and dimensions.
    fn key(mxc_uri: &str, width: u32, height: u32) -> String {
        format!("{mxc_uri}_{width}x{height}")
    }

    pub fn get(&self, mxc_uri: &str, width: u32, height: u32) -> Option<Vec<u8>> {
        let key = Self::key(mxc_uri, width, height);
        self.entries.lock().ok()?.get(&key).cloned()
    }

    pub fn insert(&self, mxc_uri: &str, width: u32, height: u32, data: Vec<u8>) {
        let key = Self::key(mxc_uri, width, height);
        if let Ok(mut entries) = self.entries.lock() {
            entries.insert(key, data);
        }
    }
}

impl Default for ThumbnailCache {
    fn default() -> Self {
        Self::new()
    }
}