Repo integration #15
No files matched your search
Generated
+7
-7
@@ -1531,11 +1531,11 @@ checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fsrs"
|
name = "fsrs"
|
||||||
version = "6.6.1"
|
version = "6.6.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b8a99ea3dec9af37c9ed3835463ff6820a578a0026a8e370b4c1c0db48dfab3f"
|
checksum = "89836c0a26b347b585b23824034600b69bfc7f0b0112d0cd656791799eb3a717"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itertools 0.14.0",
|
"itertools 0.15.0",
|
||||||
"log",
|
"log",
|
||||||
"ndarray",
|
"ndarray",
|
||||||
"priority-queue",
|
"priority-queue",
|
||||||
@@ -2430,9 +2430,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.14.0"
|
version = "0.15.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"either",
|
"either",
|
||||||
]
|
]
|
||||||
@@ -4236,9 +4236,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rhai"
|
name = "rhai"
|
||||||
version = "1.25.1"
|
version = "1.26.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dd4dd0f8c36625202a4ba553c416c19b719947cd2a31d1bda06126e4a5727daf"
|
checksum = "5e6e1be9d697d537ce450766df42ad6adf1a93f25f21d73217354ad7e3d3dae1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"bitflags 2.10.0",
|
"bitflags 2.10.0",
|
||||||
|
|||||||
+2
-2
@@ -9,7 +9,7 @@ iced_core = "0.14.0"
|
|||||||
rand = "0.10.2"
|
rand = "0.10.2"
|
||||||
dirs = "6.0.0"
|
dirs = "6.0.0"
|
||||||
serde_json = "1.0.151"
|
serde_json = "1.0.151"
|
||||||
rhai = { version = "1.25.1", features = ["sync"] }
|
rhai = { version = "1.26.0", features = ["sync"] }
|
||||||
chrono = "0.4.45"
|
chrono = "0.4.45"
|
||||||
rusqlite = { version = "0.40.2", features = ["chrono", "bundled"] }
|
rusqlite = { version = "0.40.2", features = ["chrono", "bundled"] }
|
||||||
reqwest = { version = "0.13.4", features = ["json", "stream", "multipart"] }
|
reqwest = { version = "0.13.4", features = ["json", "stream", "multipart"] }
|
||||||
@@ -26,7 +26,7 @@ rayon = "1.12.0"
|
|||||||
parking_lot = "0.12.5"
|
parking_lot = "0.12.5"
|
||||||
env_logger = "0.11.11"
|
env_logger = "0.11.11"
|
||||||
log = "0.4.34"
|
log = "0.4.34"
|
||||||
fsrs = "6.6.1"
|
fsrs = "6.6.2"
|
||||||
litemap = { version = "0.8.3", features = ["serde"] }
|
litemap = { version = "0.8.3", features = ["serde"] }
|
||||||
|
|
||||||
[profile.super-release]
|
[profile.super-release]
|
||||||
|
|||||||
+10
-51
@@ -6,9 +6,9 @@ use chrono::{DateTime, NaiveDate, Utc};
|
|||||||
use fsrs::MemoryState;
|
use fsrs::MemoryState;
|
||||||
use litemap::LiteMap;
|
use litemap::LiteMap;
|
||||||
use log::info;
|
use log::info;
|
||||||
use rayon::prelude::*;
|
use rhai::Engine;
|
||||||
use rhai::{Engine, Scope};
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
@@ -77,8 +77,6 @@ pub struct DeckSettings {
|
|||||||
pub forward: String,
|
pub forward: String,
|
||||||
pub backward: String,
|
pub backward: String,
|
||||||
pub filter: String,
|
pub filter: String,
|
||||||
pub count: Option<usize>,
|
|
||||||
pub open_mode: OrderMode,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EntityData for DeckSettings {
|
impl EntityData for DeckSettings {
|
||||||
@@ -99,8 +97,6 @@ impl DeckSettings {
|
|||||||
forward: "".to_string(),
|
forward: "".to_string(),
|
||||||
backward: "".to_string(),
|
backward: "".to_string(),
|
||||||
filter: "true".to_string(),
|
filter: "true".to_string(),
|
||||||
count: None,
|
|
||||||
open_mode: OrderMode::Default,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn check_filter(&self) -> bool {
|
pub fn check_filter(&self) -> bool {
|
||||||
@@ -110,50 +106,6 @@ impl DeckSettings {
|
|||||||
info!("Compile time is {:?}", time.elapsed());
|
info!("Compile time is {:?}", time.elapsed());
|
||||||
ast.is_ok()
|
ast.is_ok()
|
||||||
}
|
}
|
||||||
pub fn get_word_list(&self, state: &AppState) -> Vec<usize> {
|
|
||||||
todo!()
|
|
||||||
// let time = Instant::now();
|
|
||||||
// let mut list = vec![];
|
|
||||||
// let engine = Engine::new();
|
|
||||||
//
|
|
||||||
// let ast = engine.compile(&self.filter);
|
|
||||||
// if let Ok(ast) = ast {
|
|
||||||
// let groups = state.word_groups.read();
|
|
||||||
// let query = state
|
|
||||||
// .dictionary.read().query_par(| word: &WordData|{
|
|
||||||
// let mut more = rhai::Map::new();
|
|
||||||
// for iced in &word.additional {
|
|
||||||
// more.insert(iced.0.clone().into(), iced.1.clone().into());
|
|
||||||
// }
|
|
||||||
// let mut scope = Scope::new();
|
|
||||||
// let group_name = groups.get_ref(&mut groups.handle_by_id(word.group_id)).unwrap().name;
|
|
||||||
//
|
|
||||||
// scope
|
|
||||||
// .push_constant("id", word.id.0 as i64)
|
|
||||||
// .push_constant("key", word.key.clone())
|
|
||||||
// .push_constant("value", word.value.clone())
|
|
||||||
// .push_constant("tags", word.tags.clone())
|
|
||||||
// .push_constant("more", more)
|
|
||||||
// .push_constant(
|
|
||||||
// "group",
|
|
||||||
// group_name,
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
|
||||||
// if let Ok(result) = result {
|
|
||||||
// return result;
|
|
||||||
// }
|
|
||||||
// false
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// info!("Collecting available words is {:?}", time.elapsed());
|
|
||||||
//
|
|
||||||
// list
|
|
||||||
// } else {
|
|
||||||
// list
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
pub fn require_speech(&self) -> bool {
|
pub fn require_speech(&self) -> bool {
|
||||||
self.forward == "speech" || self.backward == "speech"
|
self.forward == "speech" || self.backward == "speech"
|
||||||
}
|
}
|
||||||
@@ -213,7 +165,6 @@ pub struct CustomField {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy)]
|
|
||||||
pub struct EntityHandle<E> {
|
pub struct EntityHandle<E> {
|
||||||
pub(crate) id: Id,
|
pub(crate) id: Id,
|
||||||
index: u32,
|
index: u32,
|
||||||
@@ -229,6 +180,7 @@ impl<T> Clone for EntityHandle<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<T> Copy for EntityHandle<T> {}
|
||||||
impl<T> EntityHandle<T> {
|
impl<T> EntityHandle<T> {
|
||||||
pub fn new(id: Id, index: usize, real: bool) -> Self {
|
pub fn new(id: Id, index: usize, real: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -280,6 +232,13 @@ impl<T> Ord for EntityHandle<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Hash for EntityHandle<T> {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.id.hash(state);
|
||||||
|
self.real().hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait EntityData {
|
pub trait EntityData {
|
||||||
fn id(&self) -> Id;
|
fn id(&self) -> Id;
|
||||||
fn set_id(&mut self, id: Id);
|
fn set_id(&mut self, id: Id);
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ use rayon::iter::IntoParallelRefIterator;
|
|||||||
use rayon::iter::ParallelIterator;
|
use rayon::iter::ParallelIterator;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::Deref;
|
use std::ops::{Deref, Index, IndexMut};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::slice::Iter;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use crate::data_provider::sqlite::SqliteConnection;
|
use crate::data_provider::sqlite::SqliteConnection;
|
||||||
|
|
||||||
@@ -20,6 +21,9 @@ where
|
|||||||
fn get(&self, handle: &mut EntityHandle<T>) -> Option<T>;
|
fn get(&self, handle: &mut EntityHandle<T>) -> Option<T>;
|
||||||
fn get_ref(&self, handle: &mut EntityHandle<T>) -> Option<&T>;
|
fn get_ref(&self, handle: &mut EntityHandle<T>) -> Option<&T>;
|
||||||
fn get_mut(&mut self, handle: &mut EntityHandle<T>) -> Option<&mut T>;
|
fn get_mut(&mut self, handle: &mut EntityHandle<T>) -> Option<&mut T>;
|
||||||
|
fn get_static(&self, handle: EntityHandle<T>) -> Option<T>;
|
||||||
|
fn get_ref_static(&self, handle: EntityHandle<T>) -> Option<&T>;
|
||||||
|
fn get_mut_static(&mut self, handle: EntityHandle<T>) -> Option<&mut T>;
|
||||||
fn handle_by_id(&self, id: Id) -> EntityHandle<T>;
|
fn handle_by_id(&self, id: Id) -> EntityHandle<T>;
|
||||||
fn create(&mut self, new: T) -> EntityHandle<T>;
|
fn create(&mut self, new: T) -> EntityHandle<T>;
|
||||||
fn create_now(&mut self, new: T) -> Result<EntityHandle<T>, NotificationData>;
|
fn create_now(&mut self, new: T) -> Result<EntityHandle<T>, NotificationData>;
|
||||||
@@ -30,6 +34,8 @@ where
|
|||||||
fn start(&mut self) -> MaybeError;
|
fn start(&mut self) -> MaybeError;
|
||||||
fn reload(&mut self) -> MaybeError;
|
fn reload(&mut self) -> MaybeError;
|
||||||
fn shifts(&self) -> usize;
|
fn shifts(&self) -> usize;
|
||||||
|
fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T> where T: 'a;
|
||||||
|
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> where T: 'a;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct QueryManger<T, R, D>
|
struct QueryManger<T, R, D>
|
||||||
@@ -122,10 +128,25 @@ impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Qu
|
|||||||
.filter_map(move |e| data_repository_inner.get_ref(e))
|
.filter_map(move |e| data_repository_inner.get_ref(e))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn for_each_mut<F>(&mut self, repo: &mut R, mut f: F)
|
pub fn iter_with_handles<'a>(&mut self, data_repository_inner: &'a DataRepositoryInner<T, D>) -> impl Iterator<Item = (&'a T, EntityHandle<T>)>
|
||||||
|
where
|
||||||
|
T: 'a,
|
||||||
|
{
|
||||||
|
if self.shift_stamp != data_repository_inner.shifts() {
|
||||||
|
self.load(data_repository_inner);
|
||||||
|
}
|
||||||
|
self.cache
|
||||||
|
.iter_mut()
|
||||||
|
.filter_map(move |e| Some((data_repository_inner.get_ref(e)?, e.clone())))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn for_each_mut<F>(&mut self, repo: &mut DataRepositoryInner<T, D>, mut f: F)
|
||||||
where
|
where
|
||||||
F: FnMut(&mut T),
|
F: FnMut(&mut T),
|
||||||
{
|
{
|
||||||
|
if self.shift_stamp != repo.shifts() {
|
||||||
|
self.load(repo);
|
||||||
|
}
|
||||||
for handle in &mut self.cache {
|
for handle in &mut self.cache {
|
||||||
if let Some(entity) = repo.get_mut(handle) {
|
if let Some(entity) = repo.get_mut(handle) {
|
||||||
f(entity);
|
f(entity);
|
||||||
@@ -166,6 +187,28 @@ impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Qu
|
|||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handles(&self, data_repository: &DataRepositoryInner<T, D>) -> impl Iterator<Item = EntityHandle<T>> {
|
||||||
|
self.cache.iter().cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.cache.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Index<usize> for Query<T, R, D> {
|
||||||
|
type Output = EntityHandle<T>;
|
||||||
|
|
||||||
|
fn index(&self, index: usize) -> &Self::Output {
|
||||||
|
&self.cache[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> IndexMut<usize> for Query<T, R, D> {
|
||||||
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||||
|
&mut self.cache[index]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait BatchedRepository<T, D>
|
pub trait BatchedRepository<T, D>
|
||||||
@@ -300,6 +343,30 @@ impl<T: EntityData + Clone, D: DbLayer<T>> DataRepositoryInner<T, D> {
|
|||||||
shift_stamp: self.shifts,
|
shift_stamp: self.shifts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn empty_query(&self) -> Query<T, DataRepositoryInner<T, D>, D> {
|
||||||
|
Query{
|
||||||
|
cache: vec![],
|
||||||
|
rule: Arc::new(|_| false),
|
||||||
|
parallel: false,
|
||||||
|
_marker: Default::default(),
|
||||||
|
shift_stamp: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn empty_query_par(&self) -> Query<T, DataRepositoryInner<T, D>, D> {
|
||||||
|
Query{
|
||||||
|
cache: vec![],
|
||||||
|
rule: Arc::new(|_| false),
|
||||||
|
parallel: true,
|
||||||
|
_marker: Default::default(),
|
||||||
|
shift_stamp: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.cache.len() + self.temporal.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryInner<T, D> {
|
impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryInner<T, D> {
|
||||||
@@ -333,6 +400,18 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
|||||||
Some(&mut slice[index])
|
Some(&mut slice[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_static(&self, mut handle: EntityHandle<T>) -> Option<T> {
|
||||||
|
self.get(&mut handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ref_static(&self, mut handle: EntityHandle<T>) -> Option<&T> {
|
||||||
|
self.get_ref(&mut handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mut_static(&mut self, mut handle: EntityHandle<T>) -> Option<&mut T> {
|
||||||
|
self.get_mut(&mut handle)
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_by_id(&self, id: Id) -> EntityHandle<T> {
|
fn handle_by_id(&self, id: Id) -> EntityHandle<T> {
|
||||||
EntityHandle::new(id, id.0 as usize, true)
|
EntityHandle::new(id, id.0 as usize, true)
|
||||||
}
|
}
|
||||||
@@ -439,6 +518,20 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
|||||||
fn shifts(&self) -> usize {
|
fn shifts(&self) -> usize {
|
||||||
self.shifts
|
self.shifts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn iter<'a>(&'a self) -> impl Iterator<Item=&'a T>
|
||||||
|
where
|
||||||
|
T: 'a
|
||||||
|
{
|
||||||
|
self.cache.iter().chain(self.temporal.iter())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item=&'a mut T>
|
||||||
|
where
|
||||||
|
T: 'a
|
||||||
|
{
|
||||||
|
self.cache.iter_mut().chain(self.temporal.iter_mut())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository<T, D>
|
impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository<T, D>
|
||||||
for DataRepositoryInner<T, D>
|
for DataRepositoryInner<T, D>
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
use crate::core::srs::OrderMode;
|
use crate::data::entities::DeckSettings;
|
||||||
use crate::data::entities::{CardState, DeckSettings};
|
|
||||||
use crate::data_provider::{SqliteDbLayer, TableAccessQuerySet};
|
|
||||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
|
||||||
use crate::notification::{MaybeError, NotificationData};
|
|
||||||
use chrono::NaiveDate;
|
|
||||||
use rusqlite::{Connection, Error, Params, Row};
|
|
||||||
use crate::data::repositories::DataRepository;
|
use crate::data::repositories::DataRepository;
|
||||||
use crate::data_provider::card_states::CardStatesQuerySet;
|
use crate::data_provider::{SqliteDbLayer, TableAccessQuerySet};
|
||||||
|
use rusqlite::{Error, Params, Row};
|
||||||
|
|
||||||
pub type DeckRepository = DataRepository<DeckSettings, SqliteDbLayer<DeckSettings, DeckQuerySet>>;
|
pub type DeckRepository = DataRepository<DeckSettings, SqliteDbLayer<DeckSettings, DeckQuerySet>>;
|
||||||
|
|
||||||
@@ -36,8 +31,6 @@ impl TableAccessQuerySet<DeckSettings> for DeckQuerySet {
|
|||||||
forward: row.get(2)?,
|
forward: row.get(2)?,
|
||||||
backward: row.get(3)?,
|
backward: row.get(3)?,
|
||||||
filter: row.get(4)?,
|
filter: row.get(4)?,
|
||||||
count: None,
|
|
||||||
open_mode: OrderMode::Default,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,75 +52,4 @@ impl TableAccessQuerySet<DeckSettings> for DeckQuerySet {
|
|||||||
entity.id,
|
entity.id,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, NotificationData> {
|
|
||||||
let mut stmt = connection
|
|
||||||
.prepare("SELECT id, name, forward, backward, filter FROM decks")
|
|
||||||
.expect("Failed to prepare state query");
|
|
||||||
let iter = stmt
|
|
||||||
.query_map([], |row| {
|
|
||||||
Ok(DeckSettings {
|
|
||||||
id: row.get(0)?,
|
|
||||||
name: row.get(1)?,
|
|
||||||
forward: row.get(2)?,
|
|
||||||
backward: row.get(3)?,
|
|
||||||
filter: row.get(4)?,
|
|
||||||
count: None,
|
|
||||||
open_mode: OrderMode::Default,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map_err(|_| DbLoadError)?;
|
|
||||||
|
|
||||||
let mut buffer = Vec::with_capacity(16);
|
|
||||||
iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
|
|
||||||
Ok(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_set(set: &mut DeckSettings, connection: &Connection) -> MaybeError {
|
|
||||||
let index : u32 = connection
|
|
||||||
.query_row(
|
|
||||||
"INSERT INTO decks (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
|
|
||||||
(
|
|
||||||
&set.name,
|
|
||||||
&set.forward,
|
|
||||||
&set.backward,
|
|
||||||
&set.filter,
|
|
||||||
),
|
|
||||||
|row| row.get(0)
|
|
||||||
)
|
|
||||||
.map_err(|_| DbSaveError)?;
|
|
||||||
|
|
||||||
set.id = index.into();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_deck(set: &mut DeckSettings, connection: &Connection) -> MaybeError {
|
|
||||||
if !set.id.is_valid() {
|
|
||||||
add_set(set, connection)?;
|
|
||||||
} else {
|
|
||||||
connection
|
|
||||||
.execute(
|
|
||||||
"UPDATE decks SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5",
|
|
||||||
(
|
|
||||||
&set.name,
|
|
||||||
&set.forward,
|
|
||||||
&set.backward,
|
|
||||||
&set.filter,
|
|
||||||
&set.id
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.map_err(|_| DbSaveError)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete_set(set: &DeckSettings, connection: &Connection) -> MaybeError {
|
|
||||||
if !set.id.is_valid() {
|
|
||||||
return Err(DbSaveError);
|
|
||||||
}
|
|
||||||
connection
|
|
||||||
.execute("DELETE FROM decks WHERE id = ?1", (&set.id,))
|
|
||||||
.map_err(|_| DbSaveError)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
use crate::data::entities::{DeckSettings, WordGroup};
|
use crate::data::entities::{DeckSettings, WordGroup};
|
||||||
use crate::data_provider::{SqliteDbLayer, TableAccessQuerySet};
|
use crate::data_provider::{SqliteDbLayer, TableAccessQuerySet};
|
||||||
use rusqlite::{Error, Params, Row};
|
use rusqlite::{Error, Params, Row};
|
||||||
use crate::data::repositories::DataRepository;
|
use crate::data::repositories::{DataRepository, DataRepositoryInner};
|
||||||
use crate::data_provider::decks::DeckQuerySet;
|
use crate::data_provider::decks::DeckQuerySet;
|
||||||
|
|
||||||
pub type WordGroupRepository = DataRepository<WordGroup, SqliteDbLayer<WordGroup, WordGroupQuerySet>>;
|
pub type WordGroupRepository = DataRepository<WordGroup, WordGroupDataLayer>;
|
||||||
|
pub type WordGroupRepositoryInner = DataRepositoryInner<WordGroup, WordGroupDataLayer>;
|
||||||
|
pub type WordGroupDataLayer = SqliteDbLayer<WordGroup, WordGroupQuerySet>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct WordGroupQuerySet;
|
pub struct WordGroupQuerySet;
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ use crate::notification::{MaybeError, NotificationData};
|
|||||||
use litemap::LiteMap;
|
use litemap::LiteMap;
|
||||||
use rusqlite::{Connection, Error, Params, Row, params};
|
use rusqlite::{Connection, Error, Params, Row, params};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use crate::data::repositories::DataRepository;
|
use crate::data::repositories::{BatchedDbLayer, DataRepository, DataRepositoryInner};
|
||||||
|
|
||||||
pub type WordRepository = DataRepository<WordData, SqliteDbLayer<WordData, WordQuerySet>>;
|
pub type WordRepository = DataRepository<WordData, SqliteDbLayer<WordData, WordQuerySet>>;
|
||||||
|
pub type WordRepositoryInner = DataRepositoryInner<WordData, SqliteDbLayer<WordData, WordQuerySet>>;
|
||||||
|
pub type WordDataLayer = SqliteDbLayer<WordData, WordQuerySet>;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct WordQuerySet;
|
pub struct WordQuerySet;
|
||||||
impl TableAccessQuerySet<WordData> for WordQuerySet {
|
impl TableAccessQuerySet<WordData> for WordQuerySet {
|
||||||
|
|||||||
+163
-200
@@ -1,7 +1,8 @@
|
|||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
use crate::data::Id;
|
use crate::data::Id;
|
||||||
use crate::data::entities::{WordData, WordGroup};
|
use crate::data::entities::{EntityHandle, WordData, WordGroup};
|
||||||
use crate::data_provider::words::{delete_group, delete_word, update_group, update_word};
|
use crate::data::repositories::{Query, Repository};
|
||||||
|
use crate::data_provider::words::{WordRepository, WordDataLayer, WordQuerySet, WordRepositoryInner};
|
||||||
use crate::navigation::Page::{Import, Word};
|
use crate::navigation::Page::{Import, Word};
|
||||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||||
use crate::pages::dictionary::DictionaryMessage::*;
|
use crate::pages::dictionary::DictionaryMessage::*;
|
||||||
@@ -22,37 +23,38 @@ use iced::widget::*;
|
|||||||
use iced::{Border, Color, Shadow, Task};
|
use iced::{Border, Color, Shadow, Task};
|
||||||
use iced_core::Length::Fill;
|
use iced_core::Length::Fill;
|
||||||
use log::info;
|
use log::info;
|
||||||
use parking_lot::Mutex;
|
|
||||||
use rand::random_range;
|
use rand::random_range;
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
use crate::data_provider::word_groups::{WordGroupDataLayer, WordGroupRepositoryInner};
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DictionaryState {
|
pub struct DictionaryState {
|
||||||
state: AppState,
|
state: AppState,
|
||||||
include_map: Vec<bool>,
|
included: HashSet<EntityHandle<WordData>>,
|
||||||
tag_map: HashMap<String, bool>,
|
tag_map: HashMap<String, bool>,
|
||||||
|
words_tags: Vec<(EntityHandle<WordData>, Vec<String>)>,
|
||||||
reverse: bool,
|
reverse: bool,
|
||||||
search: String,
|
search: String,
|
||||||
no_typing: bool,
|
no_typing: bool,
|
||||||
selected_group_index: usize,
|
selected_group: EntityHandle<WordGroup>,
|
||||||
reverse_list: bool,
|
reverse_list: bool,
|
||||||
auto_save_queue: HashMap<usize, DateTime<Utc>>,
|
auto_save_queue: HashMap<EntityHandle<WordData>, DateTime<Utc>>,
|
||||||
|
displayed_words_query: Query<WordData, WordRepositoryInner, WordDataLayer>,
|
||||||
|
groups_query: Query<WordGroup, WordGroupRepositoryInner, WordGroupDataLayer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Clone)]
|
||||||
pub enum DictionaryMessage {
|
pub enum DictionaryMessage {
|
||||||
Back,
|
Back,
|
||||||
SetTags(usize, String),
|
SetTags(EntityHandle<WordData>, String),
|
||||||
SetKey(usize, String),
|
SetKey(EntityHandle<WordData>, String),
|
||||||
SetValue(usize, String),
|
SetValue(EntityHandle<WordData>, String),
|
||||||
SubmitWord(usize),
|
WordAction(EntityHandle<WordData>),
|
||||||
WordAction(usize),
|
|
||||||
NewWord,
|
NewWord,
|
||||||
Include(usize, bool),
|
Include(EntityHandle<WordData>, bool),
|
||||||
IncludeTag(String, bool),
|
IncludeTag(String, bool),
|
||||||
Test,
|
GotoTest,
|
||||||
ResetTags,
|
ResetTags,
|
||||||
SetReverse(bool),
|
SetReverse(bool),
|
||||||
Search(String),
|
Search(String),
|
||||||
@@ -60,11 +62,11 @@ pub enum DictionaryMessage {
|
|||||||
CreateGroup,
|
CreateGroup,
|
||||||
EditGroup(String),
|
EditGroup(String),
|
||||||
SaveGroup,
|
SaveGroup,
|
||||||
SelectGroup(usize),
|
SelectGroup(EntityHandle<WordGroup>),
|
||||||
DeleteGroup,
|
DeleteGroup,
|
||||||
ChangeDirection,
|
ChangeDirection,
|
||||||
TrySave(usize),
|
TrySave(EntityHandle<WordData>),
|
||||||
ToImport,
|
GotoImport,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||||
@@ -72,19 +74,15 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
if let Back = message {
|
if let Back = message {
|
||||||
return Some(Page::PreviousPage);
|
return Some(Page::PreviousPage);
|
||||||
}
|
}
|
||||||
if let Test = message
|
if let GotoTest = message
|
||||||
&& self.include_map.iter().any(|x| *x)
|
&& !self.included.is_empty()
|
||||||
{
|
{
|
||||||
let mut words = vec![];
|
let mut words = vec![];
|
||||||
let dict = &self.state.lock().dictionary;
|
let dict = &self.state.dictionary.read();
|
||||||
|
|
||||||
words = self
|
let mut query = dict.query(Arc::new(|_| true));
|
||||||
.include_map
|
let handles = query.handles(dict);
|
||||||
.iter()
|
words = handles.filter(|h| self.included.contains(h)).collect();
|
||||||
.zip(0..self.include_map.len())
|
|
||||||
.filter(|(flag, _)| **flag)
|
|
||||||
.map(|(_, index)| dict[index].clone())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
||||||
words,
|
words,
|
||||||
@@ -92,27 +90,19 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
self.no_typing,
|
self.no_typing,
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
if let WordAction(index) = message {
|
if let WordAction(handle) = message
|
||||||
let word: WordData;
|
&& handle.real()
|
||||||
{
|
{
|
||||||
let state = self.state.lock();
|
return Some(Word(WordState::new(*handle, self.state.clone())));
|
||||||
let dict = &state.dictionary;
|
|
||||||
word = dict[*index].clone();
|
|
||||||
}
|
|
||||||
if word.id.is_valid() {
|
|
||||||
return Some(Word(WordState::new(word, *index, self.state.clone())));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let ToImport = message {
|
if let GotoImport = message {
|
||||||
return Some(Import(ImportState::new(self.state.clone())));
|
return Some(Import(ImportState::new(self.state.clone())));
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn navigated(&mut self) {
|
fn navigated(&mut self) {
|
||||||
let len = self.state.lock().dictionary.len();
|
self.included = Default::default();
|
||||||
|
|
||||||
self.include_map = vec![false; len];
|
|
||||||
self.tag_map = Default::default();
|
self.tag_map = Default::default();
|
||||||
self.update_tags();
|
self.update_tags();
|
||||||
}
|
}
|
||||||
@@ -120,34 +110,30 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
fn update(&mut self, message: DictionaryMessage) -> Task<RootMessage> {
|
fn update(&mut self, message: DictionaryMessage) -> Task<RootMessage> {
|
||||||
match message {
|
match message {
|
||||||
NewWord => {
|
NewWord => {
|
||||||
let mut state = self.state.lock();
|
let mut dictionary = self.state.dictionary.write();
|
||||||
let mut word = WordData::new();
|
let mut word = WordData::new();
|
||||||
word.group_id = state.word_groups[self.selected_group_index].id;
|
word.group_id = self.selected_group.id;
|
||||||
|
dictionary.create(word);
|
||||||
let dict = &mut state.dictionary;
|
|
||||||
dict.push(word);
|
|
||||||
self.include_map.push(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SetKey(i, v) => {
|
SetKey(handle, v) => {
|
||||||
{
|
let mut dictionary = self.state.dictionary.write();
|
||||||
let dict = &mut self.state.lock().dictionary;
|
dictionary.get_mut_static(handle).unwrap().key = v;
|
||||||
dict[i].key = v;
|
drop(dictionary);
|
||||||
}
|
return self.launch_auto_save_offset(handle);
|
||||||
return self.launch_auto_save_offset(i);
|
|
||||||
}
|
}
|
||||||
SetValue(i, v) => {
|
SetValue(handle, v) => {
|
||||||
{
|
let mut dictionary = self.state.dictionary.write();
|
||||||
let dict = &mut self.state.lock().dictionary;
|
dictionary.get_mut_static(handle).unwrap().value = v;
|
||||||
dict[i].value = v
|
drop(dictionary);
|
||||||
}
|
return self.launch_auto_save_offset(handle);
|
||||||
return self.launch_auto_save_offset(i);
|
|
||||||
}
|
}
|
||||||
SetTags(i, mut v) => {
|
SetTags(i, mut v) => {
|
||||||
{
|
{
|
||||||
let dict = &mut self.state.lock().dictionary;
|
let mut dictionary = self.state.dictionary.write();
|
||||||
|
|
||||||
let current_tags_value = dict[i].tags.clone();
|
let word = dictionary.get_mut_static(i).unwrap();
|
||||||
|
let current_tags_value = word.tags.clone();
|
||||||
|
|
||||||
if v.ends_with(", ") && v.len() < current_tags_value.len() {
|
if v.ends_with(", ") && v.len() < current_tags_value.len() {
|
||||||
v = v[..v.len() - 2].to_string()
|
v = v[..v.len() - 2].to_string()
|
||||||
@@ -157,7 +143,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
v.remove(index);
|
v.remove(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
dict[i].tags = v;
|
word.tags = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_tags();
|
self.update_tags();
|
||||||
@@ -165,93 +151,76 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
WordAction(i) => {
|
WordAction(i) => {
|
||||||
let state = &mut self.state.lock();
|
let mut dictionary = self.state.dictionary.write();
|
||||||
let dict = &mut state.dictionary;
|
|
||||||
let word = dict.remove(i);
|
if let Err(err) = dictionary.delete(i) {
|
||||||
self.include_map.remove(i);
|
|
||||||
self.auto_save_queue.remove(&i);
|
|
||||||
if let Err(err) = delete_word(&word, &state.connection) {
|
|
||||||
return push_error_task!(err);
|
return push_error_task!(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Include(i, b) => self.include_map[i] = b,
|
Include(i, b) => {
|
||||||
IncludeTag(t, v) => {
|
if b {
|
||||||
let index;
|
self.included.insert(i);
|
||||||
{
|
} else {
|
||||||
let state = self.state.lock();
|
self.included.remove(&i);
|
||||||
index = state.word_groups[self.selected_group_index].id;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
IncludeTag(t, v) => {
|
||||||
self.tag_map.insert(t, v);
|
self.tag_map.insert(t, v);
|
||||||
self.update_words_include(index)
|
self.update_words_include(self.selected_group.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
ResetTags => {
|
ResetTags => {
|
||||||
self.tag_map.iter_mut().for_each(|(_, v)| *v = false);
|
self.tag_map.iter_mut().for_each(|(_, v)| *v = false);
|
||||||
self.include_map.iter_mut().for_each(|x| *x = false)
|
self.included.clear();
|
||||||
}
|
}
|
||||||
SetReverse(v) => self.reverse = v,
|
SetReverse(v) => self.reverse = v,
|
||||||
Search(s) => {
|
Search(s) => {
|
||||||
self.search = s;
|
self.search = s;
|
||||||
}
|
}
|
||||||
SetTyping(b) => self.no_typing = b,
|
SetTyping(b) => self.no_typing = b,
|
||||||
SubmitWord(i) => return self.save_word(i),
|
|
||||||
Back => {}
|
Back => {}
|
||||||
Test => {}
|
GotoTest => {}
|
||||||
CreateGroup => {
|
CreateGroup => {
|
||||||
let state = &mut self.state.lock();
|
let mut groups = self.state.word_groups.write();
|
||||||
|
groups.create(WordGroup {
|
||||||
state.word_groups.push(WordGroup {
|
|
||||||
id: 0.into(),
|
id: 0.into(),
|
||||||
name: format!("Группа слов {}", random_range(100..1000)),
|
name: format!("Группа слов {}", random_range(100..1000)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
EditGroup(new) => {
|
EditGroup(new) => {
|
||||||
let state = &mut self.state.lock();
|
let mut groups = self.state.word_groups.write();
|
||||||
let group = state.word_groups.get_mut(self.selected_group_index);
|
|
||||||
|
let group = groups.get_mut(&mut self.selected_group);
|
||||||
if let Some(group) = group {
|
if let Some(group) = group {
|
||||||
group.name = new.clone();
|
group.name = new;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SaveGroup => {
|
SaveGroup => {
|
||||||
let state = &mut self.state.lock();
|
let mut groups = self.state.word_groups.write();
|
||||||
let connection = &state.connection;
|
|
||||||
let mut group = state.word_groups[self.selected_group_index].clone();
|
|
||||||
|
|
||||||
if let Err(err) = update_group(&mut group, connection) {
|
if let Err(err) = groups.save(&mut self.selected_group) {
|
||||||
return push_error_task!(err);
|
return push_error_task!(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
state.word_groups[self.selected_group_index] = group;
|
|
||||||
}
|
}
|
||||||
SelectGroup(i) => {
|
SelectGroup(i) => {
|
||||||
self.selected_group_index = i;
|
self.selected_group = i;
|
||||||
let index;
|
self.update_words_include(i.id)
|
||||||
{
|
|
||||||
let state = self.state.lock();
|
|
||||||
index = state.word_groups[i].id;
|
|
||||||
}
|
|
||||||
self.update_words_include(index)
|
|
||||||
}
|
}
|
||||||
DeleteGroup => {
|
DeleteGroup => {
|
||||||
if self.selected_group_index == 0 {
|
if self.selected_group.id.0 == 1 {
|
||||||
return Task::none();
|
return Task::none();
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let state = &mut self.state.lock();
|
let mut groups = self.state.word_groups.write();
|
||||||
|
let delete_group = self.selected_group;
|
||||||
|
self.selected_group = groups.handle_by_id(Id(1));
|
||||||
|
|
||||||
if let Some(group) = state.word_groups.get(self.selected_group_index) {
|
if let Err(err) = groups.delete(delete_group) {
|
||||||
let remove_group_id = group.id;
|
return push_error_task!(err);
|
||||||
let connection = &state.connection;
|
}
|
||||||
|
|
||||||
if let Err(err) = delete_group(group, connection) {
|
if let Err(err) = self.state.dictionary.write().reload() {
|
||||||
return push_error_task!(err);
|
return push_error_task!(err);
|
||||||
}
|
|
||||||
|
|
||||||
state.word_groups.remove(self.selected_group_index);
|
|
||||||
state
|
|
||||||
.dictionary
|
|
||||||
.retain(|word| word.group_id != remove_group_id);
|
|
||||||
self.selected_group_index = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,18 +229,21 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
ChangeDirection => {
|
ChangeDirection => {
|
||||||
self.reverse_list = !self.reverse_list;
|
self.reverse_list = !self.reverse_list;
|
||||||
}
|
}
|
||||||
TrySave(word_index) => {
|
TrySave(mut handle) => {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
if !self.auto_save_queue.contains_key(&word_index) {
|
if !self.auto_save_queue.contains_key(&handle) {
|
||||||
return Task::none();
|
return Task::none();
|
||||||
}
|
}
|
||||||
if self.auto_save_queue[&word_index] <= now {
|
if self.auto_save_queue[&handle] <= now {
|
||||||
self.auto_save_queue.remove(&word_index);
|
self.auto_save_queue.remove(&handle);
|
||||||
|
|
||||||
return self.save_word(word_index);
|
let mut dictionary = self.state.dictionary.write();
|
||||||
|
|
||||||
|
let _ = dictionary.save(&mut handle);
|
||||||
|
return Task::none();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ToImport => {}
|
GotoImport => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Task::none()
|
Task::none()
|
||||||
@@ -286,7 +258,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
row![
|
row![
|
||||||
self.add_word_button(),
|
self.add_word_button(),
|
||||||
horizontal().width(Fill),
|
horizontal().width(Fill),
|
||||||
button("Импорт").style(text).on_press(ToImport),
|
button("Импорт").style(text).on_press(GotoImport),
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
.spacing(5),
|
.spacing(5),
|
||||||
@@ -301,42 +273,41 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|||||||
|
|
||||||
impl DictionaryState {
|
impl DictionaryState {
|
||||||
pub fn new(state: AppState) -> Self {
|
pub fn new(state: AppState) -> Self {
|
||||||
let len = state.lock().dictionary.len();
|
let words_query = state.dictionary.read().empty_query_par();
|
||||||
|
let groups_query = state.word_groups.read().query(Arc::new(|_| true));
|
||||||
let mut result = DictionaryState {
|
let mut result = DictionaryState {
|
||||||
include_map: vec![false; len],
|
included: Default::default(),
|
||||||
selected_group_index: 0,
|
selected_group: EntityHandle::new(Id(1), 0, true),
|
||||||
state,
|
state,
|
||||||
tag_map: HashMap::new(),
|
tag_map: HashMap::new(),
|
||||||
|
words_tags: vec![],
|
||||||
reverse: false,
|
reverse: false,
|
||||||
search: "".to_string(),
|
search: "".to_string(),
|
||||||
no_typing: true,
|
no_typing: true,
|
||||||
reverse_list: true,
|
reverse_list: true,
|
||||||
auto_save_queue: HashMap::new(),
|
auto_save_queue: HashMap::new(),
|
||||||
|
displayed_words_query: words_query,
|
||||||
|
groups_query,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
result.init();
|
||||||
result.update_tags();
|
result.update_tags();
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_word(&mut self, i: usize) -> Task<RootMessage> {
|
fn init(&mut self) {
|
||||||
let state = &mut self.state.lock();
|
self.displayed_words_query = self.state.dictionary.read().query_par(Arc::new(|word| word.group_id == Id(1)));
|
||||||
let connection = &state.connection;
|
|
||||||
let word = &mut state.dictionary[i].clone();
|
|
||||||
|
|
||||||
if let Err(err) = update_word(word, connection) {
|
|
||||||
return push_error_task!(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
state.dictionary[i] = word.clone();
|
|
||||||
Task::none()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn launch_auto_save_offset(&mut self, index: usize) -> Task<RootMessage> {
|
fn setup_words_query(&mut self, id: Id) {
|
||||||
|
// self.displayed_words_query = self.state.dictionary.read().query_par(Arc::new(|word| word.group_id == Self::get_id(self)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn launch_auto_save_offset(&mut self, handle: EntityHandle<WordData>) -> Task<RootMessage> {
|
||||||
let save_time = Utc::now().add(TimeDelta::milliseconds(900));
|
let save_time = Utc::now().add(TimeDelta::milliseconds(900));
|
||||||
self.auto_save_queue.insert(index, save_time);
|
self.auto_save_queue.insert(handle, save_time);
|
||||||
let message = RootMessage::Dictionary(TrySave(index));
|
let message = RootMessage::Dictionary(TrySave(handle));
|
||||||
Task::perform(
|
Task::perform(
|
||||||
async { tokio::time::sleep(Duration::from_secs(1)).await },
|
async { tokio::time::sleep(Duration::from_secs(1)).await },
|
||||||
|_| message,
|
|_| message,
|
||||||
@@ -347,22 +318,17 @@ impl DictionaryState {
|
|||||||
let time = Instant::now();
|
let time = Instant::now();
|
||||||
let mut col = Column::new().width(Fill);
|
let mut col = Column::new().width(Fill);
|
||||||
|
|
||||||
let state = self.state.lock();
|
let query = &self.displayed_words_query;
|
||||||
let group_id = state.word_groups[self.selected_group_index].id;
|
let dictionary = self.state.dictionary.read();
|
||||||
|
|
||||||
let dict = &state.dictionary;
|
|
||||||
|
|
||||||
let mut index = 0;
|
for access_index in 0..query.len() {
|
||||||
|
|
||||||
for access_index in 0..dict.len() {
|
|
||||||
let mut i = access_index;
|
let mut i = access_index;
|
||||||
if self.reverse_list {
|
if self.reverse_list {
|
||||||
i = dict.len() - access_index - 1;
|
i = query.len() - access_index - 1;
|
||||||
}
|
|
||||||
let word = &dict[i];
|
|
||||||
if word.group_id != group_id {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
let word_handle = &query[i];
|
||||||
|
let word = dictionary.get_ref_static(*word_handle).unwrap();
|
||||||
|
|
||||||
if !self.search.is_empty()
|
if !self.search.is_empty()
|
||||||
&& !word.key.contains(&self.search)
|
&& !word.key.contains(&self.search)
|
||||||
@@ -373,32 +339,28 @@ impl DictionaryState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let word_line_data = WordLineState {
|
let word_line_data = WordLineState {
|
||||||
is_included: self.include_map[i],
|
is_included: self.included.contains(word_handle),
|
||||||
key: word.key.clone(),
|
key: word.key.clone().into(),
|
||||||
value: word.value.clone(),
|
value: word.value.clone().into(),
|
||||||
tags: word.tags.clone(),
|
tags: word.tags.clone().into(),
|
||||||
id: word.id,
|
handle: word_handle.clone(),
|
||||||
index: i,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
index += 1;
|
|
||||||
|
|
||||||
let lazy_line = lazy(word_line_data, move |data| {
|
let lazy_line = lazy(word_line_data, move |data| {
|
||||||
let index = data.index;
|
|
||||||
let mut line = Row::new().width(Fill).align_y(Center);
|
let mut line = Row::new().width(Fill).align_y(Center);
|
||||||
line = line.push(
|
line = line.push(
|
||||||
checkbox(data.is_included)
|
checkbox(data.is_included)
|
||||||
.label("")
|
.label("")
|
||||||
.spacing(15)
|
.spacing(15)
|
||||||
.on_toggle(move |b| Include(index, b)),
|
.on_toggle(move |b| Include(data.handle, b)),
|
||||||
);
|
);
|
||||||
|
|
||||||
line = line.push(
|
line = line.push(
|
||||||
text_input("Слово", &data.key)
|
text_input("Слово", &data.key)
|
||||||
.size(ACCENT_FONT_SIZE)
|
.size(ACCENT_FONT_SIZE)
|
||||||
.width(Fill)
|
.width(Fill)
|
||||||
.on_input(move |string| SetKey(index, string))
|
.on_input(move |string| SetKey(data.handle, string))
|
||||||
.on_submit(SubmitWord(index))
|
|
||||||
.style(|x, status| {
|
.style(|x, status| {
|
||||||
let mut default_style = default(x, status);
|
let mut default_style = default(x, status);
|
||||||
default_style.border.radius = 0.0.into();
|
default_style.border.radius = 0.0.into();
|
||||||
@@ -409,8 +371,7 @@ impl DictionaryState {
|
|||||||
text_input("Перевод", &data.value)
|
text_input("Перевод", &data.value)
|
||||||
.size(ACCENT_FONT_SIZE)
|
.size(ACCENT_FONT_SIZE)
|
||||||
.width(Fill)
|
.width(Fill)
|
||||||
.on_input(move |string| SetValue(index, string))
|
.on_input(move |string| SetValue(data.handle, string))
|
||||||
.on_submit(SubmitWord(index))
|
|
||||||
.style(|x, status| {
|
.style(|x, status| {
|
||||||
let mut default_style = default(x, status);
|
let mut default_style = default(x, status);
|
||||||
default_style.border.radius = 0.0.into();
|
default_style.border.radius = 0.0.into();
|
||||||
@@ -419,11 +380,10 @@ impl DictionaryState {
|
|||||||
);
|
);
|
||||||
|
|
||||||
line = line.push(
|
line = line.push(
|
||||||
text_input("Теги", &data.tags)
|
text_input("Теги", data.tags.as_ref())
|
||||||
.size(ACCENT_FONT_SIZE)
|
.size(ACCENT_FONT_SIZE)
|
||||||
.width(Fill)
|
.width(Fill)
|
||||||
.on_input(move |string| SetTags(index, string))
|
.on_input(move |string| SetTags(data.handle, string))
|
||||||
.on_submit(SubmitWord(index))
|
|
||||||
.style(|x, status| {
|
.style(|x, status| {
|
||||||
let mut default_style = default(x, status);
|
let mut default_style = default(x, status);
|
||||||
default_style.border.radius = 0.0.into();
|
default_style.border.radius = 0.0.into();
|
||||||
@@ -432,9 +392,9 @@ impl DictionaryState {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let line_button = || {
|
let line_button = || {
|
||||||
let action = WordAction(index);
|
let action = WordAction(data.handle);
|
||||||
|
|
||||||
if !data.id.is_valid() {
|
if !data.handle.real() {
|
||||||
return button("-").on_press(action).style(|_x, _status| Style {
|
return button("-").on_press(action).style(|_x, _status| Style {
|
||||||
background: None,
|
background: None,
|
||||||
text_color: Color::BLACK,
|
text_color: Color::BLACK,
|
||||||
@@ -444,22 +404,20 @@ impl DictionaryState {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
button("").on_press(WordAction(index)).width(15)
|
button("").on_press(WordAction(data.handle)).width(15)
|
||||||
};
|
};
|
||||||
line = line.push(line_button()).push(space().width(10));
|
line.push(line_button()).push(space().width(10))
|
||||||
line
|
|
||||||
});
|
});
|
||||||
|
|
||||||
col = col.push(lazy_line);
|
col = col.push(lazy_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Drawing {index} lines");
|
|
||||||
info!("Words rendering time: {:?}", time.elapsed());
|
info!("Words rendering time: {:?}", time.elapsed());
|
||||||
scrollable(col).height(Fill).into()
|
scrollable(col).height(Fill).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filters(&self) -> iced::Element<'_, DictionaryMessage> {
|
fn filters(&self) -> iced::Element<'_, DictionaryMessage> {
|
||||||
let dict = &self.state.lock().dictionary;
|
let dict = &self.state.dictionary.read();
|
||||||
|
|
||||||
iced::widget::column![
|
iced::widget::column![
|
||||||
text_input("Поиск", &self.search)
|
text_input("Поиск", &self.search)
|
||||||
@@ -468,7 +426,7 @@ impl DictionaryState {
|
|||||||
text!("Всего слов: {}", dict.len()),
|
text!("Всего слов: {}", dict.len()),
|
||||||
text!(
|
text!(
|
||||||
"Выбрано слов: {}",
|
"Выбрано слов: {}",
|
||||||
self.include_map.iter().filter(|i| **i).count()
|
self.included.iter().count()
|
||||||
),
|
),
|
||||||
self.tags_selector(),
|
self.tags_selector(),
|
||||||
toggler(self.no_typing)
|
toggler(self.no_typing)
|
||||||
@@ -479,7 +437,7 @@ impl DictionaryState {
|
|||||||
.on_toggle(SetReverse),
|
.on_toggle(SetReverse),
|
||||||
button(text!("Тест").center().width(Fill))
|
button(text!("Тест").center().width(Fill))
|
||||||
.style(cta_button)
|
.style(cta_button)
|
||||||
.on_press(Test)
|
.on_press(GotoTest)
|
||||||
.width(Fill),
|
.width(Fill),
|
||||||
]
|
]
|
||||||
.width(250)
|
.width(250)
|
||||||
@@ -515,7 +473,7 @@ impl DictionaryState {
|
|||||||
|
|
||||||
fn update_tags(&mut self) {
|
fn update_tags(&mut self) {
|
||||||
let mut tags_list: HashSet<String> = HashSet::new();
|
let mut tags_list: HashSet<String> = HashSet::new();
|
||||||
let dict = &self.state.lock().dictionary;
|
let dict = &self.state.dictionary.write();
|
||||||
|
|
||||||
dict.iter().for_each(|element| {
|
dict.iter().for_each(|element| {
|
||||||
split_with_coma(element.tags.as_str())
|
split_with_coma(element.tags.as_str())
|
||||||
@@ -552,22 +510,29 @@ impl DictionaryState {
|
|||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
if include_tags.is_empty() {
|
if include_tags.is_empty() {
|
||||||
self.include_map.iter_mut().for_each(|x| *x = false);
|
self.included.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let dict = &self.state.lock().dictionary;
|
let dict = &self.state.dictionary.read();
|
||||||
let time = Instant::now();
|
let time = Instant::now();
|
||||||
|
|
||||||
self.include_map = dict
|
self.included.clear();
|
||||||
.iter()
|
for (handle, tags) in &mut self.words_tags {
|
||||||
.map(|word| (split_with_coma(word.tags.as_str()), word.group_id))
|
if let Some(word) = dict.get_ref(handle) {
|
||||||
.map(|(tags, word_group_id)| {
|
if word.group_id != group_id {
|
||||||
!tags.is_empty()
|
continue;
|
||||||
&& tags.iter().all(|t| include_tags.contains(t))
|
}
|
||||||
&& word_group_id == group_id
|
|
||||||
})
|
if !tags.is_empty()
|
||||||
.collect();
|
&& tags.iter().all(|t| include_tags.contains(t)) {
|
||||||
|
self.included.insert(*handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
info!("Time {}", time.elapsed().as_micros());
|
info!("Time {}", time.elapsed().as_micros());
|
||||||
}
|
}
|
||||||
@@ -576,18 +541,19 @@ impl DictionaryState {
|
|||||||
let mut row = Row::new();
|
let mut row = Row::new();
|
||||||
row = row.push(button("+").style(text).on_press(CreateGroup));
|
row = row.push(button("+").style(text).on_press(CreateGroup));
|
||||||
|
|
||||||
let state = &self.state.lock();
|
let groups = &self.state.word_groups.read();
|
||||||
let groups = &state.word_groups;
|
|
||||||
|
|
||||||
for (index, group) in groups.iter().enumerate() {
|
for mut group in self.groups_query.handles(groups) {
|
||||||
|
let group_name = groups.get(&mut group).unwrap().name.clone();
|
||||||
row = row.push(
|
row = row.push(
|
||||||
button(text!("{}", group.name.clone()))
|
button(text!("{}", group_name))
|
||||||
.style(text)
|
.style(text)
|
||||||
.on_press(SelectGroup(index)),
|
.on_press(SelectGroup(group)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let group = state.word_groups[self.selected_group_index].clone();
|
let groups = self.state.word_groups.read();
|
||||||
|
let group = groups.get_static(self.selected_group.clone()).unwrap();
|
||||||
|
|
||||||
iced::widget::column![
|
iced::widget::column![
|
||||||
scrollable(row).width(Fill).horizontal(),
|
scrollable(row).width(Fill).horizontal(),
|
||||||
@@ -606,7 +572,7 @@ impl DictionaryState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn group_delete_button(&self) -> iced::Element<'_, DictionaryMessage> {
|
fn group_delete_button(&self) -> iced::Element<'_, DictionaryMessage> {
|
||||||
if self.selected_group_index != 0 {
|
if self.selected_group.real() && self.selected_group.id != 1.into() {
|
||||||
button("Удалить").style(danger).on_press(DeleteGroup).into()
|
button("Удалить").style(danger).on_press(DeleteGroup).into()
|
||||||
} else {
|
} else {
|
||||||
space().into()
|
space().into()
|
||||||
@@ -614,11 +580,9 @@ impl DictionaryState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add_word_button(&self) -> iced::Element<'_, DictionaryMessage> {
|
fn add_word_button(&self) -> iced::Element<'_, DictionaryMessage> {
|
||||||
let state = self.state.lock();
|
|
||||||
let group_id = state.word_groups[self.selected_group_index].id;
|
|
||||||
let button = button("Добавить слово").style(jl_button);
|
let button = button("Добавить слово").style(jl_button);
|
||||||
|
|
||||||
if group_id.is_valid() {
|
if self.selected_group.real() {
|
||||||
return button.on_press(NewWord).into();
|
return button.on_press(NewWord).into();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -636,9 +600,8 @@ pub fn split_with_coma(ts: &str) -> Vec<String> {
|
|||||||
#[derive(Hash, PartialEq, Eq)]
|
#[derive(Hash, PartialEq, Eq)]
|
||||||
struct WordLineState {
|
struct WordLineState {
|
||||||
is_included: bool,
|
is_included: bool,
|
||||||
key: String,
|
key: Box<str>,
|
||||||
value: String,
|
value: Box<str>,
|
||||||
tags: String,
|
tags: Box<str>,
|
||||||
id: Id,
|
handle: EntityHandle<WordData>,
|
||||||
index: usize,
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::data::entities::WordData;
|
use crate::data::entities::{EntityHandle, WordData};
|
||||||
use crate::navigation::Page::PreviousPage;
|
use crate::navigation::Page::PreviousPage;
|
||||||
use crate::navigation::RootMessage;
|
use crate::navigation::RootMessage;
|
||||||
use crate::navigation::*;
|
use crate::navigation::*;
|
||||||
@@ -13,10 +13,9 @@ use iced::widget::{Row, button, container, row, space, text, text_input};
|
|||||||
use iced::{Border, Element, Fill, Task, Theme, alignment};
|
use iced::{Border, Element, Fill, Task, Theme, alignment};
|
||||||
use rand::prelude::SliceRandom;
|
use rand::prelude::SliceRandom;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DictionaryQuizState {
|
pub struct DictionaryQuizState {
|
||||||
words: Vec<WordData>,
|
words: Vec<EntityHandle<WordData>>,
|
||||||
current_set: Vec<WordData>,
|
current_set: Vec<EntityHandle<WordData>>,
|
||||||
answer: String,
|
answer: String,
|
||||||
view: String,
|
view: String,
|
||||||
correct: String,
|
correct: String,
|
||||||
@@ -104,8 +103,8 @@ impl NavigatedPage<DictionaryQuizMessage> for DictionaryQuizState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DictionaryQuizState {
|
impl DictionaryQuizState {
|
||||||
pub fn new(words: Vec<WordData>, reverse: bool, no_typing: bool) -> DictionaryQuizState {
|
pub fn new(words: Vec<EntityHandle<WordData>>, reverse: bool, no_typing: bool) -> DictionaryQuizState {
|
||||||
DictionaryQuizState {
|
let state = DictionaryQuizState {
|
||||||
words,
|
words,
|
||||||
current_set: Vec::new(),
|
current_set: Vec::new(),
|
||||||
answer: "".to_string(),
|
answer: "".to_string(),
|
||||||
@@ -116,7 +115,9 @@ impl DictionaryQuizState {
|
|||||||
reverse,
|
reverse,
|
||||||
laps: 0,
|
laps: 0,
|
||||||
no_typing,
|
no_typing,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
state
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submit(&mut self) {
|
fn submit(&mut self) {
|
||||||
@@ -172,7 +173,7 @@ impl DictionaryQuizState {
|
|||||||
self.is_help = false;
|
self.is_help = false;
|
||||||
self.answer = String::new();
|
self.answer = String::new();
|
||||||
|
|
||||||
let next: WordData = match self.current_set.pop() {
|
let next: EntityHandle<WordData> = match self.current_set.pop() {
|
||||||
Some(word) => word,
|
Some(word) => word,
|
||||||
None => {
|
None => {
|
||||||
self.update_set();
|
self.update_set();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
use crate::data::entities::DeckSettings;
|
use crate::data::entities::DeckSettings;
|
||||||
use crate::data_provider::card_states::{delete_stats, load_stats_of_deck};
|
use crate::data_provider::card_states::{delete_stats, load_stats_of_deck};
|
||||||
use crate::data_provider::decks::{delete_set, update_deck};
|
|
||||||
use crate::navigation::Page::PreviousPage;
|
use crate::navigation::Page::PreviousPage;
|
||||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||||
use crate::notification::MaybeError;
|
use crate::notification::MaybeError;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ use crate::core::srs::{AppendMode, OrderMode};
|
|||||||
use crate::data::Id;
|
use crate::data::Id;
|
||||||
use crate::data::entities::{CardState, DeckSettings};
|
use crate::data::entities::{CardState, DeckSettings};
|
||||||
use crate::data_provider::card_states::{add_stat_list, load_stats_of_deck};
|
use crate::data_provider::card_states::{add_stat_list, load_stats_of_deck};
|
||||||
use crate::data_provider::decks::update_deck;
|
|
||||||
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
|
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
|
||||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||||
use crate::notification::{MaybeError, NotificationRequest};
|
use crate::notification::{MaybeError, NotificationRequest};
|
||||||
|
|||||||
+7
-5
@@ -1,5 +1,5 @@
|
|||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
use crate::data::entities::WordData;
|
use crate::data::entities::{EntityHandle, WordData};
|
||||||
use crate::data_provider::words::{delete_word, update_word};
|
use crate::data_provider::words::{delete_word, update_word};
|
||||||
use crate::navigation::Page::PreviousPage;
|
use crate::navigation::Page::PreviousPage;
|
||||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||||
@@ -16,8 +16,7 @@ use std::sync::Arc;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct WordState {
|
pub struct WordState {
|
||||||
state: AppState,
|
state: AppState,
|
||||||
index: usize,
|
handle: EntityHandle<WordData>,
|
||||||
word: WordData,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NavigatedPage<WordMessage> for WordState {
|
impl NavigatedPage<WordMessage> for WordState {
|
||||||
@@ -136,8 +135,11 @@ impl NavigatedPage<WordMessage> for WordState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WordState {
|
impl WordState {
|
||||||
pub(crate) fn new(word: WordData, index: usize, state: AppState) -> WordState {
|
pub fn new(handle: EntityHandle<WordData>, state: AppState) -> Self {
|
||||||
WordState { state, index, word }
|
Self{
|
||||||
|
state,
|
||||||
|
handle,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user