Files
jap_learn/src/pages/history.rs
T

114 lines
3.6 KiB
Rust
Raw Normal View History

2026-08-25 12:04:07 +03:00
use crate::app::AppState;
2026-09-04 11:18:23 +03:00
use crate::data::entities::{HistoryNote, WordData};
use crate::data::repositories::Repository;
use crate::navigation::*;
2026-08-01 19:36:43 +03:00
use crate::styling::*;
2026-08-01 23:40:56 +03:00
use chrono::Local;
2026-07-29 12:57:46 +03:00
use iced::alignment::Horizontal::Center;
use iced::widget::space::horizontal;
2026-07-05 01:22:19 +03:00
use iced::widget::*;
2026-08-01 19:36:43 +03:00
use iced::{Element, Fill, FillPortion, Task};
use std::sync::Arc;
2026-09-04 11:18:23 +03:00
use HistoryMessage::Back;
2026-07-05 01:22:19 +03:00
#[derive(Clone)]
pub struct HistoryState {
2026-09-04 11:18:23 +03:00
list: Vec<(WordData, HistoryNote)>,
pub state: AppState,
2026-07-05 01:22:19 +03:00
}
impl NavigatedPage<HistoryMessage> for HistoryState {
2026-08-18 12:14:35 +03:00
fn navigate(&mut self, message: &HistoryMessage) -> Option<Page> {
2026-07-05 01:22:19 +03:00
match message {
Back => Some(Page::PreviousPage),
}
}
2026-08-15 21:32:24 +03:00
fn navigated(&mut self) {}
2026-08-13 22:03:47 +03:00
fn update(&mut self, _: HistoryMessage) -> Task<RootMessage> {
Task::none()
}
fn view(&self) -> Element<'_, HistoryMessage> {
back_overlay(
iced::widget::row![
horizontal().width(FillPortion(1)),
scrollable(self.history_lines().padding(DEFAULT_SPACING))
.height(Fill)
.width(FillPortion(5)),
horizontal().width(FillPortion(1))
]
2026-09-04 11:18:23 +03:00
.height(Fill)
.width(Fill)
.into(),
2026-08-13 22:03:47 +03:00
Back,
)
}
2026-07-05 01:22:19 +03:00
}
impl HistoryState {
2026-08-31 11:46:35 +03:00
pub fn new(id: crate::data::Id, state: AppState) -> Self {
2026-09-04 11:18:23 +03:00
let dict = &state.dictionary.read();
let history_repo = &mut state.history.write();
history_repo.init().expect("TODO: panic message");
let id = id.clone();
2026-09-07 10:53:09 +03:00
let mut history = history_repo.query(Arc::new(move |h| h.deck_id == id));
2026-07-29 12:57:46 +03:00
let words = history
2026-09-04 11:18:23 +03:00
.iter(history_repo)
2026-08-20 18:18:14 +03:00
.map(
2026-09-04 11:18:23 +03:00
|item| {let handle = dict.handle_by_id(item.word_id);
let word = dict.get_static(handle).unwrap_or(WordData {
id: Default::default(),
group_id: Default::default(),
2026-08-20 18:18:14 +03:00
key: "Удаленное слово".to_string(),
2026-09-04 11:18:23 +03:00
value: "".to_string(),
tags: "".to_string(),
2026-08-20 18:18:14 +03:00
additional: Default::default(),
2026-09-04 11:18:23 +03:00
});
(word, item.clone())
}
2026-08-20 18:18:14 +03:00
)
2026-07-29 12:57:46 +03:00
.collect();
2026-07-05 01:52:19 +03:00
Self {
2026-09-04 11:18:23 +03:00
list: words,
state: state.clone(),
2026-07-05 01:52:19 +03:00
}
2026-07-05 01:22:19 +03:00
}
2026-07-29 12:57:46 +03:00
fn history_lines(&self) -> Column<'_, HistoryMessage> {
let mut column = Column::new();
2026-09-04 11:18:23 +03:00
for (index, item) in &self.list {
let activity = item;
2026-07-29 12:57:46 +03:00
column = column.push(
row![
iced::widget::column![
2026-09-04 11:18:23 +03:00
text!("{}", index.key)
2026-07-29 12:57:46 +03:00
.width(Fill)
.align_x(Center),
2026-09-04 11:18:23 +03:00
text!("{}", index.value)
2026-07-29 12:57:46 +03:00
.width(Fill)
.align_x(Center),
]
.align_x(Center),
iced::widget::column![
2026-09-04 11:18:23 +03:00
text!("{} ➞ {}", activity.before, activity.after),
2026-08-15 21:32:24 +03:00
text!(
"{}",
2026-09-04 11:18:23 +03:00
activity.timestamp.with_timezone(&Local).format("%d.%m %H:%M")
2026-08-15 21:32:24 +03:00
)
2026-07-29 12:57:46 +03:00
]
.spacing(5)
.align_x(Center)
]
2026-09-04 11:18:23 +03:00
.spacing(5),
2026-07-29 12:57:46 +03:00
);
column = column.push(rule::horizontal(1));
2026-07-05 01:52:19 +03:00
}
2026-07-29 12:57:46 +03:00
column.spacing(5)
2026-07-05 01:52:19 +03:00
}
}
2026-07-05 01:22:19 +03:00
#[derive(Clone)]
pub enum HistoryMessage {
Back,
}