wip: refactor

This commit is contained in:
reya
2025-01-08 12:34:13 +07:00
parent fe24f84b02
commit 122ee4c8bb
13 changed files with 360 additions and 271 deletions
+51 -32
View File
@@ -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))