refactoring for optimization

This commit is contained in:
leca 2024-04-22 12:55:43 +03:00
parent 8e2a13aa0f
commit 67ce4e9b36
5 changed files with 153 additions and 68 deletions

View File

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

View File

@ -1,3 +1,4 @@
#[derive(Debug)]
pub struct Card { pub struct Card {
image_path: Option<String>, image_path: Option<String>,
hieroglyph: Option<String>, hieroglyph: Option<String>,

View File

@ -9,6 +9,7 @@ mod ui;
mod widgets; mod widgets;
mod dictionary; mod dictionary;
mod card; mod card;
// mod card_object;
use crate::ui::menu::MenuScene; use crate::ui::menu::MenuScene;

View File

@ -9,13 +9,19 @@ use crate::db::*;
use crate::ui::cards::new::*; use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry; use crate::widgets::card_entry::CardEntry;
use glib::subclass::InitializingObject; use glib::subclass::InitializingObject;
use gtk::gio::ListStore;
// use gtk::glib::ffi::GString;
use gtk::glib::GString;
use gtk::glib::object::ObjectExt; use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local}; use gtk::glib::{clone, closure_local};
use gtk::prelude::WidgetExt; use gtk::prelude::WidgetExt;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use gtk::ListBox; use gtk::{
use gtk::{gio, glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window}; gio, glib, Button, CompositeTemplate, ListView, NoSelection, ScrolledWindow, SearchEntry,
Window,
};
use gtk::{prelude::*, FileDialog}; use gtk::{prelude::*, FileDialog};
use gtk::{ListBox, ListItem, SignalListItemFactory};
use rusqlite::Connection; use rusqlite::Connection;
use sha256::try_digest; use sha256::try_digest;
@ -27,7 +33,7 @@ pub struct MemoryCardsEditScene {
#[template_child] #[template_child]
pub add_button: TemplateChild<Button>, pub add_button: TemplateChild<Button>,
#[template_child] #[template_child]
pub cards_container: TemplateChild<ListBox>, pub cards_container: TemplateChild<ListView>,
#[template_child] #[template_child]
pub cards_scrolled_window: TemplateChild<ScrolledWindow>, pub cards_scrolled_window: TemplateChild<ScrolledWindow>,
#[template_child] #[template_child]
@ -55,8 +61,8 @@ impl ObjectImpl for MemoryCardsEditScene {
fn constructed(&self) { fn constructed(&self) {
self.parent_constructed(); self.parent_constructed();
self.query_cards(None); // self.query_cards(None);
self.update_card_list(); // self.update_card_list();
let binding = self.obj(); let binding = self.obj();
@ -70,7 +76,7 @@ impl ObjectImpl for MemoryCardsEditScene {
})); }));
new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| { new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| {
binding.imp().query_cards(None); binding.imp().query_cards(None);
binding.imp().update_card_list(); // binding.imp().update_card_list();
})); }));
new_win.present(); new_win.present();
})); }));
@ -86,20 +92,21 @@ impl ObjectImpl for MemoryCardsEditScene {
} }
} }
); );
binding.imp().update_card_list(); // binding.imp().update_card_list();
})); }));
self.query_cards(None);
// self.update_card_list(); // self.update_card_list();
} }
} }
impl MemoryCardsEditScene { impl MemoryCardsEditScene {
pub fn clear_displaying_card(&self) { // pub fn clear_displaying_card(&self) {
let c: &ListBox = self.cards_container.as_ref(); // let c: &ListBox = self.cards_container.as_ref();
while c.first_child() != None { // while c.first_child() != None {
c.remove(&c.first_child().unwrap()); // c.remove(&c.first_child().unwrap());
} // }
} // }
pub fn query_cards(&self, options: Option<String>) { pub fn query_cards(&self, options: Option<String>) {
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
@ -126,59 +133,108 @@ impl MemoryCardsEditScene {
Some(row.get(3).unwrap()), Some(row.get(3).unwrap()),
Some(row.get(4).unwrap()), Some(row.get(4).unwrap()),
); );
let entry = CardEntry::new(&c); // println!("{c:?} ");
let entry = CardEntry::new(Some(&c));
entry.update_state(); entry.update_state();
Ok(entry) Ok(entry)
}) })
.unwrap(); .unwrap();
self.displaying_cards.borrow_mut().clear(); self.displaying_cards.borrow_mut().clear();
for card in cards_iter {
let card_binding = card.unwrap(); for c in cards_iter {
self.displaying_cards.borrow_mut().push(card_binding); self.displaying_cards.borrow_mut().push(c.unwrap());
} }
let model = ListStore::new::<CardEntry>();
model.extend_from_slice(&*self.displaying_cards.borrow());
let factory = SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| {
let card_entry = CardEntry::new(None);
list_item
.downcast_ref::<ListItem>()
.unwrap()
.set_child(Some(&card_entry));
});
factory.connect_bind(move |_, list_item| {
let card_object = &list_item
.downcast_ref::<ListItem>()
.unwrap()
.item()
.and_downcast::<CardEntry>()
.unwrap();
let card_object_to_display = list_item
.downcast_ref::<ListItem>()
.unwrap()
.child()
.and_downcast::<CardEntry>()
.unwrap();
// card_object_to_display.get_image_widget().set_file(Some(
// // &card_object.get_image_widget().file().unwrap()
// match card_object.get_image_widget().file() {
// None => "",
// Some(t) => {
// t.to_string().to_owned().as_str()
// }
// }
// ));
card_object_to_display.set_hieroglyph(card_object.hieroglyph());
card_object_to_display.set_reading(card_object.reading());
card_object_to_display.set_translation(card_object.translation());
card_object_to_display.set_islearning(card_object.islearning());
});
println!("test");
let no_selection_model = NoSelection::new(Some(model));
let list_view = ListView::new(Some(no_selection_model), Some(factory));
self.cards_scrolled_window.set_child(Some(&list_view));
} }
pub fn update_card_list(&self) { // pub fn update_card_list(&self) {
let c: &ListBox = self.cards_container.as_ref(); // let c: &ListBox = self.cards_container.as_ref();
self.clear_displaying_card(); // self.clear_displaying_card();
for card in self.displaying_cards.borrow().iter() { // for card in self.displaying_cards.borrow().iter() {
let self_binding = self.obj(); // let self_binding = self.obj();
c.append(card); // c.append(card);
card.get_delete_button_widget().connect_closure("clicked", false, closure_local!(@strong card, @strong self_binding => move |_b: &Button| { // 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 connection = Connection::open(get_db_path()).unwrap();
let imagepath = &card.imagepath(); // let imagepath = &card.imagepath();
let imagename = &Path::new(&imagepath).file_name().unwrap().to_str().unwrap(); // let imagename = &Path::new(&imagepath).file_name().unwrap().to_str().unwrap();
fs::remove_file(get_images_store_path() + "/" + &imagename).expect("Could not remove file!"); // fs::remove_file(get_images_store_path() + "/" + &imagename).expect("Could not remove file!");
connection.execute("DELETE FROM cards WHERE imagename = ?1", [&imagename]).unwrap(); // connection.execute("DELETE FROM cards WHERE imagename = ?1", [&imagename]).unwrap();
self_binding.imp().query_cards(None); // self_binding.imp().query_cards(None);
self_binding.imp().update_card_list(); // self_binding.imp().update_card_list();
})); // }));
card.get_edit_button_widget().connect_closure("clicked", false, closure_local!(@strong self_binding as binding, @strong card => move |_b: &Button| { // card.get_edit_button_widget().connect_closure("clicked", false, closure_local!(@strong self_binding as binding, @strong card => move |_b: &Button| {
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap())); // let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
//setting corresponding properties // //setting corresponding properties
new_win.get_picture_widget().set_file(Some(&gio::File::for_path(card.imagepath()))); // new_win.get_picture_widget().set_file(Some(&gio::File::for_path(card.imagepath())));
new_win.get_hieroglyph_entry().set_text(&card.hieroglyph()); // new_win.get_hieroglyph_entry().set_text(&card.hieroglyph());
new_win.get_reading_entry().set_text(&card.reading()); // new_win.get_reading_entry().set_text(&card.reading());
new_win.get_translation_entry().set_text(&card.translation()); // new_win.get_translation_entry().set_text(&card.translation());
new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win, @strong card => move |b: &Button| { // new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win, @strong card => move |b: &Button| {
b.set_visible(false); // b.set_visible(false);
gtk::glib::MainContext::default().spawn_local(new_card_setup(Rc::clone(&new_win))); // 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 => move |_w: &Button| { // new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| {
binding.imp().query_cards(None); // binding.imp().query_cards(None);
binding.imp().update_card_list(); // binding.imp().update_card_list();
})); // }));
new_win.present(); // new_win.present();
})); // }));
card.update_file_for_image(); // card.update_file_for_image();
} // }
} // }
} }
async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) { async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {

View File

@ -18,14 +18,23 @@ glib::wrapper! {
} }
impl CardEntry { impl CardEntry {
pub fn new(card: &Card) -> Self { pub fn new(card: Option<&Card>) -> Self {
Object::builder() match card {
.property("imagepath", card.image_path().unwrap()) Some(card) => Object::builder()
.property("hieroglyph", card.hieroglyph().unwrap()) .property("imagepath", card.image_path().unwrap())
.property("reading", card.reading().unwrap()) .property("hieroglyph", card.hieroglyph().unwrap())
.property("translation", card.translation().unwrap()) .property("reading", card.reading().unwrap())
.property("islearning", card.is_learning().unwrap()) .property("translation", card.translation().unwrap())
.build() .property("islearning", card.is_learning().unwrap())
.build(),
None => Object::builder()
.property("imagepath", "")
.property("hieroglyph", "")
.property("reading", "")
.property("translation", "")
.property("islearning", false)
.build(),
}
} }
pub fn get_image_widget(&self) -> &Image { pub fn get_image_widget(&self) -> &Image {
@ -46,20 +55,38 @@ impl CardEntry {
image_binding.set_file(Some(&path)); image_binding.set_file(Some(&path));
} }
pub fn get_hieroglyph(&self) -> String { // pub fn get_hieroglyph(&self) -> String {
self.imp().hieroglyph.borrow().clone() // self.imp().hieroglyph.borrow().clone()
} // }
// pub fn set_hieroglyph(&self, h: &String) {
// self.imp().hieroglyph.borrow_mut() = h;
// }
// pub fn get_reading(&self) -> String {
// self.imp().reading.borrow().clone()
// }
// pub fn set_reading(&self, r: &String) {
// self.imp().reading.borrow_mut() = r;
// }
// pub fn get_translation(&self) -> String {
// self.imp().translation.borrow().clone()
// }
// pub fn set_translation(&self, t: &String) {
// self.imp().translation.borrow_mut() = t;
// }
pub fn update_state(&self) { pub fn update_state(&self) {
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
let is_learning_switch_binding: &Switch = self.imp().is_learning_switch.as_ref(); let is_learning_switch_binding: &Switch = self.imp().is_learning_switch.as_ref();
let h = &self.get_hieroglyph(); let h = &self.hieroglyph();
let sql = "SELECT is_learning FROM cards WHERE hieroglyph=:h;"; let sql = "SELECT is_learning FROM cards WHERE hieroglyph=:h;";
let mut stmt = conn.prepare(sql).unwrap(); let mut stmt = conn.prepare(sql).unwrap();
let iter = stmt let iter = stmt
.query_map(&[(":h", h.as_str())], |row| { .query_map(&[(":h", h.as_str())], |row| Ok(row.get(0).unwrap()))
Ok(row.get(0).unwrap())
})
.unwrap(); .unwrap();
for res in iter { for res in iter {