Skip to content

Commit

Permalink
chore: refactor add_card view
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh0412 committed Apr 8, 2024
1 parent 831e1a6 commit 7fe0b6b
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 200 deletions.
47 changes: 37 additions & 10 deletions src/components/add_card.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use gpui::{
div, prelude::*, ClickEvent, EventEmitter, Pixels, Render, View, ViewContext, WindowContext,
div, prelude::*, AnyView, ClickEvent, EventEmitter, Pixels, Render, View, ViewContext,
WindowContext,
};

use crate::{
components::tab_bar_container::TabEvent,
repositories,
state::{StackableView, StackableViewState},
theme::Theme,
ui::{button::button::Button, clickable::Clickable, text_field::text_field::TextField},
Deck,
};

pub struct AddCardView {
deck: Deck,
front_input: TextField,
back_input: TextField,
deck_input: TextField,
Expand All @@ -18,26 +22,39 @@ pub struct AddCardView {
impl EventEmitter<TabEvent> for AddCardView {}

impl AddCardView {
pub fn view(cx: &mut WindowContext) -> View<Self> {
cx.new_view(|cx: &mut gpui::ViewContext<'_, AddCardView>| Self {
front_input: TextField::new(cx, "".to_string()),
back_input: TextField::new(cx, "".to_string()),
deck_input: TextField::new(cx, "".to_string()),
pub fn view(deck_id: u32, cx: &mut WindowContext) -> View<Self> {
cx.new_view(|cx: &mut gpui::ViewContext<'_, AddCardView>| {
let deck = repositories::deck::Deck::load(
deck_id,
&cx.global::<crate::Collection>().storage.conn,
)
.unwrap();

let deck_input = TextField::new(cx, "".to_string());

deck_input.view.update(cx, |view, _| {
view.text = deck.name.clone();
});

Self {
deck,
front_input: TextField::new(cx, "".to_string()),
back_input: TextField::new(cx, "".to_string()),
deck_input,
}
})
}

fn save_click(&mut self, _event: &ClickEvent, cx: &mut ViewContext<Self>) {
let collection = cx.global::<crate::Collection>();
let front = &self.front_input.view.read(&cx).text;
let back = &self.back_input.view.read(&cx).text;
let deck = &self.deck_input.view.read(&cx).text;

let mut card =
repositories::flash_card::FlashCard::new(deck.parse().unwrap(), front, back, None);
repositories::flash_card::FlashCard::new(self.deck.id.unwrap(), front, back, None);
match card.save(&collection.storage.conn) {
Ok(_) => {
cx.emit(TabEvent::Deck);
cx.notify();
StackableViewState::update(|state, cx| state.pop(cx), cx);
}
Err(e) => {
log::error!("Error saving card: {:?}", e);
Expand Down Expand Up @@ -106,3 +123,13 @@ impl Render for AddCardView {
)
}
}

pub struct AddCardBuilder {
pub deck_id: u32,
}

impl StackableView for AddCardBuilder {
fn build(&self, cx: &mut WindowContext) -> AnyView {
AddCardView::view(self.deck_id, cx).into()
}
}
15 changes: 0 additions & 15 deletions src/components/card_browser.rs

This file was deleted.

10 changes: 8 additions & 2 deletions src/components/deck/deck_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gpui::{
};

use crate::{
components::shared::icon::Icon,
components::{add_card::AddCardBuilder, shared::icon::Icon},
models::{
collection::{self, Collection, CollectionBuilder},
deck::get_decks,
Expand Down Expand Up @@ -149,7 +149,13 @@ impl HocListItem {

fn build_deck_menu(cx: &mut WindowContext, deck_id: u32) -> View<ContextMenu> {
ContextMenu::build(cx, move |menu, _wc| {
menu.entry("Delete", None, move |wc| {
menu.entry("Add new card", None, move |wc| {
StackableViewState::update(
|state, cx| state.push(AddCardBuilder { deck_id }, cx),
wc,
);
})
.entry("Delete", None, move |wc| {
let collection = wc.global::<Collection>();
Deck::delete(deck_id, &collection.storage.conn).unwrap();
})
Expand Down
1 change: 0 additions & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod add_card;
pub mod card_browser;
pub mod deck;
pub mod shared;
pub mod tab_bar_container;
Expand Down
35 changes: 0 additions & 35 deletions src/components/tab_bar_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ impl TabBarView {
#[derive(PartialEq)]
pub enum TabEvent {
Deck,
Add,
Browse,
}

impl EventEmitter<TabEvent> for TabBarView {}
Expand All @@ -51,12 +49,10 @@ impl RenderOnce for TabBarContainer {

let view_clone1 = Rc::clone(&self.view);
let view_clone2 = Rc::clone(&self.view);
let view_clone3 = Rc::clone(&self.view);

let bg_hover = theme.overlay0;
let selected_deck_clone = Rc::clone(&self.selected);
let selected_add_clone = Rc::clone(&self.selected);
let selected_browse_clone = Rc::clone(&self.selected);

div()
.bg(theme.mantle)
Expand All @@ -80,36 +76,5 @@ impl RenderOnce for TabBarContainer {
})
}),
)
.child(
div()
.group("tab_bar")
.px_2()
.py_2()
.text_color(theme.text)
.child(Icon::FilePlus)
.hover(|s| s.rounded_md().bg(bg_hover))
.on_mouse_down(gpui::MouseButton::Left, move |_ev, cx| {
view_clone2.borrow_mut().update(cx, |_e, cx| {
cx.emit(TabEvent::Add);
cx.notify();
*selected_add_clone.borrow_mut() = TabEvent::Add;
})
}),
)
.child(
div()
.group("tab_bar")
.px_2()
.py_2()
.hover(|s| s.rounded_md().bg(bg_hover))
.child(Icon::FileSearch)
.on_mouse_down(gpui::MouseButton::Left, move |_ev, cx| {
view_clone3.borrow_mut().update(cx, |_e, cx| {
cx.emit(TabEvent::Browse);
cx.notify();
*selected_browse_clone.borrow_mut() = TabEvent::Browse;
})
}),
)
}
}
4 changes: 0 additions & 4 deletions src/components/tab_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use crate::state::TabView;
use gpui::{div, prelude::*, AnyView, ViewContext};

use super::{
add_card::AddCardView,
card_browser::CardBrowserView,
deck::DeckView,
tab_bar_container::{TabBarContainer, TabEvent},
};
Expand Down Expand Up @@ -37,8 +35,6 @@ impl TabView for TabPanelBuilder {
TabEvent::Deck => {
subscriber.content = DeckView::view(cx).into();
}
TabEvent::Add => subscriber.content = AddCardView::view(cx).into(),
TabEvent::Browse => subscriber.content = CardBrowserView::view(cx).into(),
},
)
.detach();
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/flash_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ impl FlashCard {
last_studied_time.to_string(),
self.due,
self.queue.clone() as i8,
self.data,
id.to_string(),
self.data
]
)?;
}
Expand Down
1 change: 0 additions & 1 deletion src/ui/components.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod button;
pub mod context_menu;
pub mod list;
pub mod popover;
pub mod popover_menu;
pub mod text_field;
88 changes: 0 additions & 88 deletions src/ui/components/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ enum ContextMenuItem {
handler: Rc<dyn Fn(&mut WindowContext)>,
action: Option<Box<dyn Action>>,
},
CustomEntry {
entry_render: Box<dyn Fn(&mut WindowContext) -> AnyElement>,
handler: Rc<dyn Fn(&mut WindowContext)>,
},
}

pub struct ContextMenu {
items: Vec<ContextMenuItem>,
focus_handle: FocusHandle,
action_context: Option<FocusHandle>,
selected_index: Option<usize>,
delayed: bool,
clicked: bool,
Expand Down Expand Up @@ -54,7 +49,6 @@ impl ContextMenu {
Self {
items: Default::default(),
focus_handle,
action_context: None,
selected_index: None,
delayed: false,
clicked: false,
Expand All @@ -65,11 +59,6 @@ impl ContextMenu {
})
}

pub fn context(mut self, focus: FocusHandle) -> Self {
self.action_context = Some(focus);
self
}

pub fn entry(
mut self,
label: impl Into<SharedString>,
Expand All @@ -84,63 +73,11 @@ impl ContextMenu {
self
}

pub fn custom_entry(
mut self,
entry_render: impl Fn(&mut WindowContext) -> AnyElement + 'static,
handler: impl Fn(&mut WindowContext) + 'static,
) -> Self {
self.items.push(ContextMenuItem::CustomEntry {
entry_render: Box::new(entry_render),
handler: Rc::new(handler),
});
self
}

pub fn action(mut self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
self.items.push(ContextMenuItem::Entry {
label: label.into(),
action: Some(action.boxed_clone()),
handler: Rc::new(move |cx| cx.dispatch_action(action.boxed_clone())),
});
self
}

pub fn link(mut self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
self.items.push(ContextMenuItem::Entry {
label: label.into(),
action: Some(action.boxed_clone()),
handler: Rc::new(move |cx| cx.dispatch_action(action.boxed_clone())),
});
self
}

pub fn confirm(&mut self, cx: &mut ViewContext<Self>) {
match self.selected_index.and_then(|ix| self.items.get(ix)) {
Some(
ContextMenuItem::Entry { handler, .. }
| ContextMenuItem::CustomEntry { handler, .. },
) => (handler)(cx),
_ => {}
}

cx.emit(DismissEvent);
}

pub fn cancel(&mut self, cx: &mut ViewContext<Self>) {
cx.emit(DismissEvent);
cx.emit(DismissEvent);
}

pub fn select_last(&mut self) -> Option<usize> {
for (ix, item) in self.items.iter().enumerate().rev() {
if item.is_selectable() {
self.selected_index = Some(ix);
return Some(ix);
}
}
None
}

pub fn on_action_dispatch(&mut self, dispatched: &Box<dyn Action>, cx: &mut ViewContext<Self>) {
if self.clicked {
cx.propagate();
Expand Down Expand Up @@ -178,12 +115,6 @@ impl ContextMenu {
}
}

impl ContextMenuItem {
fn is_selectable(&self) -> bool {
matches!(self, Self::Entry { .. } | Self::CustomEntry { .. })
}
}

impl Render for ContextMenu {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
div().elevation_2(cx).flex().flex_row().child(
Expand Down Expand Up @@ -242,25 +173,6 @@ impl Render for ContextMenu {
)
.into_any_element()
}
ContextMenuItem::CustomEntry {
entry_render,
handler,
} => {
let handler = handler.clone();
let menu = cx.view().downgrade();
ListItem::new(ix)
.inset(true)
.on_click(move |_, cx| {
handler(cx);
menu.update(cx, |menu, cx| {
menu.clicked = true;
cx.emit(DismissEvent);
})
.ok();
})
.child(entry_render(cx))
.into_any_element()
}
},
))),
)
Expand Down
Loading

0 comments on commit 7fe0b6b

Please sign in to comment.