detaching card pushing process

This commit is contained in:
leca 2024-04-29 15:25:24 +03:00
parent f50b4cae8b
commit a16685d009
1 changed files with 97 additions and 52 deletions

View File

@ -1,8 +1,12 @@
use std::borrow::Borrow;
use std::cell::RefCell; use std::cell::RefCell;
use std::fs; use std::sync::{Arc, Mutex};
use std::thread::spawn;
use std::{fs, thread};
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::Path; use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration;
use crate::card::Card; use crate::card::Card;
use crate::db::*; use crate::db::*;
@ -20,7 +24,7 @@ use gtk::{
}; };
use gtk::{prelude::*, FileDialog}; use gtk::{prelude::*, FileDialog};
use gtk::{ListItem, SignalListItemFactory}; use gtk::{ListItem, SignalListItemFactory};
use rusqlite::Connection; use rusqlite::{Connection, MappedRows, Statement};
use sha256::try_digest; use sha256::try_digest;
#[derive(CompositeTemplate, Default)] #[derive(CompositeTemplate, Default)]
@ -37,7 +41,7 @@ pub struct MemoryCardsEditScene {
#[template_child] #[template_child]
pub back_button: TemplateChild<Button>, pub back_button: TemplateChild<Button>,
displaying_cards: RefCell<Vec<CardEntry>>, displaying_cards: Arc<Mutex<RefCell<Vec<CardEntry>>>>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -58,9 +62,23 @@ impl ObjectSubclass for MemoryCardsEditScene {
impl ObjectImpl for MemoryCardsEditScene { impl ObjectImpl for MemoryCardsEditScene {
fn constructed(&self) { fn constructed(&self) {
self.parent_constructed(); self.parent_constructed();
// println!("test 1 outside");
// gio::spawn_blocking(|| {
// println!("test 1 inside");
// thread::sleep(Duration::from_secs(1));
// println!("test 2 inside");
// });
// println!("test 2 outside");
let binding = self.obj(); let binding = self.obj();
glib::spawn_future_local(clone!(@weak binding => async move {
println!("Starting query");
binding.imp().query_cards(None);
println!("End query");
}));
self.add_button.connect_closure("clicked", self.add_button.connect_closure("clicked",
false, false,
closure_local!(@strong binding => move |_b: &Button| { closure_local!(@strong binding => move |_b: &Button| {
@ -83,6 +101,13 @@ impl ObjectImpl for MemoryCardsEditScene {
// gio::spawn_blocking(move || { // gio::spawn_blocking(move || {
// }); // });
// gio::spawn_blocking(move || {
// binding.imp().query_cards(None);
// });
// gio::spawn_blocking(clone!(@strong binding => move || {
// binding.imp().query_cards(None);
// }));
binding.imp().query_cards( binding.imp().query_cards(
match e.text().as_str() { match e.text().as_str() {
"" => None, "" => None,
@ -103,7 +128,7 @@ impl ObjectImpl for MemoryCardsEditScene {
impl MemoryCardsEditScene { impl MemoryCardsEditScene {
pub fn update_state(&self) { pub fn update_state(&self) {
let model = ListStore::new::<CardEntry>(); let model = ListStore::new::<CardEntry>();
model.extend_from_slice(&*self.displaying_cards.borrow()); model.extend_from_slice(&*(*self.displaying_cards.lock().unwrap()).borrow());
let factory = SignalListItemFactory::new(); let factory = SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| { factory.connect_setup(move |_, list_item| {
@ -164,15 +189,13 @@ impl MemoryCardsEditScene {
(SELECT is_learning FROM cards WHERE hieroglyph = ?1), (SELECT is_learning FROM cards WHERE hieroglyph = ?1),
(SELECT imagename FROM cards WHERE hieroglyph = ?1) (SELECT imagename FROM cards WHERE hieroglyph = ?1)
)", (&hieroglyph, &reading, &translation)).unwrap(); )", (&hieroglyph, &reading, &translation)).unwrap();
let b = imagepath.unwrap().path().unwrap(); let imagepath = imagepath.unwrap().path().unwrap();
let a = b.to_str(); let imagepath = imagepath.to_str();
println!("a {}", a.unwrap());
card.set_imagepath(a.unwrap()); card.set_imagepath(imagepath.unwrap());
// card.get_image_widget().set_file(a);
card.set_hieroglyph(hieroglyph); card.set_hieroglyph(hieroglyph);
card.set_reading(reading); card.set_reading(reading);
card.set_translation(translation); card.set_translation(translation);
// println!("{}", card.get_image_widget().file().unwrap().to_string());
card.update_file_for_image(); card.update_file_for_image();
binding.imp().update_state(); binding.imp().update_state();
@ -198,8 +221,9 @@ impl MemoryCardsEditScene {
Err(_) => () Err(_) => ()
}; };
connection.execute("DELETE FROM cards WHERE hieroglyph = ?1", [&card.hieroglyph()]).unwrap(); 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(); let displaying_cards_lock = self_binding.imp().displaying_cards.lock().unwrap();
self_binding.imp().displaying_cards.borrow_mut().remove(position); let position = (**displaying_cards_lock.borrow()).borrow().iter().position(|c| *c == card).unwrap();
displaying_cards_lock.borrow_mut().remove(position);
self_binding.imp().update_state(); self_binding.imp().update_state();
})); }));
@ -216,6 +240,26 @@ impl MemoryCardsEditScene {
} }
pub fn query_cards(&self, options: Option<String>) { pub fn query_cards(&self, options: Option<String>) {
let cards = self.displaying_cards.clone();
// let cl = move || {
// spawn(async move {p(&stmt, &self.displaying_cards).await});
p(options, &cards);
// let d = &self.displaying_cards;
// gio::spawn_blocking(|| p(options, d));
// gio::spawn_blocking(||t(options));
// spawn(move || block_on(
// p(options, &cards)
// ));
// gio::spawn_blocking(p(&stmt, &self.displaying_cards));
// cl();
// glib::MainContext::spawn_local(&self, cl);
// gio::spawn_blocking(cl);
}
}
fn p(options: Option<String>, cards: &Arc<Mutex<RefCell<Vec<CardEntry>>>>) {
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
let selector = match options { let selector = match options {
@ -226,7 +270,10 @@ impl MemoryCardsEditScene {
let sql = format!( let sql = format!(
"SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}" "SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}"
); );
let mut stmt = conn.prepare(sql.as_str()).unwrap(); let mut stmt = conn.prepare(sql.as_str()).unwrap();
// let mut stmt = stmt.lock().unwrap();
let cards_iter = stmt let cards_iter = stmt
.query_map([], |row| { .query_map([], |row| {
let image_path: String = match row.get(0) { let image_path: String = match row.get(0) {
@ -245,10 +292,8 @@ impl MemoryCardsEditScene {
Ok(entry) Ok(entry)
}) })
.unwrap(); .unwrap();
self.displaying_cards.borrow_mut().clear();
for c in cards_iter { for c in cards_iter {
self.displaying_cards.borrow_mut().push(c.unwrap()); cards.lock().unwrap().borrow_mut().push(c.unwrap());
}
} }
} }