wip: refactor

This commit is contained in:
2025-01-08 17:30:06 +07:00
parent 2d254c3a28
commit 80506b72d9
6 changed files with 145 additions and 105 deletions

View File

@@ -166,39 +166,42 @@ impl RoomPanel {
pub fn subscribe(&self, cx: &mut ViewContext<Self>) {
let room_id = self.id.clone();
let messages = self.messages.clone();
let state = cx.global::<ChatRegistry>().messages();
cx.observe_global::<ChatRegistry>(move |_, cx| {
let state = cx.global::<ChatRegistry>();
let new_messages = state.get_messages(&room_id);
if let Some(state) = state.upgrade() {
cx.observe(&state, move |_, model, cx| {
let new_messages = model.read(cx).read().unwrap().get(&room_id).cloned();
if let Some(new_messages) = new_messages {
let items: Vec<RoomMessage> = new_messages
.read()
.unwrap()
.clone()
.into_iter()
.map(|m| {
RoomMessage::new(
m.event.pubkey,
m.metadata,
m.event.content,
m.event.created_at,
)
})
.collect();
if let Some(new_messages) = new_messages {
let items: Vec<RoomMessage> = new_messages
.read()
.unwrap()
.clone()
.into_iter()
.map(|m| {
RoomMessage::new(
m.event.pubkey,
m.metadata,
m.event.content,
m.event.created_at,
)
})
.collect();
cx.update_model(&messages, |model, cx| {
model.items.extend(items);
model.count = model.items.len();
cx.notify();
});
}
})
.detach();
cx.update_model(&messages, |model, cx| {
model.items.extend(items);
model.count = model.items.len();
cx.notify();
});
}
})
.detach();
}
}
fn send_message(&mut self, cx: &mut ViewContext<Self>) {
let owner = self.owner;
let members = self.members.clone();
let members2 = members.clone();
let content = self.input.read(cx).text().to_string();
let content2 = content.clone();
let content3 = content2.clone();
@@ -223,20 +226,40 @@ impl RoomPanel {
// Send message to all members
async_cx
.background_executor()
.spawn(async move { client.send_private_msg(owner, content, vec![]).await })
.spawn(async move {
for member in members.iter() {
let tags: Vec<Tag> = members
.iter()
.filter_map(|public_key| {
if public_key != member {
Some(Tag::public_key(*public_key))
} else {
None
}
})
.collect();
_ = client.send_private_msg(*member, &content, tags).await;
}
})
.detach();
// Send a copy to yourself
async_cx
.background_executor()
.spawn(async move {
client
.send_private_msg(
current_user,
content2,
vec![Tag::public_key(owner)],
)
.await
let tags: Vec<Tag> = members2
.iter()
.filter_map(|public_key| {
if public_key != &current_user {
Some(Tag::public_key(*public_key))
} else {
None
}
})
.collect();
_ = client.send_private_msg(current_user, content2, tags).await;
})
.detach();

View File

@@ -191,7 +191,7 @@ impl ContactList {
}
}
pub fn _selected(&self) -> Vec<PublicKey> {
pub fn selected(&self) -> Vec<PublicKey> {
self.selected.clone().into_iter().collect()
}

View File

@@ -165,13 +165,14 @@ pub struct Inbox {
impl Inbox {
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
let items = cx.new_model(|_| None);
let events = cx.global::<ChatRegistry>().rooms();
cx.observe_global::<ChatRegistry>(|this, cx| {
if cx.global::<ChatRegistry>().is_initialized {
this.load(cx)
}
})
.detach();
if let Some(events) = events.upgrade() {
cx.observe(&events, |this, model, cx| {
this.load(model, cx);
})
.detach();
}
Self {
items,
@@ -181,9 +182,8 @@ impl Inbox {
}
}
pub fn load(&mut self, cx: &mut ViewContext<Self>) {
// Get all room's events
let events: Vec<Event> = cx.global::<ChatRegistry>().rooms.read(cx).clone();
pub fn load(&mut self, model: Model<Vec<Event>>, cx: &mut ViewContext<Self>) {
let events = model.read(cx).clone();
cx.spawn(|view, mut async_cx| async move {
let client = get_client();

View File

@@ -36,7 +36,7 @@ impl Sidebar {
let inbox = cx.new_view(Inbox::new);
Self {
name: "Left Dock".into(),
name: "Sidebar".into(),
closeable: true,
zoomable: true,
focus_handle: cx.focus_handle(),
@@ -57,9 +57,9 @@ impl Sidebar {
.rounded(ButtonRounded::Large)
.w_full()
.on_click({
let _contact_list = contact_list.clone();
move |_, _cx| {
// TODO: open room
let contact_list = contact_list.clone();
move |_, cx| {
let _selected = contact_list.model.read(cx).selected();
}
}),
),