Files
jap_learn/src/pages/word.rs
T

191 lines
6.5 KiB
Rust
Raw Normal View History

2026-08-24 18:46:54 +03:00
use crate::app::AppState;
2026-09-02 00:19:58 +03:00
use crate::data::entities::{EntityHandle, WordData};
2026-08-01 19:36:43 +03:00
use crate::navigation::Page::PreviousPage;
use crate::navigation::{NavigatedPage, Page, RootMessage};
2026-08-24 01:22:00 +03:00
use crate::pages::word::WordMessage::*;
2026-08-24 18:46:54 +03:00
use crate::styling::*;
2026-04-17 12:38:05 +03:00
use iced::widget::button::danger;
2026-09-09 15:00:52 +03:00
use iced::widget::{button, column, row, space, text, text_input};
2026-04-17 12:38:05 +03:00
use iced::{Element, Fill, Task};
pub struct WordState {
2026-08-31 11:46:35 +03:00
state: AppState,
2026-09-02 00:19:58 +03:00
handle: EntityHandle<WordData>,
2026-04-17 12:38:05 +03:00
}
impl NavigatedPage<WordMessage> for WordState {
2026-08-18 12:14:35 +03:00
fn navigate(&mut self, message: &WordMessage) -> Option<Page> {
2026-08-01 19:36:43 +03:00
if let Back = message {
2026-04-17 12:38:05 +03:00
Some(PreviousPage)
} else {
None
}
}
2026-08-06 23:20:14 +03:00
fn navigated(&mut self) {}
2026-08-13 22:03:47 +03:00
fn update(&mut self, message: WordMessage) -> Task<RootMessage> {
2026-09-06 23:58:41 +03:00
// match message {
// Back => {}
// Save => {
// let mut state = self.state.lock();
// state.dictionary[self.index] = self.word.clone();
//
// if let Err(err) = update_word(&mut self.word, &state.connection) {
// return push_error_task!(err);
// }
// return Task::done(RootMessage::Word(Back));
// }
// Delete => {
// let mut state = self.state.lock();
// state.dictionary.remove(self.index);
// if let Err(err) = delete_word(&self.word, &state.connection) {
// return push_error_task!(err);
// }
// return Task::done(RootMessage::Word(Back));
// }
// SetTags(n) => self.word.tags = n,
// SetKey(n) => {
// self.word.key = n;
// }
// SetValue(n) => {
// self.word.value = n;
// }
// SetAdditional(key, value) => {
// self.word.additional.insert(key, value.clone());
// }
// AddAdditional(key) => {
// self.word.additional.insert(key, "".to_string());
// }
// RemoveAdditional(key) => {
// self.word.additional.remove(key.as_str());
// }
// }
2026-04-17 12:38:05 +03:00
Task::none()
}
2026-08-13 22:03:47 +03:00
fn view(&self) -> Element<'_, WordMessage> {
2026-09-06 23:58:41 +03:00
// let mut fast_add = row![];
// if !self.word.additional.contains_key("reading") {
// fast_add = fast_add.push(
// button("Чтение")
// .style(button::text)
// .on_press(AddAdditional("reading".to_string())),
// );
// }
// if !self.word.additional.contains_key("description") {
// fast_add = fast_add.push(
// button("Описание")
// .style(button::text)
// .on_press(AddAdditional("description".to_string())),
// );
// }
//
// if !self.word.additional.contains_key("context") {
// fast_add = fast_add.push(
// button("В контексте")
// .style(button::text)
// .on_press(AddAdditional("context".to_string())),
// );
// }
//
// let mut col = iced::widget::column![
// column![
// text!("Ключ"),
// text_input("key", &self.word.key).on_input(SetKey),
// ]
// .spacing(QUARTER_SPACING),
// column![
// text!("Значение"),
// text_input("value", &self.word.value).on_input(SetValue),
// ]
// .spacing(QUARTER_SPACING),
// column![
// text!("Теги"),
// text_input("tags", &self.word.tags).on_input(SetTags),
// ]
// .spacing(QUARTER_SPACING),
// rule::horizontal(2),
// scrollable(fast_add),
// ];
//
// for more in &self.word.additional {
// col = col.push(self.get_view_for_more(more));
// }
//
// back_overlay(
// column![
// col.spacing(DEFAULT_SPACING / 2.0).width(Fill).height(Fill),
// row![
// button("Сохранить").style(jl_button).on_press(Save),
// button("Удалить").style(danger).on_press(Delete),
// ]
// .spacing(DEFAULT_SPACING)
// ]
// .spacing(DEFAULT_SPACING)
// .into(),
// Back,
// )
space().into()
2026-04-17 12:38:05 +03:00
}
2026-08-13 22:03:47 +03:00
}
impl WordState {
2026-09-02 00:19:58 +03:00
pub fn new(handle: EntityHandle<WordData>, state: AppState) -> Self {
2026-09-09 15:00:52 +03:00
Self { state, handle }
2026-08-13 22:03:47 +03:00
}
}
impl WordState {
2026-04-19 20:22:35 +03:00
fn get_view_for_more(&self, value: (&String, &String)) -> Element<'_, WordMessage> {
2026-05-05 12:06:17 +03:00
match value.0.as_str() {
"reading" => self.reading_field(value.1),
"description" => self.description_field(value.1),
"context" => self.context_field(value.1),
_ => self.additional_field(value.1, value.0.clone(), value.0.clone()),
2026-05-05 12:06:17 +03:00
}
2026-04-19 20:22:35 +03:00
}
2026-08-15 21:32:24 +03:00
fn reading_field(&self, value: &str) -> Element<'_, WordMessage> {
2026-05-25 12:23:32 +03:00
self.additional_field(value, "Чтение слова".to_string(), "reading".to_string())
2026-04-19 20:22:35 +03:00
}
2026-05-10 22:15:03 +03:00
2026-08-15 13:22:59 +03:00
fn description_field(&self, value: &str) -> Element<'_, WordMessage> {
2026-05-25 12:23:32 +03:00
self.additional_field(value, "Описание".to_string(), "description".to_string())
}
2026-08-01 19:36:43 +03:00
2026-08-15 13:22:59 +03:00
fn context_field(&self, value: &str) -> Element<'_, WordMessage> {
2026-05-25 12:23:32 +03:00
self.additional_field(value, "В контексте".to_string(), "context".to_string())
}
2026-08-15 21:32:24 +03:00
fn additional_field(&self, value: &str, name: String, id: String) -> Element<'_, WordMessage> {
2026-05-10 22:15:03 +03:00
column![
2026-05-25 12:23:32 +03:00
text!("{}", name),
2026-08-01 19:36:43 +03:00
row![
text_input(id.clone().as_str(), value)
2026-08-01 19:36:43 +03:00
.on_input({
let value = id.clone();
move |string| SetAdditional(value.clone(), string)
})
.width(Fill),
button("-")
.on_press(RemoveAdditional(id.clone()))
.style(danger),
]
2026-08-04 14:09:40 +03:00
.spacing(QUARTER_SPACING),
2026-05-25 12:23:32 +03:00
]
2026-08-04 14:09:40 +03:00
.spacing(QUARTER_SPACING)
2026-08-01 19:36:43 +03:00
.into()
2026-05-25 12:23:32 +03:00
}
2026-04-17 12:38:05 +03:00
}
2026-08-04 14:09:40 +03:00
2026-04-17 12:38:05 +03:00
#[derive(Debug, Clone)]
pub enum WordMessage {
Save,
Back,
Delete,
SetTags(String),
SetKey(String),
SetValue(String),
2026-04-19 20:22:35 +03:00
AddAdditional(String),
2026-08-01 19:36:43 +03:00
RemoveAdditional(String),
2026-04-19 20:22:35 +03:00
SetAdditional(String, String),
2026-04-17 12:38:05 +03:00
}