chore: refactor root view
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -146,7 +146,6 @@ dependencies = [
|
||||
"gpui",
|
||||
"nostr-sdk",
|
||||
"state",
|
||||
"ui",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -7,7 +7,6 @@ publish = false
|
||||
[dependencies]
|
||||
common = { path = "../common" }
|
||||
state = { path = "../state" }
|
||||
ui = { path = "../ui" }
|
||||
|
||||
gpui.workspace = true
|
||||
nostr-sdk.workspace = true
|
||||
|
||||
@@ -2,21 +2,19 @@ use common::{
|
||||
constants::{ALL_MESSAGES_SUB_ID, NEW_MESSAGE_SUB_ID},
|
||||
profile::NostrProfile,
|
||||
};
|
||||
use gpui::{AnyView, App, AppContext, Global, WeakEntity};
|
||||
use gpui::{App, AppContext, Global};
|
||||
use nostr_sdk::prelude::*;
|
||||
use state::get_client;
|
||||
use std::time::Duration;
|
||||
use ui::Root;
|
||||
|
||||
pub struct AppRegistry {
|
||||
root: WeakEntity<Root>,
|
||||
user: Option<NostrProfile>,
|
||||
}
|
||||
|
||||
impl Global for AppRegistry {}
|
||||
|
||||
impl AppRegistry {
|
||||
pub fn set_global(root: WeakEntity<Root>, cx: &mut App) {
|
||||
pub fn set_global(cx: &mut App) {
|
||||
cx.observe_global::<Self>(|cx| {
|
||||
if let Some(profile) = cx.global::<Self>().user() {
|
||||
let client = get_client();
|
||||
@@ -49,7 +47,7 @@ impl AppRegistry {
|
||||
all_messages_sub_id,
|
||||
all_messages,
|
||||
Some(SubscribeAutoCloseOptions::default().exit_policy(
|
||||
ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(5)),
|
||||
ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(3)),
|
||||
)),
|
||||
)
|
||||
.await;
|
||||
@@ -57,14 +55,14 @@ impl AppRegistry {
|
||||
// Subscribe for new message
|
||||
_ = client
|
||||
.subscribe_with_id(new_message_sub_id, new_message, None)
|
||||
.await;
|
||||
.await
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.set_global(Self { root, user: None });
|
||||
cx.set_global(Self { user: None });
|
||||
}
|
||||
|
||||
pub fn set_user(&mut self, profile: Option<NostrProfile>) {
|
||||
@@ -74,12 +72,4 @@ impl AppRegistry {
|
||||
pub fn user(&self) -> Option<NostrProfile> {
|
||||
self.user.clone()
|
||||
}
|
||||
|
||||
pub fn set_root_view(&self, view: AnyView, cx: &mut App) {
|
||||
if let Err(e) = self.root.update(cx, |this, cx| {
|
||||
this.set_view(view, cx);
|
||||
}) {
|
||||
println!("Error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ pub fn create_qr(data: &str) -> Result<PathBuf, anyhow::Error> {
|
||||
let config_dir = config_dir().expect("Config directory not found");
|
||||
let path = config_dir.join("Coop/nostr_connect.png");
|
||||
|
||||
qrcode_generator::to_png_to_file(data, QrCodeEcc::Low, 1024, &path)?;
|
||||
qrcode_generator::to_png_to_file(data, QrCodeEcc::Low, 512, &path)?;
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
@@ -212,12 +212,6 @@ impl Root {
|
||||
pub fn view(&self) -> &AnyView {
|
||||
&self.view
|
||||
}
|
||||
|
||||
/// Set the root view of the Root.
|
||||
pub fn set_view(&mut self, view: AnyView, cx: &mut Context<Self>) {
|
||||
self.view = view;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Root {
|
||||
|
||||
Reference in New Issue
Block a user