6 Commits
Author SHA1 Message Date
micialware 3fcbd235cf Migrate to hashbrown structures 2026-08-16 23:50:14 +03:00
micialware dcf7352553 Fix windows autofetch 2026-08-16 23:42:56 +03:00
micialware 11b38479c1 Fixing partial adding bug 2026-08-16 14:23:00 +03:00
micialware 51c8d7be74 Adding words by parts 2026-08-15 21:32:24 +03:00
micialware 8f789facd7 Add anki21 format and fix multi import bug 2026-08-15 20:16:48 +03:00
micialware e676898135 Clippy code cleanup 2026-08-15 13:22:59 +03:00
31 changed files with 845 additions and 538 deletions
Generated
+26
View File
@@ -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
View File
@@ -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
+126
View File
@@ -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 -2
View File
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
fn bench_split(c: &mut Criterion) {
let input = "data_1";
@@ -22,7 +22,6 @@ fn bench_split_new(c: &mut Criterion) {
});
}
pub fn split_with_coma(ts: &str) -> Vec<String> {
ts.split(',')
.map(|ts| ts.to_lowercase().trim().to_string())
+5 -4
View File
@@ -1,5 +1,5 @@
use crate::lang::SetOrderMode;
use crate::repetitions::CardSetSettings;
use crate::lang::{AppendMode, CardSetSettings, OrderMode};
use rusqlite::Connection;
pub fn load_sets(connection: &Connection) -> Vec<CardSetSettings> {
@@ -16,7 +16,8 @@ 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,
append_mode: AppendMode::Manual,
})
})
.unwrap();
@@ -48,7 +49,7 @@ pub fn add_set(set: &mut CardSetSettings, connection: &Connection) {
pub fn update_card_set(set: &mut CardSetSettings, connection: &Connection) {
if set.id == 0 {
add_set(set, &connection);
add_set(set, connection);
} else {
connection
.execute(
+11 -12
View File
@@ -1,5 +1,4 @@
use crate::lang::CardStatistics;
use crate::repetitions::CardSetSettings;
use crate::lang::{CardSetSettings, CardStatistics};
use rusqlite::Connection;
use std::time::Instant;
@@ -27,16 +26,17 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
buffer
}
pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
pub fn add_stat_list(stat: &mut [CardStatistics], connection: &Connection) {
let time = Instant::now();
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()
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp()
)
})
.collect::<Vec<_>>()
@@ -48,7 +48,6 @@ pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
let count = connection.execute(query.as_str(), ());
if count.is_err() {
println!("{}", count.unwrap_err());
return;
}
@@ -60,13 +59,13 @@ pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
)
.unwrap();
let start_index = last_index - (count.unwrap() as u32) + 1;
let start_index = last_index - (stat.len() as u32) + 1;
let mut index = 0;
for id in start_index..=last_index {
for (index, id) in (start_index..=last_index).enumerate() {
stat[index].id = id;
index += 1;
}
println!("Added {} cards for {:?}", stat.len(), time.elapsed());
}
// pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
+2 -2
View File
@@ -1,7 +1,7 @@
use std::fs;
use crate::dictionary::app_data_dir;
use crate::lang::WordOpenMode;
use chrono::{DateTime, Utc};
use std::fs;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::io::{BufRead, BufReader};
@@ -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 {
+11 -6
View File
@@ -1,6 +1,6 @@
use rusqlite::Connection;
use serde_json::Value;
use std::collections::HashMap;
use hashbrown::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| {
count_stmt
.query_map((group_id as i64,), |row| {
Ok(ImportNote {
tags: row.get(0)?,
fields: row.get::<usize, String>(1)?
fields: row
.get::<usize, String>(1)?
.split('')
.map(|x| x.to_string())
.collect()
.collect(),
})
}).unwrap().map(|x| x.unwrap()).collect::<Vec<ImportNote>>()
})
.unwrap()
.map(|x| x.unwrap())
.collect::<Vec<ImportNote>>()
}
+2 -2
View File
@@ -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;
+5 -5
View File
@@ -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 {
if let Some(row) = iter.next()
&& let Ok(value) = row
{
return Some(value);
}
return None;
}
None
}
+43 -11
View File
@@ -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,43 @@ 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());
Ok(version.parse::<u32>().unwrap())
}
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();
}
+16 -10
View File
@@ -1,5 +1,5 @@
use crate::lang::{WordData, WordGroup};
use rusqlite::{params, Connection};
use rusqlite::{Connection, params};
use std::collections::HashMap;
pub fn add_word(word: &mut WordData, connection: &Connection) {
@@ -28,13 +28,21 @@ pub fn add_words(words: &mut[WordData], connection: &mut Connection) {
let count = words.len();
{
let mut stmt = tx.prepare(
let mut stmt = tx
.prepare(
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)",
).unwrap();
)
.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();
}
}
@@ -50,16 +58,14 @@ pub fn add_words(words: &mut[WordData], connection: &mut Connection) {
let start_index = last_index - (count as u32) + 1;
let mut index = 0;
for id in start_index..=last_index {
for (index, id) in (start_index..=last_index).enumerate() {
words[index].id = id;
index += 1;
}
}
pub fn update_word(word: &mut WordData, connection: &Connection) {
if word.id == 0 {
add_word(word, &connection);
add_word(word, connection);
} else {
connection
.execute(
@@ -155,7 +161,7 @@ pub fn add_group(group: &mut WordGroup, connection: &Connection) {
pub fn update_group(group: &mut WordGroup, connection: &Connection) {
if group.id == 0 {
add_group(group, &connection);
add_group(group, connection);
} else {
connection
.execute(
+14 -15
View File
@@ -18,7 +18,7 @@ use iced::widget::*;
use iced::{Border, Color, Shadow, Task};
use iced_core::Length::Fill;
use rand::random_range;
use std::collections::{HashMap, HashSet};
use hashbrown::{HashMap, HashSet};
use std::fs;
use std::ops::Add;
use std::path::PathBuf;
@@ -69,8 +69,9 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
if let Back = message {
return Some(Page::PreviousPage);
}
if let Test = message {
if self.include_map.iter().any(|x| *x) {
if let Test = message
&& self.include_map.iter().any(|x| *x)
{
let mut words = vec![];
let dict = &self.state.lock().unwrap().dictionary;
@@ -88,7 +89,6 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
self.no_typing,
)));
}
}
if let WordAction(index) = message {
let word: WordData;
{
@@ -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);
@@ -318,7 +318,7 @@ impl DictionaryState {
let connection = &state.connection;
let word = &mut state.dictionary.get(i).unwrap().clone();
update_word(word, &connection);
update_word(word, connection);
state.dictionary[i] = word.clone();
}
@@ -353,14 +353,13 @@ 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
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 {
is_included: self.include_map[i],
@@ -553,7 +552,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 +568,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();
+7 -9
View File
@@ -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)]
@@ -42,8 +42,7 @@ impl NavigatedPage<DictionaryQuizMessage> for DictionaryQuizState {
}
}
fn navigated(&mut self) {
}
fn navigated(&mut self) {}
fn update(&mut self, message: DictionaryQuizMessage) -> Task<RootMessage> {
match message {
@@ -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()
+1
View File
@@ -0,0 +1 @@
+5 -6
View File
@@ -23,8 +23,7 @@ impl NavigatedPage<HistoryMessage> for HistoryState {
Back => Some(Page::PreviousPage),
}
}
fn navigated(&mut self) {
}
fn navigated(&mut self) {}
fn update(&mut self, _: HistoryMessage) -> Task<RootMessage> {
Task::none()
}
@@ -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),
);
+22 -17
View File
@@ -63,7 +63,7 @@ pub enum ImportMessage {
impl NavigatedPage<ImportMessage> for ImportState {
fn navigate(&self, message: &ImportMessage) -> Option<Page> {
if let Some(_) = self.progress {
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 => {
@@ -129,7 +131,8 @@ impl NavigatedPage<ImportMessage> for ImportState {
ImportFinished => {
self.progress = None;
let group = self
.import_data.as_mut()
.import_data
.as_mut()
.unwrap()
.0
.get_mut(self.selected_index)
@@ -200,7 +203,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 +259,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 +365,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,8 +384,6 @@ 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)
@@ -388,6 +391,10 @@ impl ImportState {
.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 {
@@ -452,11 +459,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 +483,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();
@@ -525,15 +531,14 @@ impl ImportState {
}
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.get(*index).unwrap();
if skip_empty && str.is_empty() {
continue;
}
+143 -54
View File
@@ -1,15 +1,16 @@
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_set, 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 rayon::iter::IndexedParallelIterator;
use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use rhai::{Engine, Scope};
use serde::{Deserialize, Serialize};
use std::cmp::min;
use std::collections::HashMap;
@@ -199,14 +200,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();
@@ -286,11 +279,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();
}
@@ -325,35 +314,39 @@ impl CardSet {
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 mut current_set = load_stats_of_set(settings, &state_locked.connection);
let last_list: Vec<_> = settings
.get_word_list(&state_locked)
.iter()
.map(|w| state_locked.dictionary.get(*w).unwrap())
.cloned()
.collect();
let word_ids = last_list.iter().map(|l| l.id).collect::<Vec<u32>>();
let new_stats: &mut Vec<CardStatistics> = &mut last_list
.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(),
})
.collect();
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 new_stats: &mut Vec<CardStatistics> = &mut last_list
// .iter()
// .filter(|word| !saved_ids.contains(&word.id))
// .map(|word| CardStatistics {
// id: 0,
// word_id: word.id,
// last_open: Utc::now(),
// score: 1,
// set_id: settings.id,
// })
// .collect();
//
// 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,11 +363,11 @@ 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(),
}
@@ -383,7 +376,7 @@ impl CardSet {
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 +384,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 +392,7 @@ impl CardSet {
index
}
OrderModule::WorstWordsSRS(mut module) => {
if module.initialized == false {
if !module.initialized {
module.init(self)
}
let index = module.next(self);
@@ -413,7 +406,7 @@ impl CardSet {
}
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();
@@ -456,12 +449,18 @@ impl CardSet {
}
#[derive(Clone, PartialEq, Copy, Eq)]
pub enum SetOrderMode {
pub enum OrderMode {
Default,
TrainWorstFirst,
FullRandom,
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum AppendMode {
Full,
Manual,
}
#[derive(Clone)]
enum OrderModule {
SemiRandomSRS(SemiRandomSRSModule),
@@ -631,3 +630,93 @@ impl WorstWordsSRSModule {
self.pool = worst;
}
}
#[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: OrderMode,
pub append_mode: AppendMode,
}
impl CardSetSettings {
pub(crate) 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: OrderMode::Default,
append_mode: AppendMode::Manual,
}
}
pub(crate) 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<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)
.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"
}
}
+37 -23
View File
@@ -2,39 +2,39 @@
mod data_provider;
mod dictionary;
mod dictionary_test;
pub mod helpers;
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::{CardSetSettings, 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 iced::{Font, window};
use iced::{Subscription, Theme, keyboard};
use iced_core::Size;
use rusqlite::Connection;
use std::collections::HashMap;
use iced_core::window::Position;
use iced_core::window::settings::PlatformSpecific;
use mimalloc::MiMalloc;
use rusqlite::Connection;
use hashbrown::HashMap;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
@@ -43,7 +43,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,22 +53,22 @@ 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 {
position: Position::Centered,
min_size: Some(Size::new(700.0_f32.into(), 700.0_f32.into())),
min_size: Some(Size::new(700.0_f32, 700.0_f32)),
..Default::default()
};
#[cfg(target_os = "linux")]
{
settings.platform_specific =
PlatformSpecific {
settings.platform_specific = PlatformSpecific {
application_id: "JapLearn".to_string(),
override_redirect: false
override_redirect: false,
};
}
@@ -75,7 +76,7 @@ fn window_settings() -> window::Settings {
}
fn subscription(_state: &ScreenState) -> Subscription<RootMessage> {
keyboard::listen().map(|e| Keyboard(e))
keyboard::listen().map(Keyboard)
}
pub struct AppState {
@@ -84,19 +85,26 @@ pub struct AppState {
pub word_groups: Vec<WordGroup>,
pub connection: Connection,
pub sync_data: AppSettings,
pub activity: HashMap<u32, Vec<(NaiveDate, u32)>>
pub activity: HashMap<u32, Vec<(NaiveDate, u32)>>,
}
impl Default for AppState {
fn default() -> Self {
Self::new()
}
}
impl AppState {
pub fn new() -> Self {
Self {
dictionary: vec![],
card_sets: 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(),
}
}
@@ -116,6 +124,12 @@ fn fill_state(state: &mut AppState) {
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,
}
}
+15 -8
View File
@@ -1,9 +1,12 @@
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::message_navigation;
use crate::navigation::Page::*;
use crate::navigation::RootMessage::{DataLoaded, Keyboard, UpdateData};
@@ -24,10 +27,10 @@ use chrono::NaiveDate;
use iced::keyboard::Event;
use iced::{Element, Task};
use reqwest::Error;
use std::collections::HashMap;
use hashbrown::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use crate::import::{ImportMessage, ImportState};
use rusqlite::Connection;
impl Default for ScreenState {
fn default() -> Self {
@@ -99,7 +102,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,8 +123,7 @@ 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 {
for file in directory.read_dir().unwrap().flatten() {
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]
@@ -135,13 +139,13 @@ impl ScreenState {
}
map.insert(id, vec);
}
}
DataLoaded(map)
}
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;
@@ -167,6 +171,9 @@ impl ScreenState {
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");
+18 -16
View File
@@ -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)]
@@ -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;
}
@@ -60,8 +59,6 @@ impl NavigatedPage<QuizMessage> for QuizState {
Task::none()
}
fn view(&self) -> Element<'_, QuizMessage> {
container(
iced::widget::column![
@@ -84,16 +81,7 @@ 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)
@@ -105,6 +93,20 @@ impl NavigatedPage<QuizMessage> for QuizState {
}
}
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 {
+6 -8
View File
@@ -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)]
@@ -27,8 +27,7 @@ impl NavigatedPage<RandomizerMessage> for RandomizerState {
None
}
fn navigated(&mut self) {
}
fn navigated(&mut self) {}
fn update(&mut self, message: RandomizerMessage) -> Task<RootMessage> {
match message {
Edit(action) => {
@@ -63,7 +62,8 @@ impl NavigatedPage<RandomizerMessage> for RandomizerState {
.into(),
Back,
)
}}
}
}
impl Default for RandomizerState {
fn default() -> Self {
@@ -78,6 +78,4 @@ impl RandomizerState {
list: vec![],
}
}
}
+5 -10
View File
@@ -1,8 +1,7 @@
use crate::data_provider::voice::get_voice;
use crate::lang::{CardSet, CardStatistics, WordData, WordOpenMode};
use crate::lang::{CardSet, CardSetSettings, CardStatistics, 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;
@@ -37,8 +36,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 => {}
@@ -117,8 +115,6 @@ impl RepetitionState {
}
impl RepetitionState {
fn next(&mut self) -> Task<RootMessage> {
if self.open {
self.answer(WordOpenMode::None)
@@ -169,7 +165,6 @@ impl RepetitionState {
}
}
fn draw_forward(&self) -> Element<'_, RepetitionMessage> {
self.draw_card_view(self.settings.forward.as_str())
}
@@ -294,8 +289,8 @@ 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),
@@ -305,7 +300,7 @@ impl KeyPressedPage for RepetitionState {
_ => Task::none(),
};
}
}
Task::none()
}
}
@@ -322,7 +317,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();
+6 -9
View File
@@ -1,10 +1,10 @@
use crate::AppState;
use crate::data_provider::card_sets::{delete_set, update_card_set};
use crate::lang::CardSetSettings;
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};
@@ -45,8 +45,7 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
}
}
fn navigated(&mut self) {
}
fn navigated(&mut self) {}
fn update(&mut self, message: RepetitionSettingsMessage) -> Task<RootMessage> {
match message {
Back => {}
@@ -73,18 +72,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);
return Task::done(RootMessage::RepetitionSettings(Back));
},
}
RevertDeleteSet => self.real_delete = false,
}
Task::none()
@@ -150,8 +149,6 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
}
impl RepetitionSettingsState {
fn count_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionSettingsMessage> {
if let Some(count) = set.count {
return text!("Количество слов: {}", count).into();
+185 -164
View File
@@ -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_stats::{add_stat_list, load_stats_of_set};
use crate::history::HistoryState;
use crate::lang::{SetOrderMode, WordData};
use crate::lang::{AppendMode, CardSetSettings, CardStatistics, OrderMode};
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
use crate::navigation::{NavigatedPage, Page};
use crate::repetition::RepetitionState;
@@ -9,23 +9,27 @@ use crate::repetition_settings::RepetitionSettingsState;
use crate::repetitions::RepetitionsMessage::*;
use crate::styling::*;
use crate::{AppState, RootMessage};
use chrono::{Days, Local};
use chrono::{Days, Local, Utc};
use hashbrown::{HashMap, HashSet};
use iced::widget::button::{danger, Status};
pub use iced::widget::button::{Catalog, Style};
use iced::widget::container::bordered_box;
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::{
button, column, container, lazy, radio, row, scrollable, space, svg, text, text_input, tooltip,
Column, Row,
};
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
use iced_core::border::Radius;
use iced_core::svg::Handle;
use iced_core::Padding;
use rhai::{Engine, Scope};
use std::sync::{Arc, Mutex};
#[derive(Clone)]
pub struct RepetitionsState {
selected_set: Option<usize>,
correct_filters: Vec<bool>,
current_sets_cards_cache: HashMap<usize, (Vec<usize>, Vec<usize>)>,
word_id_index_map: HashMap<u32, usize>,
pub state: Arc<Mutex<AppState>>,
}
@@ -45,6 +49,11 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
{
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].clone();
}
if card_set.append_mode == AppendMode::Full {
self.append_all_words(&card_set);
}
Some(Repetition(RepetitionState::new(card_set, clone)))
} else if let GoToSettings = message {
Some(RepetitionSettings(RepetitionSettingsState::new(
@@ -55,9 +64,9 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
None
}
}
fn navigated(&mut self) {
self.selected_set = None;
self.current_sets_cards_cache.clear();
}
fn update(&mut self, message: RepetitionsMessage) -> Task<RootMessage> {
let mut state = self.state.lock().unwrap();
@@ -82,8 +91,17 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
}
SelectSet(index) => {
self.selected_set = Some(index);
let mut set = state.card_sets.get(index).unwrap().clone();
set.update_worst_words(&state);
let set = state.card_sets.get(index).unwrap().clone();
if !self.current_sets_cards_cache.contains_key(&index) {
let added: Vec<_> = load_stats_of_set(&set, &state.connection)
.iter()
.map(|c| self.word_id_index_map[&c.word_id])
.collect();
let total = set.get_word_list(&state);
self.current_sets_cards_cache.insert(index, (added, total));
}
state.card_sets[index] = set;
}
SetName(new) => {
@@ -121,6 +139,36 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
.unwrap()
.open_mode = mode;
}
SetAppendMode(mode) => {
let set = state.card_sets.get_mut(self.selected_set.unwrap()).unwrap();
set.append_mode = mode;
}
AppendWords(count) => {
let adding: Vec<usize>;
let set = state.card_sets.get(self.selected_set.unwrap()).unwrap();
let index = self.selected_set.unwrap();
{
let total_cache = &mut self.current_sets_cards_cache;
let cache = total_cache.get_mut(&index).unwrap().clone();
let mut created_set = HashSet::with_capacity(cache.0.len());
cache.0.iter().for_each(|c| {
created_set.insert(c);
});
adding = cache
.1
.iter()
.filter(|c| !created_set.contains(c))
.take(count)
.cloned()
.collect();
let mut new_current = cache.0.clone();
new_current.append(&mut adding.clone());
total_cache.insert(index, (new_current, cache.1.clone()));
}
Self::append_words(&state, set, adding.into_iter());
}
}
Task::none()
}
@@ -147,21 +195,61 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
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 count = state_.card_sets.len();
let mut map = HashMap::with_capacity(state_.dictionary.len());
state_.dictionary.iter().enumerate().for_each(|(index, word)| {map.insert(word.id, index);});
drop(state_);
RepetitionsState {
selected_set: None,
correct_filters: vec![true; count],
current_sets_cards_cache: Default::default(),
word_id_index_map: map,
state,
}
}
}
impl RepetitionsState {
fn append_all_words(&self, set: &CardSetSettings) {
let index = self.selected_set.unwrap();
let cache = &self.current_sets_cards_cache[&index];
let mut created_set = HashSet::with_capacity(cache.0.len());
cache.0.iter().for_each(|c| {
created_set.insert(c);
});
let required = cache
.1
.iter()
.filter(|i| !created_set.contains(i)).cloned();
Self::append_words(&self.state.lock().unwrap(), set, required);
}
fn append_words(state: &AppState, set: &CardSetSettings, indices: impl Iterator<Item = usize>) {
let words = &state.dictionary;
let stats = &mut indices
.map(|i| {
let word = words.get(i).unwrap();
CardStatistics {
id: 0,
word_id: word.id,
last_open: Utc::now(),
score: 1,
set_id: set.id,
}
})
.collect::<Vec<_>>();
if !stats.is_empty() {
add_stat_list(stats, &state.connection);
}
}
fn launch_delete_button(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
if set.id != 0 {
@@ -225,18 +313,34 @@ impl RepetitionsState {
]
.spacing(DEFAULT_SPACING)
} else {
self.filled_set_data_view(&set)
}
},
column![
self.filled_set_data_view(&set),
self.word_append_panel(&set),
radio(
"Обычный режим",
SetOrderMode::Default,
OrderMode::Default,
Some(set.open_mode),
SetOpenMode
),
radio(
"Начать с плохих слов",
OrderMode::TrainWorstFirst,
Some(set.open_mode),
SetOpenMode
),
radio(
"Полностью случайно",
OrderMode::FullRandom,
Some(set.open_mode),
SetOpenMode
),
self.words_words_view(&set),
button("История").style(jl_button).on_press(GoToHistory),
]
.spacing(HALF_SPACING)
}
},
// self.words_words_view(&set),
]
.spacing(DEFAULT_SPACING)
.padding(Padding {
top: 0.0,
@@ -253,17 +357,17 @@ impl RepetitionsState {
.spacing(DEFAULT_SPACING),
]
.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)
column![self.activity_bar(set), self.count_comparator_view()]
.align_x(Center)
.width(Fill)
.spacing(DEFAULT_SPACING)
.into()
}
fn activity_bar(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
const MAX_DAY_COUNT: f32 = 128.0;
@@ -273,7 +377,6 @@ impl RepetitionsState {
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;
@@ -300,8 +403,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 +411,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,41 +438,59 @@ 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));
}
column.into()
fn word_append_panel(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
column![
text!("Режим добавления карточек"),
row![
radio(
"Добавлять все доступные",
AppendMode::Full,
Some(set.append_mode),
SetAppendMode
),
radio(
"Добавлять вручную",
AppendMode::Manual,
Some(set.append_mode),
SetAppendMode
)
]
.spacing(HALF_SPACING),
self.adder_panel(set),
]
.spacing(HALF_SPACING)
.into()
}
fn adder_panel(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
match set.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(),
}
}
fn count_comparator_view(&self) -> Element<'_, RepetitionsMessage> {
let cache = &self.current_sets_cards_cache[&self.selected_set.unwrap()];
let now = cache.0.len();
let available = cache.1.len();
text!("{} слова добавлено из {}", now, available).into()
}
fn count_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
if let Some(count) = set.count {
return text!("Количество слов: {}", count).into();
@@ -376,19 +500,19 @@ impl RepetitionsState {
fn sets_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, set) in sets.iter().enumerate() {
column = column.push(
button(text!("{}", set.name.clone()))
.on_press_with(move || SelectSet(i.clone()))
.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()] {
text_color: if self.correct_filters[i] {
_x.palette().primary
} else {
_x.palette().warning
@@ -402,7 +526,6 @@ impl RepetitionsState {
snap: false,
}),
);
i += 1;
}
column
@@ -423,111 +546,9 @@ pub enum RepetitionsMessage {
SetBackward(String),
SetFilter(String),
TryFilter,
SetOpenMode(SetOrderMode),
SetOpenMode(OrderMode),
SetAppendMode(AppendMode),
GoToHistory,
GoToSettings,
}
#[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,
}
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());
}
AppendWords(usize),
}
+1 -5
View File
@@ -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 {
@@ -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
View File
@@ -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;
+10 -8
View File
@@ -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,
@@ -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();
@@ -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)
+5 -12
View File
@@ -49,11 +49,9 @@ impl NavigatedPage<WordMessage> for WordState {
SetValue(n) => {
self.word.value = n;
}
SetAdditional(key, value) => match key.as_str() {
_ => {
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![
+8 -13
View File
@@ -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)]
@@ -27,8 +27,7 @@ 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!(),
@@ -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!("{} ", &current.0).to_string();
self.roman_total += &*format!("{} ", &current.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()