Rewrite fully second db table into result

This commit is contained in:
micialware committed 2026-08-24 18:16:41 +03:00
1 parent 06bf9138c0
commit 8841a51897
11 files changed
+127 -74

No files matched your search

+4 -3
View File
@@ -4,7 +4,7 @@ use crate::pages::import::ImportMessage::*;
use crate::data::entities::{WordData, WordGroup};
use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::NotificationData::OpenFileError;
use crate::notification::{NotificationData, NotificationRequest};
use crate::notification::NotificationData;
use crate::styling::*;
use iced::widget::button::danger;
use iced::widget::container::success;
@@ -28,6 +28,7 @@ use tokio::task::spawn_blocking;
use zip::ZipArchive;
use crate::app::AppState;
use crate::fs::*;
use crate::push_error;
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
@@ -367,7 +368,7 @@ impl ImportState {
},
|result: Result<ImportData, NotificationData>| match result {
Ok(import) => RootMessage::Import(UpdateImport(import)),
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err)),
Err(err) => push_error!(err),
},
)
}
@@ -499,7 +500,7 @@ impl ImportState {
},
|r: Result<(), NotificationData>| match r {
Ok(_) => RootMessage::Import(ImportFinished),
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err))
Err(err) => push_error!(err)
}
)
}
+18 -11
View File
@@ -1,19 +1,20 @@
use crate::app::AppState;
use crate::core::deck::DeckData;
use crate::core::srs::WordOpenMode;
use crate::data::entities::{CardStatistics, DeckSettings, WordData};
use crate::data::Id;
use crate::data::entities::{CardStatistics, DeckSettings, WordData};
use crate::data_provider::voice::get_voice;
use crate::navigation::Page::PreviousPage;
use crate::navigation::{KeyPressedPage, NavigatedPage, Page, RootMessage};
use crate::notification::{NotificationData, NotificationRequest};
use crate::notification::NotificationData;
use crate::styling::*;
use crate::{push_error, push_error_task};
use chrono::Local;
use iced::alignment::Horizontal::Center;
use iced::keyboard::key::Physical::Code;
use iced::widget::text::Style;
use iced::widget::{button, column, container, row, rule, space, text, tooltip, Column};
use iced::{alignment, keyboard, Element, Fill, Task, Theme};
use iced::widget::{Column, button, column, container, row, rule, space, text, tooltip};
use iced::{Element, Fill, Task, Theme, alignment, keyboard};
use parking_lot::Mutex;
use rodio::MixerDeviceSink;
use std::collections::HashSet;
@@ -57,7 +58,7 @@ impl NavigatedPage<RepetitionMessage> for RepetitionState {
if self.settings.require_speech() {
return Task::perform(play_sound(self.sink.clone(), value), |r| match r {
Ok(_) => RootMessage::Repetition(RepetitionMessage::PlayFinished),
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err)),
Err(err) => push_error!(err),
});
}
}
@@ -101,8 +102,11 @@ impl NavigatedPage<RepetitionMessage> for RepetitionState {
}
impl RepetitionState {
pub(crate) fn new(set: DeckSettings, state: Arc<Mutex<AppState>>) -> RepetitionState {
let mut card_set = DeckData::new(&set, state.clone());
pub(crate) fn new(
set: DeckSettings,
state: Arc<Mutex<AppState>>,
) -> Result<RepetitionState, NotificationData> {
let mut card_set = DeckData::new(&set, state.clone())?;
let (word, stat) = card_set.next();
let sink_handle = if set.forward.contains("speech") || set.backward.contains("speech") {
@@ -111,7 +115,7 @@ impl RepetitionState {
None
};
RepetitionState {
Ok(RepetitionState {
state,
settings: set,
set: card_set,
@@ -121,7 +125,7 @@ impl RepetitionState {
can_play: true,
sink: Arc::new(sink_handle),
opened: HashSet::new(),
}
})
}
}
@@ -140,7 +144,10 @@ impl RepetitionState {
return Task::none();
}
self.set.open(mode);
if let Err(err) = self.set.open(mode) {
return push_error_task!(err);
}
self.open = false;
self.opened.insert(self.current_word.id);
let next = self.set.next();
@@ -152,7 +159,7 @@ impl RepetitionState {
let task = play_sound(sink, self.current_word.key.clone());
return Task::perform(task, |r| match r {
Ok(_) => RootMessage::Repetition(RepetitionMessage::PlayFinished),
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err)),
Err(err) => push_error!(err),
});
}
self.increment_counter();
+13 -9
View File
@@ -4,15 +4,16 @@ use crate::data_provider::card_sets::{delete_set, update_deck};
use crate::data_provider::card_stats::{delete_stats, load_stats_of_deck};
use crate::navigation::Page::PreviousPage;
use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::NotificationRequest;
use crate::notification::NotificationData;
use crate::pages::repetition_settings::RepetitionSettingsMessage::*;
use crate::styling::*;
use crate::{push_error, push_error_task};
use iced::widget::button::danger;
use iced::widget::text_input::Catalog;
use iced::widget::{button, column, row, scrollable, space, text, text_input};
use iced::{Element, Task};
use iced_core::Length::Fill;
use iced_core::{color, Padding, Theme};
use iced_core::{Padding, Theme, color};
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
@@ -62,10 +63,12 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
Save => {
let mut state = self.state.lock();
if let Err(e) = update_deck(&mut self.set, &state.connection) {
return Task::done(RootMessage::PushNotification(NotificationRequest::Error(e)));
return push_error_task!(e)
}
if self.filter_changed {
update_deck_stats(&self.set, &state);
if self.filter_changed
&& let Err(e) = update_deck_stats(&self.set, &state)
{
return push_error_task!(e)
}
state.decks[self.index] = self.set.clone();
}
@@ -98,7 +101,7 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
}
let mut state = self.state.lock();
if let Err(e) = delete_set(&self.set, &state.connection) {
return Task::done(RootMessage::PushNotification(NotificationRequest::Error(e)));
return push_error_task!(e);
}
state.decks.remove(self.index);
@@ -176,8 +179,8 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
}
}
fn update_deck_stats(deck: &DeckSettings, state: &AppState) {
let mut db_stats = load_stats_of_deck(deck, &state.connection);
fn update_deck_stats(deck: &DeckSettings, state: &AppState) -> Result<(), NotificationData> {
let mut db_stats = load_stats_of_deck(deck, &state.connection).expect("Wrong db operation");
let filtered_words = deck.get_word_list(state);
filtered_words.iter().for_each(|word| {
let word_id = state.dictionary[*word].id;
@@ -186,7 +189,8 @@ fn update_deck_stats(deck: &DeckSettings, state: &AppState) {
}
});
delete_stats(db_stats.as_slice(), &state.connection);
delete_stats(db_stats.as_slice(), &state.connection)?;
Ok(())
}
impl RepetitionSettingsState {
+37 -20
View File
@@ -6,10 +6,13 @@ use crate::data_provider::card_sets::update_deck;
use crate::data_provider::card_stats::{add_stat_list, load_stats_of_deck};
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::{NotificationData, NotificationRequest};
use crate::pages::history::HistoryState;
use crate::pages::repetition::RepetitionState;
use crate::pages::repetition_settings::RepetitionSettingsState;
use crate::pages::repetitions::RepetitionsMessage::*;
use crate::push_error;
use crate::push_error_task;
use crate::styling::*;
use chrono::{Days, Local, Utc};
use hashbrown::{HashMap, HashSet};
@@ -27,7 +30,6 @@ use iced_core::svg::Handle;
use parking_lot::Mutex;
use std::ops::Deref;
use std::sync::Arc;
use crate::notification::NotificationRequest;
#[derive(Clone)]
pub struct RepetitionsState {
@@ -51,7 +53,9 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
let selected_set = self.selected_deck_mut().unwrap();
if selected_set.append_mode == AppendMode::Full {
Self::append_all_words(&mut clone.lock(), selected_set);
if let Err(_err) = Self::append_all_words(&mut clone.lock(), selected_set){
return None;
}
selected_set.existing_words_indices = selected_set.available_words_indices.clone();
} else {
if selected_set.available_words_indices.is_empty()
@@ -61,10 +65,13 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
}
}
Some(Repetition(RepetitionState::new(
selected_set.general_settings.clone(),
clone,
)))
if let Ok(repetition_state) =
RepetitionState::new(selected_set.general_settings.clone(), clone)
{
return Some(Repetition(repetition_state));
}
None
} else if let GoToSettings = message {
Some(RepetitionSettings(RepetitionSettingsState::new(
self.selected_deck().unwrap().index.unwrap(),
@@ -83,10 +90,9 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
self.decks.remove(index);
self.clear_selection();
} else {
self.decks[index].general_settings =
state.decks[index].clone();
self.decks[index].general_settings = state.decks[index].clone();
drop(state);
self.fill_deck_word_indices();
let _ = self.fill_deck_word_indices();
}
} else {
drop(state);
@@ -139,16 +145,20 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
SelectSet(index) => {
self.select_deck(index);
let deck = self.selected_deck().unwrap();
if !deck.indices_ready {
self.fill_deck_word_indices();
if !deck.indices_ready
&& let Err(err) = self.fill_deck_word_indices()
{
return push_error_task!(err);
}
}
Save => {
let mut deck = self.selected_deck().unwrap().clone();
let mut state = self.state.lock();
{
if let Err(e) = update_deck(&mut deck.general_settings, &state.connection){
return Task::done(RootMessage::PushNotification(NotificationRequest::Error(e)))
if let Err(e) = update_deck(&mut deck.general_settings, &state.connection) {
return Task::done(RootMessage::PushNotification(
NotificationRequest::Error(e),
));
}
if let Some(index) = deck.index {
state.decks[index] = deck.general_settings.clone();
@@ -212,7 +222,11 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
let deck = self.selected_deck().unwrap();
let mut state = self.state.lock();
Self::append_words(&mut state, &deck.general_settings, adding.into_iter());
if let Err(err) =
Self::append_words(&mut state, &deck.general_settings, adding.into_iter())
{
return push_error_task!(err);
}
}
}
Task::none()
@@ -548,7 +562,7 @@ impl RepetitionsState {
}
impl RepetitionsState {
fn append_all_words(state: &mut AppState, deck: &DeckViewData) {
fn append_all_words(state: &mut AppState, deck: &DeckViewData) -> Result<(), NotificationData> {
let existing = &deck.existing_words_indices;
let mut created_set = HashSet::with_capacity(existing.len());
existing.iter().for_each(|c| {
@@ -559,13 +573,14 @@ impl RepetitionsState {
.iter()
.filter(|i| !created_set.contains(i))
.cloned();
Self::append_words(state, deck, required);
Self::append_words(state, deck, required)?;
Ok(())
}
fn append_words(
state: &mut AppState,
deck: &DeckSettings,
indices: impl Iterator<Item = usize>,
) {
) -> Result<(), NotificationData> {
let words = &state.dictionary;
let stats = &mut indices
.map(|i| {
@@ -581,8 +596,9 @@ impl RepetitionsState {
.collect::<Vec<_>>();
if !stats.is_empty() {
add_stat_list(stats, &mut state.connection);
add_stat_list(stats, &mut state.connection)?;
}
Ok(())
}
fn select_deck(&mut self, index: usize) {
@@ -602,10 +618,10 @@ impl RepetitionsState {
self.selected_deck_index = None;
}
fn fill_deck_word_indices(&mut self) {
fn fill_deck_word_indices(&mut self) -> Result<(), NotificationData> {
let state = &self.state.lock();
let mut deck = self.selected_deck().unwrap().clone();
let added: Vec<_> = load_stats_of_deck(&deck, &state.connection)
let added: Vec<_> = load_stats_of_deck(&deck, &state.connection)?
.iter()
.map(|c| self.word_id_index_map[&c.word_id])
.collect();
@@ -613,6 +629,7 @@ impl RepetitionsState {
deck.existing_words_indices = added;
deck.available_words_indices = total;
self.decks[self.selected_deck_index.unwrap()] = deck;
Ok(())
}
}
#[derive(Clone)]
+2 -1
View File
@@ -14,6 +14,7 @@ use std::time::Duration;
use log::error;
use crate::app::{fill_state, AppState};
use crate::notification::{NotificationRequest, NotificationData};
use crate::push_error;
#[derive(Clone)]
pub enum SyncMessage {
@@ -64,7 +65,7 @@ impl NavigatedPage<SyncMessage> for SyncState {
return Task::perform(first_sync(), |id| {
match id {
Ok(id) => RootMessage::Sync(IdReceived(id)),
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err))
Err(err) => push_error!(err)
}
});
}