Files
jap_learn/src/word.rs
T

190 lines
6.1 KiB
Rust
Raw Normal View History

2026-04-19 20:22:35 +03:00
use crate::data_provider::words::{delete_word, update_word};
2026-05-05 12:02:59 +03:00
use crate::lang::WordData;
2026-08-01 19:36:43 +03:00
use crate::navigation::Page::PreviousPage;
use crate::navigation::{NavigatedPage, Page};
use crate::styling::*;
use crate::word::WordMessage::*;
2026-08-01 13:04:18 +03:00
use crate::{AppState, RootMessage};
2026-04-17 12:38:05 +03:00
use iced::widget::button::danger;
2026-08-14 23:52:48 +03:00
use iced::widget::{button, column, row, rule, scrollable, text, text_input};
2026-04-17 12:38:05 +03:00
use iced::{Element, Fill, Task};
2026-08-20 00:07:09 +03:00
use parking_lot::Mutex;
use std::sync::Arc;
2026-04-17 12:38:05 +03:00
#[derive(Clone)]
pub struct WordState {
state: Arc<Mutex<AppState>>,
index: usize,
2026-05-05 12:02:59 +03:00
word: 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-04-17 12:38:05 +03:00
match message {
2026-08-01 19:36:43 +03:00
Back => {}
Save => {
2026-08-20 00:07:09 +03:00
let mut state = self.state.lock();
2026-04-17 12:38:05 +03:00
state.dictionary[self.index] = self.word.clone();
2026-05-12 16:57:05 +03:00
update_word(&mut self.word, &state.connection);
2026-08-01 19:36:43 +03:00
return Task::done(RootMessage::Word(Back));
2026-04-17 12:38:05 +03:00
}
2026-08-01 19:36:43 +03:00
Delete => {
2026-08-20 00:07:09 +03:00
let mut state = self.state.lock();
2026-04-17 12:38:05 +03:00
state.dictionary.remove(self.index);
delete_word(&self.word, &state.connection);
2026-08-01 19:36:43 +03:00
return Task::done(RootMessage::Word(Back));
2026-04-17 12:38:05 +03:00
}
2026-08-01 19:36:43 +03:00
SetTags(n) => self.word.tags = n,
SetKey(n) => {
2026-04-17 12:38:05 +03:00
self.word.key = n;
}
2026-08-01 19:36:43 +03:00
SetValue(n) => {
2026-04-17 12:38:05 +03:00
self.word.value = n;
}
2026-08-15 13:22:59 +03:00
SetAdditional(key, value) => {
self.word.additional.insert(key, value.clone());
2026-08-15 21:32:24 +03:00
}
2026-08-01 19:36:43 +03:00
AddAdditional(key) => {
2026-04-19 20:22:35 +03:00
self.word.additional.insert(key, "".to_string());
2026-05-25 12:23:32 +03:00
}
2026-08-01 19:36:43 +03:00
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-04-19 20:22:35 +03:00
let mut fast_add = row![];
if !self.word.additional.contains_key("reading") {
2026-05-25 12:23:32 +03:00
fast_add = fast_add.push(
button("Чтение")
.style(button::text)
2026-08-01 19:36:43 +03:00
.on_press(AddAdditional("reading".to_string())),
2026-05-25 12:23:32 +03:00
);
2026-04-19 20:22:35 +03:00
}
2026-05-10 22:15:03 +03:00
if !self.word.additional.contains_key("description") {
2026-05-25 12:23:32 +03:00
fast_add = fast_add.push(
button("Описание")
.style(button::text)
2026-08-01 19:36:43 +03:00
.on_press(AddAdditional("description".to_string())),
2026-05-25 12:23:32 +03:00
);
}
if !self.word.additional.contains_key("context") {
fast_add = fast_add.push(
button("В контексте")
.style(button::text)
2026-08-01 19:36:43 +03:00
.on_press(AddAdditional("context".to_string())),
2026-05-25 12:23:32 +03:00
);
2026-05-10 22:15:03 +03:00
}
2026-08-01 19:36:43 +03:00
2026-04-19 20:22:35 +03:00
let mut col = iced::widget::column![
2026-08-01 19:36:43 +03:00
column![
text!("Ключ"),
text_input("key", &self.word.key).on_input(SetKey),
]
2026-08-04 14:09:40 +03:00
.spacing(QUARTER_SPACING),
2026-08-01 19:36:43 +03:00
column![
text!("Значение"),
text_input("value", &self.word.value).on_input(SetValue),
]
2026-08-04 14:09:40 +03:00
.spacing(QUARTER_SPACING),
2026-08-01 19:36:43 +03:00
column![
text!("Теги"),
text_input("tags", &self.word.tags).on_input(SetTags),
]
2026-08-04 14:09:40 +03:00
.spacing(QUARTER_SPACING),
2026-04-19 20:22:35 +03:00
rule::horizontal(2),
scrollable(fast_add),
];
for more in &self.word.additional {
col = col.push(self.get_view_for_more(more));
}
2026-08-01 19:36:43 +03:00
back_overlay(
2026-04-17 12:38:05 +03:00
column![
2026-08-01 19:36:43 +03:00
col.spacing(DEFAULT_SPACING / 2.0).width(Fill).height(Fill),
2026-04-17 12:38:05 +03:00
row![
2026-08-01 19:36:43 +03:00
button("Сохранить").style(jl_button).on_press(Save),
button("Удалить").style(danger).on_press(Delete),
2026-04-17 12:38:05 +03:00
]
.spacing(DEFAULT_SPACING)
]
.spacing(DEFAULT_SPACING)
.into(),
2026-08-01 19:36:43 +03:00
Back,
2026-04-17 12:38:05 +03:00
)
}
2026-08-13 22:03:47 +03:00
}
impl WordState {
pub(crate) fn new(word: WordData, index: usize, state: Arc<Mutex<AppState>>) -> WordState {
WordState { state, index, word }
}
}
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
}