Files
jap_learn/src/history.rs
T

108 lines
3.2 KiB
Rust
Raw Normal View History

2026-08-01 19:36:43 +03:00
use crate::data_provider::history::{get_history_of_set, HistoryItem};
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-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};
2026-08-01 19:36:43 +03:00
use HistoryMessage::Back;
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 {
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:36:43 +03:00
back_overlay(iced::widget::row![
2026-07-29 12:57:46 +03:00
horizontal().width(FillPortion(1)),
scrollable(self.history_lines().padding(DEFAULT_SPACING))
.height(Fill)
.width(FillPortion(5)),
horizontal().width(FillPortion(1))
]
2026-08-01 19:36:43 +03:00
.height(Fill)
.width(Fill).into(), Back)
// container(
// iced::widget::column![
// button("Назад").style(jl_button).on_press(Back),
//
// ]
// .align_x(Left)
// .width(Fill),
// )
// .center_x(Fill)
// .padding(10)
// .into()
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),
text!("{}", item.timestamp.format("%d.%m %H:%M"))
]
.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,
}