implement insert to db
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
use rusqlite::Connection;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ui::cards::new::*;
|
||||
use crate::widgets::card_entry::CardEntry;
|
||||
use glib::subclass::InitializingObject;
|
||||
use gtk::glib::object::ObjectExt;
|
||||
use gtk::glib::{clone, closure_local};
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
|
||||
use gtk::{prelude::*, FileDialog};
|
||||
use sqlite;
|
||||
|
||||
use crate::ui::cards::new::*;
|
||||
use crate::widgets::card_entry::CardEntry;
|
||||
use sha256::try_digest;
|
||||
|
||||
#[derive(CompositeTemplate, Default)]
|
||||
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/edit/ui.xml")]
|
||||
@@ -66,13 +67,24 @@ async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
|
||||
let dialog: FileDialog = gtk::FileDialog::builder().build();
|
||||
let answer = dialog.open_future(Some(&*window)).await;
|
||||
let path = answer.unwrap().path().unwrap();
|
||||
let path = path.as_path().to_str();
|
||||
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref()).downcast_ref().unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
|
||||
w.get_image_widget().set_file(path);
|
||||
let path: &str = path.as_path().to_str().unwrap();
|
||||
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref())
|
||||
.downcast_ref()
|
||||
.unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
|
||||
w.get_image_widget().set_file(Some(path));
|
||||
let binding = Path::new(path);
|
||||
let hash = try_digest(binding).unwrap();
|
||||
|
||||
let conn = sqlite::open("test.db").unwrap();
|
||||
w.get_done_button().connect_closure("clicked", false, closure_local!(@strong w => move |_b: &Button| {
|
||||
let hieroglyph = w.get_hieroglyph_input();
|
||||
let reading = w.get_reading_input();
|
||||
let program_home_path = &*crate::db::get_program_home_path().read().unwrap().clone();
|
||||
let conn = Connection::open(PathBuf::new().join(program_home_path).join("db.sqlite")).unwrap();
|
||||
|
||||
conn.execute("CREATE TABLE test(field_one TEXT, field_two INTEGER); INSERT INTO test VALUES ('Suka', 15);").unwrap();
|
||||
let query = "INSERT INTO cards (image_hash, hieroglyph, translation) VALUES(?1, ?2, ?3)";
|
||||
conn.execute(query, (&hash, &hieroglyph, &reading)).unwrap();
|
||||
w.close();
|
||||
}));
|
||||
}
|
||||
|
||||
impl WidgetImpl for MemoryCardsEditScene {}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct MemoryCardsNewScene {
|
||||
#[template_child]
|
||||
pub heiroglyph_entry: TemplateChild<Entry>,
|
||||
#[template_child]
|
||||
pub translation_entry: TemplateChild<Entry>,
|
||||
pub reading_entry: TemplateChild<Entry>,
|
||||
#[template_child]
|
||||
pub done_button: TemplateChild<Button>,
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ mod imp;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{
|
||||
gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button, Image
|
||||
gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button, Image, prelude::*
|
||||
};
|
||||
|
||||
glib::wrapper! {
|
||||
@@ -24,4 +24,16 @@ impl MemoryCardsNewScene {
|
||||
pub fn get_image_widget(&self) -> &Image {
|
||||
self.imp().image.as_ref()
|
||||
}
|
||||
|
||||
pub fn get_hieroglyph_input(&self) -> String {
|
||||
self.imp().heiroglyph_entry.text().to_string()
|
||||
}
|
||||
|
||||
pub fn get_reading_input(&self) -> String {
|
||||
self.imp().reading_entry.text().to_string()
|
||||
}
|
||||
|
||||
pub fn get_done_button(&self) -> &Button {
|
||||
self.imp().done_button.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,6 @@ impl ObjectImpl for GuessingSetupScene {
|
||||
fn constructed(&self) {
|
||||
self.parent_constructed();
|
||||
|
||||
//TODO: tie switches to settings and make "start" button
|
||||
|
||||
let settings: Settings = Settings::new(APP_ID);
|
||||
|
||||
let hiragana_enable_ref: &LabledSwitch = self.hiragana_enable.as_ref();
|
||||
|
||||
Reference in New Issue
Block a user