Skip to content

Commit

Permalink
chore: Create a new deck feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh0412 committed Apr 3, 2024
1 parent 3e4bfde commit 6f85f18
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 483 deletions.
1 change: 1 addition & 0 deletions src/components/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use gpui::{div, prelude::*, MouseButton, Render, View, WindowContext};
pub mod deck_detail;
pub mod deck_list;
pub mod flash_card;
pub mod new_deck_form;

use crate::state::{StackableViewState, ViewState};

Expand Down
32 changes: 29 additions & 3 deletions src/components/deck/deck_detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use gpui::{
use crate::{
models::{
builder::Builder,
collection::{self, Collection},
collection::{self, Collection, CollectionBuilder},
queue::QueueBuilder,
},
repositories::deck::DeckStat,
state::{StackableView, StackableViewState},
theme::Theme,
ui::{button::button::Button, clickable::Clickable},
Expand All @@ -35,13 +36,38 @@ impl Render for DeckDetail {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let theme = cx.global::<Theme>();
let col = cx.global::<collection::Collection>();
let deck = self.get_deck(col);
let mut deck = self.get_deck(col);

let timing_at_stamp = CollectionBuilder::timing_for_timestamp(
&col.storage.conn,
chrono::Local::now().timestamp(),
);

let decks_stats =
Deck::get_decks_stats(&col.storage.conn, timing_at_stamp.days_elapsed).unwrap();

if let Some(st) = decks_stats.get(&deck.id.unwrap()) {
deck.stats = Some(DeckStat {
id: Some(deck.id.unwrap()),
new: st.new,
learning: st.learning,
due: st.due,
});
}

let mut queue_builder = QueueBuilder::new(self.deck_id);
queue_builder.collect_cards(&col);
let card_queue = queue_builder.build().unwrap();

let stats = deck.get_deck_stats();
let stats = match deck.stats {
Some(stats) => stats,
None => DeckStat {
id: Some(deck.id.unwrap()),
new: 0,
learning: 0,
due: 0,
},
};

div()
.flex()
Expand Down
124 changes: 78 additions & 46 deletions src/components/deck/deck_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gpui::{
div, AnyView, InteractiveElement, IntoElement, ParentElement, Render, RenderOnce, Styled, View,
VisualContext, WindowContext,
div, AnyView, InteractiveElement, IntoElement, ParentElement, Pixels, Render, RenderOnce,
Styled, View, VisualContext, WindowContext,
};

use crate::{
Expand All @@ -11,10 +11,11 @@ use crate::{
repositories::deck::DeckStat,
state::{StackableView, StackableViewState},
theme::Theme,
ui::{button::button::Button, clickable::Clickable},
Deck,
};

use super::deck_detail::DeckDetailBuilder;
use super::{deck_detail::DeckDetailBuilder, new_deck_form::NewDeckFormBuilder};

pub struct DeckListView;

Expand Down Expand Up @@ -43,60 +44,83 @@ impl DeckListView {
decks
.into_iter()
.map(|mut deck| {
let st = decks_stats.get(&deck.id.unwrap()).unwrap();
deck.stats = Some(DeckStat {
id: Some(deck.id.unwrap()),
new: st.new,
learning: st.learning,
due: st.due,
});
let st = decks_stats.get(&deck.id.unwrap());

if let Some(st) = st {
deck.stats = Some(DeckStat {
id: Some(deck.id.unwrap()),
new: st.new,
learning: st.learning,
due: st.due,
});
}

deck
})
.collect()
}

fn new_deck_click(&mut self, _event: &gpui::ClickEvent, cx: &mut gpui::ViewContext<Self>) {
StackableViewState::update(|state, cx| state.push(NewDeckFormBuilder {}, cx), cx);
}
}

impl Render for DeckListView {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::prelude::IntoElement {
let theme = cx.global::<Theme>();
let collection = cx.global::<crate::Collection>();

div().flex().size_full().justify_center().child(
div().mt_20().child(
div()
.border_1()
.border_color(theme.crust)
.rounded_xl()
.text_color(theme.text)
.p_3()
.child(
div()
.flex()
.flex_row()
.text_sm()
.child(div().px_2().min_w_80().child(format!("Deck")))
.child(div().min_w_20().flex().justify_center().child("New"))
.child(div().min_w_20().flex().justify_center().child("Learn"))
.child(div().min_w_20().flex().justify_center().child("Due"))
.pb_2()
.border_b_1()
.border_color(theme.crust)
.mb_2(),
)
.children(
self.get_all_decks_and_stats(collection)
.into_iter()
.map(|deck| {
let deck_id = deck.id.unwrap();
HocListItem::init(
cx.new_view(|_| ListItem::new(deck)).into(),
deck_id,
)
})
.collect::<Vec<_>>(),
div()
.flex()
.flex_col()
.size_full()
.justify_between()
.child(
div().mt_20().flex().justify_center().child(
div()
.border_1()
.border_color(theme.crust)
.rounded_xl()
.text_color(theme.text)
.p_3()
.child(
div()
.flex()
.flex_row()
.text_sm()
.child(div().px_2().min_w_80().child(format!("Deck")))
.child(div().min_w_20().flex().justify_center().child("New"))
.child(div().min_w_20().flex().justify_center().child("Learn"))
.child(div().min_w_20().flex().justify_center().child("Due"))
.pb_2()
.border_b_1()
.border_color(theme.crust)
.mb_2(),
)
.children(
self.get_all_decks_and_stats(collection)
.into_iter()
.map(|deck| {
let deck_id = deck.id.unwrap();
HocListItem::init(
cx.new_view(|_| ListItem::new(deck)).into(),
deck_id,
)
})
.collect::<Vec<_>>(),
),
),
)
.child(
div().mb_16().flex().justify_center().child(
div().w(Pixels(300.0)).flex().justify_center().child(
div().child(
Button::new("create_deck", "Create Deck")
.on_click(cx.listener(Self::new_deck_click)),
),
),
),
)
),
)
}
}

Expand Down Expand Up @@ -155,7 +179,15 @@ impl Render for ListItem {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
let theme = cx.global::<Theme>();

let stats = self.deck.stats.as_ref().unwrap();
let stats = match self.deck.stats.as_ref() {
Some(stats) => stats,
None => &DeckStat {
id: None,
new: 0,
learning: 0,
due: 0,
},
};

div()
.flex()
Expand Down
96 changes: 96 additions & 0 deletions src/components/deck/new_deck_form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use gpui::{
div, AnyView, ClickEvent, FontWeight, ParentElement, Pixels, Render, Styled, View, ViewContext,
VisualContext, WindowContext,
};

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

struct NewDeckForm {
text_input: TextField,
}

impl NewDeckForm {
pub fn view(cx: &mut WindowContext) -> View<Self> {
cx.new_view(|cx: &mut gpui::ViewContext<'_, NewDeckForm>| Self {
text_input: TextField::new(cx, "Deck Name".to_string()),
})
}

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

let mut deck = repositories::deck::Deck::new(text);
match deck.save(&collection.storage.conn) {
Ok(_) => {
StackableViewState::update(|state, cx| state.pop(cx), cx);
cx.notify();
}
Err(e) => {
log::error!("Error saving deck: {:?}", e);
}
}
}
}

impl Render for NewDeckForm {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::prelude::IntoElement {
let theme = cx.global::<Theme>();

div().flex().size_full().justify_center().child(
div().mt_20().child(
div()
.flex()
.w_full()
.flex_col()
.text_color(theme.text)
.relative()
.h_full()
.child(
div()
.w(Pixels(500.0))
.child(
div()
.text_xl()
.font_weight(FontWeight::EXTRA_BOLD)
.pb_5()
.border_b_1()
.border_color(theme.crust)
.child("New Deck"),
)
.child(div().mt_6().child(self.text_input.clone()))
.child(
div()
.mt_6()
.justify_end()
.flex()
.child(
Button::new("btn-save", "Create")
.on_click(cx.listener(Self::save_click)),
)
.child(div().ml_2().child(
Button::new("btn-cancel", "Cancel").on_click(
|event, _cx| {
log::info!("Button clicked: {:?}", event);
},
),
)),
),
),
),
)
}
}

pub struct NewDeckFormBuilder {}

impl StackableView for NewDeckFormBuilder {
fn build(&self, cx: &mut WindowContext) -> AnyView {
NewDeckForm::view(cx).into()
}
}
2 changes: 1 addition & 1 deletion src/repositories/flash_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use time::OffsetDateTime;

use crate::models::collection::CollectionBuilder;

use super::{card_data::CardData, session::Session};
use super::card_data::CardData;

#[derive(PartialEq, Debug, Clone)]
pub enum Status {
Expand Down
1 change: 0 additions & 1 deletion src/ui/components.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod button;
pub mod editor;
pub mod text_field;
5 changes: 0 additions & 5 deletions src/ui/components/editor.rs

This file was deleted.

Loading

0 comments on commit 6f85f18

Please sign in to comment.