diff --git a/crates/client/src/event.rs b/crates/client/src/event.rs new file mode 100644 index 0000000..721833c --- /dev/null +++ b/crates/client/src/event.rs @@ -0,0 +1,39 @@ +use std::time::Duration; + +use gpui::{Context, Window}; + +use crate::gui::Yumush; + +const CHECK_INTERVAL: Duration = Duration::from_secs(3); + +impl Yumush { + pub fn time_based_repetitive_checks_after_login( + &self, + window: &mut Window, + cx: &mut Context, + ) { + cx.spawn_in(window, async move |this, cx| { + loop { + cx.background_executor().timer(CHECK_INTERVAL).await; + + let keep_going = this.update_in(cx, |yumush, window, cx| { + if !yumush.is_logged_in() { + return false; + } + + if yumush.is_connected() { + yumush.get_chat_users(window, cx); + yumush.get_communities(window, cx); + } + + true + }); + + if !matches!(keep_going, Ok(true)) { + break; + } + } + }) + .detach(); + } +} diff --git a/crates/client/src/gui.rs b/crates/client/src/gui.rs index 937a805..dc84963 100644 --- a/crates/client/src/gui.rs +++ b/crates/client/src/gui.rs @@ -35,10 +35,10 @@ impl Yumush { window: &mut gpui::Window, cx: &mut gpui::Context, ) -> Self { - cx.spawn(async move |this, cx| { + cx.spawn_in(window, async move |this, cx| { while let Some(event) = network_event_receiver.recv().await { - let updated = this.update(cx, |yumush, cx| { - yumush.handle_network_event(event, cx); + let updated = this.update_in(cx, |yumush, window, cx| { + yumush.handle_network_event(event, window, cx); }); if updated.is_err() { @@ -60,7 +60,6 @@ impl Yumush { } fn set_user(&mut self, user: User) { - self.chat.push_user(&user); self.user = Some(user); } @@ -72,6 +71,14 @@ impl Yumush { self.user = None; } + pub fn is_connected(&self) -> bool { + self.connected + } + + pub fn is_logged_in(&self) -> bool { + self.user.is_some() + } + fn get_username(&self) -> Option<&str> { self.user.as_ref().map(|user| user.get_username()) } @@ -83,6 +90,7 @@ impl Yumush { cx: &mut gpui::Context, ) { self.current_route = new_route; + cx.notify(); match self.current_route { Route::Register => self.register.focus(window, cx), @@ -91,12 +99,26 @@ impl Yumush { } } - fn handle_network_event(&mut self, event: NetworkEvent, cx: &mut gpui::Context) { + fn handle_network_event( + &mut self, + event: NetworkEvent, + window: &mut gpui::Window, + cx: &mut gpui::Context, + ) { match event { - NetworkEvent::Connected => self.connected = true, + NetworkEvent::Connected(user) => { + self.connected = true; + match user { + Some(user) => self.set_user(user), + None if self.is_logged_in() => { + self.reset_user(); + self.change_page(Route::Login, window, cx); + } + None => {} + } + } NetworkEvent::ConnectionFailed(_) | NetworkEvent::Disconnected(_) => { self.connected = false; - self.reset_user(); } NetworkEvent::MessageReceived(message) => self.chat.push_message(message), } @@ -126,7 +148,7 @@ impl Render for Yumush { .child(match self.current_route { Route::Register => self.register_page(cx).into_any_element(), Route::Login => self.login_page(cx).into_any_element(), - Route::Chat => self.chat_page().into_any_element(), + Route::Chat => self.chat_page(cx).into_any_element(), }), ) .child(Root::read(window, cx).notification.clone()) diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index e6a4776..920ad6c 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -12,7 +12,8 @@ use gpui_component::{ use crate::gui::Yumush; pub struct Chat { - community: Community, + communities: Vec, + selected_community: Option, users: Vec, chat_messages: Vec, message_input: Entity, @@ -27,9 +28,6 @@ impl Chat { input_state }); - let community = Community::new("oko1k3hk3r21dhqa20az", "The Community"); - - let community = || community.clone(); cx.subscribe_in(&message_input, window, |this, input, event, window, cx| { if matches!(event, InputEvent::PressEnter { .. }) { let message_body = input.read(cx).text().to_string(); @@ -45,7 +43,10 @@ impl Chat { }; let network = this.network.clone(); - let community_id = this.chat.community.get_community_id().to_string(); + let Some(community_id) = this.chat.selected_community.clone() else { + window.push_notification(Notification::error("No Community Selected"), cx); + return; + }; cx.spawn_in(window, async move |this, cx| { if let Err(error_value) = network @@ -70,7 +71,8 @@ impl Chat { .detach(); Self { - community: community(), + communities: vec![], + selected_community: None, users: vec![], chat_messages: vec![], message_input, @@ -87,17 +89,76 @@ impl Chat { self.chat_messages.push(message); } - pub fn push_user(&mut self, user: &User) { - self.users.push(user.to_owned()); + pub fn update_user_list(&mut self, users: Vec) { + self.users = users; + } + + pub fn update_community_list(&mut self, communities: Vec) { + if self.selected_community.is_none() { + self.selected_community = communities + .first() + .map(|community| community.get_community_id().to_string()); + } + + self.communities = communities; } } impl Yumush { - pub fn chat_page(&self) -> impl IntoElement { + pub fn chat_page(&self, cx: &mut Context) -> impl IntoElement { + let entity = cx.entity(); div() .flex() .flex_row() .size_full() + .child( + div() + .id("community_list") + .h_full() + .flex() + .flex_col() + .flex_shrink_0() + .w_1_5() + .border_r_1() + .border_color(rgb(0x2f3136)) + .bg(rgb(0x2b2d31)) + .p_2() + .gap_1() + .child( + div() + .text_color(rgb(0x949ba4)) + .font_weight(FontWeight::SEMIBOLD) + .child("Communities"), + ) + .children(self.chat.communities.iter().enumerate().map( + |(index, community)| { + let community_id = community.get_community_id().to_string(); + let selected = + self.chat.selected_community.as_deref() == Some(&community_id); + let entity = entity.clone(); + + div() + .id(("community", index)) + .p_1() + .cursor_pointer() + .bg(if selected { + rgb(0x404249) + } else { + rgb(0x2b2d31) + }) + .child(community.get_community_name().to_string()) + .on_click(move |_, window, cx| { + entity.update(cx, |yumush, cx| { + yumush.chat.selected_community = + Some(community_id.to_owned()); + yumush.chat.update_user_list(vec![]); + yumush.get_chat_users(window, cx); + cx.notify(); + }) + }) + }, + )), + ) .child( div() .flex() @@ -114,7 +175,16 @@ impl Yumush { .overflow_y_scroll() .p_2() .gap_1() - .children(self.chat.chat_messages.iter().map(render_message)), + .children( + self.chat + .chat_messages + .iter() + .filter(|message| { + Some(message.get_community_id()) + == self.chat.selected_community.as_deref() + }) + .map(|message| render_message(message, &self.chat.users)), + ), ) .child( div() @@ -151,9 +221,82 @@ impl Yumush { ), ) } + + pub fn get_chat_users(&self, window: &mut Window, cx: &mut Context) { + let network = self.network.clone(); + + let Some(community_id) = self.chat.selected_community.to_owned() else { + return; + }; + + cx.spawn_in(window, async move |this, cx| { + let user_ids = match network.users_in(&community_id).await { + Ok(user_ids) => user_ids, + Err(error_value) => { + let _ = this.update_in(cx, |_, window, cx| { + window.push_notification(Notification::error(error_value.to_string()), cx); + }); + + return; + } + }; + + let mut users = Vec::with_capacity(user_ids.len()); + for user_id in &user_ids { + if let Ok(user) = network.read_user(user_id).await { + users.push(user); + } + } + + let _ = this.update(cx, |yumush, cx| { + yumush.chat.update_user_list(users); + cx.notify(); + }); + }) + .detach(); + } + + pub fn get_communities(&self, window: &mut Window, cx: &mut Context) { + let Some(user) = self.get_user() else { + return; + }; + + let network = self.network.clone(); + + cx.spawn_in(window, async move |this, cx| { + let community_ids = match network.community_of(user.get_user_id()).await { + Ok(community_ids) => community_ids, + Err(error_value) => { + let _ = this.update_in(cx, |_, window, cx| { + window.push_notification(Notification::error(error_value.to_string()), cx); + }); + + return; + } + }; + + let mut communities = Vec::with_capacity(community_ids.len()); + for community_id in community_ids { + if let Ok(community) = network.read_community(&community_id).await { + communities.push(community); + } + } + + let _ = this.update(cx, |yumush, cx| { + yumush.chat.update_community_list(communities); + cx.notify(); + }); + }) + .detach(); + } } -fn render_message(message: &Message) -> impl IntoElement { +fn render_message(message: &Message, users: &[User]) -> impl IntoElement { + let username = users + .iter() + .find(|user| user.get_user_id() == message.get_user_id()) + .map(User::get_username) + .unwrap_or(message.get_user_id()); div() .flex() .flex_col() @@ -162,7 +305,7 @@ fn render_message(message: &Message) -> impl IntoElement { div() .text_color(rgb(0x7289da)) .font_weight(FontWeight::BOLD) - .child(message.get_user_id().to_string()), + .child(username.to_string()), ) .child(div().pl_2().child(message.get_message_body().to_string())) } diff --git a/crates/client/src/gui/login.rs b/crates/client/src/gui/login.rs index 38ab057..ab45b88 100644 --- a/crates/client/src/gui/login.rs +++ b/crates/client/src/gui/login.rs @@ -55,6 +55,7 @@ fn login_action( Ok(user) => { yumush.set_user(user); yumush.change_page(Route::Chat, window, cx); + yumush.time_based_repetitive_checks_after_login(window, cx); } Err(error_value) => { window.push_notification(Notification::error(error_value.to_string()), cx); diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 137c433..5f916fe 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -1,5 +1,6 @@ use common::utils::TOML; +pub mod event; pub mod gui; pub mod network; mod request;