From 3adf3f9697c4e2beb10e652947046d5fddda2ed4 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 12 Feb 2022 16:57:19 +0100 Subject: Say when the next card will be available --- src/db/db.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'src/db') diff --git a/src/db/db.rs b/src/db/db.rs index 9ed88ce..4c2d9a2 100644 --- a/src/db/db.rs +++ b/src/db/db.rs @@ -7,7 +7,8 @@ use anyhow::Result; use rand::{rngs::ThreadRng, Rng}; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; -use std::time::SystemTime; + +use crate::util::time; pub fn init(database: String) -> Result { let mut conn = Connection::open(database)?; @@ -25,7 +26,7 @@ pub fn init(database: String) -> Result { /// - keep existing cards, /// - hide unused cards (keep state in case the card is added back afterward). pub fn synchronize(conn: &Connection, entries: Vec) -> Result<()> { - let now = get_current_time()?; + let now = time::now()?; let state = serde_json::to_string(&space_repetition::init())?; let mut rng = rand::thread_rng(); @@ -81,22 +82,20 @@ fn delete_read_before(conn: &Connection, t: u64) -> Result<()> { Ok(()) } -pub fn pick_ready(conn: &Connection) -> Option { - let now = get_current_time().ok()?; - +pub fn next_ready(conn: &Connection) -> Option { let mut stmt = conn .prepare( " - SELECT question, responses, state + SELECT question, responses, state, ready FROM cards - WHERE ready <= ? AND deleted IS NULL + WHERE deleted IS NULL ORDER BY ready LIMIT 1 ", ) .ok()?; - let mut rows = stmt.query([now]).ok()?; + let mut rows = stmt.query([]).ok()?; let row = rows.next().ok()??; let state_str: String = row.get(2).ok()?; let responses_str: String = row.get(1).ok()?; @@ -105,11 +104,12 @@ pub fn pick_ready(conn: &Connection) -> Option { question: row.get(0).ok()?, responses: serialization::line_to_words(&responses_str), state: serde_json::from_str(&state_str).ok()?, + ready: row.get(3).ok()?, }) } pub fn update(conn: &Connection, question: &String, state: &space_repetition::State) -> Result<()> { - let now = get_current_time()?; + let now = time::now()?; let ready = now + state.get_interval_seconds(); let state_str = serde_json::to_string(state)?; @@ -124,9 +124,3 @@ pub fn update(conn: &Connection, question: &String, state: &space_repetition::St Ok(()) } - -fn get_current_time() -> Result { - Ok(SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH)? - .as_secs()) -} -- cgit v1.2.3