Files
jap_learn/src/pages/word.rs
T

177 lines
5.7 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-09-11 12:38:21 +03:00
use crate::data::repositories::Repository;
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-11 12:38:21 +03:00
use iced::widget::space::horizontal;
use iced::widget::{button, column, row, rule, scrollable, text, text_input};
use iced::{Element, Fill};
2026-04-17 12:38:05 +03:00
2026-09-11 12:38:21 +03:00
pub struct WordEditor<Message> {
2026-08-31 11:46:35 +03:00
state: AppState,
2026-09-02 00:19:58 +03:00
handle: EntityHandle<WordData>,
2026-09-11 12:38:21 +03:00
on_delete: Option<Message>,
on_edit: Option<Message>,
2026-04-17 12:38:05 +03:00
}
2026-09-11 12:38:21 +03:00
impl<Message> WordEditor<Message> {
pub(crate) fn update(&mut self, message: WordMessage) -> Option<Message> {
let dictionary = &mut self.state.dictionary.write();
let word = dictionary.get_mut(&mut self.handle).unwrap();
match message {
Delete => {
return self.on_delete.take();
}
SetTags(n) => word.tags = n,
SetKey(n) => {
word.key = n;
}
SetValue(n) => {
word.value = n;
}
SetAdditional(key, value) => {
word.additional.insert(key, value.clone());
}
AddAdditional(key) => {
word.additional.insert(key, "".to_string());
}
RemoveAdditional(key) => {
word.additional.remove(key.as_str());
}
}
self.on_edit.take()
2026-04-17 12:38:05 +03:00
}
2026-09-11 11:47:04 +03:00
pub(crate) fn view(&self) -> Element<'_, WordMessage> {
2026-09-11 12:03:13 +03:00
let mut fast_add = row![];
let word = self.state.dictionary.read().get_static(self.handle).unwrap();
if !word.additional.contains_key("reading") {
fast_add = fast_add.push(
button("Чтение")
.style(button::text)
.on_press(AddAdditional("reading".to_string())),
);
}
if !word.additional.contains_key("description") {
fast_add = fast_add.push(
button("Описание")
.style(button::text)
.on_press(AddAdditional("description".to_string())),
);
}
if !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", &word.key).on_input(SetKey),
]
.spacing(QUARTER_SPACING),
column![
text!("Значение"),
text_input("value", &word.value).on_input(SetValue),
]
.spacing(QUARTER_SPACING),
column![
text!("Теги"),
text_input("tags", &word.tags).on_input(SetTags),
]
.spacing(QUARTER_SPACING),
rule::horizontal(2),
scrollable(fast_add),
];
for more in &word.additional {
col = col.push(self.get_view_for_more(more));
}
column![
col.spacing(DEFAULT_SPACING / 2.0).width(Fill).height(Fill),
row![
2026-09-11 12:38:21 +03:00
horizontal().width(Fill),
2026-09-11 12:03:13 +03:00
button("Удалить").style(danger).on_press(Delete),
]
.spacing(DEFAULT_SPACING)
]
.spacing(DEFAULT_SPACING).into()
2026-04-17 12:38:05 +03:00
}
2026-08-13 22:03:47 +03:00
}
2026-09-11 12:38:21 +03:00
impl<Message> WordEditor<Message> {
2026-09-02 00:19:58 +03:00
pub fn new(handle: EntityHandle<WordData>, state: AppState) -> Self {
2026-09-11 12:38:21 +03:00
Self { state, handle, on_delete: None, on_edit: None }
}
pub fn on_delete(mut self, message: Message) -> Self {
self.on_delete = Some(message);
self
}
pub fn on_edit(mut self, message: Message) -> Self {
self.on_edit = Some(message);
self
2026-08-13 22:03:47 +03:00
}
}
2026-09-11 12:38:21 +03:00
impl<Message> WordEditor<Message> {
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 {
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
}