Files
jap_learn/src/history.rs
T

103 lines
2.9 KiB
Rust
Raw Normal View History

2026-08-01 23:40:56 +03:00
use crate::data_provider::history::{HistoryItem, get_history_of_set};
2026-07-29 12:57:46 +03:00
use crate::lang::WordData;
2026-08-01 19:36:43 +03:00
use crate::navigation::{NavigatedPage, Page};
use crate::styling::*;
2026-08-01 13:04:18 +03:00
use crate::{AppState, RootMessage};
2026-08-01 23:40:56 +03:00
use HistoryMessage::Back;
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-07-05 01:22:19 +03:00
use std::sync::{Arc, Mutex};
#[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 {
fn navigate(&self, message: &HistoryMessage) -> Option<Page> {
match message {
Back => Some(Page::PreviousPage),
}
}
}
impl HistoryState {
2026-07-29 12:57:46 +03:00
pub fn new(id: u32, state: Arc<Mutex<AppState>>) -> Self {
let state = state.lock().unwrap();
let history = get_history_of_set(id);
let words = history
.iter()
.map(|item| {
state
.dictionary
.iter()
.find(|w| w.id == item.word_id)
.unwrap()
.clone()
})
.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-08-01 13:04:18 +03:00
pub fn update(&mut self, _: HistoryMessage) -> Task<RootMessage> {
2026-07-05 01:22:19 +03:00
Task::none()
}
pub fn view(&self) -> Element<'_, HistoryMessage> {
2026-08-01 19:43:07 +03:00
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,
)
2026-07-05 01:22:19 +03:00
}
2026-07-05 01:52:19 +03:00
2026-07-29 12:57:46 +03:00
fn history_lines(&self) -> Column<'_, HistoryMessage> {
let mut column = Column::new();
for (item, index) in self.list.iter().zip(0..self.list.len()) {
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-01 23:40:56 +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)
2026-08-01 23:40:56 +03:00
2026-07-29 12:57:46 +03:00
]
.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,
}