wip: refactor
This commit is contained in:
@@ -10,12 +10,12 @@ use ui::{
|
||||
Icon, IconName, Sizable,
|
||||
};
|
||||
|
||||
use crate::states::app::AppRegistry;
|
||||
use crate::{constants::IMAGE_SERVICE, get_client};
|
||||
|
||||
actions!(account, [ToDo]);
|
||||
|
||||
pub struct Account {
|
||||
#[allow(dead_code)]
|
||||
public_key: PublicKey,
|
||||
metadata: Model<Option<Metadata>>,
|
||||
}
|
||||
@@ -23,32 +23,46 @@ pub struct Account {
|
||||
impl Account {
|
||||
pub fn new(public_key: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let metadata = cx.new_model(|_| None);
|
||||
let async_metadata = metadata.clone();
|
||||
let refreshs = cx.global_mut::<AppRegistry>().refreshs();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let query = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().metadata(public_key).await })
|
||||
.await;
|
||||
|
||||
if let Ok(metadata) = query {
|
||||
_ = async_cx.update_model(&async_metadata, |a, b| {
|
||||
*a = metadata;
|
||||
b.notify();
|
||||
});
|
||||
};
|
||||
if let Some(refreshs) = refreshs.upgrade() {
|
||||
cx.observe(&refreshs, |this, _, cx| {
|
||||
this.load_metadata(cx);
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
Self {
|
||||
public_key,
|
||||
metadata,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_metadata(&self, cx: &mut ViewContext<Self>) {
|
||||
let mut async_cx = cx.to_async();
|
||||
let async_metadata = self.metadata.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn({
|
||||
let client = get_client();
|
||||
let public_key = self.public_key;
|
||||
|
||||
async move {
|
||||
let metadata = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().metadata(public_key).await })
|
||||
.await;
|
||||
|
||||
if let Ok(metadata) = metadata {
|
||||
_ = async_cx.update_model(&async_metadata, |model, cx| {
|
||||
*model = metadata;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Account {
|
||||
@@ -59,20 +73,25 @@ impl Render for Account {
|
||||
.reverse()
|
||||
.ghost()
|
||||
.icon(Icon::new(IconName::ChevronDownSmall))
|
||||
.when_some(self.metadata.read(cx).as_ref(), |this, metadata| {
|
||||
this.map(|this| {
|
||||
if let Some(picture) = metadata.picture.clone() {
|
||||
this.flex_shrink_0().child(
|
||||
img(format!("{}/?url={}&w=72&h=72&n=-1", IMAGE_SERVICE, picture))
|
||||
.size_5()
|
||||
.rounded_full()
|
||||
.object_fit(ObjectFit::Cover),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||
}
|
||||
})
|
||||
.map(|this| {
|
||||
if let Some(metadata) = self.metadata.read(cx).as_ref() {
|
||||
this.map(|this| {
|
||||
if let Some(picture) = metadata.picture.clone() {
|
||||
this.flex_shrink_0().child(
|
||||
img(format!("{}/?url={}&w=72&h=72&n=-1", IMAGE_SERVICE, picture))
|
||||
.size_5()
|
||||
.rounded_full()
|
||||
.object_fit(ObjectFit::Cover),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(img("brand/avatar.png").size_5().rounded_full())
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(img("brand/avatar.png").size_5().rounded_full())
|
||||
}
|
||||
})
|
||||
.popup_menu(move |this, _cx| {
|
||||
this.menu("Profile", Box::new(ToDo))
|
||||
|
||||
@@ -2,8 +2,7 @@ use super::{
|
||||
account::Account, chat::ChatPanel, onboarding::Onboarding, sidebar::Sidebar,
|
||||
welcome::WelcomePanel,
|
||||
};
|
||||
use crate::states::{account::AccountRegistry, chat::Room};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use crate::states::{app::AppRegistry, chat::Room};
|
||||
use gpui::{
|
||||
div, impl_actions, px, Axis, Context, Edges, InteractiveElement, IntoElement, Model,
|
||||
ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView, WindowContext,
|
||||
@@ -64,20 +63,31 @@ impl AppView {
|
||||
// Dock
|
||||
let dock = cx.new_view(|cx| DockArea::new(DOCK_AREA.id, Some(DOCK_AREA.version), cx));
|
||||
|
||||
cx.observe_global::<AccountRegistry>(move |view, cx| {
|
||||
if let Some(public_key) = cx.global::<AccountRegistry>().get() {
|
||||
Self::init_layout(view.dock.downgrade(), cx);
|
||||
// TODO: save dock state and load previous state on startup
|
||||
// Get current user from app state
|
||||
let current_user = cx.global::<AppRegistry>().current_user();
|
||||
|
||||
let view = cx.new_view(|cx| Account::new(public_key, cx));
|
||||
if let Some(current_user) = current_user.upgrade() {
|
||||
cx.observe(¤t_user, move |view, model, cx| {
|
||||
if let Some(public_key) = model.read(cx).clone().as_ref() {
|
||||
Self::init_layout(view.dock.downgrade(), cx);
|
||||
// TODO: save dock state and load previous state on startup
|
||||
|
||||
cx.update_model(&async_account, |model, cx| {
|
||||
*model = Some(view);
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
let view = cx.new_view(|cx| {
|
||||
let view = Account::new(*public_key, cx);
|
||||
// Initial load metadata
|
||||
view.load_metadata(cx);
|
||||
|
||||
view
|
||||
});
|
||||
|
||||
cx.update_model(&async_account, |model, cx| {
|
||||
*model = Some(view);
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
AppView {
|
||||
account,
|
||||
@@ -138,11 +148,10 @@ impl Render for AppView {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let modal_layer = Root::render_modal_layer(cx);
|
||||
let notification_layer = Root::render_notification_layer(cx);
|
||||
let state = cx.global::<AccountRegistry>();
|
||||
|
||||
let mut content = div().size_full().flex().flex_col();
|
||||
|
||||
if state.is_loading {
|
||||
if cx.global::<AppRegistry>().is_loading {
|
||||
content = content.child(div()).child(
|
||||
div()
|
||||
.flex_1()
|
||||
@@ -151,7 +160,7 @@ impl Render for AppView {
|
||||
.justify_center()
|
||||
.child(Indicator::new().small()),
|
||||
)
|
||||
} else if state.is_user_logged_in() {
|
||||
} else if let Some(account) = self.account.read(cx).as_ref() {
|
||||
content = content
|
||||
.child(
|
||||
TitleBar::new()
|
||||
@@ -165,9 +174,7 @@ impl Render for AppView {
|
||||
.justify_end()
|
||||
.gap_1()
|
||||
.px_2()
|
||||
.when_some(self.account.read(cx).as_ref(), |this, account| {
|
||||
this.child(account.clone())
|
||||
}),
|
||||
.child(account.clone()),
|
||||
),
|
||||
)
|
||||
.child(self.dock.clone())
|
||||
|
||||
@@ -15,10 +15,7 @@ use ui::{
|
||||
use super::message::RoomMessage;
|
||||
use crate::{
|
||||
get_client,
|
||||
states::{
|
||||
account::AccountRegistry,
|
||||
chat::{ChatRegistry, Room},
|
||||
},
|
||||
states::chat::{ChatRegistry, Room},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -202,7 +199,6 @@ impl RoomPanel {
|
||||
|
||||
fn send_message(&mut self, cx: &mut ViewContext<Self>) {
|
||||
let owner = self.owner;
|
||||
let current_user = cx.global::<AccountRegistry>().get().unwrap();
|
||||
let content = self.input.read(cx).text().to_string();
|
||||
let content2 = content.clone();
|
||||
let content3 = content2.clone();
|
||||
@@ -216,6 +212,14 @@ impl RoomPanel {
|
||||
let client = get_client();
|
||||
|
||||
async move {
|
||||
let current_user = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
let signer = client.signer().await.unwrap();
|
||||
signer.get_public_key().await.unwrap()
|
||||
})
|
||||
.await;
|
||||
|
||||
// Send message to all members
|
||||
async_cx
|
||||
.background_executor()
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
use gpui::{
|
||||
div, IntoElement,
|
||||
ParentElement, Render, Styled, View, ViewContext, VisualContext,
|
||||
};
|
||||
use crate::{constants::KEYRING_SERVICE, get_client, states::app::AppRegistry};
|
||||
use gpui::{div, IntoElement, ParentElement, Render, Styled, View, ViewContext, VisualContext};
|
||||
use nostr_sdk::prelude::*;
|
||||
use ui::{
|
||||
input::{InputEvent, TextInput},
|
||||
label::Label,
|
||||
};
|
||||
|
||||
use crate::{constants::KEYRING_SERVICE, get_client, states::account::AccountRegistry};
|
||||
|
||||
pub struct Onboarding {
|
||||
input: View<TextInput>,
|
||||
}
|
||||
@@ -40,7 +36,6 @@ impl Onboarding {
|
||||
let secret = keys.secret_key().to_secret_hex();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
let view_id = cx.entity_id();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn({
|
||||
@@ -50,10 +45,8 @@ impl Onboarding {
|
||||
async move {
|
||||
if task.await.is_ok() {
|
||||
_ = client.set_signer(keys).await;
|
||||
// Update global state
|
||||
_ = async_cx.update_global::<AccountRegistry, _>(|state, cx| {
|
||||
state.set_user(Some(public_key));
|
||||
cx.notify(Some(view_id));
|
||||
_ = async_cx.update_global::<AppRegistry, _>(|state, cx| {
|
||||
state.set_user(public_key, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use crate::{
|
||||
constants::IMAGE_SERVICE, get_client, states::account::AccountRegistry, utils::show_npub,
|
||||
};
|
||||
use crate::{constants::IMAGE_SERVICE, get_client, utils::show_npub};
|
||||
use gpui::{
|
||||
div, img, impl_actions, list, px, Context, ElementId, FocusHandle, InteractiveElement,
|
||||
IntoElement, ListAlignment, ListState, Model, ParentElement, Pixels, Render, RenderOnce,
|
||||
@@ -24,12 +22,12 @@ impl_actions!(contacts, [SelectContact]);
|
||||
struct ContactListItem {
|
||||
id: ElementId,
|
||||
public_key: PublicKey,
|
||||
metadata: Option<Metadata>,
|
||||
metadata: Metadata,
|
||||
selected: bool,
|
||||
}
|
||||
|
||||
impl ContactListItem {
|
||||
pub fn new(public_key: PublicKey, metadata: Option<Metadata>) -> Self {
|
||||
pub fn new(public_key: PublicKey, metadata: Metadata) -> Self {
|
||||
let id = SharedString::from(public_key.to_hex()).into();
|
||||
|
||||
Self {
|
||||
@@ -55,36 +53,6 @@ impl Selectable for ContactListItem {
|
||||
impl RenderOnce for ContactListItem {
|
||||
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||
let fallback = show_npub(self.public_key, 16);
|
||||
let mut content = div().flex().items_center().gap_2().text_sm();
|
||||
|
||||
if let Some(metadata) = self.metadata {
|
||||
content = content
|
||||
.map(|this| {
|
||||
if let Some(picture) = metadata.picture {
|
||||
this.flex_shrink_0().child(
|
||||
img(format!(
|
||||
"{}/?url={}&w=72&h=72&fit=cover&mask=circle&n=-1",
|
||||
IMAGE_SERVICE, picture
|
||||
))
|
||||
.size_6(),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||
}
|
||||
})
|
||||
.map(|this| {
|
||||
if let Some(display_name) = metadata.display_name {
|
||||
this.flex_1().child(display_name)
|
||||
} else {
|
||||
this.flex_1().child(fallback)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
content = content
|
||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||
.child(fallback)
|
||||
}
|
||||
|
||||
div()
|
||||
.id(self.id)
|
||||
@@ -95,7 +63,34 @@ impl RenderOnce for ContactListItem {
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_between()
|
||||
.child(content)
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.text_sm()
|
||||
.map(|this| {
|
||||
if let Some(picture) = self.metadata.picture {
|
||||
this.flex_shrink_0().child(
|
||||
img(format!(
|
||||
"{}/?url={}&w=72&h=72&fit=cover&mask=circle&n=-1",
|
||||
IMAGE_SERVICE, picture
|
||||
))
|
||||
.size_6(),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||
}
|
||||
})
|
||||
.map(|this| {
|
||||
if let Some(display_name) = self.metadata.display_name {
|
||||
this.flex_1().child(display_name)
|
||||
} else {
|
||||
this.flex_1().child(fallback)
|
||||
}
|
||||
}),
|
||||
)
|
||||
.when(self.selected, |this| {
|
||||
this.child(
|
||||
Icon::new(IconName::CircleCheck)
|
||||
@@ -141,20 +136,24 @@ impl ContactList {
|
||||
cx.foreground_executor()
|
||||
.spawn({
|
||||
let client = get_client();
|
||||
let current_user = cx.global::<AccountRegistry>().get();
|
||||
|
||||
async move {
|
||||
if let Some(public_key) = current_user {
|
||||
if let Ok(profiles) = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().contacts(public_key).await })
|
||||
.await
|
||||
{
|
||||
_ = async_cx.update_model(&async_contacts, |model, cx| {
|
||||
*model = profiles;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
let query: anyhow::Result<BTreeSet<Profile>, 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?;
|
||||
|
||||
Ok(profiles)
|
||||
})
|
||||
.await;
|
||||
|
||||
if let Ok(profiles) = query {
|
||||
_ = async_cx.update_model(&async_contacts, |model, cx| {
|
||||
*model = profiles;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -166,9 +165,7 @@ impl ContactList {
|
||||
count: profiles.len(),
|
||||
items: profiles
|
||||
.into_iter()
|
||||
.map(|contact| {
|
||||
ContactListItem::new(contact.public_key(), Some(contact.metadata()))
|
||||
})
|
||||
.map(|contact| ContactListItem::new(contact.public_key(), contact.metadata()))
|
||||
.collect(),
|
||||
};
|
||||
|
||||
@@ -194,7 +191,7 @@ impl ContactList {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selected(&self) -> Vec<PublicKey> {
|
||||
pub fn _selected(&self) -> Vec<PublicKey> {
|
||||
self.selected.clone().into_iter().collect()
|
||||
}
|
||||
|
||||
@@ -210,7 +207,7 @@ impl ContactList {
|
||||
let public_key = contact.public_key();
|
||||
let metadata = contact.metadata();
|
||||
|
||||
ContactListItem::new(public_key, Some(metadata))
|
||||
ContactListItem::new(contact.public_key(), metadata)
|
||||
.selected(self.selected.contains(&public_key))
|
||||
})
|
||||
.collect(),
|
||||
@@ -238,7 +235,7 @@ impl Render for ContactList {
|
||||
.flex()
|
||||
.flex_col()
|
||||
.gap_1()
|
||||
.child(div().font_semibold().child("Contacts"))
|
||||
.child(div().font_semibold().text_sm().child("Contacts"))
|
||||
.child(
|
||||
div()
|
||||
.p_1()
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use crate::{
|
||||
constants::IMAGE_SERVICE,
|
||||
get_client,
|
||||
states::chat::ChatRegistry,
|
||||
states::chat::Room,
|
||||
utils::get_room_id,
|
||||
utils::{ago, show_npub},
|
||||
states::{
|
||||
app::AppRegistry,
|
||||
chat::{ChatRegistry, Room},
|
||||
},
|
||||
utils::{ago, get_room_id, show_npub},
|
||||
views::app::{AddPanel, PanelKind},
|
||||
};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
@@ -20,12 +21,21 @@ use ui::{skeleton::Skeleton, theme::ActiveTheme, v_flex, Collapsible, Icon, Icon
|
||||
struct InboxListItem {
|
||||
id: SharedString,
|
||||
event: Event,
|
||||
metadata: Option<Metadata>,
|
||||
metadata: Model<Option<Metadata>>,
|
||||
}
|
||||
|
||||
impl InboxListItem {
|
||||
pub fn new(event: Event, metadata: Option<Metadata>, _cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
pub fn new(event: Event, metadata: Option<Metadata>, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let id = SharedString::from(get_room_id(&event.pubkey, &event.tags));
|
||||
let metadata = cx.new_model(|_| metadata);
|
||||
let refreshs = cx.global_mut::<AppRegistry>().refreshs();
|
||||
|
||||
if let Some(refreshs) = refreshs.upgrade() {
|
||||
cx.observe(&refreshs, |this, _, cx| {
|
||||
this.load_metadata(cx);
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
Self {
|
||||
id,
|
||||
@@ -34,8 +44,35 @@ impl InboxListItem {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_metadata(&self, cx: &mut ViewContext<Self>) {
|
||||
let mut async_cx = cx.to_async();
|
||||
let async_metadata = self.metadata.clone();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn({
|
||||
let client = get_client();
|
||||
let public_key = self.event.pubkey;
|
||||
|
||||
async move {
|
||||
let metadata = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().metadata(public_key).await })
|
||||
.await;
|
||||
|
||||
if let Ok(metadata) = metadata {
|
||||
_ = async_cx.update_model(&async_metadata, |model, cx| {
|
||||
*model = metadata;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
pub fn action(&self, cx: &mut WindowContext<'_>) {
|
||||
let room = Arc::new(Room::parse(&self.event, self.metadata.clone()));
|
||||
let metadata = self.metadata.read(cx).clone();
|
||||
let room = Arc::new(Room::parse(&self.event, metadata));
|
||||
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
panel: PanelKind::Room(room),
|
||||
@@ -53,7 +90,7 @@ impl Render for InboxListItem {
|
||||
.font_medium()
|
||||
.text_color(cx.theme().sidebar_accent_foreground);
|
||||
|
||||
if let Some(metadata) = self.metadata.clone() {
|
||||
if let Some(metadata) = self.metadata.read(cx).as_ref() {
|
||||
content = content
|
||||
.flex()
|
||||
.items_center()
|
||||
@@ -145,9 +182,6 @@ impl Inbox {
|
||||
}
|
||||
|
||||
pub fn load(&mut self, cx: &mut ViewContext<Self>) {
|
||||
// Hide loading indicator
|
||||
self.set_loading(cx);
|
||||
|
||||
// Get all room's events
|
||||
let events: Vec<Event> = cx.global::<ChatRegistry>().rooms.read(cx).clone();
|
||||
|
||||
@@ -179,15 +213,13 @@ impl Inbox {
|
||||
*model = Some(views);
|
||||
cx.notify()
|
||||
});
|
||||
|
||||
this.is_loading = false;
|
||||
cx.notify();
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn set_loading(&mut self, cx: &mut ViewContext<Self>) {
|
||||
self.is_loading = false;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Collapsible for Inbox {
|
||||
|
||||
@@ -57,8 +57,8 @@ impl Sidebar {
|
||||
.rounded(ButtonRounded::Large)
|
||||
.w_full()
|
||||
.on_click({
|
||||
let contact_list = contact_list.clone();
|
||||
move |_, cx| {
|
||||
let _contact_list = contact_list.clone();
|
||||
move |_, _cx| {
|
||||
// TODO: open room
|
||||
}
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user