Skip to content

Commit

Permalink
fix: including all time spent on the app in time spent reading
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Aug 23, 2024
1 parent 434509a commit 4e5396f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src-tauri/src/language_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use std::{
io::{BufRead, BufReader, Write},
process,
sync::Arc,
time::Duration,
};

use crate::{
commands::new_command,
spyglys_integration::{get_alternate_forms, handle_lemma, load_spyglys},
KalbaError, KalbaState, LanguageParser, SharedInfo,
};
use chrono::Utc;
use log::{info, trace};
use lol_html::{element, text, RewriteStrSettings};
use shared::*;
Expand Down Expand Up @@ -363,6 +365,12 @@ pub async fn words_from_string(
chrono::Utc::now(),
words.iter().filter(|v| v.clickable).count(),
));
state.in_reader = true;
state
.to_save
.sessions
.push((Utc::now(), Duration::new(0, 0)));
log::info!("starting new session");
Ok((sentences, words))
}

Expand Down
36 changes: 28 additions & 8 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ struct SharedInfo {
errors: Vec<KalbaError>,
can_save: bool,
language_cached_data: HashMap<String, CachedData>,
in_reader: bool,
}

#[derive(Debug, Deserialize, Serialize, Default)]
Expand Down Expand Up @@ -200,7 +201,6 @@ impl Default for SharedInfo {
.last_language
.clone()
.or_else(|| settings.languages.keys().next().cloned());
to_save.sessions.push((Utc::now(), Duration::new(0, 0)));
for err in &errors {
log::warn!("{}", err);
log::warn!("{:?}", err);
Expand All @@ -215,6 +215,7 @@ impl Default for SharedInfo {
dict_info: Default::default(),
can_save,
language_cached_data,
in_reader: false,
}
}
}
Expand Down Expand Up @@ -282,6 +283,7 @@ fn main() {
get_words_added,
get_started,
get_url_contents,
switch_page,
])
.on_window_event(handle_window_event)
.run(tauri::generate_context!())
Expand Down Expand Up @@ -409,13 +411,15 @@ fn handle_window_event(window: &Window, event: &WindowEvent) {
.join("kalba")
.join("saved_data.toml");
locked_state.to_save.last_language = locked_state.current_language.clone();
let session = locked_state
.to_save
.sessions
.last_mut()
.expect("sessions should exist");
session.1 =
TimeDelta::to_std(&(Utc::now() - session.0)).expect("time should be valid");
if locked_state.in_reader {
let session = locked_state
.to_save
.sessions
.last_mut()
.expect("sessions should exist");
session.1 = TimeDelta::to_std(&(Utc::now() - session.0))
.expect("time should be valid");
}
let conts =
toml::to_string(&locked_state.to_save).expect("Error serializing to toml");
fs::write(saved_state_file, conts).expect("error writing to file");
Expand Down Expand Up @@ -619,3 +623,19 @@ async fn always_change_lemma(
.insert(lemma, updated_lemma);
Ok(())
}

#[tauri::command]
async fn switch_page(state: State<'_, KalbaState>) -> Result<(), String> {
let mut state = state.0.lock().await;
if state.in_reader {
state.in_reader = false;
let session = state
.to_save
.sessions
.last_mut()
.expect("sessions should exist");
session.1 = TimeDelta::to_std(&(Utc::now() - session.0)).expect("time should be valid");
log::info!("saving session");
}
Ok(())
}
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Reader from "./pages/reader/Index.vue";
import Dashboard from "./pages/dashboard/Index.vue";
import ComingSoon from "./pages/ComingSoon.vue";
import "./styles.css";
import { invoke } from "@tauri-apps/api/core";

const router = createRouter({
history: createWebHistory(),
Expand All @@ -29,6 +30,11 @@ const router = createRouter({
],
});

router.afterEach(async (_to, _from) => {
await invoke("switch_page");
return true;
});

const SyncApp = {
template: "<Suspense><App /></Suspense>",
components: { App },
Expand Down

0 comments on commit 4e5396f

Please sign in to comment.