From 47fd5b3b9daea9b0876327cc148a32eadf69548c Mon Sep 17 00:00:00 2001 From: leca Date: Sun, 14 Apr 2024 12:12:18 +0300 Subject: [PATCH] updating cards --- cards.drawio | 23 ++++++++--- resources/cards/new/ui.xml | 7 ++++ resources/widgets/card_entry/template.ui.xml | 2 +- src/cards.rs | 3 ++ src/db.rs | 5 ++- src/dictionary.rs | 43 +++++++++++++++----- src/game.rs | 1 - src/main.rs | 2 +- src/ui/cards/edit/imp.rs | 13 +++--- src/ui/cards/new/imp.rs | 2 + src/ui/cards/new/mod.rs | 4 ++ src/widgets/card_display/mod.rs | 2 +- src/widgets/card_entry/imp.rs | 8 ++-- src/widgets/card_entry/mod.rs | 4 +- 14 files changed, 87 insertions(+), 32 deletions(-) create mode 100644 src/cards.rs diff --git a/cards.drawio b/cards.drawio index 6568a72..e90bd46 100644 --- a/cards.drawio +++ b/cards.drawio @@ -1,11 +1,11 @@ - + - + @@ -41,20 +41,33 @@ - + + + + + + + + + + + + + + - + - + diff --git a/resources/cards/new/ui.xml b/resources/cards/new/ui.xml index c3e2986..a5bacbd 100644 --- a/resources/cards/new/ui.xml +++ b/resources/cards/new/ui.xml @@ -36,6 +36,13 @@ Reading + + + GTK_ALIGN_CENTER + GTK_ALIGN_CENTER + Translation + + GTK_ALIGN_CENTER diff --git a/resources/widgets/card_entry/template.ui.xml b/resources/widgets/card_entry/template.ui.xml index 8244ba4..09a6099 100644 --- a/resources/widgets/card_entry/template.ui.xml +++ b/resources/widgets/card_entry/template.ui.xml @@ -19,7 +19,7 @@ - + GTK_ALIGN_END GTK_ALIGN_CENTER 50 diff --git a/src/cards.rs b/src/cards.rs new file mode 100644 index 0000000..9e5766f --- /dev/null +++ b/src/cards.rs @@ -0,0 +1,3 @@ +pub struct Card { + +} \ No newline at end of file diff --git a/src/db.rs b/src/db.rs index 65d16f1..7fd175c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -43,8 +43,9 @@ pub fn init() { connection.execute("CREATE TABLE IF NOT EXISTS cards ( id INTEGER PRIMARY KEY AUTOINCREMENT, imagename CHAR(64), - hieroglyph VARCHAR(2), - reading VARCHAR(128) + hieroglyph VARCHAR(64), + reading VARCHAR(64), + translation VARCHAR(128) )",()).unwrap(); } diff --git a/src/dictionary.rs b/src/dictionary.rs index 5c8a396..f3694a0 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -1,5 +1,8 @@ +use rusqlite::Connection; use std::fs; +use crate::db::get_db_path; + #[derive(PartialEq)] pub struct DictionaryEntry { hieroglyph: String, @@ -9,7 +12,11 @@ pub struct DictionaryEntry { impl std::clone::Clone for DictionaryEntry { fn clone(&self) -> Self { - Self { hieroglyph: self.hieroglyph.clone(), reading: self.reading.clone(), translation: self.translation.clone() } + Self { + hieroglyph: self.hieroglyph.clone(), + reading: self.reading.clone(), + translation: self.translation.clone(), + } } } @@ -70,9 +77,8 @@ fn read_and_parse(path: &String) -> Vec { result } -fn deduplicate (entries: &Vec) -> Vec{ - - let mut deduplicated:Vec = Vec::new(); +fn deduplicate(entries: &Vec) -> Vec { + let mut deduplicated: Vec = Vec::new(); for entry in entries.iter() { let mut is_dup = false; @@ -85,20 +91,37 @@ fn deduplicate (entries: &Vec) -> Vec{ } if is_dup { - let pos = deduplicated.iter().position(|x| x.hieroglyph == entry.hieroglyph).unwrap(); - deduplicated[pos].translation += &(String::from(" ; ") + &entry.translation); + let pos = deduplicated + .iter() + .position(|x| x.hieroglyph == entry.hieroglyph) + .unwrap(); + deduplicated[pos].translation += &(String::from(";") + &entry.translation); } else { deduplicated.push(entry.clone()); } } deduplicated -} +} -pub fn import(path: &String) -> Vec{ +fn write_to_database(dict: &Vec) { + let conn = Connection::open(get_db_path()).unwrap(); + + for line in dict.iter() { + match conn.execute("INSERT OR REPLACE INTO cards (id, hieroglyph, reading, translation) VALUES ((SELECT id FROM cards WHERE hieroglyph = ?1), ?1, ?2, ?3)", [&line.hieroglyph, &line.reading, &line.translation]) { + Ok(_) => (), + Err(e) => { + println!("{e}" ); + panic!("Cannot update database with words form dictionary") + } + }; + } +} + +pub fn import(path: &String) { let dictionary = read_and_parse(path); let dictionary = deduplicate(&dictionary); - dictionary + // write_to_database(&dictionary); } // pub fn write_to_file(path: &String, dict: &Vec) { @@ -117,4 +140,4 @@ pub fn import(path: &String) -> Vec{ // eprintln!("Couldn't write to file: {}", e); // } // } -// } \ No newline at end of file +// } diff --git a/src/game.rs b/src/game.rs index 02f142a..f6e2f7b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,3 @@ - use rand::Rng; const VOWELS: usize = 5; diff --git a/src/main.rs b/src/main.rs index c8495f6..0f866d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ fn main() -> glib::ExitCode { db::init(); // Create database if not exists, create program's home if not exists, etc... - dictionary::import(&String::from("./reparsed.txt")); // + dictionary::import(&String::from("./dictionary.txt")); // Read file, parse it, deduplicate and append to the database. let app: Application = Application::builder().application_id(APP_ID).build(); diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs index 5ec30a8..c41f0ba 100644 --- a/src/ui/cards/edit/imp.rs +++ b/src/ui/cards/edit/imp.rs @@ -80,7 +80,7 @@ impl ObjectImpl for MemoryCardsEditScene { "" => None, _ => { let search_option = e.text().to_string(); - let conditions = format!("hieroglyph LIKE '%{}%' OR reading LIKE '%{}%'", search_option, search_option); + let conditions = format!("hieroglyph LIKE '%{}%'", search_option); Some(conditions) } } @@ -108,7 +108,7 @@ impl MemoryCardsEditScene { None => "".to_owned() }; - let sql = format!("SELECT imagename, hieroglyph, reading FROM cards {selector}"); + let sql = format!("SELECT imagename, hieroglyph, reading, translation FROM cards {selector}"); let mut stmt = conn .prepare(sql.as_str()) .unwrap(); @@ -118,7 +118,9 @@ impl MemoryCardsEditScene { let image_path: String = get_images_store_path() + &image_path; let hieroglyph: String = row.get(1).unwrap(); let reading: String = row.get(2).unwrap(); - let entry = CardEntry::new(&image_path, &hieroglyph, &reading); + let hieroglyph = format!("{} ({})", hieroglyph, reading); + let translation: String = row.get(3).unwrap(); + let entry = CardEntry::new(&image_path, &hieroglyph, &translation); Ok(entry) }) .unwrap(); @@ -188,10 +190,11 @@ async fn file_choose_dialog>(window: Rc) { let hieroglyph = w.get_hieroglyph_input(); let reading = w.get_reading_input(); + let translation = w.get_translation_input(); let conn = Connection::open(get_db_path()).unwrap(); - let query = "INSERT INTO cards (imagename, hieroglyph, reading) VALUES(?1, ?2, ?3)"; - conn.execute(query, (&new_filename, &hieroglyph, &reading)).unwrap(); + let query = "INSERT INTO cards (imagename, hieroglyph, reading, translation) VALUES(?1, ?2, ?3, ?4)"; + conn.execute(query, (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); w.close(); }), ); diff --git a/src/ui/cards/new/imp.rs b/src/ui/cards/new/imp.rs index 6cb1c97..ab67fb7 100644 --- a/src/ui/cards/new/imp.rs +++ b/src/ui/cards/new/imp.rs @@ -14,6 +14,8 @@ pub struct MemoryCardsNewScene { #[template_child] pub reading_entry: TemplateChild, #[template_child] + pub translation_entry: TemplateChild, + #[template_child] pub done_button: TemplateChild