Rewrite history page
This commit is contained in:
1 parent
f19a6f64c3
commit
532640058e
11 files changed
+201
-230
No files matched your search
@@ -81,7 +81,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
let dict = &self.state.dictionary.read();
|
||||
|
||||
let mut query = dict.query(Arc::new(|_| true));
|
||||
let handles = query.handles(dict);
|
||||
let handles = query.handles();
|
||||
words = handles.filter(|h| self.included.contains(h)).collect();
|
||||
|
||||
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
||||
@@ -552,7 +552,7 @@ impl DictionaryState {
|
||||
|
||||
let groups = &self.state.word_groups.read();
|
||||
|
||||
for mut group in self.groups_query.handles(groups) {
|
||||
for mut group in self.groups_query.handles() {
|
||||
let group_name = groups.get(&mut group).unwrap().name.clone();
|
||||
row = row.push(
|
||||
button(text!("{}", group_name))
|
||||
|
||||
+33
-31
@@ -1,22 +1,20 @@
|
||||
use crate::app::AppState;
|
||||
use crate::data::INVALID_ID;
|
||||
use crate::data::entities::{HistoryItem, WordData};
|
||||
use crate::data_provider::history::get_history_of_set;
|
||||
use crate::data::entities::{HistoryNote, WordData};
|
||||
use crate::data::repositories::Repository;
|
||||
use crate::navigation::*;
|
||||
use crate::styling::*;
|
||||
use HistoryMessage::Back;
|
||||
use chrono::Local;
|
||||
use iced::alignment::Horizontal::Center;
|
||||
use iced::widget::space::horizontal;
|
||||
use iced::widget::*;
|
||||
use iced::{Element, Fill, FillPortion, Task};
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use HistoryMessage::Back;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HistoryState {
|
||||
list: Vec<HistoryItem>,
|
||||
words: Vec<WordData>,
|
||||
list: Vec<(WordData, HistoryNote)>,
|
||||
pub state: AppState,
|
||||
}
|
||||
|
||||
impl NavigatedPage<HistoryMessage> for HistoryState {
|
||||
@@ -39,9 +37,9 @@ impl NavigatedPage<HistoryMessage> for HistoryState {
|
||||
.width(FillPortion(5)),
|
||||
horizontal().width(FillPortion(1))
|
||||
]
|
||||
.height(Fill)
|
||||
.width(Fill)
|
||||
.into(),
|
||||
.height(Fill)
|
||||
.width(Fill)
|
||||
.into(),
|
||||
Back,
|
||||
)
|
||||
}
|
||||
@@ -49,56 +47,60 @@ impl NavigatedPage<HistoryMessage> for HistoryState {
|
||||
|
||||
impl HistoryState {
|
||||
pub fn new(id: crate::data::Id, state: AppState) -> Self {
|
||||
let state = state.lock();
|
||||
let history = get_history_of_set(id).unwrap_or_default();
|
||||
let dict = &state.dictionary.read();
|
||||
let history_repo = &mut state.history.write();
|
||||
history_repo.init().expect("TODO: panic message");
|
||||
let id = id.clone();
|
||||
let mut history = history_repo.query(Arc::new(move |h| h.set_id == id));
|
||||
let words = history
|
||||
.iter()
|
||||
.iter(history_repo)
|
||||
.map(
|
||||
|item| match state.dictionary.iter().find(|w| w.id == item.word_id) {
|
||||
None => WordData {
|
||||
id,
|
||||
|item| {let handle = dict.handle_by_id(item.word_id);
|
||||
let word = dict.get_static(handle).unwrap_or(WordData {
|
||||
id: Default::default(),
|
||||
group_id: Default::default(),
|
||||
key: "Удаленное слово".to_string(),
|
||||
value: "Удаленное слово".to_string(),
|
||||
tags: "Удаленное слово".to_string(),
|
||||
value: "".to_string(),
|
||||
tags: "".to_string(),
|
||||
additional: Default::default(),
|
||||
group_id: INVALID_ID,
|
||||
},
|
||||
Some(word) => word.clone(),
|
||||
},
|
||||
});
|
||||
(word, item.clone())
|
||||
}
|
||||
)
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
list: history,
|
||||
words,
|
||||
list: words,
|
||||
state: state.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn history_lines(&self) -> Column<'_, HistoryMessage> {
|
||||
let mut column = Column::new();
|
||||
for (index, item) in self.list.iter().enumerate() {
|
||||
for (index, item) in &self.list {
|
||||
|
||||
let activity = item;
|
||||
column = column.push(
|
||||
row![
|
||||
iced::widget::column![
|
||||
text!("{}", self.words[index].key)
|
||||
text!("{}", index.key)
|
||||
.width(Fill)
|
||||
.align_x(Center),
|
||||
text!("{}", self.words[index].value)
|
||||
text!("{}", index.value)
|
||||
.width(Fill)
|
||||
.align_x(Center),
|
||||
]
|
||||
.align_x(Center),
|
||||
iced::widget::column![
|
||||
text!("{} ➞ {}", item.before, item.after),
|
||||
text!("{} ➞ {}", activity.before, activity.after),
|
||||
text!(
|
||||
"{}",
|
||||
item.timestamp.with_timezone(&Local).format("%d.%m %H:%M")
|
||||
activity.timestamp.with_timezone(&Local).format("%d.%m %H:%M")
|
||||
)
|
||||
]
|
||||
.spacing(5)
|
||||
.align_x(Center)
|
||||
]
|
||||
.spacing(5),
|
||||
.spacing(5),
|
||||
);
|
||||
column = column.push(rule::horizontal(1));
|
||||
}
|
||||
|
||||
+33
-30
@@ -1,5 +1,5 @@
|
||||
use crate::app::AppState;
|
||||
use crate::data::entities::{WordData, WordGroup};
|
||||
use crate::data::entities::{EntityData, WordData, WordGroup};
|
||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||
use crate::data_provider::words::{add_group, add_words};
|
||||
use crate::fs::*;
|
||||
@@ -29,6 +29,7 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::task::spawn_blocking;
|
||||
use zip::ZipArchive;
|
||||
use crate::data::repositories::{BatchedRepository, Repository};
|
||||
|
||||
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
||||
|
||||
@@ -431,29 +432,18 @@ impl ImportState {
|
||||
info!("{}", group.name);
|
||||
Task::perform(
|
||||
async move {
|
||||
spawn_blocking(move || {
|
||||
let words_list = spawn_blocking(move || {
|
||||
let temp_file_path = temp_import_path();
|
||||
let connection =
|
||||
Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let import_list = get_words_of_group(&connection, group.id)?;
|
||||
let mut words_list = Vec::with_capacity(import_list.len());
|
||||
|
||||
let map_indices = Self::get_mapping_indices(&group);
|
||||
let mut group_entity = WordGroup {
|
||||
id: 0.into(),
|
||||
name: group.name.clone(),
|
||||
};
|
||||
{
|
||||
let state = state.lock();
|
||||
add_group(&mut group_entity, &state.connection)?;
|
||||
}
|
||||
|
||||
let group_id = group_entity.id;
|
||||
for import in import_list {
|
||||
let mut word = WordData::new();
|
||||
|
||||
word.tags = import.tags.trim().replace(" ", ", ");
|
||||
word.group_id = group_id;
|
||||
for (dest, indices) in &map_indices {
|
||||
let collected_string = Self::collect_strings(
|
||||
&import.fields,
|
||||
@@ -480,24 +470,37 @@ impl ImportState {
|
||||
words_list.push(word);
|
||||
}
|
||||
|
||||
let mut state = state.lock();
|
||||
let connection = &mut state.connection;
|
||||
let mut index = 0;
|
||||
let total_len = words_list.len() as f32 / 1024.0;
|
||||
for word in &mut words_list.chunks_mut(1024) {
|
||||
add_words(word, connection)?;
|
||||
index += 1;
|
||||
let mut progress = progress.lock();
|
||||
*progress = (index as f32 / total_len).min(1.0);
|
||||
}
|
||||
|
||||
state.dictionary.append(&mut words_list);
|
||||
state.word_groups.push(group_entity);
|
||||
|
||||
Ok(())
|
||||
Ok::<_, NotificationData>(words_list)
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()?;
|
||||
|
||||
let group_handle = {
|
||||
let group_entity = WordGroup {
|
||||
id: 0.into(),
|
||||
name: group.name.clone(),
|
||||
};
|
||||
let mut groups_repo = state.word_groups.write();
|
||||
groups_repo.create_now(group_entity)?
|
||||
};
|
||||
|
||||
let group_id = group_handle.id;
|
||||
let mut words_list = words_list;
|
||||
for word in &mut words_list {
|
||||
word.group_id = group_id;
|
||||
}
|
||||
|
||||
let mut words_repo = state.dictionary.write();
|
||||
let mut index = 0;
|
||||
let total_len = words_list.len() as f32 / 1024.0;
|
||||
for words in &mut words_list.chunks_mut(1024) {
|
||||
words_repo.create_now_many(words)?;
|
||||
index += 1;
|
||||
let mut progress = progress.lock();
|
||||
*progress = (index as f32 / total_len).min(1.0);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
|r: Result<(), NotificationData>| match r {
|
||||
Ok(_) => RootMessage::Import(ImportFinished),
|
||||
|
||||
@@ -167,7 +167,7 @@ impl RepetitionState {
|
||||
}
|
||||
|
||||
fn increment_counter(&mut self) {
|
||||
let mut state = self.state.lock();
|
||||
let mut state = self.state;
|
||||
let id = self.settings.id;
|
||||
let activity = state.activity.get_mut(&id);
|
||||
|
||||
|
||||
Reference in new issue
Block a user