From 8fb457d5639d11d5075d664001e98148e223f257 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 15 Jul 2023 10:09:30 +0200 Subject: Re synchronize deck if necessary between cards --- src/gui/mod.rs | 16 ++++++++++++++-- src/main.rs | 10 +++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/gui/mod.rs b/src/gui/mod.rs index caf97f8..3abe238 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -2,10 +2,12 @@ pub mod message; pub mod question; pub mod util; +use crate::sync; use crate::{db, space_repetition, util::time}; use anyhow::Result; use crossterm::terminal; use rusqlite::Connection; +use std::fs; use std::io::Stdout; use tui::{backend::CrosstermBackend, Terminal}; @@ -27,13 +29,22 @@ pub fn restore_terminal(term: &mut Term) -> Result<()> { } pub fn start( - conn: &Connection, + conn: &mut Connection, term: &mut Term, + deck_path: &str, deck_name: &str, + mut deck_last_sync: u64, hide_remaining: bool, ) -> Result<()> { loop { - let now = time::seconds_since_unix_epoch()?; + // Synchronize deck if necessary + let deck_last_update = + time::seconds_since_unix_epoch_of(fs::metadata(deck_path)?.modified()?)?; + if deck_last_update > deck_last_sync { + sync::run(conn, deck_path)?; + deck_last_sync = time::seconds_since_unix_epoch()?; + } + let title = title( deck_name, db::count_available(conn).unwrap_or(0), @@ -54,6 +65,7 @@ pub fn start( None => { let message = match db::next_ready(conn) { Some(ready) => { + let now = time::seconds_since_unix_epoch()?; let duration = time::pp_duration(ready - now); format!("Prochaine carte disponible dans {duration}.") } diff --git a/src/main.rs b/src/main.rs index ddc6edc..b18cb1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,10 +29,18 @@ fn main() -> Result<()> { let deck_name = deck::pp_from_path(&deck_path).unwrap_or_else(|| "Deck".to_string()); sync::run(&mut conn, &deck_path)?; + let deck_last_sync = util::time::seconds_since_unix_epoch()?; let mut term = gui::setup_terminal()?; - match gui::start(&conn, &mut term, &deck_name, args.hide_remaining) { + match gui::start( + &mut conn, + &mut term, + &deck_path, + &deck_name, + deck_last_sync, + args.hide_remaining, + ) { Ok(()) => (), Err(msg) => { // Show errors in TUI, otherwise they are hidden -- cgit v1.2.3