wip: startup flow

This commit is contained in:
2024-11-21 08:43:42 +07:00
parent fb71f02ef3
commit c60a37a245
15 changed files with 910 additions and 82 deletions

View File

@@ -0,0 +1,57 @@
use components::theme::ActiveTheme;
use gpui::*;
use crate::state::AppState;
use super::{chatspace::ChatSpaceView, setup::SetupView};
pub struct AppView {
onboarding: Model<Option<AnyView>>, // TODO: create onboarding view
setup: View<SetupView>,
chat_space: View<ChatSpaceView>,
}
impl AppView {
pub fn new(cx: &mut ViewContext<'_, Self>) -> AppView {
// Onboarding model
let onboarding = cx.new_model(|_| None);
// Setup view
let setup = cx.new_view(SetupView::new);
// Chat Space view
let chat_space = cx.new_view(ChatSpaceView::new);
cx.foreground_executor()
.spawn(async move {
// TODO: create onboarding view for the first time open app
})
.detach();
AppView {
onboarding,
setup,
chat_space,
}
}
}
impl Render for AppView {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let mut content = div().size_full().flex().items_center().justify_center();
if cx.global::<AppState>().accounts.is_empty() {
content = content.child(self.setup.clone())
} else {
content = content.child(self.chat_space.clone())
}
if let Some(onboarding) = self.onboarding.read(cx).as_ref() {
content = content.child(onboarding.clone())
}
div()
.bg(cx.theme().background)
.text_color(rgb(0xFFFFFF))
.size_full()
.child(content)
}
}

View File

@@ -0,0 +1,24 @@
use gpui::*;
pub struct ChatSpaceView {
pub text: SharedString,
}
impl ChatSpaceView {
pub fn new(_cx: &mut ViewContext<'_, Self>) -> ChatSpaceView {
ChatSpaceView {
text: "chat".into(),
}
}
}
impl Render for ChatSpaceView {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.flex()
.size_full()
.justify_center()
.items_center()
.child(format!("Hello, {}!", &self.text))
}
}

View File

@@ -0,0 +1,3 @@
pub mod app;
pub mod chatspace;
pub mod setup;

View File

@@ -0,0 +1,48 @@
use components::{
input::{InputEvent, TextInput},
label::Label,
};
use gpui::*;
use nostr_sdk::prelude::*;
use crate::state::AppState;
pub struct SetupView {
input: View<TextInput>,
}
impl SetupView {
pub fn new(cx: &mut ViewContext<'_, Self>) -> SetupView {
let input = cx.new_view(|cx| {
let mut input = TextInput::new(cx);
input.set_size(components::Size::Medium, cx);
input
});
cx.subscribe(&input, move |_, text_input, input_event, cx| {
if let InputEvent::PressEnter = input_event {
let content = text_input.read(cx).text().to_string();
if let Ok(public_key) = PublicKey::parse(content) {
cx.global_mut::<AppState>().accounts.insert(public_key);
cx.notify();
};
}
})
.detach();
SetupView { input }
}
}
impl Render for SetupView {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.size_1_3()
.flex()
.flex_col()
.gap_1()
.child(Label::new("Private Key").text_sm())
.child(self.input.clone())
}
}