Skip to content

Commit

Permalink
chore: implement the interval
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh0412 committed Mar 17, 2024
1 parent 39ab569 commit a0263fd
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 60 deletions.
35 changes: 25 additions & 10 deletions src/components/deck/flash_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,28 @@ impl FlashCard {
}

fn again_click(&mut self, _event: &ClickEvent, cx: &mut ViewContext<Self>) {
if self.show_answer {
self.show_answer = false;
let card = self.queue.pop_back().unwrap();
self.queue.push_front(card);
} else {
self.show_answer = true;
}
self.show_answer = false;
let card = self.queue.pop_back().unwrap();
self.queue.push_front(card);

cx.notify();
}

fn easy_click(&mut self, _event: &ClickEvent, cx: &mut ViewContext<Self>) {
let collection = cx.global::<crate::Collection>();
self.answer(Answer::Easy, collection);
cx.notify();
}

fn hard_click(&mut self, _event: &ClickEvent, cx: &mut ViewContext<Self>) {
let collection = cx.global::<crate::Collection>();
self.answer(Answer::Hard, collection);
cx.notify();
}

fn good_click(&mut self, _event: &ClickEvent, cx: &mut ViewContext<Self>) {
let collection = cx.global::<crate::Collection>();
self.answer(Answer::Good, collection);
cx.notify();
}

