Automatic enable card on adding image

This commit is contained in:
leca 2024-04-18 23:19:43 +03:00
parent 9fa5524c9d
commit 8e2a13aa0f
3 changed files with 28 additions and 17 deletions

View File

@ -1,3 +0,0 @@
pub struct Card {
}

View File

@ -1,8 +1,5 @@
/* /*
TODOS: TODOS:
1. remake db scheme for https://raw.githubusercontent.com/schneems/united-dictionary/master/public/languages/japanese/japanese-english.txt
2. parse it
3. add support for it
4. With incorrect answerd card, it shows hieroglyp, reading ant its translation 4. With incorrect answerd card, it shows hieroglyp, reading ant its translation
*/ */

View File

@ -4,10 +4,10 @@ use std::io::ErrorKind;
use std::path::Path; use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use crate::card::Card;
use crate::db::*; 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 crate::card::Card;
use glib::subclass::InitializingObject; use glib::subclass::InitializingObject;
use gtk::glib::object::ObjectExt; use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local}; use gtk::glib::{clone, closure_local};
@ -109,8 +109,9 @@ impl MemoryCardsEditScene {
None => "".to_owned(), None => "".to_owned(),
}; };
let sql = let sql = format!(
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 cards_iter = stmt let cards_iter = stmt
.query_map([], |row| { .query_map([], |row| {
@ -123,7 +124,7 @@ impl MemoryCardsEditScene {
Some(row.get(1).unwrap()), Some(row.get(1).unwrap()),
Some(row.get(2).unwrap()), Some(row.get(2).unwrap()),
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); let entry = CardEntry::new(&c);
entry.update_state(); entry.update_state();
@ -198,6 +199,7 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
let path: String = path.as_path().to_str().unwrap().to_owned(); let path: String = path.as_path().to_str().unwrap().to_owned();
picture_widget.set_file(Some(&gio::File::for_path(&path))); picture_widget.set_file(Some(&gio::File::for_path(&path)));
println!("Image path has changed");
let images_store_path = get_program_home_path() + "/images"; let images_store_path = get_program_home_path() + "/images";
@ -223,10 +225,25 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
let translation = w.get_translation_input(); let translation = w.get_translation_input();
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
let query = "INSERT OR REPLACE INTO cards (id, imagename, hieroglyph, reading, translation, is_learning) VALUES((SELECT id FROM cards WHERE hieroglyph = ?2), ?1, ?2, ?3, ?4, (SELECT is_learning FROM cards WHERE hieroglyph = ?2))"; conn.execute("INSERT OR REPLACE INTO cards ( id,
conn.execute(query, (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); imagename,
println!("new imagepath: {new_filename}"); hieroglyph,
reading,
translation,
is_learning
) VALUES
(
(SELECT id FROM cards WHERE hieroglyph = ?2),
?1,
?2,
?3,
?4,
(SELECT is_learning FROM cards WHERE hieroglyph = ?2
)
)", (&new_filename, &hieroglyph, &reading, &translation)).unwrap();
conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap();
w.close(); w.close();
}), }),
); );
} }