chore: refactor root view

This commit is contained in:
2025-02-10 15:24:42 +07:00
parent 0347e8b3c5
commit bfc9588738
8 changed files with 40 additions and 62 deletions

View File

@@ -44,16 +44,14 @@ fn main() {
// Get client
let client = get_client();
let (signal_tx, mut signal_rx) = tokio::sync::mpsc::channel::<Signal>(4096);
let (signal_tx, mut signal_rx) = tokio::sync::mpsc::channel::<Signal>(2048);
spawn(async move {
// Add some bootstrap relays
_ = client.add_relay("wss://relay.damus.io/").await;
_ = client.add_relay("wss://relay.primal.net/").await;
_ = client.add_relay("wss://nos.lol/").await;
_ = client.add_discovery_relay("wss://user.kindpag.es/").await;
_ = client.add_discovery_relay("wss://directory.yabu.me/").await;
_ = client.add_relay("wss://user.kindpag.es/").await;
_ = client.add_relay("wss://directory.yabu.me/").await;
// Connect to all relays
_ = client.connect().await
@@ -217,6 +215,8 @@ fn main() {
.with_assets(Assets)
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new()))
.run(move |cx| {
// Initialize app global state
AppRegistry::set_global(cx);
// Initialize chat global state
ChatRegistry::set_global(cx);
@@ -260,14 +260,10 @@ fn main() {
})
.detach();
let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx));
let weak_root = root.downgrade();
let window_handle = window.window_handle();
let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx));
let task = cx.read_credentials(KEYRING_SERVICE);
// Initialize app global state
AppRegistry::set_global(weak_root, cx);
cx.spawn(|mut cx| async move {
if let Ok(Some((npub, secret))) = task.await {
let (tx, rx) = oneshot::channel::<NostrProfile>();
@@ -295,24 +291,18 @@ fn main() {
.detach();
if let Ok(profile) = rx.await {
cx.update_window(window_handle, |_, window, cx| {
cx.update_global::<AppRegistry, _>(|this, cx| {
this.set_user(Some(profile.clone()));
this.set_root_view(
app::init(profile, window, cx).into(),
cx,
);
_ = cx.update_window(window_handle, |_, window, cx| {
window.replace_root(cx, |window, cx| {
Root::new(app::init(profile, window, cx).into(), window, cx)
});
})
.unwrap();
});
}
} else {
cx.update_window(window_handle, |_, window, cx| {
cx.update_global::<AppRegistry, _>(|this, cx| {
this.set_root_view(onboarding::init(window, cx).into(), cx);
_ = cx.update_window(window_handle, |_, window, cx| {
window.replace_root(cx, |window, cx| {
Root::new(onboarding::init(window, cx).into(), window, cx)
});
})
.unwrap();
});
}
})
.detach();

View File

@@ -226,15 +226,15 @@ impl AppView {
}
fn on_logout_action(&mut self, _action: &Logout, window: &mut Window, cx: &mut Context<Self>) {
cx.update_global::<AppRegistry, _>(|this, cx| {
cx.background_executor()
.spawn(async move { get_client().reset().await })
.detach();
cx.background_spawn(async move { get_client().reset().await })
.detach();
// Remove user
cx.update_global::<AppRegistry, _>(|this, _cx| {
this.set_user(None);
// Update root view
this.set_root_view(onboarding::init(window, cx).into(), cx);
});
window.replace_root(cx, |window, cx| {
Root::new(onboarding::init(window, cx).into(), window, cx)
});
}
}

View File

@@ -13,12 +13,12 @@ use ui::{
input::{InputEvent, TextInput},
notification::NotificationType,
theme::{scale::ColorScaleStep, ActiveTheme},
ContextModal, Size, StyledExt,
ContextModal, Root, Size, StyledExt,
};
use super::app;
const ALPHA_MESSAGE: &str = "Coop is in the alpha stage; it doesn't store any credentials. You will need to log in again when you relanch.";
const ALPHA_MESSAGE: &str = "Coop is in the alpha stage; it doesn't store any credentials. You will need to log in again when you relaunch.";
const JOIN_URL: &str = "https://start.njump.me/";
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Onboarding> {
@@ -113,9 +113,12 @@ impl Onboarding {
if let Ok(profile) = rx.await {
_ = cx.update_window(window_handle, |_, window, cx| {
cx.update_global::<AppRegistry, _>(|this, cx| {
cx.update_global::<AppRegistry, _>(|this, _cx| {
this.set_user(Some(profile.clone()));
this.set_root_view(app::init(profile, window, cx).into(), cx);
});
window.replace_root(cx, |window, cx| {
Root::new(app::init(profile, window, cx).into(), window, cx)
});
})
}
@@ -178,9 +181,12 @@ impl Onboarding {
if let Ok(profile) = rx.await {
_ = cx.update_window(window_handle, |_, window, cx| {
cx.update_global::<AppRegistry, _>(|this, cx| {
cx.update_global::<AppRegistry, _>(|this, _cx| {
this.set_user(Some(profile.clone()));
this.set_root_view(app::init(profile, window, cx).into(), cx);
});
window.replace_root(cx, |window, cx| {
Root::new(app::init(profile, window, cx).into(), window, cx)
});
})
}
@@ -371,7 +377,7 @@ impl Render for Onboarding {
)
.child(
div()
.text_align(gpui::TextAlign::Center)
.text_center()
.child(
div()
.text_lg()