From 944d3b9e46fca387d9fbf89c6244ef2ab4d9c3ce Mon Sep 17 00:00:00 2001 From: reya Date: Sat, 1 Feb 2025 09:07:01 +0700 Subject: [PATCH] wip: refactor --- crates/app/src/views/app.rs | 29 ++++++--- crates/app/src/views/chat/mod.rs | 36 ++++++----- crates/app/src/views/onboarding/mod.rs | 10 +-- crates/app/src/views/sidebar/compose.rs | 86 +++++++++++++------------ crates/app/src/views/sidebar/inbox.rs | 16 ++--- 5 files changed, 96 insertions(+), 81 deletions(-) diff --git a/crates/app/src/views/app.rs b/crates/app/src/views/app.rs index 2a8e443..f701b81 100644 --- a/crates/app/src/views/app.rs +++ b/crates/app/src/views/app.rs @@ -1,4 +1,4 @@ -use super::{chat::ChatPanel, sidebar::Sidebar, welcome::WelcomePanel}; +use super::{chat::ChatPanel, onboarding::Onboarding, sidebar::Sidebar, welcome::WelcomePanel}; use app_state::registry::AppRegistry; use chat::registry::ChatRegistry; use common::profile::NostrProfile; @@ -116,7 +116,6 @@ impl AppView { if let Some(weak_room) = cx.global::().get_room(id, cx) { if let Some(room) = weak_room.upgrade() { let panel = Arc::new(ChatPanel::new(room, window, cx)); - self.dock.update(cx, |dock_area, cx| { dock_area.add_panel(panel, action.position, window, cx); }); @@ -137,8 +136,8 @@ impl AppView { fn on_profile_action( &mut self, _action: &OpenProfile, - window: &mut Window, - cx: &mut Context, + _window: &mut Window, + _cx: &mut Context, ) { // TODO } @@ -146,8 +145,8 @@ impl AppView { fn on_contacts_action( &mut self, _action: &OpenContacts, - window: &mut Window, - cx: &mut Context, + _window: &mut Window, + _cx: &mut Context, ) { // TODO } @@ -155,17 +154,27 @@ impl AppView { fn on_settings_action( &mut self, _action: &OpenSettings, - window: &mut Window, - cx: &mut Context, + _window: &mut Window, + _cx: &mut Context, ) { // TODO } - fn on_logout_action(&mut self, _action: &Logout, _window: &mut Window, cx: &mut Context) { - cx.update_global::(|_this, cx| { + fn on_logout_action(&mut self, _action: &Logout, window: &mut Window, cx: &mut Context) { + cx.update_global::(|this, cx| { cx.background_executor() .spawn(async move { get_client().reset().await }) .detach(); + + // Remove user + this.set_user(None); + + // Update root view + if let Some(root) = this.root() { + cx.update_entity(&root, |this: &mut Root, cx| { + this.set_view(cx.new(|cx| Onboarding::new(window, cx)).into(), cx); + }); + } }); } } diff --git a/crates/app/src/views/chat/mod.rs b/crates/app/src/views/chat/mod.rs index 957582b..d30a6dc 100644 --- a/crates/app/src/views/chat/mod.rs +++ b/crates/app/src/views/chat/mod.rs @@ -135,7 +135,7 @@ impl ChatPanel { }) } - fn load_messages(&self, window: &mut Window, cx: &mut Context) { + fn load_messages(&self, _window: &mut Window, cx: &mut Context) { let room = self.room.read(cx); let members = room.members.clone(); let owner = room.owner.clone(); @@ -221,7 +221,7 @@ impl ChatPanel { fn load_new_messages( &self, model: WeakEntity, - window: &mut Window, + _window: &mut Window, cx: &mut Context, ) { if let Some(model) = model.upgrade() { @@ -265,6 +265,7 @@ impl ChatPanel { window: &mut Window, cx: &mut Context, ) { + let window_handle = window.window_handle(); let room = self.room.read(cx); let owner = room.owner.clone(); let mut members = room.members.to_vec(); @@ -297,11 +298,9 @@ impl ChatPanel { }); } - /* - cx.spawn(|this, mut async_cx| async move { + cx.spawn(|this, mut cx| async move { // Send message to all members - async_cx - .background_executor() + cx.background_executor() .spawn({ let client = get_client(); let content = content.clone().to_string(); @@ -328,7 +327,7 @@ impl ChatPanel { .detach(); if let Some(view) = this.upgrade() { - _ = async_cx.update_entity(&view, |this, cx| { + _ = cx.update_entity(&view, |this, cx| { cx.update_entity(&this.state, |model, cx| { let message = Message::new( owner, @@ -339,23 +338,26 @@ impl ChatPanel { model.items.extend(vec![message]); model.count = model.items.len(); cx.notify(); - }) + }); + cx.notify(); }); } if let Some(input) = view.upgrade() { - _ = async_cx.update_entity(&input, |input, cx| { - input.set_loading(false, window, cx); - input.set_disabled(false, window, cx); - input.set_text("", window, cx); - }); + cx.update_window(window_handle, |_, window, cx| { + cx.update_entity(&input, |input, cx| { + input.set_loading(false, window, cx); + input.set_disabled(false, window, cx); + input.set_text("", window, cx); + }); + }) + .unwrap() } }) .detach(); - */ } - fn upload(&mut self, window: &mut Window, cx: &mut Context) { + fn upload(&mut self, _window: &mut Window, cx: &mut Context) { let attaches = self.attaches.clone(); let paths = cx.prompt_for_paths(PathPromptOptions { files: true, @@ -412,7 +414,7 @@ impl ChatPanel { .detach(); } - fn remove(&mut self, url: &Url, window: &mut Window, cx: &mut Context) { + fn remove(&mut self, url: &Url, _window: &mut Window, cx: &mut Context) { self.attaches.update(cx, |model, cx| { if let Some(urls) = model.as_mut() { let ix = urls.iter().position(|x| x == url).unwrap(); @@ -469,7 +471,7 @@ impl Focusable for ChatPanel { } impl Render for ChatPanel { - fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { v_flex() .size_full() .child(list(self.list.clone()).flex_1()) diff --git a/crates/app/src/views/onboarding/mod.rs b/crates/app/src/views/onboarding/mod.rs index 98f237b..4c2228b 100644 --- a/crates/app/src/views/onboarding/mod.rs +++ b/crates/app/src/views/onboarding/mod.rs @@ -1,4 +1,4 @@ -use common::{constants::KEYRING_SERVICE, profile::NostrProfile}; +use common::constants::KEYRING_SERVICE; use gpui::{div, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Window}; use nostr_sdk::prelude::*; use state::get_client; @@ -33,7 +33,7 @@ impl Onboarding { fn save_keys( content: &str, - window: &mut Window, + _window: &mut Window, cx: &mut Context, ) -> anyhow::Result<(), anyhow::Error> { let keys = Keys::parse(content)?; @@ -41,7 +41,7 @@ impl Onboarding { let bech32 = public_key.to_bech32()?; let secret = keys.secret_key().to_secret_hex(); - let mut async_cx = cx.to_async(); + let async_cx = cx.to_async(); cx.foreground_executor() .spawn({ @@ -67,7 +67,7 @@ impl Onboarding { }) .await; - if let Ok(metadata) = query { + if let Ok(_metadata) = query { // } } @@ -80,7 +80,7 @@ impl Onboarding { } impl Render for Onboarding { - fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { div() .size_full() .flex() diff --git a/crates/app/src/views/sidebar/compose.rs b/crates/app/src/views/sidebar/compose.rs index 16f5e1a..e4137b8 100644 --- a/crates/app/src/views/sidebar/compose.rs +++ b/crates/app/src/views/sidebar/compose.rs @@ -78,38 +78,33 @@ impl Compose { ) .detach(); - cx.spawn(|this, mut async_cx| { - let client = get_client(); + cx.spawn(|this, mut async_cx| async move { + let query: anyhow::Result, anyhow::Error> = async_cx + .background_executor() + .spawn(async move { + let client = get_client(); + let signer = client.signer().await?; + let public_key = signer.get_public_key().await?; + let profiles = client.database().contacts(public_key).await?; + let members: Vec = profiles + .into_iter() + .map(|profile| NostrProfile::new(profile.public_key(), profile.metadata())) + .collect(); - async move { - let query: anyhow::Result, anyhow::Error> = async_cx - .background_executor() - .spawn(async move { - let signer = client.signer().await?; - let public_key = signer.get_public_key().await?; - let profiles = client.database().contacts(public_key).await?; - let members: Vec = profiles - .into_iter() - .map(|profile| { - NostrProfile::new(profile.public_key(), profile.metadata()) - }) - .collect(); - - Ok(members) - }) - .await; - - if let Ok(contacts) = query { - if let Some(view) = this.upgrade() { - _ = async_cx.update_entity(&view, |this, cx| { - this.contacts.update(cx, |this, cx| { - *this = Some(contacts); - cx.notify(); - }); + Ok(members) + }) + .await; + if let Ok(contacts) = query { + if let Some(view) = this.upgrade() { + _ = async_cx.update_entity(&view, |this, cx| { + this.contacts.update(cx, |this, cx| { + *this = Some(contacts); cx.notify(); }); - } + + cx.notify(); + }); } } }) @@ -161,7 +156,7 @@ impl Compose { } } - pub fn label(&self, window: &Window, cx: &App) -> SharedString { + pub fn label(&self, _window: &Window, cx: &App) -> SharedString { if self.selected.read(cx).len() > 1 { "Create Group DM".into() } else { @@ -170,12 +165,12 @@ impl Compose { } fn add(&mut self, window: &mut Window, cx: &mut Context) { + let window_handle = window.window_handle(); let content = self.user_input.read(cx).text().to_string(); let input = self.user_input.downgrade(); // Show loading spinner - self.is_loading = true; - cx.notify(); + self.set_loading(true, cx); if let Ok(public_key) = PublicKey::parse(&content) { cx.spawn(|this, mut async_cx| async move { @@ -206,14 +201,15 @@ impl Compose { cx.notify(); }); - this.is_loading = false; - cx.notify(); + this.set_loading(false, cx); }); } if let Some(input) = input.upgrade() { - _ = async_cx.update_entity(&input, |input, cx| { - // input.set_text("", window, cx); + _ = async_cx.update_window(window_handle, |_, window, cx| { + cx.update_entity(&input, |this, cx| { + this.set_text("", window, cx); + }) }); } } @@ -224,10 +220,15 @@ impl Compose { } } + fn set_loading(&mut self, status: bool, cx: &mut Context) { + self.is_loading = status; + cx.notify(); + } + fn on_action_select( &mut self, action: &SelectContact, - window: &mut Window, + _window: &mut Window, cx: &mut Context, ) { self.selected.update(cx, |this, cx| { @@ -242,7 +243,7 @@ impl Compose { } impl Render for Compose { - fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let msg = "Start a conversation with someone using their npub or NIP-05 (like foo@bar.com)."; @@ -319,7 +320,7 @@ impl Render for Compose { cx.entity().clone(), "contacts", contacts.len(), - move |this, range, window, cx| { + move |this, range, _window, cx| { let selected = this.selected.read(cx); let mut items = Vec::new(); @@ -366,9 +367,12 @@ impl Render for Compose { .step(cx, ColorScaleStep::THREE)) }) .on_click(move |_, window, cx| { - cx.dispatch_action(&SelectContact( - item.public_key(), - )); + window.dispatch_action( + Box::new(SelectContact( + item.public_key(), + )), + cx, + ); }), ); } diff --git a/crates/app/src/views/sidebar/inbox.rs b/crates/app/src/views/sidebar/inbox.rs index 696f2c1..00e3fdd 100644 --- a/crates/app/src/views/sidebar/inbox.rs +++ b/crates/app/src/views/sidebar/inbox.rs @@ -5,7 +5,6 @@ use gpui::{ div, img, percentage, prelude::FluentBuilder, px, Context, InteractiveElement, IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window, }; -use std::sync::Arc; use ui::{ dock_area::dock::DockPlacement, skeleton::Skeleton, @@ -103,13 +102,14 @@ impl Inbox { } } - fn action(&self, id: u64, _window: &mut Window, cx: &mut Context) { - let action = AddPanel { - panel: PanelKind::Room(id), - position: DockPlacement::Center, - }; - - cx.dispatch_action(&action) + fn action(&self, id: u64, window: &mut Window, cx: &mut Context) { + window.dispatch_action( + Box::new(AddPanel { + panel: PanelKind::Room(id), + position: DockPlacement::Center, + }), + cx, + ); } }