Skip to content

Commit

Permalink
chore: display a flashcard
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh0412 committed Feb 24, 2024
1 parent 141d068 commit dbdf79f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/components/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
32 changes: 21 additions & 11 deletions src/components/deck/deck_detail.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
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,
}

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 {
Expand Down Expand Up @@ -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,
);
}),
)),
),
)
}
Expand Down
57 changes: 57 additions & 0 deletions src/components/deck/flash_card.rs
Original file line number Diff line number Diff line change
@@ -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<flash_card::FlashCard>,
}

impl FlashCard {
pub fn view(cx: &mut WindowContext, cards: Vec<flash_card::FlashCard>) -> AnyView {
cx.new_view(|vc| Self { cards }).into()
}
}

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

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<flash_card::FlashCard>,
}

impl<'a> StackableView for FlashCardBuilder<'a> {
fn build(&self, cx: &mut WindowContext) -> AnyView {
FlashCard::view(cx, self.cards.clone().into())
}
}
7 changes: 2 additions & 5 deletions src/repositories/flash_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>,
deck_id: i32,
Expand All @@ -23,7 +24,6 @@ pub struct FlashCard {
last_studied_time: Option<SystemTime>,
ef: f32,
interval: f64,
status: Status,
performance_metrics: HashMap<String, i32>,
}

Expand All @@ -39,7 +39,6 @@ impl FlashCard {
performance_metrics: HashMap::new(),
ef: ef.unwrap_or(2.5),
interval: 1.0,
status: Status::New,
}
}

Expand Down Expand Up @@ -102,7 +101,6 @@ impl FlashCard {
performance_metrics: HashMap::new(),
ef: row.get(5)?,
interval: row.get(6)?,
status: Status::New,
})
})?;

Expand Down Expand Up @@ -162,7 +160,6 @@ impl FlashCard {
performance_metrics: HashMap::new(),
ef: row.get(6)?,
interval: row.get(7)?,
status: Status::New,
})
})?;

Expand Down

0 comments on commit dbdf79f

Please sign in to comment.