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::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,10 +62,24 @@ 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();
|
||||||
|
|
||||||
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,
|
false,
|
||||||
closure_local!(@strong binding => move |_b: &Button| {
|
closure_local!(@strong binding => move |_b: &Button| {
|
||||||
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
|
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| {
|
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.
|
// Put query to the separate thread so that cards load on fly.
|
||||||
// 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| {
|
||||||
|
@ -151,31 +176,29 @@ impl MemoryCardsEditScene {
|
||||||
let translation = w.get_translation_input();
|
let translation = w.get_translation_input();
|
||||||
let imagepath = w.get_picture_widget().file();
|
let imagepath = w.get_picture_widget().file();
|
||||||
conn.execute("INSERT OR REPLACE INTO cards ( id,
|
conn.execute("INSERT OR REPLACE INTO cards ( id,
|
||||||
hieroglyph,
|
hieroglyph,
|
||||||
reading,
|
reading,
|
||||||
translation,
|
translation,
|
||||||
is_learning,
|
is_learning,
|
||||||
imagename
|
imagename
|
||||||
) VALUES (
|
) VALUES (
|
||||||
(SELECT id FROM cards WHERE hieroglyph = ?1),
|
(SELECT id FROM cards WHERE hieroglyph = ?1),
|
||||||
?1,
|
?1,
|
||||||
?2,
|
?2,
|
||||||
?3,
|
?3,
|
||||||
(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();
|
||||||
w.close();
|
w.close();
|
||||||
}));
|
}));
|
||||||
new_win.present();
|
new_win.present();
|
||||||
|
@ -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,39 +240,60 @@ impl MemoryCardsEditScene {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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 {
|
fn p(options: Option<String>, cards: &Arc<Mutex<RefCell<Vec<CardEntry>>>>) {
|
||||||
Some(s) => "WHERE ".to_owned() + &s,
|
let conn = Connection::open(get_db_path()).unwrap();
|
||||||
None => "".to_owned(),
|
|
||||||
|
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 c = Card::new(
|
||||||
let sql = format!(
|
Some(get_images_store_path() + &image_path),
|
||||||
"SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}"
|
Some(row.get(1).unwrap()),
|
||||||
);
|
Some(row.get(2).unwrap()),
|
||||||
let mut stmt = conn.prepare(sql.as_str()).unwrap();
|
Some(row.get(3).unwrap()),
|
||||||
let cards_iter = stmt
|
Some(row.get(4).unwrap()),
|
||||||
.query_map([], |row| {
|
);
|
||||||
let image_path: String = match row.get(0) {
|
let entry = CardEntry::new(Some(&c));
|
||||||
Ok(path) => path,
|
entry.update_state();
|
||||||
Err(_) => String::from(""),
|
Ok(entry)
|
||||||
};
|
})
|
||||||
let c = Card::new(
|
.unwrap();
|
||||||
Some(get_images_store_path() + &image_path),
|
for c in cards_iter {
|
||||||
Some(row.get(1).unwrap()),
|
cards.lock().unwrap().borrow_mut().push(c.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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue