Files
jap_learn/src/pages/history.rs
T

111 lines
3.4 KiB
Rust
Raw Normal View History

2026-08-20 18:18:14 +03:00
use crate::data_provider::history::{get_history_of_set, HistoryItem};
use crate::lang::{WordData, INVALID_ID};
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};
2026-08-20 00:07:09 +03:00
use parking_lot::Mutex;
use std::sync::Arc;
2026-08-20 18:18:14 +03:00
use HistoryMessage::Back;
use crate::app::AppState;
2026-07-05 01:22:19 +03:00
#[derive(Clone)]
pub struct HistoryState {
2026-07-05 01:52:19 +03:00
list: Vec<HistoryItem>,
2026-07-29 12:57:46 +03:00
words: Vec<WordData>,
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-08-15 21:32:24 +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-18 12:47:06 +03:00
pub fn new(id: crate::lang::Id, state: Arc<Mutex<AppState>>) -> Self {
2026-08-20 00:07:09 +03:00
let state = state.lock();
2026-08-24 01:22:00 +03:00
let history = get_history_of_set(id).unwrap_or_default();
2026-07-29 12:57:46 +03:00
let words = history
.iter()
2026-08-20 18:18:14 +03:00
.map(
|item| match state.dictionary.iter().find(|w| w.id == item.word_id) {
None => WordData {
id,
key: "Удаленное слово".to_string(),
value: "Удаленное слово".to_string(),
tags: "Удаленное слово".to_string(),
additional: Default::default(),
group_id: INVALID_ID,
},
Some(word) => word.clone(),
},
)
2026-07-29 12:57:46 +03:00
.collect();
2026-07-05 01:52:19 +03:00
Self {
2026-07-29 12:57:46 +03:00
list: history,
words,
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-08-20 18:18:14 +03:00
for (index, item) in self.list.iter().enumerate() {
2026-07-29 12:57:46 +03:00
column = column.push(
row![
iced::widget::column![
text!("{}", self.words[index].key)
.width(Fill)
.align_x(Center),
text!("{}", self.words[index].value)
.width(Fill)
.align_x(Center),
]
.align_x(Center),
iced::widget::column![
text!("{} ➞ {}", item.before, item.after),
2026-08-15 21:32:24 +03:00
text!(
"{}",
item.timestamp.with_timezone(&Local).format("%d.%m %H:%M")
)
2026-07-29 12:57:46 +03:00
]
.spacing(5)
.align_x(Center)
]
.spacing(5),
);
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,
}