detaching card pushing process
This commit is contained in:
parent
f50b4cae8b
commit
a16685d009
|
@ -1,8 +1,12 @@
|
|||
use std::borrow::Borrow;
|
||||
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::path::Path;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::card::Card;
|
||||
use crate::db::*;
|
||||
|
@ -20,7 +24,7 @@ use gtk::{
|
|||
};
|
||||
use gtk::{prelude::*, FileDialog};
|
||||
use gtk::{ListItem, SignalListItemFactory};
|
||||
use rusqlite::Connection;
|
||||
use rusqlite::{Connection, MappedRows, Statement};
|
||||
use sha256::try_digest;
|
||||
|
||||
#[derive(CompositeTemplate, Default)]
|
||||
|
@ -37,7 +41,7 @@ pub struct MemoryCardsEditScene {
|
|||
#[template_child]
|
||||
pub back_button: TemplateChild<Button>,
|
||||
|
||||
displaying_cards: RefCell<Vec<CardEntry>>,
|
||||
displaying_cards: Arc<Mutex<RefCell<Vec<CardEntry>>>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
@ -58,10 +62,24 @@ impl ObjectSubclass for MemoryCardsEditScene {
|
|||
impl ObjectImpl for MemoryCardsEditScene {
|
||||
fn constructed(&self) {
|
||||
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();
|
||||
|
||||
self.add_button.connect_closure("clicked",
|
||||
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",
|
||||
false,
|
||||
closure_local!(@strong binding => move |_b: &Button| {
|
||||
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
|
||||
|
@ -78,11 +96,18 @@ impl ObjectImpl for MemoryCardsEditScene {
|
|||
}));
|
||||
|
||||
self.search_entry.connect_closure("activate", false, closure_local!(@strong binding => move |e: &SearchEntry| {
|
||||
|
||||
|
||||
// Put query to the separate thread so that cards load on fly.
|
||||
// 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(
|
||||
match e.text().as_str() {
|
||||
"" => None,
|
||||
|
@ -103,7 +128,7 @@ impl ObjectImpl for MemoryCardsEditScene {
|
|||
impl MemoryCardsEditScene {
|
||||
pub fn update_state(&self) {
|
||||
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();
|
||||
factory.connect_setup(move |_, list_item| {
|
||||
|
@ -151,31 +176,29 @@ impl MemoryCardsEditScene {
|
|||
let translation = w.get_translation_input();
|
||||
let imagepath = w.get_picture_widget().file();
|
||||
conn.execute("INSERT OR REPLACE INTO cards ( id,
|
||||
hieroglyph,
|
||||
reading,
|
||||
translation,
|
||||
hieroglyph,
|
||||
reading,
|
||||
translation,
|
||||
is_learning,
|
||||
imagename
|
||||
) VALUES (
|
||||
(SELECT id FROM cards WHERE hieroglyph = ?1),
|
||||
?1,
|
||||
?2,
|
||||
?3,
|
||||
?1,
|
||||
?2,
|
||||
?3,
|
||||
(SELECT is_learning FROM cards WHERE hieroglyph = ?1),
|
||||
(SELECT imagename FROM cards WHERE hieroglyph = ?1)
|
||||
)", (&hieroglyph, &reading, &translation)).unwrap();
|
||||
let b = imagepath.unwrap().path().unwrap();
|
||||
let a = b.to_str();
|
||||
println!("a {}", a.unwrap());
|
||||
card.set_imagepath(a.unwrap());
|
||||
// card.get_image_widget().set_file(a);
|
||||
let imagepath = imagepath.unwrap().path().unwrap();
|
||||
let imagepath = imagepath.to_str();
|
||||
|
||||
card.set_imagepath(imagepath.unwrap());
|
||||
card.set_hieroglyph(hieroglyph);
|
||||
card.set_reading(reading);
|
||||
card.set_translation(translation);
|
||||
// println!("{}", card.get_image_widget().file().unwrap().to_string());
|
||||
card.update_file_for_image();
|
||||
|
||||
binding.imp().update_state();
|
||||
binding.imp().update_state();
|
||||
w.close();
|
||||
}));
|
||||
new_win.present();
|
||||
|
@ -198,8 +221,9 @@ impl MemoryCardsEditScene {
|
|||
Err(_) => ()
|
||||
};
|
||||
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);
|
||||
let displaying_cards_lock = self_binding.imp().displaying_cards.lock().unwrap();
|
||||
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();
|
||||
}));
|
||||
|
||||
|
@ -216,39 +240,60 @@ impl MemoryCardsEditScene {
|
|||
}
|
||||
|
||||
pub fn query_cards(&self, options: Option<String>) {
|
||||
let conn = Connection::open(get_db_path()).unwrap();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
let selector = match options {
|
||||
Some(s) => "WHERE ".to_owned() + &s,
|
||||
None => "".to_owned(),
|
||||
fn p(options: Option<String>, cards: &Arc<Mutex<RefCell<Vec<CardEntry>>>>) {
|
||||
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, translation, is_learning FROM cards {selector}"
|
||||
);
|
||||
|
||||
let mut stmt = conn.prepare(sql.as_str()).unwrap();
|
||||
|
||||
// let mut stmt = stmt.lock().unwrap();
|
||||
let cards_iter = stmt
|
||||
.query_map([], |row| {
|
||||
let image_path: String = match row.get(0) {
|
||||
Ok(path) => path,
|
||||
Err(_) => String::from(""),
|
||||
};
|
||||
|
||||
let sql = format!(
|
||||
"SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}"
|
||||
);
|
||||
let mut stmt = conn.prepare(sql.as_str()).unwrap();
|
||||
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()),
|
||||
);
|
||||
let entry = CardEntry::new(Some(&c));
|
||||
entry.update_state();
|
||||
Ok(entry)
|
||||
})
|
||||
.unwrap();
|
||||
self.displaying_cards.borrow_mut().clear();
|
||||
for c in cards_iter {
|
||||
self.displaying_cards.borrow_mut().push(c.unwrap());
|
||||
}
|
||||
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()),
|
||||
);
|
||||
let entry = CardEntry::new(Some(&c));
|
||||
entry.update_state();
|
||||
Ok(entry)
|
||||
})
|
||||
.unwrap();
|
||||
for c in cards_iter {
|
||||
cards.lock().unwrap().borrow_mut().push(c.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue