search now works

This commit is contained in:
leca 2024-04-08 02:18:55 +03:00
parent 857613a01d
commit 5f768be350
2 changed files with 51 additions and 11 deletions

View File

@ -37,7 +37,7 @@
<property name="valign">GTK_ALIGN_FILL</property>
<property name="vexpand">true</property>
<child>
<object class="GtkBox" id="cards">
<object class="GtkBox" id="cards_container">
<property name="orientation">vertical</property>
<property name="halign">GTK_ALIGN_FILL</property>
<property name="valign">GTK_ALIGN_FILL</property>

View File

@ -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<Button>,
#[template_child]
pub cards: TemplateChild<Box>,
pub cards_container: TemplateChild<Box>,
#[template_child]
pub cards_scrolled_window: TemplateChild<ScrolledWindow>,
displaying_cards: RefCell<Vec<CardEntry>>
}
#[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<String>) {
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();
}
}
}