Skip to main content

iris_chat_core/
desktop_nearby.rs

1use crate::FfiApp;
2use std::sync::{Arc, RwLock};
3
4#[derive(uniffi::Record, Clone, Debug, PartialEq, Eq)]
5pub struct DesktopNearbyPeerSnapshot {
6    pub id: String,
7    pub name: String,
8    pub owner_pubkey_hex: Option<String>,
9    pub picture_url: Option<String>,
10    pub profile_event_id: Option<String>,
11    pub last_seen_secs: u64,
12}
13
14#[derive(uniffi::Record, Clone, Debug, PartialEq, Eq)]
15pub struct DesktopNearbySnapshot {
16    pub visible: bool,
17    pub status: String,
18    pub peers: Vec<DesktopNearbyPeerSnapshot>,
19}
20
21#[uniffi::export(callback_interface)]
22pub trait DesktopNearbyObserver: Send + Sync + 'static {
23    fn desktop_nearby_changed(&self, snapshot: DesktopNearbySnapshot);
24}
25
26/// Compatibility facade for desktop callers that still own the Nearby UI lifecycle.
27/// FIPS owns discovery and transport; peer snapshots arrive through `AppUpdate`.
28#[derive(uniffi::Object)]
29pub struct FfiDesktopNearby {
30    observer: Arc<dyn DesktopNearbyObserver>,
31    snapshot: RwLock<DesktopNearbySnapshot>,
32}
33
34#[uniffi::export]
35impl FfiDesktopNearby {
36    #[uniffi::constructor]
37    pub fn new(_app: Arc<FfiApp>, observer: Box<dyn DesktopNearbyObserver>) -> Arc<Self> {
38        Arc::new(Self {
39            observer: observer.into(),
40            snapshot: RwLock::new(DesktopNearbySnapshot {
41                visible: false,
42                status: "Off".to_string(),
43                peers: Vec::new(),
44            }),
45        })
46    }
47
48    pub fn start(&self, _local_name: String) {
49        self.set_visible(true);
50    }
51
52    pub fn stop(&self) {
53        self.set_visible(false);
54    }
55
56    pub fn snapshot(&self) -> DesktopNearbySnapshot {
57        match self.snapshot.read() {
58            Ok(snapshot) => snapshot.clone(),
59            Err(poison) => poison.into_inner().clone(),
60        }
61    }
62
63    pub fn publish(
64        &self,
65        _event_id: String,
66        _kind: u32,
67        _created_at_secs: u64,
68        _event_json: String,
69    ) {
70    }
71}
72
73impl FfiDesktopNearby {
74    fn set_visible(&self, visible: bool) {
75        let snapshot = DesktopNearbySnapshot {
76            visible,
77            status: if visible { "Visible" } else { "Off" }.to_string(),
78            peers: Vec::new(),
79        };
80        match self.snapshot.write() {
81            Ok(mut current) => *current = snapshot.clone(),
82            Err(poison) => *poison.into_inner() = snapshot.clone(),
83        }
84        self.observer.desktop_nearby_changed(snapshot);
85    }
86}