diff --git a/src/components/deck.rs b/src/components/deck.rs index 880d0d0..de74e22 100644 --- a/src/components/deck.rs +++ b/src/components/deck.rs @@ -2,6 +2,7 @@ use gpui::{div, prelude::*, MouseButton, Render, View, WindowContext}; pub mod deck_detail; pub mod deck_list; +pub mod flash_card; use crate::state::{StackableViewState, ViewState}; diff --git a/src/components/deck/deck_detail.rs b/src/components/deck/deck_detail.rs index b5ad4ab..3d2cac6 100644 --- a/src/components/deck/deck_detail.rs +++ b/src/components/deck/deck_detail.rs @@ -1,10 +1,17 @@ use gpui::{ - div, green, AnyView, Font, FontWeight, IntoElement, ParentElement, Pixels, Render, Styled, - ViewContext, VisualContext, WindowContext, + div, AnyView, FontWeight, IntoElement, ParentElement, Pixels, Render, Styled, ViewContext, + VisualContext, WindowContext, }; use rusqlite::Connection; -use crate::{state::StackableView, theme::Theme, ui::button::button::Button, Deck, FlashCard}; +use crate::{ + state::{StackableView, StackableViewState}, + theme::Theme, + ui::{button::button::Button, clickable::Clickable}, + Deck, FlashCard, +}; + +use super::flash_card::FlashCardBuilder; pub struct DeckDetail { pub deck_id: i32, @@ -12,7 +19,7 @@ pub struct DeckDetail { impl DeckDetail { pub fn view(deck_id: i32, cx: &mut WindowContext) -> AnyView { - cx.new_view(|vc| Self { deck_id }).into() + cx.new_view(|_vc| Self { deck_id }).into() } fn get_deck(&self) -> Deck { @@ -89,13 +96,16 @@ impl Render for DeckDetail { ), ), ) - .child( - div() - .mt_5() - .flex() - .justify_center() - .child(Button::new("study-btn", "Study Now")), - ), + .child(div().mt_5().flex().justify_center().child( + Button::new("study-btn", "Study Now").on_click(move |_e, cx| { + StackableViewState::update( + |state, cx| { + state.push(FlashCardBuilder { cards: &deck.cards }, cx) + }, + cx, + ); + }), + )), ), ) } diff --git a/src/components/deck/flash_card.rs b/src/components/deck/flash_card.rs new file mode 100644 index 0000000..db07a0e --- /dev/null +++ b/src/components/deck/flash_card.rs @@ -0,0 +1,57 @@ +use gpui::{ + div, green, AnyView, FontWeight, IntoElement, ParentElement, Pixels, Render, Styled, + ViewContext, VisualContext, WindowContext, +}; + +use crate::{repositories::flash_card, state::StackableView, theme::Theme}; + +pub struct FlashCard { + cards: Vec, +} + +impl FlashCard { + pub fn view(cx: &mut WindowContext, cards: Vec) -> AnyView { + cx.new_view(|vc| Self { cards }).into() + } +} + +impl Render for FlashCard { + fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { + let theme = cx.global::(); + + let card = self.cards.first().unwrap(); + + div() + .flex() + .w_full() + .flex_col() + .pt_20() + .text_color(theme.text) + .justify_center() + .items_center() + .child( + div() + .max_w(Pixels(500.0)) + .child( + div() + .text_xl() + .font_weight(FontWeight::EXTRA_BOLD) + .pb_5() + .border_b_1() + .border_color(theme.crust) + .child(card.get_question().to_string()), + ) + .child(div().pt_5().text_xl().child(card.get_answer().to_string())), + ) + } +} + +pub struct FlashCardBuilder<'a> { + pub cards: &'a Vec, +} + +impl<'a> StackableView for FlashCardBuilder<'a> { + fn build(&self, cx: &mut WindowContext) -> AnyView { + FlashCard::view(cx, self.cards.clone().into()) + } +} diff --git a/src/repositories/flash_card.rs b/src/repositories/flash_card.rs index b685266..a9f0b21 100644 --- a/src/repositories/flash_card.rs +++ b/src/repositories/flash_card.rs @@ -7,13 +7,14 @@ use chrono::{DateTime, Utc}; use rusqlite::{Connection, Result}; use time::OffsetDateTime; -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] pub enum Status { New, Learning, Due, } +#[derive(Clone)] pub struct FlashCard { id: Option, deck_id: i32, @@ -23,7 +24,6 @@ pub struct FlashCard { last_studied_time: Option, ef: f32, interval: f64, - status: Status, performance_metrics: HashMap, } @@ -39,7 +39,6 @@ impl FlashCard { performance_metrics: HashMap::new(), ef: ef.unwrap_or(2.5), interval: 1.0, - status: Status::New, } } @@ -102,7 +101,6 @@ impl FlashCard { performance_metrics: HashMap::new(), ef: row.get(5)?, interval: row.get(6)?, - status: Status::New, }) })?; @@ -162,7 +160,6 @@ impl FlashCard { performance_metrics: HashMap::new(), ef: row.get(6)?, interval: row.get(7)?, - status: Status::New, }) })?;