diff --git a/resources/cards/edit/ui.xml b/resources/cards/edit/ui.xml index 06143de..e3d2dc4 100644 --- a/resources/cards/edit/ui.xml +++ b/resources/cards/edit/ui.xml @@ -37,7 +37,7 @@ GTK_ALIGN_FILL true - + vertical GTK_ALIGN_FILL GTK_ALIGN_FILL diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs index 005b83e..133f735 100644 --- a/src/ui/cards/edit/imp.rs +++ b/src/ui/cards/edit/imp.rs @@ -1,3 +1,4 @@ +use std::cell::RefCell; use std::fs; use std::io::ErrorKind; use std::path::Path; @@ -24,9 +25,11 @@ pub struct MemoryCardsEditScene { #[template_child] pub add_button: TemplateChild, #[template_child] - pub cards: TemplateChild, + pub cards_container: TemplateChild, #[template_child] pub cards_scrolled_window: TemplateChild, + + displaying_cards: RefCell> } #[glib::object_subclass] @@ -48,6 +51,9 @@ impl ObjectImpl for MemoryCardsEditScene { fn constructed(&self) { self.parent_constructed(); + self.query_cards(None); + self.update_card_list(); + let binding = self.obj(); self.add_button.connect_closure("clicked", @@ -58,26 +64,50 @@ impl ObjectImpl for MemoryCardsEditScene { gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win))); })); new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| { + binding.imp().query_cards(None); binding.imp().update_card_list(); })); new_win.present(); })); + + self.search_entry.connect_closure("changed", false, closure_local!(@strong binding => move |e: &SearchEntry| { + binding.imp().query_cards( + match e.text().as_str() { + "" => None, + _ => { + let search_option = e.text().to_string(); + let conditions = format!("hieroglyph LIKE '%{}%' OR reading LIKE '%{}%'", search_option, search_option); + Some(conditions) + } + } + ); + binding.imp().update_card_list(); + })); self.update_card_list(); } } impl MemoryCardsEditScene { - pub fn update_card_list(&self) { - let c: &Box = self.cards.as_ref(); + pub fn clear_displaying_card(&self) { + let c: &Box = self.cards_container.as_ref(); while c.first_child() != None { c.remove(&c.first_child().unwrap()); } + } + pub fn query_cards(&self, options: Option) { let conn = Connection::open(get_db_path()).unwrap(); + + let selector = match options { + Some(s) => "WHERE ".to_owned() + &s, + None => "".to_owned() + }; + + let sql = format!("SELECT imagename, hieroglyph, reading FROM cards {selector}"); let mut stmt = conn - .prepare("SELECT imagename, hieroglyph, reading FROM cards") + .prepare(sql.as_str()) .unwrap(); let cards_iter = stmt .query_map([], |row| { @@ -89,21 +119,31 @@ impl MemoryCardsEditScene { Ok(entry) }) .unwrap(); - + self.displaying_cards.borrow_mut().clear(); for card in cards_iter { let card_binding = card.unwrap(); - let self_binding = self.obj(); - c.append(&card_binding); + self.displaying_cards.borrow_mut().push(card_binding); + } + } - card_binding.get_delete_button_widget().connect_closure("clicked", false, closure_local!(@strong card_binding, @strong self_binding => move |_b: &Button| { + pub fn update_card_list(&self) { + let c: &Box = self.cards_container.as_ref(); + + self.clear_displaying_card(); + + for card in self.displaying_cards.borrow().iter() { + let self_binding = self.obj(); + c.append(card); + + card.get_delete_button_widget().connect_closure("clicked", false, closure_local!(@strong card, @strong self_binding => move |_b: &Button| { let connection = Connection::open(get_db_path()).unwrap(); - let imagepath = card_binding.imagepath(); + let imagepath = card.imagepath(); let imagename = &Path::new(&imagepath).file_name().unwrap().to_str().unwrap(); fs::remove_file(get_images_store_path() + "/" + &imagename).expect("Could not remove file!"); connection.execute("DELETE FROM cards WHERE imagename = ?1", [&imagename]).unwrap(); self_binding.imp().update_card_list(); })); - card_binding.update_file_for_picture(); + card.update_file_for_picture(); } } }