Files
jap_learn/src/pages/history.rs
T
2026-09-20 22:24:42 +03:00

106 lines
3.4 KiB
Rust

use crate::app::AppState;
use crate::data::entities::{HistoryNote, WordData};
use crate::data::repositories::Repository;
use crate::navigation::*;
use crate::styling::*;
use HistoryMessage::Back;
use chrono::Local;
use iced::alignment::Horizontal::Center;
use iced::widget::space::horizontal;
use iced::widget::*;
use iced::{Element, Fill, FillPortion, Task};
use std::sync::Arc;
pub struct HistoryState {
list: Vec<(WordData, HistoryNote)>,
}
impl NavigatedPage<HistoryMessage> for HistoryState {
fn navigate(&mut self, message: &HistoryMessage) -> Option<Page> {
match message {
Back => Some(Page::PreviousPage),
}
}
fn navigated(&mut self) {}
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))
]
.height(Fill)
.width(Fill)
.into(),
Back,
)
}
}
impl HistoryState {
pub fn new(id: crate::data::Id, state: AppState) -> Self {
let dict = &state.dictionary.read();
let history_repo = &mut state.history.write();
history_repo.init().expect("TODO: panic message");
let mut history = history_repo.query(Arc::new(move |h| h.deck_id == id));
let words = history
.iter(history_repo)
.map(|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(),
key: "Удаленное слово".to_string(),
value: String::new(),
tags: String::new(),
additional: Default::default(),
});
(word, item.clone())
})
.collect();
Self { list: words }
}
fn history_lines(&self) -> Column<'_, HistoryMessage> {
let mut column = Column::new();
for (index, item) in &self.list {
let activity = item;
column = column.push(
row![
iced::widget::column![
text!("{}", index.key).width(Fill).align_x(Center),
text!("{}", index.value).width(Fill).align_x(Center),
]
.align_x(Center),
iced::widget::column![
text!("{} ➞ {}", activity.before, activity.after),
text!(
"{}",
activity
.timestamp
.with_timezone(&Local)
.format("%d.%m %H:%M")
)
]
.spacing(5)
.align_x(Center)
]
.spacing(5),
);
column = column.push(rule::horizontal(1));
}
column.spacing(5)
}
}
#[derive(Clone)]
pub enum HistoryMessage {
Back,
}