From ef4773d72406fc336ac1b3cfadc08683a5444bde Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 5 Jul 2026 01:52:19 +0300 Subject: [PATCH] History parsing --- src/history.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/src/history.rs b/src/history.rs index 387f284..4ad19d9 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,13 +1,18 @@ +use crate::dictionary::app_data_dir; +use crate::lang::{WordData, WordOpenMode}; use crate::{AppState, NavigatedPage, Page, RootMessage}; +use chrono::{DateTime, Utc}; use iced::widget::*; use iced::{Element, Fill, Left, Task}; +use std::fs::File; +use std::io::{BufRead, BufReader}; use std::sync::{Arc, Mutex}; use HistoryMessage::Back; #[derive(Clone)] pub struct HistoryState { set_id: u32, - list: Vec + list: Vec, } impl NavigatedPage for HistoryState { @@ -21,7 +26,10 @@ impl NavigatedPage for HistoryState { impl HistoryState { pub fn new(id: u32, state: Arc>) -> Self { - Self { set_id: id, list: vec![] } + Self { + set_id: id, + list: get_history_of_set(id, &state.lock().unwrap().dictionary), + } } pub fn update(&mut self, message: HistoryMessage) -> Task { @@ -47,12 +55,64 @@ impl HistoryState { } } +fn get_history_of_set(id: u32, words: &Vec) -> Vec { + let app_dir = app_data_dir(); + let head = app_dir.clone().join(format!("set_{}_history.csv", id)); + let tail = app_dir.clone().join(format!("set_{}_history.csv", id)); + let mut lines = Vec::new(); + if tail.exists() { + let reader = BufReader::new(File::open(tail).unwrap()); + for line in reader.lines() { + lines.push(line.unwrap()); + } + } + + if head.exists() { + let reader = BufReader::new(File::open(head).unwrap()); + for line in reader.lines() { + lines.push(line.unwrap()); + } + } + + parse_history_items(lines, words) +} + +fn parse_history_items(strings: Vec, words: &Vec) -> Vec { + let mut items = Vec::with_capacity(strings.len()); + for string in strings { + if let [time, word, mode, before, after] = string.split(';').collect::>()[..] { + items.push(HistoryItem { + timestamp: DateTime::from_timestamp(time.parse().unwrap(), 0).unwrap(), + word: words + .iter() + .find(|w| w.id == word.parse::().unwrap()) + .unwrap() + .clone(), + mode: match mode.parse::().unwrap() { + 2 => WordOpenMode::Hard, + 3 => WordOpenMode::Ok, + 4 => WordOpenMode::Easy, + _ => WordOpenMode::None, + }, + before: before.parse().unwrap(), + after: after.parse().unwrap(), + }); + } + } + + items +} + #[derive(Clone)] pub enum HistoryMessage { Back, } #[derive(Clone)] -pub struct HistoryItem{ - -} \ No newline at end of file +pub struct HistoryItem { + timestamp: DateTime, + word: WordData, + mode: WordOpenMode, + before: u8, + after: u8, +}