From 8207a76a2ffd67aee6f47d3b5637249b78cd65b9 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 13 May 2024 23:59:21 +0300 Subject: [PATCH] some ux improvements --- src/main.rs | 3 +- src/ui/cards/edit/imp.rs | 117 ++++++++++++++++++++++----------------- src/ui/cards/game/imp.rs | 7 ++- src/ui/cards/game/mod.rs | 5 ++ 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/main.rs b/src/main.rs index 67c890b..457c046 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,7 +125,7 @@ fn build_ui(app: &Application) { @strong app, @strong memory_cards_edit => move |_b: &Button| { switch_to(&app, AppWindow::MemoryCardsEditScene); memory_cards_edit.imp().query_cards(None); - memory_cards_edit.imp().update_state(); + // memory_cards_edit.imp().update_state(); } ), ); @@ -147,6 +147,7 @@ fn build_ui(app: &Application) { @strong app, @strong memory_cards_game => move |_b: &Button| { switch_to(&app, AppWindow::MemoryCardsGameScene); memory_cards_game.imp().update_card(); + // memory_cards_game.imp().u } ), ); diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs index 5a95199..4ef4712 100644 --- a/src/ui/cards/edit/imp.rs +++ b/src/ui/cards/edit/imp.rs @@ -72,7 +72,7 @@ impl ObjectImpl for MemoryCardsEditScene { })); new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| { binding.imp().query_cards(None); - binding.imp().update_state(); + binding.imp().build_factory(); })); new_win.present(); })); @@ -88,14 +88,15 @@ impl ObjectImpl for MemoryCardsEditScene { } } ); - binding.imp().update_state(); + // binding.imp().update_state(); })); - self.update_state(); + self.query_cards(None); + self.build_factory(); } } impl MemoryCardsEditScene { - pub fn update_state(&self) { + pub fn build_factory(&self) { let model = ListStore::new::(); model.extend_from_slice(&self.displaying_cards.borrow()); @@ -137,7 +138,7 @@ impl MemoryCardsEditScene { b.set_visible(false); gtk::glib::MainContext::default().spawn_local(new_card_setup(Rc::clone(&new_win))); })); - new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding, @strong new_win as w, @strong card => move |_w: &Button| { + new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong new_win as w, @strong card => move |_w: &Button| { let conn = Connection::open(get_db_path()).unwrap(); let hieroglyph = w.get_hieroglyph_input(); @@ -166,7 +167,6 @@ impl MemoryCardsEditScene { card.set_reading(reading); card.set_translation(translation); - binding.imp().update_state(); w.close(); })); new_win.present(); @@ -191,7 +191,7 @@ impl MemoryCardsEditScene { connection.execute("DELETE FROM cards WHERE hieroglyph = ?1", [&card.hieroglyph()]).unwrap(); let position = self_binding.imp().displaying_cards.borrow().iter().position(|c| *c == card).unwrap(); self_binding.imp().displaying_cards.borrow_mut().remove(position); - self_binding.imp().update_state(); + // self_binding.imp().update_state(); })); card_gobject.bind_property("imagepath", card_object_to_display.get_image_widget(), "file").sync_create().build(); @@ -222,16 +222,12 @@ impl MemoryCardsEditScene { let cards_iter = stmt .query_map([], |row| { - let image_path: String = match row.get(0) { - Ok(path) => path, - Err(_) => String::from(""), - }; let c = Card::new( - Some(get_images_store_path() + &image_path), - Some(row.get(1).unwrap()), - Some(row.get(2).unwrap()), - Some(row.get(3).unwrap()), - Some(row.get(4).unwrap()), + Some(get_images_store_path() + &row.get(0).unwrap_or(String::from(""))), + Some(row.get(1).unwrap_or(String::from(""))), + Some(row.get(2).unwrap_or(String::from(""))), + Some(row.get(3).unwrap_or(String::from(""))), + Some(row.get(4).unwrap_or(false)), ); let entry = CardGObject::new(Some(c)); Ok(entry) @@ -261,47 +257,66 @@ async fn new_card_setup>(window: Rc) { ); let path = RefCell::new(String::from("")); - dialog.run_async(clone!( @strong path, @strong w, @strong picture_widget => move |a, _b| { - let c = a.file().unwrap() - .path().unwrap() - .as_os_str().to_owned() - .to_str().unwrap() - .to_string(); // 0_0 - *path.borrow_mut() = c; - a.close(); + dialog.run_async( + clone!( @strong path, @strong w, @strong picture_widget => move |a, _b| { + let c = a.file().unwrap() + .path().unwrap() + .as_os_str().to_owned() + .to_str().unwrap() + .to_string(); // 0_0 + *path.borrow_mut() = c; + a.close(); - picture_widget.set_file(Some(&gio::File::for_path(Path::new(path.borrow().as_str())))); + picture_widget.set_file(Some(&gio::File::for_path(Path::new(path.borrow().as_str())))); - let images_store_path = get_program_home_path() + "/images"; + let images_store_path = get_program_home_path() + "/images"; - match fs::create_dir_all(&images_store_path) { - Ok(_) => {} - Err(error) => match error.kind() { - ErrorKind::AlreadyExists => {} - _ => panic!("Could not create directory for storing images!"), - }, - }; + match fs::create_dir_all(&images_store_path) { + Ok(_) => {} + Err(error) => match error.kind() { + ErrorKind::AlreadyExists => {} + _ => panic!("Could not create directory for storing images!"), + }, + }; - w.get_done_button().connect_closure( - "clicked", - false, - closure_local!(@strong w, @strong path => move |_b: &Button| { - let p = path.borrow(); - let p = p.as_str(); - let hash = try_digest(path.borrow().as_str()).unwrap(); - let extenstion = Path::new(p).extension().unwrap().to_str().unwrap(); - let new_filename: String = hash.as_str().to_owned() + "." + extenstion; - let stored_image_path = Path::new(&images_store_path).join(&new_filename); - fs::copy(&p, stored_image_path).expect("Error copying image to store"); + w.get_done_button().connect_closure( + "clicked", + false, + closure_local!(@strong w, @strong path => move |_b: &Button| { + let p = path.borrow(); + let p = p.as_str(); + let hash = try_digest(path.borrow().as_str()).unwrap(); + let extenstion = Path::new(p).extension().unwrap().to_str().unwrap(); + let new_filename: String = hash.as_str().to_owned() + "." + extenstion; + let stored_image_path = Path::new(&images_store_path).join(&new_filename); + fs::copy(&p, stored_image_path).expect("Error copying image to store"); - let hieroglyph = w.get_hieroglyph_input(); - let conn = Connection::open(get_db_path()).unwrap(); + 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(); - conn.execute("UPDATE cards SET imagename = ?1 WHERE hieroglyph = ?2", (&new_filename, &hieroglyph)).unwrap(); - conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap(); - }), - ); - })); + conn.execute("INSERT OR REPLACE INTO cards ( id, + imagename, + hieroglyph, + reading, + translation, + is_learning + ) VALUES ( + (SELECT id FROM cards WHERE hieroglyph = ?2), + ?1, + ?2, + ?3, + ?4, + (SELECT is_learning FROM cards WHERE hieroglyph = ?2 OR TRUE + ) + )", (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); + w.close(); + }), + ); + + }), + ); } impl WidgetImpl for MemoryCardsEditScene {} diff --git a/src/ui/cards/game/imp.rs b/src/ui/cards/game/imp.rs index b2d6d1d..f1c81a5 100644 --- a/src/ui/cards/game/imp.rs +++ b/src/ui/cards/game/imp.rs @@ -81,6 +81,7 @@ impl MemoryCardsGameScene { self.card_display.set_reading(card.reading()); self.card_display.set_translation(card.translation()); self.card_display.update_file_for_image(); + self.obj().hide_no_cards_msg(); } None => self.obj().show_no_cards_msg(), } @@ -98,7 +99,7 @@ fn generate_card() -> Option { row.get(1).unwrap(), row.get(2).unwrap(), row.get(3).unwrap(), - None, + Some(true), )) }) .unwrap(); @@ -111,17 +112,19 @@ fn generate_card() -> Option { Some(card) => card, None => return None, }; + let generated_card = CardDisplay::new( &(get_images_store_path() + "/" + match &random_card.image_path() { - Some(path) => &path[..], + Some(path) => path, None => return None, }), &random_card.hieroglyph().unwrap(), Some(&random_card.reading().unwrap()), &random_card.translation().unwrap(), ); + Some(generated_card) } diff --git a/src/ui/cards/game/mod.rs b/src/ui/cards/game/mod.rs index 34b1827..9c3b54f 100644 --- a/src/ui/cards/game/mod.rs +++ b/src/ui/cards/game/mod.rs @@ -37,4 +37,9 @@ impl MemoryCardsGameScene { self.imp().stats_label.set_visible(false); self.imp().no_cards_label.set_visible(true); } + pub fn hide_no_cards_msg(&self) { + self.imp().card_display.set_visible(true); + self.imp().stats_label.set_visible(true); + self.imp().no_cards_label.set_visible(false); + } }