Compare commits
20
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39f8845119 | ||
|
|
e7023752f1 | ||
|
|
746071125a | ||
|
|
bb673ed7c7 | ||
|
|
98c8093961 | ||
|
|
a4d77196d0 | ||
|
|
4187344a29 | ||
|
|
d3c28179b6 | ||
|
|
3cd7c12baa | ||
|
|
306febc514 | ||
|
|
80785ff9e6 | ||
|
|
9e28b05167 | ||
|
|
97c76350e1 | ||
|
|
99b3818181 | ||
|
|
3fcbd235cf | ||
|
|
dcf7352553 | ||
|
|
11b38479c1 | ||
|
|
51c8d7be74 | ||
|
|
8f789facd7 | ||
|
|
e676898135 |
Generated
+26
@@ -73,6 +73,12 @@ dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "allocator-api2"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "alsa"
|
||||
version = "0.11.0"
|
||||
@@ -1750,6 +1756,8 @@ version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"equivalent",
|
||||
"foldhash 0.2.0",
|
||||
]
|
||||
|
||||
@@ -2306,11 +2314,13 @@ dependencies = [
|
||||
"chrono",
|
||||
"criterion",
|
||||
"dirs",
|
||||
"hashbrown 0.17.1",
|
||||
"hex",
|
||||
"iced",
|
||||
"iced_core",
|
||||
"mimalloc",
|
||||
"rand",
|
||||
"rayon",
|
||||
"reqwest",
|
||||
"rfd",
|
||||
"rhai",
|
||||
@@ -2783,6 +2793,15 @@ dependencies = [
|
||||
"jni-sys 0.3.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "no-std-compat"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c"
|
||||
dependencies = [
|
||||
"spin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
@@ -3986,6 +4005,7 @@ checksum = "dd4dd0f8c36625202a4ba553c416c19b719947cd2a31d1bda06126e4a5727daf"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"bitflags 2.10.0",
|
||||
"no-std-compat",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
"rhai_codegen",
|
||||
@@ -4582,6 +4602,12 @@ dependencies = [
|
||||
"x11rb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "spirv"
|
||||
version = "0.3.0+sdk-1.3.268.0"
|
||||
|
||||
+6
-4
@@ -10,7 +10,7 @@ rand = "0.10.2"
|
||||
dirs = "6.0.0"
|
||||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
serde_json = "1.0.151"
|
||||
rhai = "1.25.1"
|
||||
rhai = { version = "1.25.1", features = ["sync"] }
|
||||
chrono = "0.4.45"
|
||||
rusqlite = { version = "0.40.2", features = ["chrono", "bundled"] }
|
||||
reqwest = { version = "0.13.4", features = ["json", "stream", "multipart"] }
|
||||
@@ -22,6 +22,8 @@ zstd = "0.13.3"
|
||||
zip = "8.6.0"
|
||||
rfd = "0.17.2"
|
||||
mimalloc = "0.1.52"
|
||||
hashbrown = "0.17.1"
|
||||
rayon = "1.12.0"
|
||||
|
||||
[profile.super-release]
|
||||
inherits = "release"
|
||||
@@ -32,10 +34,10 @@ strip = true
|
||||
panic = "abort"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.8.2"
|
||||
criterion = { version = "0.8.2", features = ["html_reports"] }
|
||||
|
||||
[[bench]]
|
||||
name = "split_bench" # Имя файла в benches/ без расширения
|
||||
harness = false # Отключаем стандартный тестовый раннер
|
||||
name = "map_bench"
|
||||
harness = false
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
use criterion::{Criterion, criterion_group, criterion_main};
|
||||
use std::collections::HashMap as StdHashMap;
|
||||
use std::hint::black_box;
|
||||
|
||||
use hashbrown::HashMap as BrownHashMap;
|
||||
|
||||
const SIZES: [usize; 3] = [5, 10, 50];
|
||||
const QUERY_COUNT: usize = 4096;
|
||||
|
||||
/// Детерминированный ключ ~17 символов, например "key_9e3779b9_0042".
|
||||
fn make_key(i: u64) -> String {
|
||||
let h = i.wrapping_mul(0x9E37_79B9_7F4A_7C15);
|
||||
format!("key_{:08x}_{:04}", h & 0xFFFF_FFFF, i)
|
||||
}
|
||||
|
||||
// Для длинных ключей (пути/URL) замени на:
|
||||
// fn make_key(i: u64) -> String {
|
||||
// format!(
|
||||
// "/api/v2/users/{i}/settings/visibility_{:08x}",
|
||||
// i.wrapping_mul(0x9E37_79B9)
|
||||
// )
|
||||
// }
|
||||
|
||||
fn make_vec(n: usize) -> Vec<(String, u64)> {
|
||||
(0..n as u64).map(|i| (make_key(i), i)).collect()
|
||||
}
|
||||
|
||||
fn make_std_map(data: &[(String, u64)]) -> StdHashMap<String, u64> {
|
||||
data.iter().map(|(k, v)| (k.clone(), *v)).collect()
|
||||
}
|
||||
|
||||
fn make_brown_map(data: &[(String, u64)]) -> BrownHashMap<String, u64> {
|
||||
data.iter().map(|(k, v)| (k.clone(), *v)).collect()
|
||||
}
|
||||
|
||||
fn make_queries(n: usize, with_misses: bool) -> Vec<String> {
|
||||
let keys: Vec<String> = (0..n as u64).map(make_key).collect();
|
||||
let missing = "__missing_key__".to_string();
|
||||
|
||||
(0..QUERY_COUNT)
|
||||
.map(|i| {
|
||||
if with_misses && i % 16 == 15 {
|
||||
missing.clone()
|
||||
} else {
|
||||
let idx = i.wrapping_mul(2_654_435_761) % n;
|
||||
keys[idx].clone()
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn lookup_std(map: &StdHashMap<String, u64>, queries: &[String]) -> u64 {
|
||||
let mut sum = 0u64;
|
||||
for q in queries {
|
||||
match map.get(black_box(q.as_str())) {
|
||||
Some(v) => sum = sum.wrapping_add(*v),
|
||||
None => sum = sum.wrapping_add(1),
|
||||
}
|
||||
}
|
||||
black_box(sum)
|
||||
}
|
||||
|
||||
fn lookup_brown(map: &BrownHashMap<String, u64>, queries: &[String]) -> u64 {
|
||||
let mut sum = 0u64;
|
||||
for q in queries {
|
||||
match map.get(black_box(q.as_str())) {
|
||||
Some(v) => sum = sum.wrapping_add(*v),
|
||||
None => sum = sum.wrapping_add(1),
|
||||
}
|
||||
}
|
||||
black_box(sum)
|
||||
}
|
||||
|
||||
fn lookup_vec(data: &[(String, u64)], queries: &[String]) -> u64 {
|
||||
let mut sum = 0u64;
|
||||
for q in queries {
|
||||
let q = black_box(q);
|
||||
match data.iter().find(|(k, _)| k == q) {
|
||||
Some((_, v)) => sum = sum.wrapping_add(*v),
|
||||
None => sum = sum.wrapping_add(1),
|
||||
}
|
||||
}
|
||||
black_box(sum)
|
||||
}
|
||||
|
||||
fn bench(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("map_vs_vec_str");
|
||||
|
||||
for n in SIZES {
|
||||
let vec_data = make_vec(n);
|
||||
let std_map = make_std_map(&vec_data);
|
||||
let brown_map = make_brown_map(&vec_data);
|
||||
|
||||
let queries_hit = make_queries(n, false);
|
||||
let queries_mixed = make_queries(n, true);
|
||||
|
||||
// std::collections::HashMap (SipHash)
|
||||
group.bench_function(format!("std_hashmap_hit/{n}"), |b| {
|
||||
b.iter(|| lookup_std(black_box(&std_map), black_box(&queries_hit)))
|
||||
});
|
||||
group.bench_function(format!("std_hashmap_mixed/{n}"), |b| {
|
||||
b.iter(|| lookup_std(black_box(&std_map), black_box(&queries_mixed)))
|
||||
});
|
||||
|
||||
// hashbrown (SwissTable + foldhash)
|
||||
group.bench_function(format!("hashbrown_hit/{n}"), |b| {
|
||||
b.iter(|| lookup_brown(black_box(&brown_map), black_box(&queries_hit)))
|
||||
});
|
||||
group.bench_function(format!("hashbrown_mixed/{n}"), |b| {
|
||||
b.iter(|| lookup_brown(black_box(&brown_map), black_box(&queries_mixed)))
|
||||
});
|
||||
|
||||
// Vec<(String, u64)>, линейный поиск
|
||||
group.bench_function(format!("vec_linear_hit/{n}"), |b| {
|
||||
b.iter(|| lookup_vec(black_box(&vec_data), black_box(&queries_hit)))
|
||||
});
|
||||
group.bench_function(format!("vec_linear_mixed/{n}"), |b| {
|
||||
b.iter(|| lookup_vec(black_box(&vec_data), black_box(&queries_mixed)))
|
||||
});
|
||||
}
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench);
|
||||
criterion_main!(benches);
|
||||
@@ -1,43 +0,0 @@
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
|
||||
fn bench_split(c: &mut Criterion) {
|
||||
let input = "data_1";
|
||||
|
||||
c.bench_function("split_with_comma", |b| {
|
||||
b.iter(|| {
|
||||
// black_box запрещает компилятору оптимизировать результат
|
||||
black_box(split_with_coma(black_box(input)))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_split_new(c: &mut Criterion) {
|
||||
let input = "data_1";
|
||||
|
||||
c.bench_function("split_with_comma gpt", |b| {
|
||||
b.iter(|| {
|
||||
// black_box запрещает компилятору оптимизировать результат
|
||||
black_box(split_with_coma(black_box(input)))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pub fn split_with_coma(ts: &str) -> Vec<String> {
|
||||
ts.split(',')
|
||||
.map(|ts| ts.to_lowercase().trim().to_string())
|
||||
.filter(|t| !t.is_empty())
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
pub fn new_split_with_coma(ts: &str) -> Vec<String> {
|
||||
ts.split(',')
|
||||
.map(|s| s.trim()) // 0 аллокаций, просто срез
|
||||
.filter(|s| !s.is_empty()) // отбрасываем пустые до аллокации
|
||||
.map(|s| s.to_lowercase()) // 1 аллокация на валидный токен
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Можно добавить несколько бенчмарков в группу
|
||||
criterion_group!(benches, bench_split, bench_split_new);
|
||||
criterion_main!(benches);
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::lang::SetOrderMode;
|
||||
use crate::repetitions::CardSetSettings;
|
||||
use crate::lang::{DeckSettings, OrderMode};
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub fn load_sets(connection: &Connection) -> Vec<CardSetSettings> {
|
||||
pub fn load_sets(connection: &Connection) -> Vec<DeckSettings> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, name, forward, backward, filter FROM card_set")
|
||||
.unwrap();
|
||||
let iter = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(CardSetSettings {
|
||||
Ok(DeckSettings {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
forward: row.get(2)?,
|
||||
@@ -16,7 +16,7 @@ pub fn load_sets(connection: &Connection) -> Vec<CardSetSettings> {
|
||||
filter: row.get(4)?,
|
||||
count: None,
|
||||
worst_words_list: None,
|
||||
open_mode: SetOrderMode::Default,
|
||||
open_mode: OrderMode::Default,
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
@@ -29,7 +29,7 @@ pub fn load_sets(connection: &Connection) -> Vec<CardSetSettings> {
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn add_set(set: &mut CardSetSettings, connection: &Connection) {
|
||||
pub fn add_set(set: &mut DeckSettings, connection: &Connection) {
|
||||
let index = connection
|
||||
.query_row(
|
||||
"INSERT INTO card_set (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
|
||||
@@ -43,12 +43,12 @@ pub fn add_set(set: &mut CardSetSettings, connection: &Connection) {
|
||||
)
|
||||
.unwrap_or_else(|e| {println!("{}", e); 0});
|
||||
|
||||
set.id = index;
|
||||
set.id = index.into();
|
||||
}
|
||||
|
||||
pub fn update_card_set(set: &mut CardSetSettings, connection: &Connection) {
|
||||
if set.id == 0 {
|
||||
add_set(set, &connection);
|
||||
pub fn update_deck(set: &mut DeckSettings, connection: &Connection) {
|
||||
if !set.id.is_valid() {
|
||||
add_set(set, connection);
|
||||
} else {
|
||||
connection
|
||||
.execute(
|
||||
@@ -65,8 +65,8 @@ pub fn update_card_set(set: &mut CardSetSettings, connection: &Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_set(set: &CardSetSettings, connection: &Connection) {
|
||||
if set.id == 0 {
|
||||
pub fn delete_set(set: &DeckSettings, connection: &Connection) {
|
||||
if !set.id.is_valid() {
|
||||
return;
|
||||
}
|
||||
connection
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::lang::CardStatistics;
|
||||
use crate::repetitions::CardSetSettings;
|
||||
use rusqlite::Connection;
|
||||
use crate::lang::{CardStatistics, DeckSettings};
|
||||
use rusqlite::{params, Connection};
|
||||
use std::time::Instant;
|
||||
|
||||
pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<CardStatistics> {
|
||||
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1")
|
||||
.unwrap();
|
||||
@@ -27,67 +26,41 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
|
||||
let inserting = stat
|
||||
.iter()
|
||||
.map(|stat| {
|
||||
format!(
|
||||
"({}, {}, {}, {})",
|
||||
stat.word_id.to_string(),
|
||||
stat.set_id.to_string(),
|
||||
stat.score.to_string(),
|
||||
stat.last_open.timestamp().to_string()
|
||||
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) {
|
||||
let time = Instant::now();
|
||||
let tx = connection.transaction().unwrap();
|
||||
let count = stats.len();
|
||||
|
||||
{
|
||||
let mut stmt = tx
|
||||
.prepare(
|
||||
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4)",
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let query = format!(
|
||||
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES {}",
|
||||
inserting
|
||||
);
|
||||
let count = connection.execute(query.as_str(), ());
|
||||
.unwrap();
|
||||
|
||||
if count.is_err() {
|
||||
println!("{}", count.unwrap_err());
|
||||
return;
|
||||
for stat in stats.iter() {
|
||||
stmt.execute(params![
|
||||
stat.word_id,
|
||||
stat.set_id,
|
||||
stat.score,
|
||||
stat.last_open.timestamp()
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
let last_index: u32 = connection
|
||||
.query_one(
|
||||
"SELECT seq from sqlite_sequence WHERE name == ?1",
|
||||
("card_stats".to_string(),),
|
||||
|row| row.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
let last_index: u32 = tx.last_insert_rowid() as u32;
|
||||
tx.commit().unwrap();
|
||||
|
||||
let start_index = last_index - (count.unwrap() as u32) + 1;
|
||||
let start_index = last_index - (count as u32) + 1;
|
||||
|
||||
let mut index = 0;
|
||||
for id in start_index..=last_index {
|
||||
stat[index].id = id;
|
||||
index += 1;
|
||||
for (index, id) in (start_index..=last_index).enumerate() {
|
||||
stats[index].id = id.into();
|
||||
}
|
||||
|
||||
println!("Added {} cards for {:?}", stats.len(), time.elapsed());
|
||||
}
|
||||
|
||||
// pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
|
||||
// let time = Instant::now();
|
||||
// let index = connection
|
||||
// .query_row(
|
||||
// "INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4) RETURNING id",
|
||||
// (
|
||||
// &stat.word_id,
|
||||
// &stat.set_id,
|
||||
// &stat.score,
|
||||
// &stat.last_open.timestamp(),
|
||||
// ),
|
||||
// |row| row.get(0)
|
||||
// )
|
||||
// .unwrap_or_else(|e| {println!("{}", e); 0});
|
||||
//
|
||||
// stat.id = index;
|
||||
// println!("Added stat: {}", time.elapsed().as_millis());
|
||||
// }
|
||||
|
||||
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
|
||||
let time = Instant::now();
|
||||
|
||||
@@ -104,7 +77,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
|
||||
}
|
||||
|
||||
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
|
||||
if stat.id == 0 {
|
||||
if !stat.id.is_valid() {
|
||||
return;
|
||||
}
|
||||
connection
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use std::fs;
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::lang::WordOpenMode;
|
||||
use crate::lang::{Id, WordOpenMode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::fs;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn get_history_of_set(id: u32) -> Vec<HistoryItem> {
|
||||
pub fn get_history_of_set(id: Id) -> Vec<HistoryItem> {
|
||||
let app_dir = history_dir();
|
||||
let head = app_dir.clone().join(format!("set_{}_history.csv", id));
|
||||
let mut lines = Vec::new();
|
||||
@@ -32,7 +32,7 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
||||
if let [time, word, mode, before, after] = string.split(';').collect::<Vec<&str>>()[..] {
|
||||
items.push(HistoryItem {
|
||||
timestamp: DateTime::from_timestamp(time.parse().unwrap(), 0).unwrap(),
|
||||
word_id: word.parse::<u32>().unwrap(),
|
||||
word_id: word.parse::<Id>().unwrap(),
|
||||
mode: match mode.parse::<u8>().unwrap() {
|
||||
2 => WordOpenMode::Hard,
|
||||
3 => WordOpenMode::Ok,
|
||||
@@ -48,7 +48,7 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
||||
items
|
||||
}
|
||||
|
||||
pub fn push_note(set_id: u32, item: HistoryItem) {
|
||||
pub fn push_note(set_id: Id, item: HistoryItem) {
|
||||
let app_dir = history_dir();
|
||||
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
|
||||
|
||||
@@ -70,7 +70,7 @@ pub fn push_note(set_id: u32, item: HistoryItem) {
|
||||
item.before,
|
||||
item.after
|
||||
);
|
||||
writeln!(&mut file, "{}", line_str.to_string()).unwrap();
|
||||
writeln!(&mut file, "{}", line_str).unwrap();
|
||||
}
|
||||
|
||||
pub fn history_dir() -> PathBuf {
|
||||
@@ -84,7 +84,7 @@ pub fn history_dir() -> PathBuf {
|
||||
#[derive(Clone)]
|
||||
pub struct HistoryItem {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub word_id: u32,
|
||||
pub word_id: Id,
|
||||
pub mode: WordOpenMode,
|
||||
pub before: u8,
|
||||
pub after: u8,
|
||||
|
||||
+15
-10
@@ -1,6 +1,6 @@
|
||||
use hashbrown::HashMap;
|
||||
use rusqlite::Connection;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ImportData(pub(crate) Vec<ImportGroup>);
|
||||
@@ -12,7 +12,7 @@ pub struct ImportGroup {
|
||||
pub fields: Vec<String>,
|
||||
pub length: u64,
|
||||
pub mapping: HashMap<String, String>,
|
||||
pub imported: bool
|
||||
pub imported: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -68,13 +68,18 @@ pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Vec<ImportN
|
||||
.prepare("select tags, flds from notes where mid == ?1;")
|
||||
.unwrap();
|
||||
|
||||
count_stmt.query_map((group_id as i64, ), |row| {
|
||||
Ok(ImportNote {
|
||||
tags: row.get(0)?,
|
||||
fields: row.get::<usize, String>(1)?
|
||||
.split('')
|
||||
.map(|x| x.to_string())
|
||||
.collect()
|
||||
count_stmt
|
||||
.query_map((group_id as i64,), |row| {
|
||||
Ok(ImportNote {
|
||||
tags: row.get(0)?,
|
||||
fields: row
|
||||
.get::<usize, String>(1)?
|
||||
.split('')
|
||||
.map(|x| x.to_string())
|
||||
.collect(),
|
||||
})
|
||||
})
|
||||
}).unwrap().map(|x| x.unwrap()).collect::<Vec<ImportNote>>()
|
||||
.unwrap()
|
||||
.map(|x| x.unwrap())
|
||||
.collect::<Vec<ImportNote>>()
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
pub(crate) mod card_sets;
|
||||
pub(crate) mod card_stats;
|
||||
pub(crate) mod history;
|
||||
pub(crate) mod import;
|
||||
pub(crate) mod settings;
|
||||
pub(crate) mod sqlite;
|
||||
pub(crate) mod voice;
|
||||
pub(crate) mod words;
|
||||
pub(crate) mod web_api;
|
||||
pub(crate) mod import;
|
||||
pub(crate) mod words;
|
||||
|
||||
@@ -4,14 +4,14 @@ pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT value FROM settings WHERE id = ?1")
|
||||
.unwrap();
|
||||
let iter = stmt.query_map((key,), |row| row.get(0)).unwrap();
|
||||
let mut iter = stmt.query_map((key,), |row| row.get(0)).unwrap();
|
||||
|
||||
for row in iter {
|
||||
if let Ok(value) = row {
|
||||
return Some(value);
|
||||
}
|
||||
return None;
|
||||
if let Some(row) = iter.next()
|
||||
&& let Ok(value) = row
|
||||
{
|
||||
return Some(value);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::dictionary::app_data_dir;
|
||||
use std::io;
|
||||
use tokio::fs::{File, OpenOptions};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
|
||||
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder};
|
||||
|
||||
// const API_URL: &str = "http://127.0.0.1:8089/";
|
||||
const API_URL: &str = "https://learning.micialware.ru/";
|
||||
@@ -17,15 +17,22 @@ pub async fn send_data(id: String) {
|
||||
let id_url = format!("{api_url}upload/stream/{id}");
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let new_version = client.post(&id_url).body(data).send().await.unwrap().text().await.unwrap();
|
||||
let new_version = client
|
||||
.post(&id_url)
|
||||
.body(data)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
set_local_version(new_version.parse::<u32>().unwrap()).await;
|
||||
}
|
||||
|
||||
fn compress(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).unwrap();
|
||||
io::copy(&mut &data[..], &mut encoder).unwrap();
|
||||
let compressed = encoder.finish().unwrap();
|
||||
compressed
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub async fn load_data(id: String, temp: bool) {
|
||||
@@ -50,7 +57,14 @@ pub async fn load_data(id: String, temp: bool) {
|
||||
tokio::fs::write(db_file, data).await.unwrap();
|
||||
|
||||
let version_url = format!("{API_URL}{id}/version");
|
||||
let version = client.get(&version_url).send().await.unwrap().text().await.unwrap();
|
||||
let version = client
|
||||
.get(&version_url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
set_local_version(version.parse::<u32>().unwrap()).await;
|
||||
}
|
||||
|
||||
@@ -84,25 +98,44 @@ pub async fn get_web_version(key: &str) -> Result<u32, reqwest::Error> {
|
||||
let id_url = format!("{API_URL}{key}/version");
|
||||
let client = reqwest::Client::new();
|
||||
let version = client.get(&id_url).send().await?.text().await?;
|
||||
return Ok(version.parse::<u32>().unwrap());
|
||||
println!("version: {}", version);
|
||||
Ok(version.parse::<u32>().unwrap_or_default())
|
||||
}
|
||||
|
||||
pub async fn get_local_version() -> u32 {
|
||||
let file = app_data_dir().join("data.meta");
|
||||
if file.exists() {
|
||||
let mut data = String::new();
|
||||
File::open(file.clone()).await.unwrap().read_to_string(&mut data).await.unwrap();
|
||||
File::open(file.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
.read_to_string(&mut data)
|
||||
.await
|
||||
.unwrap();
|
||||
return data.parse::<u32>().unwrap();
|
||||
}
|
||||
|
||||
let mut file = OpenOptions::new().write(true).create(true).open(file).await.unwrap();
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(file)
|
||||
.await
|
||||
.unwrap();
|
||||
file.write_all("0".as_bytes()).await.unwrap();
|
||||
0
|
||||
}
|
||||
|
||||
pub async fn set_local_version(version: u32) {
|
||||
let file_path = app_data_dir().join("data.meta");
|
||||
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file_path).await.unwrap();
|
||||
file.write_all(version.to_string().as_bytes()).await.unwrap();
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(file_path)
|
||||
.await
|
||||
.unwrap();
|
||||
file.write_all(version.to_string().as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
||||
+25
-25
@@ -20,46 +20,46 @@ pub fn add_word(word: &mut WordData, connection: &Connection) {
|
||||
0
|
||||
});
|
||||
|
||||
word.id = index;
|
||||
word.id = index.into();
|
||||
}
|
||||
|
||||
pub fn add_words(words: &mut[WordData], connection: &mut Connection) {
|
||||
pub fn add_words(words: &mut [WordData], connection: &mut Connection) {
|
||||
let tx = connection.transaction().unwrap();
|
||||
let count = words.len();
|
||||
|
||||
{
|
||||
let mut stmt = tx.prepare(
|
||||
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
).unwrap();
|
||||
|
||||
let mut stmt = tx
|
||||
.prepare(
|
||||
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for word in words.iter() {
|
||||
stmt.execute(params![word.key, word.value, word.tags, serde_json::to_string(&word.additional).unwrap(), word.group_id]).unwrap();
|
||||
stmt.execute(params![
|
||||
word.key,
|
||||
word.value,
|
||||
word.tags,
|
||||
serde_json::to_string(&word.additional).unwrap(),
|
||||
word.group_id
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
let last_index: u32 = tx.last_insert_rowid() as u32;
|
||||
tx.commit().unwrap();
|
||||
|
||||
let last_index: u32 = connection
|
||||
.query_one(
|
||||
"SELECT seq from sqlite_sequence WHERE name == ?1",
|
||||
("words".to_string(),),
|
||||
|row| row.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let start_index = last_index - (count as u32) + 1;
|
||||
|
||||
let mut index = 0;
|
||||
for id in start_index..=last_index {
|
||||
words[index].id = id;
|
||||
index += 1;
|
||||
for (index, id) in (start_index..=last_index).enumerate() {
|
||||
words[index].id = id.into();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_word(word: &mut WordData, connection: &Connection) {
|
||||
if word.id == 0 {
|
||||
add_word(word, &connection);
|
||||
if !word.id.is_valid() {
|
||||
add_word(word, connection);
|
||||
} else {
|
||||
connection
|
||||
.execute(
|
||||
@@ -80,7 +80,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) {
|
||||
}
|
||||
|
||||
pub fn delete_word(word: &WordData, connection: &Connection) {
|
||||
if word.id == 0 {
|
||||
if !word.id.is_valid() {
|
||||
return;
|
||||
}
|
||||
connection
|
||||
@@ -150,12 +150,12 @@ pub fn add_group(group: &mut WordGroup, connection: &Connection) {
|
||||
0
|
||||
});
|
||||
|
||||
group.id = index;
|
||||
group.id = index.into();
|
||||
}
|
||||
|
||||
pub fn update_group(group: &mut WordGroup, connection: &Connection) {
|
||||
if group.id == 0 {
|
||||
add_group(group, &connection);
|
||||
if !group.id.is_valid() {
|
||||
add_group(group, connection);
|
||||
} else {
|
||||
connection
|
||||
.execute(
|
||||
@@ -170,7 +170,7 @@ pub fn update_group(group: &mut WordGroup, connection: &Connection) {
|
||||
}
|
||||
|
||||
pub fn delete_group(group: &WordGroup, connection: &Connection) {
|
||||
if group.id == 0 {
|
||||
if !group.id.is_valid() {
|
||||
return;
|
||||
}
|
||||
connection
|
||||
|
||||
+45
-49
@@ -9,6 +9,7 @@ use crate::styling::*;
|
||||
use crate::word::WordState;
|
||||
use crate::{AppState, RootMessage};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use iced::alignment::Vertical::Center;
|
||||
use iced::widget::button::Style;
|
||||
use iced::widget::button::{danger, text};
|
||||
@@ -18,7 +19,6 @@ use iced::widget::*;
|
||||
use iced::{Border, Color, Shadow, Task};
|
||||
use iced_core::Length::Fill;
|
||||
use rand::random_range;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::ops::Add;
|
||||
use std::path::PathBuf;
|
||||
@@ -65,29 +65,29 @@ pub enum DictionaryMessage {
|
||||
}
|
||||
|
||||
impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
fn navigate(&self, message: &DictionaryMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &DictionaryMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
return Some(Page::PreviousPage);
|
||||
}
|
||||
if let Test = message {
|
||||
if self.include_map.iter().any(|x| *x) {
|
||||
let mut words = vec![];
|
||||
let dict = &self.state.lock().unwrap().dictionary;
|
||||
if let Test = message
|
||||
&& self.include_map.iter().any(|x| *x)
|
||||
{
|
||||
let mut words = vec![];
|
||||
let dict = &self.state.lock().unwrap().dictionary;
|
||||
|
||||
words = self
|
||||
.include_map
|
||||
.iter()
|
||||
.zip(0..self.include_map.len())
|
||||
.filter(|(flag, _)| **flag)
|
||||
.map(|(_, index)| dict[index].clone())
|
||||
.collect();
|
||||
words = self
|
||||
.include_map
|
||||
.iter()
|
||||
.zip(0..self.include_map.len())
|
||||
.filter(|(flag, _)| **flag)
|
||||
.map(|(_, index)| dict[index].clone())
|
||||
.collect();
|
||||
|
||||
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
||||
words,
|
||||
self.reverse,
|
||||
self.no_typing,
|
||||
)));
|
||||
}
|
||||
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
||||
words,
|
||||
self.reverse,
|
||||
self.no_typing,
|
||||
)));
|
||||
}
|
||||
if let WordAction(index) = message {
|
||||
let word: WordData;
|
||||
@@ -96,7 +96,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
let dict = &state.dictionary;
|
||||
word = dict[*index].clone();
|
||||
}
|
||||
if word.id != 0 {
|
||||
if word.id.is_valid() {
|
||||
return Some(Word(WordState::new(word, *index, self.state.clone())));
|
||||
}
|
||||
}
|
||||
@@ -119,7 +119,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
NewWord => {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
let mut word = WordData::new();
|
||||
word.group_id = state.word_groups[self.selected_group_index].id.clone();
|
||||
word.group_id = state.word_groups[self.selected_group_index].id;
|
||||
|
||||
let dict = &mut state.dictionary;
|
||||
dict.push(word);
|
||||
@@ -150,12 +150,11 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
v = v[..v.len() - 2].to_string()
|
||||
}
|
||||
|
||||
while v.contains(",,") {
|
||||
let index = v.find(",,").unwrap();
|
||||
while let Some(index) = v.find(",,") {
|
||||
v.remove(index);
|
||||
}
|
||||
|
||||
dict.get_mut(i).unwrap().tags = v;
|
||||
dict[i].tags = v;
|
||||
}
|
||||
|
||||
self.update_tags();
|
||||
@@ -172,7 +171,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
}
|
||||
Include(i, b) => self.include_map[i] = b,
|
||||
IncludeTag(t, v) => {
|
||||
let index: u32;
|
||||
let index;
|
||||
{
|
||||
let state = self.state.lock().unwrap();
|
||||
index = state.word_groups[self.selected_group_index].id;
|
||||
@@ -197,7 +196,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
let state = &mut self.state.lock().unwrap();
|
||||
|
||||
state.word_groups.push(WordGroup {
|
||||
id: 0,
|
||||
id: 0.into(),
|
||||
name: format!("Группа слов {}", random_range(100..1000)),
|
||||
});
|
||||
}
|
||||
@@ -211,18 +210,16 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
SaveGroup => {
|
||||
let state = &mut self.state.lock().unwrap();
|
||||
let connection = &state.connection;
|
||||
let group = &mut state
|
||||
.word_groups
|
||||
.get(self.selected_group_index)
|
||||
.unwrap()
|
||||
let mut group = state
|
||||
.word_groups[self.selected_group_index]
|
||||
.clone();
|
||||
|
||||
update_group(group, connection);
|
||||
state.word_groups[self.selected_group_index] = group.clone();
|
||||
update_group(&mut group, connection);
|
||||
state.word_groups[self.selected_group_index] = group;
|
||||
}
|
||||
SelectGroup(i) => {
|
||||
self.selected_group_index = i;
|
||||
let index: u32;
|
||||
let index;
|
||||
{
|
||||
let state = self.state.lock().unwrap();
|
||||
index = state.word_groups[i].id;
|
||||
@@ -316,9 +313,9 @@ impl DictionaryState {
|
||||
fn save_word(&mut self, i: usize) {
|
||||
let state = &mut self.state.lock().unwrap();
|
||||
let connection = &state.connection;
|
||||
let word = &mut state.dictionary.get(i).unwrap().clone();
|
||||
let word = &mut state.dictionary[i].clone();
|
||||
|
||||
update_word(word, &connection);
|
||||
update_word(word, connection);
|
||||
state.dictionary[i] = word.clone();
|
||||
}
|
||||
|
||||
@@ -353,13 +350,12 @@ impl DictionaryState {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !self.search.is_empty() {
|
||||
if word.key.contains(&self.search) == false
|
||||
&& word.value.contains(&self.search) == false
|
||||
&& word.tags.contains(&self.search) == false
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if !self.search.is_empty()
|
||||
&& !word.key.contains(&self.search)
|
||||
&& !word.value.contains(&self.search)
|
||||
&& !word.tags.contains(&self.search)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let word_line_data = WordLineState {
|
||||
@@ -424,7 +420,7 @@ impl DictionaryState {
|
||||
let line_button = || {
|
||||
let action = WordAction(index);
|
||||
|
||||
if data.id == 0 {
|
||||
if !data.id.is_valid() {
|
||||
return button("-").on_press(action).style(|_x, _status| Style {
|
||||
background: None,
|
||||
text_color: Color::BLACK,
|
||||
@@ -533,7 +529,7 @@ impl DictionaryState {
|
||||
});
|
||||
}
|
||||
|
||||
fn update_words_include(&mut self, group_id: u32) {
|
||||
fn update_words_include(&mut self, group_id: crate::lang::Id) {
|
||||
let include_tags = self
|
||||
.tag_map
|
||||
.iter()
|
||||
@@ -553,7 +549,9 @@ impl DictionaryState {
|
||||
.iter()
|
||||
.map(|word| (split_with_coma(word.tags.as_str()), word.group_id))
|
||||
.map(|(tags, word_group_id)| {
|
||||
tags.iter().all(|t| include_tags.contains(t)) && tags.len() != 0 && word_group_id == group_id
|
||||
!tags.is_empty()
|
||||
&& tags.iter().all(|t| include_tags.contains(t))
|
||||
&& word_group_id == group_id
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -567,14 +565,12 @@ impl DictionaryState {
|
||||
let state = &self.state.lock().unwrap();
|
||||
let groups = &state.word_groups;
|
||||
|
||||
let mut index = 0;
|
||||
for group in groups {
|
||||
for (index, group) in groups.iter().enumerate() {
|
||||
row = row.push(
|
||||
button(text!("{}", group.name.clone()))
|
||||
.style(text)
|
||||
.on_press(SelectGroup(index)),
|
||||
);
|
||||
index = index + 1;
|
||||
}
|
||||
|
||||
let group = state.word_groups[self.selected_group_index].clone();
|
||||
@@ -627,6 +623,6 @@ struct WordLineState {
|
||||
key: String,
|
||||
value: String,
|
||||
tags: String,
|
||||
id: u32,
|
||||
id: crate::lang::Id,
|
||||
index: usize,
|
||||
}
|
||||
|
||||
+13
-15
@@ -1,3 +1,4 @@
|
||||
use crate::RootMessage;
|
||||
use crate::dictionary::split_with_coma;
|
||||
use crate::dictionary_test::DictionaryQuizMessage::*;
|
||||
use crate::lang::WordData;
|
||||
@@ -5,12 +6,11 @@ use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::*;
|
||||
use crate::quiz::Score;
|
||||
use crate::styling::*;
|
||||
use crate::RootMessage;
|
||||
use iced::Background::Color;
|
||||
use iced::border::Radius;
|
||||
use iced::widget::container::Style;
|
||||
use iced::widget::{button, container, row, space, text, text_input, Row};
|
||||
use iced::Background::Color;
|
||||
use iced::{alignment, Border, Element, Fill, Task, Theme};
|
||||
use iced::widget::{Row, button, container, row, space, text, text_input};
|
||||
use iced::{Border, Element, Fill, Task, Theme, alignment};
|
||||
use rand::prelude::SliceRandom;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -35,15 +35,14 @@ pub enum DictionaryQuizMessage {
|
||||
}
|
||||
|
||||
impl NavigatedPage<DictionaryQuizMessage> for DictionaryQuizState {
|
||||
fn navigate(&self, message: &DictionaryQuizMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &DictionaryQuizMessage) -> Option<Page> {
|
||||
match message {
|
||||
Back => Some(PreviousPage),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
|
||||
fn update(&mut self, message: DictionaryQuizMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
@@ -95,12 +94,12 @@ impl NavigatedPage<DictionaryQuizMessage> for DictionaryQuizState {
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_x(alignment::Horizontal::Center),
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_x(alignment::Horizontal::Center),
|
||||
)
|
||||
.center_y(Fill)
|
||||
.center_x(Fill)
|
||||
.into()
|
||||
.center_y(Fill)
|
||||
.center_x(Fill)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +119,6 @@ impl DictionaryQuizState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn submit(&mut self) {
|
||||
if self.view == "---" {
|
||||
self.show_next();
|
||||
@@ -151,7 +149,7 @@ impl DictionaryQuizState {
|
||||
if self.answer == self.correct
|
||||
|| split_with_coma(self.correct.as_str()).contains(&self.answer)
|
||||
{
|
||||
if self.is_help == false {
|
||||
if !self.is_help {
|
||||
self.score.correct += 1;
|
||||
}
|
||||
self.show_next()
|
||||
@@ -208,7 +206,7 @@ impl DictionaryQuizState {
|
||||
}
|
||||
|
||||
fn appeal_button(&self) -> Element<'_, DictionaryQuizMessage> {
|
||||
if self.is_help && self.no_typing == false {
|
||||
if self.is_help && !self.no_typing {
|
||||
return button("Апелляция").style(jl_button).on_press(Appeal).into();
|
||||
}
|
||||
space().into()
|
||||
|
||||
+10
-11
@@ -18,13 +18,12 @@ pub struct HistoryState {
|
||||
}
|
||||
|
||||
impl NavigatedPage<HistoryMessage> for HistoryState {
|
||||
fn navigate(&self, message: &HistoryMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &HistoryMessage) -> Option<Page> {
|
||||
match message {
|
||||
Back => Some(Page::PreviousPage),
|
||||
}
|
||||
}
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
fn update(&mut self, _: HistoryMessage) -> Task<RootMessage> {
|
||||
Task::none()
|
||||
}
|
||||
@@ -38,16 +37,16 @@ impl NavigatedPage<HistoryMessage> for HistoryState {
|
||||
.width(FillPortion(5)),
|
||||
horizontal().width(FillPortion(1))
|
||||
]
|
||||
.height(Fill)
|
||||
.width(Fill)
|
||||
.into(),
|
||||
.height(Fill)
|
||||
.width(Fill)
|
||||
.into(),
|
||||
Back,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl HistoryState {
|
||||
pub fn new(id: u32, state: Arc<Mutex<AppState>>) -> Self {
|
||||
pub fn new(id: crate::lang::Id, state: Arc<Mutex<AppState>>) -> Self {
|
||||
let state = state.lock().unwrap();
|
||||
let history = get_history_of_set(id);
|
||||
let words = history
|
||||
@@ -68,8 +67,6 @@ impl HistoryState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn history_lines(&self) -> Column<'_, HistoryMessage> {
|
||||
let mut column = Column::new();
|
||||
for (item, index) in self.list.iter().zip(0..self.list.len()) {
|
||||
@@ -86,11 +83,13 @@ impl HistoryState {
|
||||
.align_x(Center),
|
||||
iced::widget::column![
|
||||
text!("{} ➞ {}", item.before, item.after),
|
||||
text!("{}", item.timestamp.with_timezone(&Local).format("%d.%m %H:%M"))
|
||||
text!(
|
||||
"{}",
|
||||
item.timestamp.with_timezone(&Local).format("%d.%m %H:%M")
|
||||
)
|
||||
]
|
||||
.spacing(5)
|
||||
.align_x(Center)
|
||||
|
||||
]
|
||||
.spacing(5),
|
||||
);
|
||||
|
||||
+37
-39
@@ -1,15 +1,15 @@
|
||||
use crate::AppState;
|
||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||
use crate::data_provider::import::{get_words_of_group, load_groups, ImportData, ImportGroup};
|
||||
use crate::data_provider::words::{add_group, add_words};
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::import::ImportMessage::*;
|
||||
use crate::lang::{WordData, WordGroup};
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::styling::*;
|
||||
use crate::AppState;
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::container::success;
|
||||
use iced::widget::{
|
||||
Row, button, checkbox, column, container, progress_bar, row, rule, text, text_input,
|
||||
button, checkbox, column, container, progress_bar, row, rule, text, text_input, Row,
|
||||
};
|
||||
use iced::widget::{scrollable, space};
|
||||
use iced::{Element, Task};
|
||||
@@ -62,8 +62,8 @@ pub enum ImportMessage {
|
||||
}
|
||||
|
||||
impl NavigatedPage<ImportMessage> for ImportState {
|
||||
fn navigate(&self, message: &ImportMessage) -> Option<Page> {
|
||||
if let Some(_) = self.progress {
|
||||
fn navigate(&mut self, message: &ImportMessage) -> Option<Page> {
|
||||
if self.progress.is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -80,10 +80,12 @@ impl NavigatedPage<ImportMessage> for ImportState {
|
||||
Back => {}
|
||||
UpdateFile(path) => {
|
||||
self.path = Some(path);
|
||||
self.import_data = None;
|
||||
return self.load_package();
|
||||
}
|
||||
SelectFile => return Self::select_import_file(),
|
||||
UpdateImport(import) => {
|
||||
self.selected_index = 0;
|
||||
self.import_data = Some(import);
|
||||
}
|
||||
NextGroup => {
|
||||
@@ -128,12 +130,7 @@ impl NavigatedPage<ImportMessage> for ImportState {
|
||||
}
|
||||
ImportFinished => {
|
||||
self.progress = None;
|
||||
let group = self
|
||||
.import_data.as_mut()
|
||||
.unwrap()
|
||||
.0
|
||||
.get_mut(self.selected_index)
|
||||
.unwrap();
|
||||
let group = &mut self.import_data.as_mut().unwrap().0[self.selected_index];
|
||||
group.imported = true;
|
||||
}
|
||||
}
|
||||
@@ -200,7 +197,9 @@ impl ImportState {
|
||||
|
||||
fn import_button(&self, group: &ImportGroup) -> Element<'_, ImportMessage> {
|
||||
if group.imported {
|
||||
return container(text!("Группа слов успешно импортирована")).padding(HALF_SPACING).align_x(Center)
|
||||
return container(text!("Группа слов успешно импортирована"))
|
||||
.padding(HALF_SPACING)
|
||||
.align_x(Center)
|
||||
.style(success)
|
||||
.width(Fill)
|
||||
.into();
|
||||
@@ -254,7 +253,7 @@ impl ImportState {
|
||||
}
|
||||
|
||||
fn property_selector(&self) -> Element<'_, ImportMessage> {
|
||||
if self.selected_property == None {
|
||||
if self.selected_property.is_none() {
|
||||
return space().into();
|
||||
}
|
||||
column![
|
||||
@@ -360,7 +359,7 @@ impl ImportState {
|
||||
spawn_blocking(move || {
|
||||
Self::extract_import_file(path)?;
|
||||
let data = Self::read_import_file()?;
|
||||
return Ok(data);
|
||||
Ok(data)
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -379,15 +378,17 @@ impl ImportState {
|
||||
let file = File::open(path).map_err(|_| ())?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut archive = ZipArchive::new(reader).map_err(|_| ())?;
|
||||
let db_file = archive.by_name("collection.anki2");
|
||||
if let Ok(mut file) = db_file {
|
||||
let temp_file_path = app_data_dir().join("import");
|
||||
let mut temp_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&temp_file_path)
|
||||
.map_err(|_| ())?;
|
||||
let temp_file_path = app_data_dir().join("import");
|
||||
let mut temp_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&temp_file_path)
|
||||
.map_err(|_| ())?;
|
||||
if let Ok(mut file) = archive.by_name("collection.anki21") {
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| ())?;
|
||||
Ok(())
|
||||
} else if let Ok(mut file) = archive.by_name("collection.anki2") {
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| ())?;
|
||||
Ok(())
|
||||
} else {
|
||||
@@ -402,7 +403,7 @@ impl ImportState {
|
||||
}
|
||||
fn setup_mapping(&mut self, property_name: String) {
|
||||
let group = self.import_data.as_mut().unwrap();
|
||||
let group = group.0.get_mut(self.selected_index).unwrap();
|
||||
let group = &mut group.0[self.selected_index];
|
||||
group
|
||||
.mapping
|
||||
.insert(self.selected_property.clone().unwrap(), property_name);
|
||||
@@ -411,7 +412,7 @@ impl ImportState {
|
||||
}
|
||||
fn remove_mapping(&mut self, property_name: String) {
|
||||
let group = self.import_data.as_mut().unwrap();
|
||||
let group = group.0.get_mut(self.selected_index).unwrap();
|
||||
let group = &mut group.0[self.selected_index];
|
||||
let remove_key = group
|
||||
.mapping
|
||||
.keys()
|
||||
@@ -439,7 +440,7 @@ impl ImportState {
|
||||
|
||||
let map_indices = Self::get_mapping_indices(&group);
|
||||
let mut group_entity = WordGroup {
|
||||
id: 0,
|
||||
id: 0.into(),
|
||||
name: group.name.clone(),
|
||||
};
|
||||
{
|
||||
@@ -452,11 +453,11 @@ impl ImportState {
|
||||
let mut word = WordData::new();
|
||||
|
||||
word.tags = import.tags.trim().replace(" ", ", ");
|
||||
word.group_id = group_id.clone();
|
||||
word.group_id = group_id;
|
||||
for (dest, indices) in &map_indices {
|
||||
let collected_string = Self::collect_strings(
|
||||
&import.fields,
|
||||
&indices,
|
||||
indices,
|
||||
&separator,
|
||||
skip_empty,
|
||||
);
|
||||
@@ -476,15 +477,14 @@ impl ImportState {
|
||||
}
|
||||
}
|
||||
|
||||
println!("{word:?}");
|
||||
words_list.push(word);
|
||||
}
|
||||
|
||||
let mut state = state.lock().unwrap();
|
||||
let connection = &mut state.connection;
|
||||
let mut index = 0;
|
||||
let total_len = words_list.len() as f32 / 256.0;
|
||||
for word in &mut words_list.chunks_mut(256) {
|
||||
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().unwrap();
|
||||
@@ -511,29 +511,27 @@ impl ImportState {
|
||||
.iter()
|
||||
.map(|name| (name.clone(), Vec::<usize>::with_capacity(1)))
|
||||
.collect::<Vec<_>>();
|
||||
for key in group.mapping.keys() {
|
||||
let endpoint = group.mapping.get(key).unwrap();
|
||||
for (key, endpoint) in &group.mapping {
|
||||
let property_index = group.fields.iter().position(|f| f == key).unwrap();
|
||||
let group_index = result
|
||||
.iter()
|
||||
.position(|(name, _)| name == endpoint)
|
||||
.unwrap();
|
||||
result.get_mut(group_index).unwrap().1.push(property_index);
|
||||
result[group_index].1.push(property_index);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn collect_strings(
|
||||
properties: &Vec<String>,
|
||||
indices: &Vec<usize>,
|
||||
properties: &[String],
|
||||
indices: &[usize],
|
||||
separator: &str,
|
||||
skip_empty: bool,
|
||||
) -> String {
|
||||
let mut working_words = Vec::with_capacity(indices.len());
|
||||
for i in 0..indices.len() {
|
||||
let index = indices[i];
|
||||
let str = properties.get(index).unwrap();
|
||||
for index in indices {
|
||||
let str = &properties[*index];
|
||||
if skip_empty && str.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
+193
-113
@@ -1,18 +1,23 @@
|
||||
use crate::data_provider::card_stats::{
|
||||
add_stat_list, delete_stat, load_stats_of_set, update_stat_score,
|
||||
};
|
||||
use crate::data_provider::history::{push_note, HistoryItem};
|
||||
use crate::repetitions::CardSetSettings;
|
||||
use crate::AppState;
|
||||
use crate::data_provider::card_stats::{delete_stat, load_stats_of_deck, update_stat_score};
|
||||
use crate::data_provider::history::{HistoryItem, push_note};
|
||||
use chrono::{DateTime, Utc};
|
||||
use rand::distr::weighted::WeightedIndex;
|
||||
use rand::distr::Distribution;
|
||||
use rand::distr::weighted::WeightedIndex;
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::rng;
|
||||
use rand::rngs::ThreadRng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::min;
|
||||
use rayon::iter::IndexedParallelIterator;
|
||||
use rayon::iter::IntoParallelRefIterator;
|
||||
use rayon::iter::ParallelIterator;
|
||||
use rhai::{Engine, Scope};
|
||||
use rusqlite::ToSql;
|
||||
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
|
||||
use std::cmp::PartialEq;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::num::ParseIntError;
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -20,6 +25,55 @@ const MAX_HISTORY_LEN: usize = 20;
|
||||
const MAX_HISTORY_LEN_PART: f32 = 0.33;
|
||||
const MAX_SCORE: u8 = 25;
|
||||
const FADE_PER_DAY: f32 = 0.95;
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy)]
|
||||
pub struct Id(u32);
|
||||
pub const INVALID_ID: Id = Id(0);
|
||||
impl FromStr for Id {
|
||||
type Err = ParseIntError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Id(s.parse::<u32>()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for Id {
|
||||
fn from(value: u32) -> Self {
|
||||
Id(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u32> for Id {
|
||||
fn into(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
impl Display for Id {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for Id {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Integer(i) => Ok(Id(i as u32)),
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Id {
|
||||
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.0 as i64))
|
||||
}
|
||||
}
|
||||
|
||||
impl Id {
|
||||
pub(crate) fn is_valid(self) -> bool {
|
||||
self.0 != 0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct KanaSet {
|
||||
name: String,
|
||||
@@ -27,13 +81,11 @@ pub struct KanaSet {
|
||||
pub(crate) dictionary: Vec<Vec<(String, String)>>,
|
||||
pub(crate) include_map: [bool; 10],
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum KanaType {
|
||||
Hiragana,
|
||||
Katakana,
|
||||
}
|
||||
|
||||
impl KanaSet {
|
||||
pub fn hiragana() -> Self {
|
||||
Self {
|
||||
@@ -199,14 +251,6 @@ impl KanaSet {
|
||||
}
|
||||
}
|
||||
|
||||
/* pub fn next(&mut self) -> (String, String) {
|
||||
let current_set = self.list();
|
||||
|
||||
let mut rand = rand::rng();
|
||||
let index: u32 = rand.random();
|
||||
current_set[index as usize % current_set.len()].clone()
|
||||
}*/
|
||||
|
||||
pub fn list(&self) -> Vec<(String, String)> {
|
||||
let mut current_set: Vec<(String, String)> = Vec::new();
|
||||
|
||||
@@ -221,7 +265,6 @@ impl KanaSet {
|
||||
current_set
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KanaSet {
|
||||
fn default() -> Self {
|
||||
KanaSet::hiragana()
|
||||
@@ -232,45 +275,40 @@ impl PartialEq<Self> for KanaSet {
|
||||
self.name == other.name
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct WordData {
|
||||
pub id: u32,
|
||||
pub id: Id,
|
||||
pub key: String,
|
||||
pub value: String,
|
||||
pub tags: String,
|
||||
pub additional: HashMap<String, String>,
|
||||
pub group_id: u32,
|
||||
pub group_id: Id,
|
||||
}
|
||||
|
||||
impl WordData {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
id: 0,
|
||||
id: INVALID_ID,
|
||||
key: String::new(),
|
||||
value: String::new(),
|
||||
tags: String::new(),
|
||||
additional: Default::default(),
|
||||
group_id: 1,
|
||||
group_id: 1.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WordGroup {
|
||||
pub id: u32,
|
||||
pub id: Id,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct CardStatistics {
|
||||
pub id: u32,
|
||||
pub word_id: u32,
|
||||
pub set_id: u32,
|
||||
pub id: Id,
|
||||
pub word_id: Id,
|
||||
pub set_id: Id,
|
||||
pub last_open: DateTime<Utc>,
|
||||
pub score: u8,
|
||||
}
|
||||
|
||||
impl CardStatistics {
|
||||
pub fn update(&mut self, status: WordOpenMode) {
|
||||
match status {
|
||||
@@ -286,11 +324,7 @@ impl CardStatistics {
|
||||
}
|
||||
}
|
||||
|
||||
if self.score < 1 {
|
||||
self.score = 1
|
||||
} else if self.score > MAX_SCORE {
|
||||
self.score = MAX_SCORE
|
||||
}
|
||||
self.score = self.score.clamp(1, MAX_SCORE);
|
||||
self.last_open = Utc::now();
|
||||
}
|
||||
|
||||
@@ -301,7 +335,6 @@ impl CardStatistics {
|
||||
(self.score as f32 * multiplier).max(1.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum WordOpenMode {
|
||||
Easy,
|
||||
@@ -309,51 +342,29 @@ pub enum WordOpenMode {
|
||||
Hard,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CardSet {
|
||||
pub struct DeckData {
|
||||
words: Vec<WordData>,
|
||||
set: Vec<CardStatistics>,
|
||||
current_word_index: Option<usize>,
|
||||
state: Arc<Mutex<AppState>>,
|
||||
order_module: OrderModule,
|
||||
settings: CardSetSettings,
|
||||
settings: DeckSettings,
|
||||
}
|
||||
|
||||
impl CardSet {
|
||||
pub fn new(settings: &CardSetSettings, state: Arc<Mutex<AppState>>) -> Self {
|
||||
impl DeckData {
|
||||
pub fn new(settings: &DeckSettings, state: Arc<Mutex<AppState>>) -> Self {
|
||||
let state_for = state.clone();
|
||||
let state_locked = state.lock().unwrap();
|
||||
|
||||
let mut current_set = load_stats_of_set(&settings, &state_locked.connection);
|
||||
let last_list = settings.get_word_list(&state_locked);
|
||||
let saved_ids = current_set.iter().map(|l| l.word_id).collect::<Vec<u32>>();
|
||||
let word_ids = last_list.iter().map(|l| l.id).collect::<Vec<u32>>();
|
||||
|
||||
let new_stats: &mut Vec<CardStatistics> = &mut last_list
|
||||
let mut current_set = load_stats_of_deck(settings, &state_locked.connection);
|
||||
let last_list: Vec<_> = settings
|
||||
.get_word_list(&state_locked)
|
||||
.iter()
|
||||
.filter(|word| !saved_ids.contains(&word.id))
|
||||
.map(|word| CardStatistics {
|
||||
id: 0,
|
||||
word_id: word.id.clone(),
|
||||
last_open: Utc::now(),
|
||||
score: 1,
|
||||
set_id: settings.id.clone(),
|
||||
})
|
||||
.map(|w| &state_locked.dictionary[*w])
|
||||
.cloned()
|
||||
.collect();
|
||||
let word_ids = last_list.iter().map(|l| l.id).collect::<Vec<Id>>();
|
||||
|
||||
let time = Instant::now();
|
||||
|
||||
if !new_stats.is_empty() {
|
||||
add_stat_list(new_stats, &state_locked.connection);
|
||||
current_set.append(new_stats);
|
||||
}
|
||||
|
||||
println!(
|
||||
"Added {} stats: {}",
|
||||
new_stats.len(),
|
||||
time.elapsed().as_millis()
|
||||
);
|
||||
let mut index = 0;
|
||||
for stat in current_set.clone() {
|
||||
if !word_ids.contains(&stat.word_id) {
|
||||
@@ -370,20 +381,19 @@ impl CardSet {
|
||||
current_word_index: None,
|
||||
state: state_for,
|
||||
order_module: match settings.open_mode {
|
||||
SetOrderMode::Default => OrderModule::SemiRandomSRS(SemiRandomSRSModule::new()),
|
||||
SetOrderMode::TrainWorstFirst => {
|
||||
OrderMode::Default => OrderModule::SemiRandomSRS(SemiRandomSRSModule::new()),
|
||||
OrderMode::TrainWorstFirst => {
|
||||
OrderModule::WorstWordsSRS(WorstWordsSRSModule::new())
|
||||
}
|
||||
SetOrderMode::FullRandom => OrderModule::RandomSRS(RandomSRSModule::new()),
|
||||
OrderMode::FullRandom => OrderModule::RandomSRS(RandomSRSModule::new()),
|
||||
},
|
||||
settings: settings.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> (WordData, CardStatistics) {
|
||||
let index = match self.order_module.clone() {
|
||||
OrderModule::SemiRandomSRS(mut module) => {
|
||||
if module.initialized == false {
|
||||
if !module.initialized {
|
||||
module.init(self)
|
||||
}
|
||||
let index = module.next(self);
|
||||
@@ -391,7 +401,7 @@ impl CardSet {
|
||||
index
|
||||
}
|
||||
OrderModule::RandomSRS(mut module) => {
|
||||
if module.initialized == false {
|
||||
if !module.initialized {
|
||||
module.init(self)
|
||||
}
|
||||
let index = module.next(self);
|
||||
@@ -399,7 +409,7 @@ impl CardSet {
|
||||
index
|
||||
}
|
||||
OrderModule::WorstWordsSRS(mut module) => {
|
||||
if module.initialized == false {
|
||||
if !module.initialized {
|
||||
module.init(self)
|
||||
}
|
||||
let index = module.next(self);
|
||||
@@ -411,9 +421,8 @@ impl CardSet {
|
||||
self.current_word_index = Some(index);
|
||||
(self.words[index].clone(), self.set[index].clone())
|
||||
}
|
||||
|
||||
pub fn open(&mut self, status: WordOpenMode) {
|
||||
if let None = self.current_word_index {
|
||||
if self.current_word_index.is_none() {
|
||||
return;
|
||||
}
|
||||
let index = self.current_word_index.unwrap();
|
||||
@@ -443,44 +452,44 @@ impl CardSet {
|
||||
HistoryItem {
|
||||
timestamp: Utc::now(),
|
||||
word_id: word.word_id,
|
||||
mode: WordOpenMode::Easy,
|
||||
mode: status,
|
||||
before: old_score,
|
||||
after: new_score,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.set.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Eq)]
|
||||
pub enum SetOrderMode {
|
||||
pub enum OrderMode {
|
||||
Default,
|
||||
TrainWorstFirst,
|
||||
FullRandom,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq, Default)]
|
||||
pub enum AppendMode {
|
||||
Full,
|
||||
#[default]
|
||||
Manual,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
enum OrderModule {
|
||||
SemiRandomSRS(SemiRandomSRSModule),
|
||||
RandomSRS(RandomSRSModule),
|
||||
WorstWordsSRS(WorstWordsSRSModule),
|
||||
}
|
||||
|
||||
trait SRSModule {
|
||||
fn next(&mut self, set: &mut CardSet) -> usize;
|
||||
fn next(&mut self, set: &mut DeckData) -> usize;
|
||||
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: CardStatistics);
|
||||
fn init(&mut self, set: &mut CardSet);
|
||||
fn init(&mut self, set: &mut DeckData);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RandomSRSModule {
|
||||
basket: Vec<usize>,
|
||||
initialized: bool,
|
||||
}
|
||||
|
||||
impl RandomSRSModule {
|
||||
fn new() -> RandomSRSModule {
|
||||
Self {
|
||||
@@ -489,9 +498,8 @@ impl RandomSRSModule {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SRSModule for RandomSRSModule {
|
||||
fn next(&mut self, set: &mut CardSet) -> usize {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize {
|
||||
if self.basket.is_empty() {
|
||||
self.basket = (0..set.words.len()).collect::<Vec<usize>>();
|
||||
self.basket.shuffle(&mut rand::rng())
|
||||
@@ -502,13 +510,12 @@ impl SRSModule for RandomSRSModule {
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, _: usize, _: CardStatistics) {}
|
||||
|
||||
fn init(&mut self, set: &mut CardSet) {
|
||||
fn init(&mut self, set: &mut DeckData) {
|
||||
self.initialized = true;
|
||||
self.basket = (0..set.words.len()).collect::<Vec<usize>>();
|
||||
self.basket.shuffle(&mut rand::rng())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SemiRandomSRSModule {
|
||||
history: Vec<usize>,
|
||||
@@ -516,7 +523,6 @@ struct SemiRandomSRSModule {
|
||||
generator: ThreadRng,
|
||||
initialized: bool,
|
||||
}
|
||||
|
||||
impl SemiRandomSRSModule {
|
||||
fn new() -> SemiRandomSRSModule {
|
||||
SemiRandomSRSModule {
|
||||
@@ -527,9 +533,8 @@ impl SemiRandomSRSModule {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SRSModule for SemiRandomSRSModule {
|
||||
fn next(&mut self, set: &mut CardSet) -> usize {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize {
|
||||
let index = self.last_weights.sample(&mut self.generator);
|
||||
|
||||
if self.history.contains(&index) {
|
||||
@@ -543,15 +548,13 @@ impl SRSModule for SemiRandomSRSModule {
|
||||
|
||||
index
|
||||
}
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, index: usize, word: CardStatistics) {
|
||||
let new_weight = (100.0 / word.calculated_score()).powf(2.0);
|
||||
self.last_weights
|
||||
.update_weights(&[(index, &new_weight)])
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn init(&mut self, set: &mut CardSet) {
|
||||
fn init(&mut self, set: &mut DeckData) {
|
||||
self.initialized = true;
|
||||
let weights = set
|
||||
.set
|
||||
@@ -561,16 +564,11 @@ impl SRSModule for SemiRandomSRSModule {
|
||||
self.last_weights = WeightedIndex::new(weights).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl SemiRandomSRSModule {
|
||||
fn history_len(&self, set: &CardSet) -> usize {
|
||||
min(
|
||||
MAX_HISTORY_LEN,
|
||||
(set.len() as f32 * MAX_HISTORY_LEN_PART) as usize,
|
||||
)
|
||||
fn history_len(&self, set: &DeckData) -> usize {
|
||||
((set.len() as f32 * MAX_HISTORY_LEN_PART) as usize).clamp(1, MAX_HISTORY_LEN)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct WorstWordsSRSModule {
|
||||
initialized: bool,
|
||||
@@ -580,9 +578,8 @@ struct WorstWordsSRSModule {
|
||||
queue: Vec<usize>,
|
||||
rounds_count: u8,
|
||||
}
|
||||
|
||||
impl SRSModule for WorstWordsSRSModule {
|
||||
fn next(&mut self, set: &mut CardSet) -> usize {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize {
|
||||
if self.rounds_remaining == 0 {
|
||||
self.fill_pool(set);
|
||||
self.rounds_remaining = self.rounds_count;
|
||||
@@ -598,11 +595,10 @@ impl SRSModule for WorstWordsSRSModule {
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, _: usize, _: CardStatistics) {}
|
||||
|
||||
fn init(&mut self, _: &mut CardSet) {
|
||||
fn init(&mut self, _: &mut DeckData) {
|
||||
self.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
impl WorstWordsSRSModule {
|
||||
fn new() -> WorstWordsSRSModule {
|
||||
WorstWordsSRSModule {
|
||||
@@ -614,8 +610,7 @@ impl WorstWordsSRSModule {
|
||||
queue: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_pool(&mut self, set: &CardSet) {
|
||||
fn fill_pool(&mut self, set: &DeckData) {
|
||||
let mut sorted = set
|
||||
.set
|
||||
.clone()
|
||||
@@ -631,3 +626,88 @@ impl WorstWordsSRSModule {
|
||||
self.pool = worst;
|
||||
}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct DeckSettings {
|
||||
pub id: Id,
|
||||
pub name: String,
|
||||
pub forward: String,
|
||||
pub backward: String,
|
||||
pub filter: String,
|
||||
pub count: Option<usize>,
|
||||
pub worst_words_list: Option<Vec<WordData>>,
|
||||
pub open_mode: OrderMode,
|
||||
}
|
||||
impl DeckSettings {
|
||||
pub(crate) fn with_name(name: String) -> DeckSettings {
|
||||
DeckSettings {
|
||||
id: 0.into(),
|
||||
name,
|
||||
forward: "".to_string(),
|
||||
backward: "".to_string(),
|
||||
filter: "true".to_string(),
|
||||
count: None,
|
||||
worst_words_list: None,
|
||||
open_mode: OrderMode::Default,
|
||||
}
|
||||
}
|
||||
pub fn check_filter(&self) -> bool {
|
||||
let time = Instant::now();
|
||||
let engine = Engine::new();
|
||||
let ast = engine.compile(&self.filter);
|
||||
println!("Compile time is {:?}", time.elapsed());
|
||||
ast.is_ok()
|
||||
}
|
||||
pub fn get_word_list(&self, state: &AppState) -> Vec<usize> {
|
||||
let time = Instant::now();
|
||||
let mut list = vec![];
|
||||
let engine = Engine::new();
|
||||
|
||||
let ast = engine.compile(&self.filter);
|
||||
if ast.is_err() {
|
||||
return list;
|
||||
}
|
||||
|
||||
let ast = ast.unwrap();
|
||||
|
||||
let groups = &state.word_groups;
|
||||
|
||||
list = state
|
||||
.dictionary
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.filter(|(_, word)| {
|
||||
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();
|
||||
scope
|
||||
.push_constant("id", word.id.0)
|
||||
.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",
|
||||
groups
|
||||
.iter()
|
||||
.find(|g| g.id == word.group_id)
|
||||
.cloned()
|
||||
.unwrap()
|
||||
.name,
|
||||
);
|
||||
|
||||
let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
||||
result.is_ok() && result.unwrap()
|
||||
})
|
||||
.map(|(index, _)| index)
|
||||
.collect();
|
||||
|
||||
println!("Collecting available words is {:?}", time.elapsed());
|
||||
|
||||
list
|
||||
}
|
||||
pub fn require_speech(&self) -> bool {
|
||||
self.forward == "speech" || self.backward == "speech"
|
||||
}
|
||||
}
|
||||
|
||||
+45
-32
@@ -3,39 +3,38 @@ mod data_provider;
|
||||
mod dictionary;
|
||||
mod dictionary_test;
|
||||
mod history;
|
||||
pub mod import;
|
||||
mod lang;
|
||||
pub mod navigation;
|
||||
mod quiz;
|
||||
mod randomizer;
|
||||
mod repetition;
|
||||
mod repetition_settings;
|
||||
mod repetitions;
|
||||
mod selector;
|
||||
pub mod styling;
|
||||
mod sync;
|
||||
mod word;
|
||||
mod writing;
|
||||
mod repetition_settings;
|
||||
pub mod import;
|
||||
|
||||
use crate::RootMessage::Keyboard;
|
||||
use crate::data_provider::card_sets::load_sets;
|
||||
use crate::data_provider::settings::get_setting;
|
||||
use crate::data_provider::sqlite::default_connection;
|
||||
use crate::data_provider::words::{load_word_groups, load_words};
|
||||
use crate::lang::{WordData, WordGroup};
|
||||
use crate::lang::{DeckSettings, Id, WordData, WordGroup};
|
||||
use crate::navigation::{AppSettings, RootMessage, ScreenState};
|
||||
use crate::quiz::*;
|
||||
use crate::repetitions::CardSetSettings;
|
||||
use crate::RootMessage::Keyboard;
|
||||
use chrono::NaiveDate;
|
||||
use iced::{keyboard, Subscription, Theme};
|
||||
use iced::{window, Font};
|
||||
use iced_core::window::Position;
|
||||
use hashbrown::HashMap;
|
||||
use iced::{Font, window};
|
||||
use iced::{Subscription, Theme, keyboard};
|
||||
use iced_core::Size;
|
||||
use rusqlite::Connection;
|
||||
use std::collections::HashMap;
|
||||
use iced_core::window::settings::PlatformSpecific;
|
||||
use iced_core::window::Position;
|
||||
use mimalloc::MiMalloc;
|
||||
|
||||
use rusqlite::Connection;
|
||||
#[cfg(target_os = "linux")]
|
||||
use iced_core::window::settings::PlatformSpecific;
|
||||
#[global_allocator]
|
||||
static GLOBAL: MiMalloc = MiMalloc;
|
||||
|
||||
@@ -43,7 +42,8 @@ const USER_FONT: Font = Font::with_name("Noto Sans JP");
|
||||
|
||||
fn main() -> iced::Result {
|
||||
println!("Welcome to JapLearn");
|
||||
iced::application(ScreenState::boot, ScreenState::update, ScreenState::view).window(window_settings())
|
||||
iced::application(ScreenState::boot, ScreenState::update, ScreenState::view)
|
||||
.window(window_settings())
|
||||
.subscription(subscription)
|
||||
.title("JapLearn")
|
||||
.settings(iced::Settings {
|
||||
@@ -52,51 +52,58 @@ fn main() -> iced::Result {
|
||||
})
|
||||
.font(include_bytes!("../noto.ttf"))
|
||||
.default_font(USER_FONT)
|
||||
.theme(Theme::GruvboxDark).run()
|
||||
.theme(Theme::GruvboxDark)
|
||||
.run()
|
||||
}
|
||||
|
||||
fn window_settings() -> window::Settings {
|
||||
let mut settings = window::Settings{
|
||||
let mut settings = window::Settings {
|
||||
position: Position::Centered,
|
||||
min_size: Some(Size::new(700.0_f32.into(), 700.0_f32.into())),
|
||||
.. Default::default()
|
||||
min_size: Some(Size::new(700.0_f32, 700.0_f32)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
settings.platform_specific =
|
||||
PlatformSpecific {
|
||||
application_id: "JapLearn".to_string(),
|
||||
override_redirect: false
|
||||
};
|
||||
settings.platform_specific = PlatformSpecific {
|
||||
application_id: "JapLearn".to_string(),
|
||||
override_redirect: false,
|
||||
};
|
||||
}
|
||||
|
||||
settings
|
||||
}
|
||||
|
||||
fn subscription(_state: &ScreenState) -> Subscription<RootMessage> {
|
||||
keyboard::listen().map(|e| Keyboard(e))
|
||||
keyboard::listen().map(Keyboard)
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub dictionary: Vec<WordData>,
|
||||
pub card_sets: Vec<CardSetSettings>,
|
||||
pub decks: Vec<DeckSettings>,
|
||||
pub word_groups: Vec<WordGroup>,
|
||||
pub connection: Connection,
|
||||
pub sync_data: AppSettings,
|
||||
pub activity: HashMap<u32, Vec<(NaiveDate, u32)>>
|
||||
pub activity: HashMap<Id, Vec<(NaiveDate, u32)>>,
|
||||
}
|
||||
|
||||
impl Default for AppState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
|
||||
|
||||
Self {
|
||||
dictionary: vec![],
|
||||
card_sets: vec![],
|
||||
decks: vec![],
|
||||
word_groups: vec![],
|
||||
connection: default_connection(),
|
||||
sync_data: AppSettings { key: None, auto_web_fetch: false },
|
||||
sync_data: AppSettings {
|
||||
key: None,
|
||||
auto_web_fetch: false,
|
||||
},
|
||||
activity: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -109,13 +116,19 @@ fn fill_state(state: &mut AppState) {
|
||||
let setting = load_settings(&state.connection);
|
||||
|
||||
state.dictionary = list;
|
||||
state.card_sets = sets;
|
||||
state.decks = sets;
|
||||
state.word_groups = groups;
|
||||
state.sync_data = setting
|
||||
}
|
||||
|
||||
fn load_settings(connection: &Connection) -> AppSettings {
|
||||
let key = get_setting("SYNC_KEY".to_string(), connection);
|
||||
let fetch = get_setting("AUTO_WEB_FETCH".to_string(), connection).unwrap_or("false".to_string()).parse::<bool>().unwrap();
|
||||
AppSettings { key, auto_web_fetch: fetch }
|
||||
let fetch = get_setting("AUTO_WEB_FETCH".to_string(), connection)
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap();
|
||||
AppSettings {
|
||||
key,
|
||||
auto_web_fetch: fetch,
|
||||
}
|
||||
}
|
||||
|
||||
+32
-24
@@ -1,9 +1,13 @@
|
||||
use crate::data_provider::history::{get_history_of_set, history_dir};
|
||||
use crate::data_provider::sqlite::{create_db, default_connection};
|
||||
use crate::data_provider::web_api::{get_local_version, get_web_version, load_data, set_local_version};
|
||||
use crate::dictionary::{app_data_dir, DictionaryMessage, DictionaryState};
|
||||
use crate::data_provider::web_api::{
|
||||
get_local_version, get_web_version, load_data, set_local_version,
|
||||
};
|
||||
use crate::dictionary::{DictionaryMessage, DictionaryState, app_data_dir};
|
||||
use crate::dictionary_test::{DictionaryQuizMessage, DictionaryQuizState};
|
||||
use crate::history::{HistoryMessage, HistoryState};
|
||||
use crate::import::{ImportMessage, ImportState};
|
||||
use crate::lang::Id;
|
||||
use crate::message_navigation;
|
||||
use crate::navigation::Page::*;
|
||||
use crate::navigation::RootMessage::{DataLoaded, Keyboard, UpdateData};
|
||||
@@ -21,13 +25,13 @@ use crate::word::{WordMessage, WordState};
|
||||
use crate::writing::{WritingMessage, WritingState};
|
||||
use crate::{AppState, fill_state};
|
||||
use chrono::NaiveDate;
|
||||
use hashbrown::HashMap;
|
||||
use iced::keyboard::Event;
|
||||
use iced::{Element, Task};
|
||||
use reqwest::Error;
|
||||
use std::collections::HashMap;
|
||||
use rusqlite::Connection;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
use crate::import::{ImportMessage, ImportState};
|
||||
|
||||
impl Default for ScreenState {
|
||||
fn default() -> Self {
|
||||
@@ -57,7 +61,7 @@ pub enum RootMessage {
|
||||
RepetitionSettings(RepetitionSettingsMessage),
|
||||
Import(ImportMessage),
|
||||
Keyboard(Event),
|
||||
DataLoaded(HashMap<u32, Vec<(NaiveDate, u32)>>),
|
||||
DataLoaded(HashMap<Id, Vec<(NaiveDate, u32)>>),
|
||||
None,
|
||||
UpdateData,
|
||||
}
|
||||
@@ -99,7 +103,9 @@ impl ScreenState {
|
||||
final_task = Task::batch([
|
||||
reading_additional_task,
|
||||
Task::perform(Self::load_web_backup(key), |result| {
|
||||
if let Ok(update) = result && update {
|
||||
if let Ok(update) = result
|
||||
&& update
|
||||
{
|
||||
UpdateData
|
||||
} else {
|
||||
RootMessage::None
|
||||
@@ -118,23 +124,21 @@ impl ScreenState {
|
||||
async fn load_additional_data() -> RootMessage {
|
||||
let directory = history_dir();
|
||||
let mut map = HashMap::new();
|
||||
for file in directory.read_dir().unwrap() {
|
||||
if let Ok(file) = file {
|
||||
let mut vec = vec![];
|
||||
let history_file_name = file.file_name().into_string().unwrap();
|
||||
let id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<u32>()
|
||||
.unwrap();
|
||||
for file in directory.read_dir().unwrap().flatten() {
|
||||
let mut vec = vec![];
|
||||
let history_file_name = file.file_name().into_string().unwrap();
|
||||
let id: Id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<Id>()
|
||||
.unwrap();
|
||||
|
||||
let history = get_history_of_set(id);
|
||||
let by_date = history.chunk_by(|x, x1| {
|
||||
x.timestamp.naive_local().date() == x1.timestamp.naive_local().date()
|
||||
});
|
||||
for group in by_date {
|
||||
vec.push((group[0].timestamp.naive_local().date(), group.len() as u32));
|
||||
}
|
||||
map.insert(id, vec);
|
||||
let history = get_history_of_set(id);
|
||||
let by_date = history.chunk_by(|x, x1| {
|
||||
x.timestamp.naive_local().date() == x1.timestamp.naive_local().date()
|
||||
});
|
||||
for group in by_date {
|
||||
vec.push((group[0].timestamp.naive_local().date(), group.len() as u32));
|
||||
}
|
||||
map.insert(id, vec);
|
||||
}
|
||||
DataLoaded(map)
|
||||
}
|
||||
@@ -142,11 +146,12 @@ impl ScreenState {
|
||||
async fn load_web_backup(string: String) -> Result<bool, Error> {
|
||||
let local = get_local_version().await;
|
||||
let web = get_web_version(string.as_str()).await?;
|
||||
println!("{} {}", local, web);
|
||||
if web > local {
|
||||
println!("Web version is newer than local version");
|
||||
load_data(string, true).await;
|
||||
set_local_version(web).await;
|
||||
}else {
|
||||
} else {
|
||||
return Ok(false);
|
||||
}
|
||||
Ok(true)
|
||||
@@ -164,9 +169,12 @@ impl ScreenState {
|
||||
return Task::none();
|
||||
}
|
||||
|
||||
if let UpdateData = message{
|
||||
if let UpdateData = message {
|
||||
println!("Loading data");
|
||||
let mut state = self.app_state.lock().unwrap();
|
||||
if cfg!(windows) {
|
||||
state.connection = Connection::open_in_memory().unwrap();
|
||||
}
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let temp_db_file = path.join("data.db.tmp");
|
||||
@@ -323,7 +331,7 @@ macro_rules! state_navigate_handle {
|
||||
}
|
||||
|
||||
pub trait NavigatedPage<T> {
|
||||
fn navigate(&self, message: &T) -> Option<Page>;
|
||||
fn navigate(&mut self, message: &T) -> Option<Page>;
|
||||
fn navigated(&mut self);
|
||||
fn update(&mut self, message: T) -> Task<RootMessage>;
|
||||
fn view(&self) -> Element<'_, T>;
|
||||
|
||||
+25
-23
@@ -5,7 +5,7 @@ use crate::quiz::QuizMessage::*;
|
||||
use crate::styling::*;
|
||||
use crate::{RootMessage, USER_FONT};
|
||||
use iced::widget::*;
|
||||
use iced::{alignment, Element, Fill, Task};
|
||||
use iced::{Element, Fill, Task, alignment};
|
||||
use rand::seq::SliceRandom;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -20,7 +20,7 @@ pub struct QuizState {
|
||||
}
|
||||
|
||||
impl NavigatedPage<QuizMessage> for QuizState {
|
||||
fn navigate(&self, message: &QuizMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &QuizMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
Some(PreviousPage)
|
||||
} else {
|
||||
@@ -28,8 +28,7 @@ impl NavigatedPage<QuizMessage> for QuizState {
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
|
||||
fn update(&mut self, message: QuizMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
@@ -42,7 +41,7 @@ impl NavigatedPage<QuizMessage> for QuizState {
|
||||
}
|
||||
self.current_roman = content;
|
||||
if self.correct_roman == self.current_roman {
|
||||
if self.is_help == false {
|
||||
if !self.is_help {
|
||||
self.score.correct += 1;
|
||||
}
|
||||
|
||||
@@ -55,13 +54,11 @@ impl NavigatedPage<QuizMessage> for QuizState {
|
||||
self.update_showed()
|
||||
}
|
||||
}
|
||||
Back => todo!(),
|
||||
Back => {},
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn view(&self) -> Element<'_, QuizMessage> {
|
||||
container(
|
||||
iced::widget::column![
|
||||
@@ -84,27 +81,32 @@ impl NavigatedPage<QuizMessage> for QuizState {
|
||||
.size(28)
|
||||
.width(150)
|
||||
.on_input(ContentChanged),
|
||||
row![
|
||||
text!("{}", self.score.total.to_string()).size(25),
|
||||
text!("{}", self.score.correct.to_string())
|
||||
.size(25)
|
||||
.color(iced::Color::from_rgb8(60, 170, 60)),
|
||||
text!("{}", self.score.fail.to_string())
|
||||
.color(iced::Color::from_rgb8(255, 79, 0))
|
||||
.size(25),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
score_display(&self.score),
|
||||
button("Закончить").style(jl_button).on_press(Back),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_x(alignment::Horizontal::Center),
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_x(alignment::Horizontal::Center),
|
||||
)
|
||||
.center_y(Fill)
|
||||
.center_x(Fill)
|
||||
.into()
|
||||
.center_y(Fill)
|
||||
.center_x(Fill)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
fn score_display<'a, T: 'a>(score: &Score) -> Element<'a, T> {
|
||||
row![
|
||||
text!("{}", score.total.to_string()).size(25),
|
||||
text!("{}", score.correct.to_string())
|
||||
.size(25)
|
||||
.color(iced::Color::from_rgb8(60, 170, 60)),
|
||||
text!("{}", score.fail.to_string())
|
||||
.color(iced::Color::from_rgb8(255, 79, 0))
|
||||
.size(25),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into()
|
||||
}
|
||||
|
||||
impl QuizState {
|
||||
pub(crate) fn new() -> QuizState {
|
||||
QuizState {
|
||||
|
||||
+9
-11
@@ -1,9 +1,9 @@
|
||||
use crate::RootMessage;
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::randomizer::RandomizerMessage::{Back, Edit, Start};
|
||||
use crate::styling::{back_overlay, jl_button, HALF_SPACING};
|
||||
use crate::RootMessage;
|
||||
use iced::widget::{button, text_editor};
|
||||
use crate::styling::{HALF_SPACING, back_overlay, jl_button};
|
||||
use iced::Task;
|
||||
use iced::widget::{button, text_editor};
|
||||
use rand::prelude::SliceRandom;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -20,15 +20,14 @@ pub enum RandomizerMessage {
|
||||
}
|
||||
|
||||
impl NavigatedPage<RandomizerMessage> for RandomizerState {
|
||||
fn navigate(&self, message: &RandomizerMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &RandomizerMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
return Some(Page::PreviousPage);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
fn update(&mut self, message: RandomizerMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
Edit(action) => {
|
||||
@@ -59,11 +58,12 @@ impl NavigatedPage<RandomizerMessage> for RandomizerState {
|
||||
.placeholder("Каждый элемент с новой строки"),
|
||||
button("Перемешать").style(jl_button).on_press(Start),
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into(),
|
||||
.spacing(HALF_SPACING)
|
||||
.into(),
|
||||
Back,
|
||||
)
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RandomizerState {
|
||||
fn default() -> Self {
|
||||
@@ -78,6 +78,4 @@ impl RandomizerState {
|
||||
list: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+23
-27
@@ -1,8 +1,8 @@
|
||||
use crate::data_provider::voice::get_voice;
|
||||
use crate::lang::{CardSet, CardStatistics, WordData, WordOpenMode};
|
||||
use crate::lang::Id;
|
||||
use crate::lang::{CardStatistics, DeckData, DeckSettings, WordData, WordOpenMode};
|
||||
use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{KeyPressedPage, NavigatedPage, Page};
|
||||
use crate::repetitions::CardSetSettings;
|
||||
use crate::styling::*;
|
||||
use crate::{AppState, RootMessage};
|
||||
use chrono::Local;
|
||||
@@ -18,18 +18,18 @@ use tokio::task::spawn_blocking;
|
||||
|
||||
pub struct RepetitionState {
|
||||
state: Arc<Mutex<AppState>>,
|
||||
pub settings: CardSetSettings,
|
||||
pub set: CardSet,
|
||||
pub settings: DeckSettings,
|
||||
pub set: DeckData,
|
||||
current_word: WordData,
|
||||
current_statistic: CardStatistics,
|
||||
open: bool,
|
||||
can_play: bool,
|
||||
sink: Arc<MixerDeviceSink>,
|
||||
opened: HashSet<u32>,
|
||||
opened: HashSet<Id>,
|
||||
}
|
||||
|
||||
impl NavigatedPage<RepetitionMessage> for RepetitionState {
|
||||
fn navigate(&self, message: &RepetitionMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &RepetitionMessage) -> Option<Page> {
|
||||
if let RepetitionMessage::Back = message {
|
||||
Some(PreviousPage)
|
||||
} else {
|
||||
@@ -37,8 +37,7 @@ impl NavigatedPage<RepetitionMessage> for RepetitionState {
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
fn update(&mut self, message: RepetitionMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
RepetitionMessage::Back => {}
|
||||
@@ -88,17 +87,17 @@ impl NavigatedPage<RepetitionMessage> for RepetitionState {
|
||||
(self.opened.len() as f32 / self.set.len() as f32 * 10000.0).round() / 100.0
|
||||
)
|
||||
]
|
||||
.height(Fill)
|
||||
.width(Fill)
|
||||
.into(),
|
||||
.height(Fill)
|
||||
.width(Fill)
|
||||
.into(),
|
||||
RepetitionMessage::Back,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl RepetitionState {
|
||||
pub(crate) fn new(set: CardSetSettings, state: Arc<Mutex<AppState>>) -> RepetitionState {
|
||||
let mut card_set = CardSet::new(&set, state.clone());
|
||||
pub(crate) fn new(set: DeckSettings, state: Arc<Mutex<AppState>>) -> RepetitionState {
|
||||
let mut card_set = DeckData::new(&set, state.clone());
|
||||
let (word, stat) = card_set.next();
|
||||
let sink_handle = rodio::DeviceSinkBuilder::open_default_sink().unwrap();
|
||||
|
||||
@@ -117,8 +116,6 @@ impl RepetitionState {
|
||||
}
|
||||
|
||||
impl RepetitionState {
|
||||
|
||||
|
||||
fn next(&mut self) -> Task<RootMessage> {
|
||||
if self.open {
|
||||
self.answer(WordOpenMode::None)
|
||||
@@ -169,7 +166,6 @@ impl RepetitionState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn draw_forward(&self) -> Element<'_, RepetitionMessage> {
|
||||
self.draw_card_view(self.settings.forward.as_str())
|
||||
}
|
||||
@@ -294,18 +290,18 @@ impl KeyPressedPage for RepetitionState {
|
||||
text: _,
|
||||
repeat: _,
|
||||
} = message
|
||||
&& let Code(code) = pk
|
||||
{
|
||||
if let Code(code) = pk {
|
||||
return match code {
|
||||
keyboard::key::Code::Space => self.next(),
|
||||
keyboard::key::Code::Digit1 => self.answer(WordOpenMode::None),
|
||||
keyboard::key::Code::Digit2 => self.answer(WordOpenMode::Hard),
|
||||
keyboard::key::Code::Digit3 => self.answer(WordOpenMode::Ok),
|
||||
keyboard::key::Code::Digit4 => self.answer(WordOpenMode::Easy),
|
||||
_ => Task::none(),
|
||||
};
|
||||
}
|
||||
return match code {
|
||||
keyboard::key::Code::Space => self.next(),
|
||||
keyboard::key::Code::Digit1 => self.answer(WordOpenMode::None),
|
||||
keyboard::key::Code::Digit2 => self.answer(WordOpenMode::Hard),
|
||||
keyboard::key::Code::Digit3 => self.answer(WordOpenMode::Ok),
|
||||
keyboard::key::Code::Digit4 => self.answer(WordOpenMode::Easy),
|
||||
_ => Task::none(),
|
||||
};
|
||||
}
|
||||
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
@@ -322,7 +318,7 @@ pub enum RepetitionMessage {
|
||||
async fn play_sound(sink: Arc<MixerDeviceSink>, text: String) {
|
||||
let data = get_voice(text.as_str()).await;
|
||||
spawn_blocking(move || {
|
||||
rodio::play(&sink.mixer(), data).unwrap().sleep_until_end();
|
||||
rodio::play(sink.mixer(), data).unwrap().sleep_until_end();
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
+30
-21
@@ -1,23 +1,25 @@
|
||||
use crate::data_provider::card_sets::{delete_set, update_card_set};
|
||||
use crate::AppState;
|
||||
use crate::data_provider::card_sets::{delete_set, update_deck};
|
||||
use crate::lang::DeckSettings;
|
||||
use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::repetition_settings::RepetitionSettingsMessage::*;
|
||||
use crate::repetitions::CardSetSettings;
|
||||
use crate::styling::*;
|
||||
use crate::AppState;
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::{button, column, row, scrollable, space, text, text_input};
|
||||
use iced::{Element, Task};
|
||||
use iced_core::Length::Fill;
|
||||
use iced_core::Padding;
|
||||
use iced_core::{color, Padding, Theme};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
use iced::widget::text_input::Catalog;
|
||||
|
||||
pub struct RepetitionSettingsState {
|
||||
set: CardSetSettings,
|
||||
set: DeckSettings,
|
||||
index: usize,
|
||||
state: Arc<Mutex<AppState>>,
|
||||
real_delete: bool,
|
||||
correct_filter: bool,
|
||||
}
|
||||
|
||||
impl RepetitionSettingsState {
|
||||
@@ -25,19 +27,22 @@ impl RepetitionSettingsState {
|
||||
let set;
|
||||
{
|
||||
let local_state = p1.lock().unwrap();
|
||||
set = local_state.card_sets[index].clone();
|
||||
set = local_state.decks[index].clone();
|
||||
}
|
||||
let filter_state = set.check_filter();
|
||||
|
||||
RepetitionSettingsState {
|
||||
index,
|
||||
set,
|
||||
state: p1,
|
||||
real_delete: false,
|
||||
correct_filter: filter_state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
fn navigate(&self, message: &RepetitionSettingsMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &RepetitionSettingsMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
Some(PreviousPage)
|
||||
} else {
|
||||
@@ -45,15 +50,14 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
fn update(&mut self, message: RepetitionSettingsMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
Back => {}
|
||||
Save => {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
update_card_set(&mut self.set, &state.connection);
|
||||
state.card_sets[self.index] = self.set.clone();
|
||||
update_deck(&mut self.set, &state.connection);
|
||||
state.decks[self.index] = self.set.clone();
|
||||
}
|
||||
SetName(new) => {
|
||||
self.set.name = new;
|
||||
@@ -66,6 +70,7 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
}
|
||||
SetFilter(new) => {
|
||||
self.set.filter = new;
|
||||
self.correct_filter = self.set.check_filter();
|
||||
}
|
||||
TryFilter => {
|
||||
let state = self.state.lock().unwrap();
|
||||
@@ -73,18 +78,18 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
self.set.count = Some(count);
|
||||
}
|
||||
DeleteSet => {
|
||||
if self.real_delete == false {
|
||||
if !self.real_delete {
|
||||
self.real_delete = true;
|
||||
return Task::future(async {
|
||||
tokio::time::sleep(Duration::from_millis(3000)).await;
|
||||
return RootMessage::RepetitionSettings(RevertDeleteSet);
|
||||
RootMessage::RepetitionSettings(RevertDeleteSet)
|
||||
});
|
||||
}
|
||||
let mut state = self.state.lock().unwrap();
|
||||
delete_set(&self.set, &state.connection);
|
||||
state.card_sets.remove(self.index);
|
||||
state.decks.remove(self.index);
|
||||
return Task::done(RootMessage::RepetitionSettings(Back));
|
||||
},
|
||||
}
|
||||
RevertDeleteSet => self.real_delete = false,
|
||||
}
|
||||
Task::none()
|
||||
@@ -112,7 +117,13 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
.spacing(QUARTER_SPACING),
|
||||
column![
|
||||
text!("Фильтр"),
|
||||
text_input("", &self.set.filter).on_input(SetFilter),
|
||||
text_input("", &self.set.filter).style(|x, status| {
|
||||
let mut primary = Theme::default()(x, status);
|
||||
if !self.correct_filter {
|
||||
primary.border.color = color!(255, 255, 0);
|
||||
}
|
||||
primary
|
||||
}).on_input(SetFilter),
|
||||
button("Проверить фильтр")
|
||||
.style(jl_button)
|
||||
.on_press(TryFilter),
|
||||
@@ -142,17 +153,15 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into(),
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into(),
|
||||
Back,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl RepetitionSettingsState {
|
||||
|
||||
|
||||
fn count_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionSettingsMessage> {
|
||||
fn count_view(&self, set: &DeckSettings) -> Element<'_, RepetitionSettingsMessage> {
|
||||
if let Some(count) = set.count {
|
||||
return text!("Количество слов: {}", count).into();
|
||||
}
|
||||
|
||||
+384
-265
@@ -1,7 +1,7 @@
|
||||
use crate::data_provider::card_sets::{delete_set, update_card_set};
|
||||
use crate::data_provider::card_stats::load_stats_of_set;
|
||||
use crate::data_provider::card_sets::update_deck;
|
||||
use crate::data_provider::card_stats::{add_stat_list, load_stats_of_deck};
|
||||
use crate::history::HistoryState;
|
||||
use crate::lang::{SetOrderMode, WordData};
|
||||
use crate::lang::{AppendMode, CardStatistics, DeckSettings, Id, OrderMode};
|
||||
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::repetition::RepetitionState;
|
||||
@@ -9,117 +9,219 @@ use crate::repetition_settings::RepetitionSettingsState;
|
||||
use crate::repetitions::RepetitionsMessage::*;
|
||||
use crate::styling::*;
|
||||
use crate::{AppState, RootMessage};
|
||||
use chrono::{Days, Local};
|
||||
use iced::widget::button::{danger, Status};
|
||||
use chrono::{Days, Local, Utc};
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
pub use iced::widget::button::{Catalog, Style};
|
||||
use iced::widget::container::bordered_box;
|
||||
use iced::widget::button::{Status, danger};
|
||||
use iced::widget::tooltip::Position::Top;
|
||||
use iced::widget::{button, column, container, lazy, radio, row, scrollable, space, svg, text, text_input, tooltip, Column, Row};
|
||||
use iced::widget::{
|
||||
Column, Row, button, column, container, lazy, radio, row, scrollable, space, svg, text,
|
||||
text_input, tooltip,
|
||||
};
|
||||
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
|
||||
use iced_core::Padding;
|
||||
use iced_core::border::Radius;
|
||||
use iced_core::svg::Handle;
|
||||
use iced_core::Padding;
|
||||
use rhai::{Engine, Scope};
|
||||
use std::ops::Deref;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RepetitionsState {
|
||||
selected_set: Option<usize>,
|
||||
correct_filters: Vec<bool>,
|
||||
pub state: Arc<Mutex<AppState>>,
|
||||
word_id_index_map: HashMap<Id, usize>,
|
||||
selected_deck_index: Option<usize>,
|
||||
decks: Vec<DeckViewData>,
|
||||
state: Arc<Mutex<AppState>>,
|
||||
}
|
||||
|
||||
impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
fn navigate(&self, message: &RepetitionsMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &RepetitionsMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
Some(PreviousPage)
|
||||
} else if let GoToHistory = message {
|
||||
let card_set;
|
||||
{
|
||||
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].id;
|
||||
}
|
||||
Some(History(HistoryState::new(card_set, self.state.clone())))
|
||||
Some(History(HistoryState::new(
|
||||
self.selected_deck().unwrap().id,
|
||||
self.state.clone(),
|
||||
)))
|
||||
} else if let GoToRepetition = message {
|
||||
let clone = self.state.clone();
|
||||
let card_set;
|
||||
{
|
||||
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].clone();
|
||||
|
||||
let selected_set = self.selected_deck_mut().unwrap();
|
||||
if selected_set.append_mode == AppendMode::Full {
|
||||
Self::append_all_words(&mut clone.lock().unwrap(), selected_set);
|
||||
selected_set.existing_words_indices = selected_set.available_words_indices.clone();
|
||||
} else {
|
||||
if selected_set
|
||||
.available_words_indices
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
|| selected_set
|
||||
.existing_words_indices
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(Repetition(RepetitionState::new(card_set, clone)))
|
||||
|
||||
Some(Repetition(RepetitionState::new(
|
||||
selected_set.general_settings.clone(),
|
||||
clone,
|
||||
)))
|
||||
} else if let GoToSettings = message {
|
||||
Some(RepetitionSettings(RepetitionSettingsState::new(
|
||||
self.selected_set.unwrap(),
|
||||
self.selected_deck().unwrap().index.unwrap(),
|
||||
self.state.clone(),
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
self.selected_set = None;
|
||||
let index = self.selected_deck_index.unwrap();
|
||||
let state = self.state.lock().unwrap();
|
||||
if let Some(global_deck) = state.decks.get(index) {
|
||||
if global_deck.id != self.selected_deck().unwrap().id {
|
||||
drop(state);
|
||||
self.decks.remove(index);
|
||||
self.clear_selection();
|
||||
}
|
||||
} else {
|
||||
drop(state);
|
||||
self.decks.remove(index);
|
||||
self.clear_selection()
|
||||
}
|
||||
}
|
||||
fn update(&mut self, message: RepetitionsMessage) -> Task<RootMessage> {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
match message {
|
||||
Next => {}
|
||||
Back => {}
|
||||
GoToRepetition => {}
|
||||
GoToHistory => {}
|
||||
GoToSettings => {}
|
||||
|
||||
CreateSet => {
|
||||
let index = state.card_sets.len() + 1;
|
||||
state
|
||||
.card_sets
|
||||
.push(CardSetSettings::with_name(format!("Card set #{}", index)));
|
||||
self.correct_filters.push(true);
|
||||
let index = self.decks.len() + 1;
|
||||
self.decks.push(DeckViewData {
|
||||
general_settings: DeckSettings::with_name(format!("Колода #{}", index)),
|
||||
existing_words_indices: None,
|
||||
available_words_indices: None,
|
||||
append_mode: Default::default(),
|
||||
index: None,
|
||||
});
|
||||
}
|
||||
DeleteSet => {
|
||||
let set = state.card_sets.remove(self.selected_set.unwrap());
|
||||
self.correct_filters.remove(self.selected_set.unwrap());
|
||||
self.selected_set = None;
|
||||
delete_set(&set, &state.connection);
|
||||
let index = self.selected_deck_index.unwrap();
|
||||
self.clear_selection();
|
||||
let deck = self.decks.remove(index);
|
||||
if deck.id.is_valid() {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
state.decks.remove(index);
|
||||
self.decks = self
|
||||
.decks
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|mut deck| {
|
||||
deck.index = state.decks.iter().position(|d| d.id == deck.id);
|
||||
deck
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
SetName(value) => {
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.general_settings.name = value;
|
||||
}
|
||||
SelectSet(index) => {
|
||||
self.selected_set = Some(index);
|
||||
let mut set = state.card_sets.get(index).unwrap().clone();
|
||||
set.update_worst_words(&state);
|
||||
state.card_sets[index] = set;
|
||||
}
|
||||
SetName(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].name = new;
|
||||
self.select_deck(index);
|
||||
let deck = self.selected_deck().unwrap();
|
||||
if deck.existing_words_indices.is_none() {
|
||||
let state = self.state.lock().unwrap();
|
||||
let added: Vec<_> = load_stats_of_deck(deck, &state.connection)
|
||||
.iter()
|
||||
.map(|c| self.word_id_index_map[&c.word_id])
|
||||
.collect();
|
||||
let total = deck.get_word_list(&state);
|
||||
drop(state);
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.existing_words_indices = Some(added);
|
||||
deck.available_words_indices = Some(total);
|
||||
}
|
||||
}
|
||||
Save => {
|
||||
for i in 0..state.card_sets.len() {
|
||||
self.correct_filters[i] = state.card_sets[i].check_filter();
|
||||
let mut deck = self.selected_deck().unwrap().clone();
|
||||
let mut state = self.state.lock().unwrap();
|
||||
{
|
||||
update_deck(&mut deck.general_settings, &state.connection);
|
||||
if let Some(index) = deck.index {
|
||||
state.decks[index] = deck.general_settings.clone();
|
||||
} else {
|
||||
state.decks.push(deck.general_settings.clone());
|
||||
deck.index = Some(state.decks.len() - 1);
|
||||
self.decks[self.selected_deck_index.unwrap()] = deck.clone();
|
||||
}
|
||||
}
|
||||
|
||||
let set = &mut state.card_sets[self.selected_set.unwrap()].clone();
|
||||
|
||||
update_card_set(set, &state.connection);
|
||||
|
||||
state.card_sets[self.selected_set.unwrap()] = set.clone();
|
||||
}
|
||||
SetForward(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].forward = new;
|
||||
SetForward(value) => {
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.general_settings.forward = value;
|
||||
}
|
||||
SetBackward(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].backward = new;
|
||||
SetBackward(value) => {
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.general_settings.backward = value;
|
||||
}
|
||||
SetFilter(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].filter = new;
|
||||
SetFilter(value) => {
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.general_settings.filter = value;
|
||||
}
|
||||
TryFilter => {
|
||||
let set = state.card_sets.get(self.selected_set.unwrap()).unwrap();
|
||||
let count = set.get_word_list(&state).len();
|
||||
state.card_sets[self.selected_set.unwrap()].count = Some(count);
|
||||
let state = self.state.lock().unwrap();
|
||||
let deck = self.selected_deck().unwrap();
|
||||
let count = deck.get_word_list(&state).len();
|
||||
drop(state);
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.general_settings.count = Some(count);
|
||||
}
|
||||
SetOpenMode(mode) => {
|
||||
state
|
||||
.card_sets
|
||||
.get_mut(self.selected_set.unwrap())
|
||||
.unwrap()
|
||||
.open_mode = mode;
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.general_settings.open_mode = mode;
|
||||
}
|
||||
SetAppendMode(mode) => {
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.append_mode = mode;
|
||||
}
|
||||
AppendWords(count) => {
|
||||
let adding: Vec<usize>;
|
||||
{
|
||||
{
|
||||
let deck = self.selected_deck().unwrap();
|
||||
let existing_words_indices = deck.existing_words_indices.as_ref().unwrap();
|
||||
let mut created_set = HashSet::with_capacity(existing_words_indices.len());
|
||||
existing_words_indices.iter().for_each(|c| {
|
||||
created_set.insert(c);
|
||||
});
|
||||
adding = deck
|
||||
.available_words_indices
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|c| !created_set.contains(c))
|
||||
.take(count)
|
||||
.cloned()
|
||||
.collect();
|
||||
}
|
||||
|
||||
let deck = self.selected_deck_mut().unwrap();
|
||||
deck.existing_words_indices
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.append(&mut adding.clone());
|
||||
}
|
||||
|
||||
let deck = self.selected_deck().unwrap();
|
||||
let mut state = self.state.lock().unwrap();
|
||||
Self::append_words(&mut state, &deck.general_settings, adding.into_iter());
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
@@ -129,7 +231,7 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
back_overlay(
|
||||
row![
|
||||
column![
|
||||
scrollable(self.sets_list()).height(Fill),
|
||||
scrollable(self.decks_list()).height(Fill),
|
||||
button("Добавить")
|
||||
.style(jl_button)
|
||||
.width(Fill)
|
||||
@@ -137,34 +239,65 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Length::FillPortion(1)),
|
||||
self.selected_set_view(),
|
||||
self.selected_deck_view(),
|
||||
]
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.into(),
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.into(),
|
||||
Back,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl RepetitionsState {
|
||||
pub(crate) fn new(state: Arc<Mutex<AppState>>) -> RepetitionsState {
|
||||
let count = state.lock().unwrap().card_sets.len();
|
||||
let state_ = state.lock().unwrap();
|
||||
let mut map = HashMap::with_capacity(state_.dictionary.len());
|
||||
|
||||
state_
|
||||
.dictionary
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(index, word)| {
|
||||
map.insert(word.id, index);
|
||||
});
|
||||
|
||||
let decks = Self::get_decks_from_state(&state_);
|
||||
drop(state_);
|
||||
RepetitionsState {
|
||||
selected_set: None,
|
||||
correct_filters: vec![true; count],
|
||||
word_id_index_map: map,
|
||||
selected_deck_index: None,
|
||||
decks,
|
||||
state,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_decks_from_state(state: &AppState) -> Vec<DeckViewData> {
|
||||
state
|
||||
.decks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, deck)| DeckViewData {
|
||||
general_settings: deck.clone(),
|
||||
existing_words_indices: None,
|
||||
available_words_indices: None,
|
||||
append_mode: Default::default(),
|
||||
index: Some(index),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl RepetitionsState {
|
||||
|
||||
fn launch_delete_button(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
if set.id != 0 {
|
||||
fn launch_delete_button(&self, deck: &DeckViewData) -> Element<'_, RepetitionsMessage> {
|
||||
if deck.id.is_valid() {
|
||||
if deck.append_mode == AppendMode::Manual
|
||||
&& deck.existing_words_indices.as_ref().unwrap().is_empty()
|
||||
{
|
||||
return text!("Добавьте карточки или измените фильтр").into();
|
||||
}
|
||||
return row![
|
||||
space().width(Fill),
|
||||
button(text!("Начать повторение").width(200).center())
|
||||
@@ -175,24 +308,15 @@ impl RepetitionsState {
|
||||
}
|
||||
button("Удалить").style(danger).on_press(DeleteSet).into()
|
||||
}
|
||||
|
||||
fn selected_set_view(&self) -> Element<'_, RepetitionsMessage> {
|
||||
if let Some(index) = self.selected_set {
|
||||
let set;
|
||||
{
|
||||
let state = self.state.lock().unwrap();
|
||||
if state.card_sets.len() <= index {
|
||||
return space().width(Length::FillPortion(3)).into();
|
||||
}
|
||||
set = state.card_sets[index].clone();
|
||||
}
|
||||
fn selected_deck_view(&self) -> Element<'_, RepetitionsMessage> {
|
||||
if let Some(deck) = &self.selected_deck() {
|
||||
return column![
|
||||
scrollable(
|
||||
column![
|
||||
column![
|
||||
text!("Название набора"),
|
||||
row![
|
||||
text_input("", &set.name).on_input(SetName),
|
||||
text_input("", &deck.name).on_input(SetName),
|
||||
button(svg(Handle::from_memory(SETTINGS_ICON)))
|
||||
.width(43)
|
||||
.on_press(GoToSettings),
|
||||
@@ -201,41 +325,57 @@ impl RepetitionsState {
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
{
|
||||
if set.id == 0 {
|
||||
if !deck.id.is_valid() {
|
||||
column![
|
||||
column![
|
||||
text!("Передняя сторона"),
|
||||
text_input("", &set.forward).on_input(SetForward),
|
||||
text_input("", &deck.forward).on_input(SetForward),
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
column![
|
||||
text!("Задняя сторона"),
|
||||
text_input("", &set.backward).on_input(SetBackward),
|
||||
text_input("", &deck.backward).on_input(SetBackward),
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
column![
|
||||
text!("Фильтр"),
|
||||
text_input("", &set.filter).on_input(SetFilter),
|
||||
text_input("", &deck.filter).on_input(SetFilter),
|
||||
button("Проверить фильтр")
|
||||
.style(jl_button)
|
||||
.on_press(TryFilter),
|
||||
self.count_view(&set),
|
||||
self.count_view(deck),
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
} else {
|
||||
self.filled_set_data_view(&set)
|
||||
column![
|
||||
self.filled_deck_data_view(deck),
|
||||
self.word_append_panel(deck),
|
||||
radio(
|
||||
"Обычный режим",
|
||||
OrderMode::Default,
|
||||
Some(deck.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Начать с плохих слов",
|
||||
OrderMode::TrainWorstFirst,
|
||||
Some(deck.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Полностью случайно",
|
||||
OrderMode::FullRandom,
|
||||
Some(deck.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
button("История").style(jl_button).on_press(GoToHistory),
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
}
|
||||
},
|
||||
radio(
|
||||
"Обычный режим",
|
||||
SetOrderMode::Default,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
self.words_words_view(&set),
|
||||
button("История").style(jl_button).on_press(GoToHistory),
|
||||
// self.words_words_view(&set),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.padding(Padding {
|
||||
@@ -248,40 +388,36 @@ impl RepetitionsState {
|
||||
.height(Fill),
|
||||
row![
|
||||
button("Сохранить").style(jl_button).on_press(Save),
|
||||
self.launch_delete_button(&set)
|
||||
self.launch_delete_button(deck)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_y(Center),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Length::FillPortion(3))
|
||||
.width(Length::FillPortion(2))
|
||||
.into();
|
||||
}
|
||||
space().width(Length::FillPortion(3)).into()
|
||||
space().width(Length::FillPortion(2)).into()
|
||||
}
|
||||
|
||||
fn filled_set_data_view(&self, set: &CardSetSettings) -> Column<'_, RepetitionsMessage> {
|
||||
column![self.activity_bar(set)].align_x(Center)
|
||||
fn filled_deck_data_view(&self, deck: &DeckSettings) -> Column<'_, RepetitionsMessage> {
|
||||
column![self.activity_bar(deck), self.count_comparator_view()]
|
||||
.align_x(Center)
|
||||
.width(Fill)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into()
|
||||
}
|
||||
fn activity_bar(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
fn activity_bar(&self, deck: &DeckSettings) -> Element<'_, RepetitionsMessage> {
|
||||
const MAX_DAY_COUNT: f32 = 128.0;
|
||||
|
||||
let state = self.state.lock().unwrap();
|
||||
let history = state.activity.get(&set.id);
|
||||
let history = state.activity.get(&deck.id);
|
||||
let mut counts: Vec<u32> = vec![0; 30 * 7];
|
||||
let now = Local::now().date_naive();
|
||||
|
||||
|
||||
if let Some(history) = history {
|
||||
for (date, activity) in history {
|
||||
let distance = now - *date;
|
||||
let index = counts.len() as i64 - distance.num_days() - 1;
|
||||
|
||||
if index == 210 {
|
||||
println!("invalid index")
|
||||
}
|
||||
if index < 0 {
|
||||
continue;
|
||||
}
|
||||
@@ -300,8 +436,7 @@ impl RepetitionsState {
|
||||
})
|
||||
.spacing(QUARTER_SPACING);
|
||||
|
||||
|
||||
let mut iter = counts.into_iter();
|
||||
let mut iter = counts.iter();
|
||||
for i in 0..30 {
|
||||
let mut column = Column::new().spacing(QUARTER_SPACING);
|
||||
|
||||
@@ -309,13 +444,17 @@ impl RepetitionsState {
|
||||
let value = *iter.next().unwrap() as f32;
|
||||
let k = (value / MAX_DAY_COUNT).min(1.0) * 0.9 + 0.1;
|
||||
|
||||
let date = now.clone().checked_sub_days(Days::new(30 * 7 - i * 7 - j - 1)).unwrap();
|
||||
let date = (*now)
|
||||
.checked_sub_days(Days::new(30 * 7 - i * 7 - j - 1))
|
||||
.unwrap();
|
||||
|
||||
column = column.push(tooltip(
|
||||
iced::widget::container(space().height(15).width(15)).style(
|
||||
move |x: &Theme| container::Style {
|
||||
text_color: None,
|
||||
background: Some(Background::Color(x.palette().primary.scale_alpha(k))),
|
||||
background: Some(Background::Color(
|
||||
x.palette().primary.scale_alpha(k),
|
||||
)),
|
||||
border: Border {
|
||||
color: Default::default(),
|
||||
width: 0.0,
|
||||
@@ -332,67 +471,81 @@ impl RepetitionsState {
|
||||
|
||||
row = row.push(column);
|
||||
}
|
||||
scrollable(column!["Карта активности", row].spacing(QUARTER_SPACING).align_x(Center))
|
||||
}).into()
|
||||
|
||||
}
|
||||
|
||||
fn words_words_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
column![
|
||||
text!("Худшие слова"),
|
||||
container(scrollable(self.worst_words_list(&set)).height(200)).style(bordered_box),
|
||||
radio(
|
||||
"Начать с плохих слов",
|
||||
SetOrderMode::TrainWorstFirst,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Полностью случайно",
|
||||
SetOrderMode::FullRandom,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
scrollable(
|
||||
column!["Карта активности", row]
|
||||
.spacing(QUARTER_SPACING)
|
||||
.align_x(Center),
|
||||
)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
fn worst_words_list(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
let mut column = Column::new();
|
||||
|
||||
for word in set.worst_words_list.clone().unwrap() {
|
||||
column = column.push(text!("{} | {}", &word.key, &word.value));
|
||||
fn word_append_panel(&self, deck: &DeckViewData) -> Element<'_, RepetitionsMessage> {
|
||||
column![
|
||||
text!("Режим добавления карточек"),
|
||||
row![
|
||||
radio(
|
||||
"Добавлять все доступные",
|
||||
AppendMode::Full,
|
||||
Some(deck.append_mode),
|
||||
SetAppendMode
|
||||
),
|
||||
radio(
|
||||
"Добавлять вручную",
|
||||
AppendMode::Manual,
|
||||
Some(deck.append_mode),
|
||||
SetAppendMode
|
||||
)
|
||||
]
|
||||
.spacing(HALF_SPACING),
|
||||
self.adder_panel(deck),
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into()
|
||||
}
|
||||
fn adder_panel(&self, deck: &DeckViewData) -> Element<'_, RepetitionsMessage> {
|
||||
match deck.append_mode {
|
||||
AppendMode::Full => space().into(),
|
||||
AppendMode::Manual => row![
|
||||
button("+5").on_press(AppendWords(5)),
|
||||
button("+10").on_press(AppendWords(10)),
|
||||
button("+15").on_press(AppendWords(15)),
|
||||
button("+20").on_press(AppendWords(20))
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into(),
|
||||
}
|
||||
column.into()
|
||||
}
|
||||
|
||||
fn count_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
if let Some(count) = set.count {
|
||||
fn count_comparator_view(&self) -> Element<'_, RepetitionsMessage> {
|
||||
let selected_deck = self.selected_deck().unwrap();
|
||||
let now = selected_deck.existing_words_indices.as_ref().unwrap().len();
|
||||
let available = selected_deck
|
||||
.available_words_indices
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.len();
|
||||
text!("{} слова добавлено из {}", now, available).into()
|
||||
}
|
||||
fn count_view(&self, deck: &DeckSettings) -> Element<'_, RepetitionsMessage> {
|
||||
if let Some(count) = deck.count {
|
||||
return text!("Количество слов: {}", count).into();
|
||||
}
|
||||
space().into()
|
||||
}
|
||||
|
||||
fn sets_list(&self) -> Column<'_, RepetitionsMessage> {
|
||||
fn decks_list(&self) -> Column<'_, RepetitionsMessage> {
|
||||
let mut column = Column::new();
|
||||
let mut i = 0;
|
||||
let sets = &self.state.lock().unwrap().card_sets;
|
||||
for set in sets {
|
||||
|
||||
for (i, deck) in self.decks.iter().enumerate() {
|
||||
column = column.push(
|
||||
button(text!("{}", set.name.clone()))
|
||||
.on_press_with(move || SelectSet(i.clone()))
|
||||
button(text!("{}", &deck.name))
|
||||
.on_press_with(move || SelectSet(i))
|
||||
.style(move |_x: &Theme, status: Status| Style {
|
||||
background: if status == Status::Hovered {
|
||||
Some(Background::Color(Color::WHITE.scale_alpha(0.2)))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
text_color: if self.correct_filters[i.clone()] {
|
||||
_x.palette().primary
|
||||
} else {
|
||||
_x.palette().warning
|
||||
},
|
||||
text_color: _x.palette().primary,
|
||||
border: Border {
|
||||
color: Default::default(),
|
||||
width: 0.0,
|
||||
@@ -402,13 +555,65 @@ impl RepetitionsState {
|
||||
snap: false,
|
||||
}),
|
||||
);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
column
|
||||
}
|
||||
}
|
||||
|
||||
impl RepetitionsState {
|
||||
fn append_all_words(state: &mut AppState, deck: &DeckViewData) {
|
||||
let existing = deck.existing_words_indices.as_ref().unwrap();
|
||||
let mut created_set = HashSet::with_capacity(existing.len());
|
||||
existing.iter().for_each(|c| {
|
||||
created_set.insert(c);
|
||||
});
|
||||
let required = deck
|
||||
.available_words_indices
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|i| !created_set.contains(i))
|
||||
.cloned();
|
||||
Self::append_words(state, deck, required);
|
||||
}
|
||||
fn append_words(state: &mut AppState, deck: &DeckSettings, indices: impl Iterator<Item = usize>) {
|
||||
let words = &state.dictionary;
|
||||
let stats = &mut indices
|
||||
.map(|i| {
|
||||
let word = &words[i];
|
||||
CardStatistics {
|
||||
id: 0.into(),
|
||||
word_id: word.id,
|
||||
last_open: Utc::now(),
|
||||
score: 1,
|
||||
set_id: deck.id,
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if !stats.is_empty() {
|
||||
add_stat_list(stats, &mut state.connection);
|
||||
}
|
||||
}
|
||||
|
||||
fn select_deck(&mut self, index: usize) {
|
||||
if self.decks.get(index).is_some() {
|
||||
self.selected_deck_index = Some(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn selected_deck(&self) -> Option<&DeckViewData> {
|
||||
self.decks.get(self.selected_deck_index?)
|
||||
}
|
||||
fn selected_deck_mut(&mut self) -> Option<&mut DeckViewData> {
|
||||
self.decks.get_mut(self.selected_deck_index?)
|
||||
}
|
||||
|
||||
fn clear_selection(&mut self) {
|
||||
self.selected_deck_index = None;
|
||||
}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub enum RepetitionsMessage {
|
||||
Next,
|
||||
@@ -423,111 +628,25 @@ pub enum RepetitionsMessage {
|
||||
SetBackward(String),
|
||||
SetFilter(String),
|
||||
TryFilter,
|
||||
SetOpenMode(SetOrderMode),
|
||||
SetOpenMode(OrderMode),
|
||||
SetAppendMode(AppendMode),
|
||||
GoToHistory,
|
||||
GoToSettings,
|
||||
AppendWords(usize),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CardSetSettings {
|
||||
pub id: u32,
|
||||
pub name: String,
|
||||
pub forward: String,
|
||||
pub backward: String,
|
||||
pub filter: String,
|
||||
pub count: Option<usize>,
|
||||
pub worst_words_list: Option<Vec<WordData>>,
|
||||
pub open_mode: SetOrderMode,
|
||||
struct DeckViewData {
|
||||
general_settings: DeckSettings,
|
||||
existing_words_indices: Option<Vec<usize>>,
|
||||
available_words_indices: Option<Vec<usize>>,
|
||||
append_mode: AppendMode,
|
||||
index: Option<usize>,
|
||||
}
|
||||
impl Deref for DeckViewData {
|
||||
type Target = DeckSettings;
|
||||
|
||||
impl CardSetSettings {
|
||||
fn with_name(name: String) -> CardSetSettings {
|
||||
CardSetSettings {
|
||||
id: 0,
|
||||
name,
|
||||
forward: "".to_string(),
|
||||
backward: "".to_string(),
|
||||
filter: "true".to_string(),
|
||||
count: None,
|
||||
worst_words_list: None,
|
||||
open_mode: SetOrderMode::Default,
|
||||
}
|
||||
}
|
||||
|
||||
fn check_filter(&self) -> bool {
|
||||
let engine = Engine::new();
|
||||
let ast = engine.compile(&self.filter);
|
||||
ast.is_ok()
|
||||
}
|
||||
|
||||
pub fn get_word_list(&self, state: &AppState) -> Vec<WordData> {
|
||||
let mut list = vec![];
|
||||
let engine = Engine::new();
|
||||
let ast = engine.compile(&self.filter);
|
||||
if ast.is_err() {
|
||||
return list;
|
||||
}
|
||||
|
||||
let ast = ast.unwrap();
|
||||
|
||||
let groups = &state.word_groups;
|
||||
|
||||
for word in &state.dictionary {
|
||||
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();
|
||||
scope
|
||||
.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",
|
||||
groups
|
||||
.iter()
|
||||
.find(|g| g.id == word.group_id)
|
||||
.cloned()
|
||||
.unwrap()
|
||||
.name,
|
||||
);
|
||||
|
||||
let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
||||
if result.is_ok() && result.unwrap() {
|
||||
list.push(word.clone());
|
||||
}
|
||||
}
|
||||
|
||||
list
|
||||
}
|
||||
|
||||
pub fn require_speech(&self) -> bool {
|
||||
self.forward == "speech" || self.backward == "speech"
|
||||
}
|
||||
|
||||
fn update_worst_words(&mut self, state: &AppState) {
|
||||
if let Some(_) = self.worst_words_list {
|
||||
return;
|
||||
}
|
||||
|
||||
let connection = &state.connection;
|
||||
let mut stats = load_stats_of_set(self, connection);
|
||||
stats.sort_by_key(|s| s.calculated_score() as i32);
|
||||
let avg = stats.iter().map(|s| s.calculated_score()).sum::<f32>() / stats.len() as f32;
|
||||
let avg = avg * 0.7;
|
||||
let bad: Vec<WordData> = stats
|
||||
.iter()
|
||||
.take_while(|word| word.calculated_score() < avg)
|
||||
.map(|stat| {
|
||||
state.dictionary[state
|
||||
.dictionary
|
||||
.binary_search_by_key(&stat.word_id, |x| x.id)
|
||||
.unwrap()]
|
||||
.clone()
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.worst_words_list = Some(bad.clone());
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.general_settings
|
||||
}
|
||||
}
|
||||
|
||||
+5
-9
@@ -36,7 +36,7 @@ pub enum SelectorMessage {
|
||||
}
|
||||
|
||||
impl NavigatedPage<SelectorMessage> for SelectorState {
|
||||
fn navigate(&self, message: &SelectorMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &SelectorMessage) -> Option<Page> {
|
||||
if let SelectorMessage::Goto = message {
|
||||
return if self.is_writing {
|
||||
let writing = WritingState::new(&self.set);
|
||||
@@ -62,8 +62,7 @@ impl NavigatedPage<SelectorMessage> for SelectorState {
|
||||
None
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
fn update(&mut self, message: SelectorMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
SelectorMessage::Change => match self.set.chars_type {
|
||||
@@ -105,10 +104,10 @@ impl NavigatedPage<SelectorMessage> for SelectorState {
|
||||
.on_press(SelectorMessage::Goto)
|
||||
.style(cta_button),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
.spacing(DEFAULT_SPACING),
|
||||
)
|
||||
.padding(10)
|
||||
.into()
|
||||
.padding(10)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +120,6 @@ impl SelectorState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn nav_style(theme: &Theme, status: Status) -> Style {
|
||||
let mut basic = button::text(theme, status);
|
||||
basic.background = if status == Status::Hovered {
|
||||
@@ -133,8 +131,6 @@ impl SelectorState {
|
||||
basic
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn rows_selector(&self) -> Element<'_, SelectorMessage> {
|
||||
let mut row = Row::new();
|
||||
|
||||
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
use crate::repetitions::Style;
|
||||
use iced::Element;
|
||||
use iced::border::Radius;
|
||||
use iced::widget::button::*;
|
||||
use iced::widget::{button, container};
|
||||
use iced::Element;
|
||||
use iced_core::alignment::Horizontal::Left;
|
||||
use iced_core::Length::Fill;
|
||||
use iced_core::alignment::Horizontal::Left;
|
||||
use iced_core::{Border, Theme};
|
||||
|
||||
pub const HALF_SPACING: f32 = DEFAULT_SPACING / 2.0;
|
||||
|
||||
+12
-10
@@ -4,7 +4,7 @@ use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::styling::*;
|
||||
use crate::sync::SyncMessage::*;
|
||||
use crate::{fill_state, AppState, RootMessage};
|
||||
use crate::{AppState, RootMessage, fill_state};
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::container::rounded_box;
|
||||
use iced::widget::{button, column, container, progress_bar, row, space, text, toggler};
|
||||
@@ -12,7 +12,6 @@ use iced::{Center, Element, Fill, Font, Length, Task};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum SyncMessage {
|
||||
Back,
|
||||
@@ -38,7 +37,7 @@ pub struct SyncState {
|
||||
}
|
||||
|
||||
impl NavigatedPage<SyncMessage> for SyncState {
|
||||
fn navigate(&self, message: &SyncMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &SyncMessage) -> Option<Page> {
|
||||
if let Back = message
|
||||
&& !self.frozen
|
||||
{
|
||||
@@ -67,7 +66,7 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
}
|
||||
KeyCopied => {}
|
||||
IdReceived(new_id) => {
|
||||
if validate_id(&new_id) == false {
|
||||
if !validate_id(&new_id) {
|
||||
return Task::none();
|
||||
}
|
||||
let mut state = self.state.lock().unwrap();
|
||||
@@ -120,7 +119,7 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
state.sync_data = updated_state.sync_data;
|
||||
state.connection = updated_state.connection;
|
||||
state.card_sets = updated_state.card_sets;
|
||||
state.decks = updated_state.decks;
|
||||
state.dictionary = updated_state.dictionary;
|
||||
state.word_groups = updated_state.word_groups;
|
||||
}
|
||||
@@ -130,13 +129,16 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
state.sync_data.key = None;
|
||||
delete_settings("SYNC_KEY".to_string(), &state.connection);
|
||||
delete_settings("AUTO_WEB_FETCH".to_string(), &state.connection);
|
||||
|
||||
}
|
||||
SwitchAutoSync => {
|
||||
self.auto_fetch = !self.auto_fetch;
|
||||
let mut state = self.state.lock().unwrap();
|
||||
state.sync_data.auto_web_fetch = self.auto_fetch;
|
||||
set_setting("AUTO_WEB_FETCH".to_string(), self.auto_fetch.to_string(), &state.connection);
|
||||
set_setting(
|
||||
"AUTO_WEB_FETCH".to_string(),
|
||||
self.auto_fetch.to_string(),
|
||||
&state.connection,
|
||||
);
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
@@ -167,8 +169,6 @@ impl SyncState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn sync_column(&self) -> Element<'_, SyncMessage> {
|
||||
let network_view: Element<'_, SyncMessage> = if self.frozen {
|
||||
progress_bar(0.0..=5.0, self.progress).into()
|
||||
@@ -198,7 +198,9 @@ impl SyncState {
|
||||
button("↓ Скачать").style(jl_button).on_press(GetLast)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
toggler(self.auto_fetch).label("Автоматическая синхронизация").on_toggle(|_| SwitchAutoSync),
|
||||
toggler(self.auto_fetch)
|
||||
.label("Автоматическая синхронизация")
|
||||
.on_toggle(|_| SwitchAutoSync),
|
||||
container(network_view).width(Fill),
|
||||
button("Отключить синхронизацию")
|
||||
.style(danger)
|
||||
|
||||
+8
-15
@@ -18,7 +18,7 @@ pub struct WordState {
|
||||
}
|
||||
|
||||
impl NavigatedPage<WordMessage> for WordState {
|
||||
fn navigate(&self, message: &WordMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &WordMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
Some(PreviousPage)
|
||||
} else {
|
||||
@@ -49,11 +49,9 @@ impl NavigatedPage<WordMessage> for WordState {
|
||||
SetValue(n) => {
|
||||
self.word.value = n;
|
||||
}
|
||||
SetAdditional(key, value) => match key.as_str() {
|
||||
_ => {
|
||||
self.word.additional.insert(key, value.clone());
|
||||
}
|
||||
},
|
||||
SetAdditional(key, value) => {
|
||||
self.word.additional.insert(key, value.clone());
|
||||
}
|
||||
AddAdditional(key) => {
|
||||
self.word.additional.insert(key, "".to_string());
|
||||
}
|
||||
@@ -145,23 +143,18 @@ impl WordState {
|
||||
}
|
||||
}
|
||||
|
||||
fn reading_field(&self, value: &String) -> Element<'_, WordMessage> {
|
||||
fn reading_field(&self, value: &str) -> Element<'_, WordMessage> {
|
||||
self.additional_field(value, "Чтение слова".to_string(), "reading".to_string())
|
||||
}
|
||||
|
||||
fn description_field(&self, value: &String) -> Element<'_, WordMessage> {
|
||||
fn description_field(&self, value: &str) -> Element<'_, WordMessage> {
|
||||
self.additional_field(value, "Описание".to_string(), "description".to_string())
|
||||
}
|
||||
|
||||
fn context_field(&self, value: &String) -> Element<'_, WordMessage> {
|
||||
fn context_field(&self, value: &str) -> Element<'_, WordMessage> {
|
||||
self.additional_field(value, "В контексте".to_string(), "context".to_string())
|
||||
}
|
||||
fn additional_field(
|
||||
&self,
|
||||
value: &String,
|
||||
name: String,
|
||||
id: String,
|
||||
) -> Element<'_, WordMessage> {
|
||||
fn additional_field(&self, value: &str, name: String, id: String) -> Element<'_, WordMessage> {
|
||||
column![
|
||||
text!("{}", name),
|
||||
row![
|
||||
|
||||
+16
-21
@@ -1,10 +1,10 @@
|
||||
use crate::RootMessage;
|
||||
use crate::lang::KanaSet;
|
||||
use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::styling::*;
|
||||
use crate::RootMessage;
|
||||
use iced::widget::*;
|
||||
use iced::{alignment, Element, Fill, Task};
|
||||
use iced::{Element, Fill, Task, alignment};
|
||||
use rand::seq::SliceRandom;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -19,7 +19,7 @@ pub struct WritingState {
|
||||
}
|
||||
|
||||
impl NavigatedPage<WritingMessage> for WritingState {
|
||||
fn navigate(&self, message: &WritingMessage) -> Option<Page> {
|
||||
fn navigate(&mut self, message: &WritingMessage) -> Option<Page> {
|
||||
if let WritingMessage::Back = message {
|
||||
Some(PreviousPage)
|
||||
} else {
|
||||
@@ -27,11 +27,10 @@ impl NavigatedPage<WritingMessage> for WritingState {
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
fn update(&mut self, message: WritingMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
WritingMessage::Back => todo!(),
|
||||
WritingMessage::Back => {},
|
||||
WritingMessage::Next => self.next(),
|
||||
WritingMessage::SwitchShowMode(b) => self.show_all = b,
|
||||
}
|
||||
@@ -56,13 +55,13 @@ impl NavigatedPage<WritingMessage> for WritingState {
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_x(alignment::Horizontal::Center),
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.align_x(alignment::Horizontal::Center),
|
||||
)
|
||||
.center_x(Fill)
|
||||
.center_y(Fill)
|
||||
.padding(10)
|
||||
.into()
|
||||
.center_x(Fill)
|
||||
.center_y(Fill)
|
||||
.padding(10)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,8 +82,6 @@ impl WritingState {
|
||||
}
|
||||
|
||||
impl WritingState {
|
||||
|
||||
|
||||
fn next(&mut self) {
|
||||
if self.set.is_empty() {
|
||||
self.kana = "".to_string();
|
||||
@@ -97,24 +94,22 @@ impl WritingState {
|
||||
}
|
||||
|
||||
if self.show_all {
|
||||
if self.set.is_empty() == false && self.kana_total.is_empty() == false {
|
||||
if !self.set.is_empty() && !self.kana_total.is_empty() {
|
||||
self.set.clear();
|
||||
}
|
||||
for pair in &self.set {
|
||||
self.kana = "---".to_string();
|
||||
self.roman_total += &*format!("{} ", &pair.1.clone()).to_string();
|
||||
self.kana_total += &*format!("{} ", &pair.0).to_string();
|
||||
self.roman_total += &*format!("{} ", pair.1.clone()).to_string();
|
||||
self.kana_total += &*format!("{} ", pair.0).to_string();
|
||||
}
|
||||
} else {
|
||||
let current = self.set.pop().unwrap();
|
||||
self.kana_total += &*format!("{} ", ¤t.0).to_string();
|
||||
self.roman_total += &*format!("{} ", ¤t.1.clone()).to_string();
|
||||
self.kana_total += &*format!("{} ", current.0).to_string();
|
||||
self.roman_total += &*format!("{} ", current.1.clone()).to_string();
|
||||
self.kana = current.1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn answers(&self) -> Element<'_, WritingMessage> {
|
||||
if self.set.is_empty() {
|
||||
text!("{}", self.kana_total).size(36).into()
|
||||
|
||||
Reference in New Issue
Block a user