Expand Down Expand Up @@ -128,9 +143,9 @@ impl Render for FlashCard {
.flex()
.justify_between()
.child(Button::new("again", "Again").on_click(cx.listener(Self::again_click)))
.child(Button::new("easy", "Easy"))
.child(Button::new("good", "Good"))
.child(Button::new("hard", "Hard")),
.child(Button::new("easy", "Easy").on_click(cx.listener(Self::easy_click)))
.child(Button::new("good", "Good").on_click(cx.listener(Self::good_click)))
.child(Button::new("hard", "Hard").on_click(cx.listener(Self::hard_click))),
)
} else {
div()
Expand Down
24 changes: 6 additions & 18 deletions src/models/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,19 @@ pub fn get_current_card_state(card: &FlashCard) -> CardState {
let ef = card.ease_factor();

match *card.get_queue() {
CardQueue::New => NewState {}.into(),
CardQueue::New => NewState { position: 0 }.into(),
CardQueue::Learning => LearningState {
remaining_steps: 0,
scheduled_secs: 0,
elapsed_secs: 0,
memory_state: None,
}
.into(),
CardQueue::Review => ReviewState { memory_state: None }.into(),
}
}

pub fn apply_state(card: &mut FlashCard, next: CardState) {
match next {
CardState::New(_) => {
card.set_queue(CardQueue::New);
}
CardState::Learning(next_learning_state) => {
card.set_queue(CardQueue::Learning);
card.memory_state = next_learning_state.memory_state;
}
CardState::Review(next_review_state) => {
card.set_queue(CardQueue::Review);
card.memory_state = next_review_state.memory_state;
CardQueue::Review => ReviewState {
scheduled_days: interval,
ease_factor: ef,
memory_state: None,
}
_ => {}
.into(),
}
}
54 changes: 45 additions & 9 deletions src/models/collection.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::path::PathBuf;

use chrono::Local;
use gpui::{AppContext, Global};

use crate::{errors::Result, storage::sqlite::SqliteStorage, FlashCard};
use crate::{
errors::Result, repositories::flash_card::CardQueue, storage::sqlite::SqliteStorage, FlashCard,
};

use super::{
answer::{self, Answer},
builder::Builder,
card::{self, apply_state, get_current_card_state},
queue::Queue,
answer::Answer, builder::Builder, card::get_current_card_state, queue::Queue,
states::card_state::CardState, timing::SchedTimingToday,
};

pub struct CollectionBuilder {
Expand All @@ -21,6 +22,18 @@ impl CollectionBuilder {
collection_path: Some(col_path),
}
}

pub(crate) fn timing_for_timestamp(&mut self, now: i64) -> SchedTimingToday {
// Get current utc offset from the system
let days_elapsed = now / 86_400;
let next_day_at = (days_elapsed + 1) * 86_400;

SchedTimingToday {
now,
days_elapsed: days_elapsed as u32,
next_day_at,
}
}
}

impl Builder for CollectionBuilder {
Expand All @@ -33,11 +46,13 @@ impl Builder for CollectionBuilder {
.unwrap_or_else(|| PathBuf::from(":memory:"));

let storage = SqliteStorage::open_or_create(&col_path)?;
let timing = self.timing_for_timestamp(Local::now().timestamp());

let col = Collection {
storage,
col_path,
card_queues: None,
timing,
};

Ok(col)
Expand All @@ -48,13 +63,34 @@ pub struct Collection {
pub storage: SqliteStorage,
pub col_path: PathBuf,
pub card_queues: Option<Queue>,
pub timing: SchedTimingToday,
}

impl Collection {
pub fn init(col: Self, cx: &mut AppContext) {
cx.set_global(col);
}

pub fn apply_state(&self, card: &mut FlashCard, next: CardState) {
match next {
CardState::New(next_new_state) => {
card.due = next_new_state.position as i32;
card.set_queue(CardQueue::New);
}
CardState::Learning(next_learning_state) => {
card.set_queue(CardQueue::Learning);
card.memory_state = next_learning_state.memory_state;
}
CardState::Review(next_review_state) => {
card.set_queue(CardQueue::Review);
card.interval = next_review_state.scheduled_days;
card.due = (self.timing.days_elapsed + next_review_state.scheduled_days) as i32;
card.memory_state = next_review_state.memory_state;
}
_ => {}
}
}

pub fn answer_card(&self, card_id: u32, answer: Answer) {
let mut card = FlashCard::load(card_id, &self.storage.conn).unwrap();

Expand All @@ -64,19 +100,19 @@ impl Collection {
match answer {
Answer::Again => {
let next = next_state.again;
apply_state(&mut card, next);
self.apply_state(&mut card, next);
}
Answer::Hard => {
let next = next_state.hard;
apply_state(&mut card, next);
self.apply_state(&mut card, next);
}
Answer::Good => {
let next = next_state.good;
apply_state(&mut card, next);
self.apply_state(&mut card, next);
}
Answer::Easy => {
let next = next_state.easy;
apply_state(&mut card, next);
self.apply_state(&mut card, next);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod collection;
pub mod deck;
pub mod queue;
pub mod states;
pub mod timing;
36 changes: 22 additions & 14 deletions src/models/states/learning_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use fsrs::MemoryState;

use super::card_state::{CardState, CardStateTrait};
use super::{
card_state::{CardState, CardStateTrait},
review_state::ReviewState,
state_context::StateContext,
};
use crate::models::queue::SchedulingStates;

#[derive(Clone)]
Expand All @@ -27,33 +31,37 @@ impl LearningState {
fn answer_again(&self) -> LearningState {
LearningState {
remaining_steps: 0,
scheduled_secs: 0,
scheduled_secs: 60,
elapsed_secs: 0,
memory_state: None,
}
}
fn answer_hard(&self) -> LearningState {
LearningState {
remaining_steps: 0,
scheduled_secs: 0,
scheduled_secs: 60,
elapsed_secs: 0,
memory_state: None,
}
}
fn answer_good(&self) -> LearningState {
LearningState {
remaining_steps: 0,
scheduled_secs: 0,
elapsed_secs: 0,

fn answer_good(&self) -> ReviewState {
let ctx = StateContext::default();

ReviewState {
ease_factor: ctx.initial_ease_factor,
memory_state: None,
..Default::default()
}
}
fn answer_easy(&self) -> LearningState {
LearningState {
remaining_steps: 0,
scheduled_secs: 0,
elapsed_secs: 0,
memory_state: None,

fn answer_easy(&self) -> ReviewState {
let ctx = StateContext::default();

ReviewState {
ease_factor: ctx.initial_ease_factor,
memory_state: self.memory_state.clone(),
..Default::default()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/models/states/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod learning_state;
pub mod new_state;
pub mod relearning_state;
pub mod review_state;
pub mod state_context;
4 changes: 3 additions & 1 deletion src/models/states/new_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use super::{
};

#[derive(Clone)]
pub struct NewState {}
pub struct NewState {
pub position: u32,
}

impl CardStateTrait for NewState {
fn next_states(self) -> SchedulingStates {
Expand Down
Loading

0 comments on commit a0263fd

Please sign in to comment.