create database for cards

This commit is contained in:
leca 2024-04-05 15:01:24 +03:00
parent 5d65c0b93a
commit 43531a1a12
5 changed files with 109 additions and 5 deletions

View File

@ -9,6 +9,7 @@ edition = "2021"
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] } gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] }
rand = "0.8.5" rand = "0.8.5"
sqlite = "0.34.0"
[build-dependencies] [build-dependencies]
dirs = "5.0.1" dirs = "5.0.1"

65
cards.drawio Normal file
View File

@ -0,0 +1,65 @@
<mxfile host="app.diagrams.net" modified="2024-04-05T10:44:42.193Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" etag="npSfEZ8StWjAv_rJHnvl" version="24.2.2" type="device">
<diagram id="R2lEEEUBdFMjLlhIrx00" name="Page-1">
<mxGraphModel dx="989" dy="516" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0" extFonts="Permanent Marker^https://fonts.googleapis.com/css?family=Permanent+Marker">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="C-vyLk0tnHw3VtMMgP7b-23" value="cards&#xa;" style="shape=table;startSize=30;container=1;collapsible=1;childLayout=tableLayout;fixedRows=1;rowLines=0;fontStyle=1;align=center;resizeLast=1;" parent="1" vertex="1">
<mxGeometry x="125" y="123" width="330" height="150" as="geometry" />
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-24" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=1;" parent="C-vyLk0tnHw3VtMMgP7b-23" vertex="1">
<mxGeometry y="30" width="330" height="30" as="geometry" />
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-25" value="PK" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;fontStyle=1;" parent="C-vyLk0tnHw3VtMMgP7b-24" vertex="1">
<mxGeometry width="30" height="30" as="geometry">
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-26" value="id INTEGER PRIMARY KEY AUTOINCREMENT" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;fontStyle=5;" parent="C-vyLk0tnHw3VtMMgP7b-24" vertex="1">
<mxGeometry x="30" width="300" height="30" as="geometry">
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-27" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=0;" parent="C-vyLk0tnHw3VtMMgP7b-23" vertex="1">
<mxGeometry y="60" width="330" height="30" as="geometry" />
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-28" value="" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;" parent="C-vyLk0tnHw3VtMMgP7b-27" vertex="1">
<mxGeometry width="30" height="30" as="geometry">
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-29" value="image_hash CHAR(64)" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;" parent="C-vyLk0tnHw3VtMMgP7b-27" vertex="1">
<mxGeometry x="30" width="300" height="30" as="geometry">
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="B8Iv1AbKk0bG_LbG7-Me-11" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=0;" vertex="1" parent="C-vyLk0tnHw3VtMMgP7b-23">
<mxGeometry y="90" width="330" height="30" as="geometry" />
</mxCell>
<mxCell id="B8Iv1AbKk0bG_LbG7-Me-12" value="                    " style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;" vertex="1" parent="B8Iv1AbKk0bG_LbG7-Me-11">
<mxGeometry width="30" height="30" as="geometry">
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="B8Iv1AbKk0bG_LbG7-Me-13" value="hieroglyph VARCHAR(2)" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;" vertex="1" parent="B8Iv1AbKk0bG_LbG7-Me-11">
<mxGeometry x="30" width="300" height="30" as="geometry">
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="B8Iv1AbKk0bG_LbG7-Me-14" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=0;" vertex="1" parent="C-vyLk0tnHw3VtMMgP7b-23">
<mxGeometry y="120" width="330" height="30" as="geometry" />
</mxCell>
<mxCell id="B8Iv1AbKk0bG_LbG7-Me-15" value="" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;" vertex="1" parent="B8Iv1AbKk0bG_LbG7-Me-14">
<mxGeometry width="30" height="30" as="geometry">
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="B8Iv1AbKk0bG_LbG7-Me-16" value="translation VARCHAR(128)" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;" vertex="1" parent="B8Iv1AbKk0bG_LbG7-Me-14">
<mxGeometry x="30" width="300" height="30" as="geometry">
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

33
src/db.rs Normal file
View File

@ -0,0 +1,33 @@
use sqlite;
use std::{path::PathBuf, process::Command};
pub fn init() {
let program_home_path = if cfg!(target_os = "windows") {
let binding = std::env::var("APP_DATA").expect("No APP_DATA directory");
let mut appdata = PathBuf::from(&binding);
appdata = appdata.join("learn-hieroglyph");
Command::new("mkdir").arg(&appdata);
appdata
} else {
let binding = std::env::var("HOME").expect("No HOME directory");
let mut home = PathBuf::from(&binding);
home = home.join(".config/learn-hieroglyph");
Command::new("mkdir").args(["-p", &home.to_str().unwrap()]).spawn().expect(&format!("Cannot create program's home: {}", &home.to_str().unwrap()));
home
};
println!("Program's home: {}", program_home_path.as_path().to_str().unwrap());
let connection = sqlite::open(program_home_path.join("db.sqlite")).unwrap();
connection.execute("CREATE TABLE IF NOT EXISTS cards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_hash CHAR(64),
hieroglyph VARCHAR(2),
translation VARCHAR(128)
)").unwrap();
}

View File

@ -1,6 +1,7 @@
mod game; mod game;
mod ui; mod ui;
mod widgets; mod widgets;
mod db;
use crate::ui::menu::MenuScene; use crate::ui::menu::MenuScene;
@ -12,6 +13,8 @@ const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
fn main() -> glib::ExitCode { fn main() -> glib::ExitCode {
gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources"); gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources");
db::init();
let app: Application = Application::builder().application_id(APP_ID).build(); let app: Application = Application::builder().application_id(APP_ID).build();
app.connect_activate(test_ui); app.connect_activate(test_ui);

View File

@ -6,6 +6,7 @@ use gtk::glib::{clone, closure_local};
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window}; use gtk::{glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{prelude::*, FileDialog}; use gtk::{prelude::*, FileDialog};
use sqlite;
use crate::ui::cards::new::*; use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry; use crate::widgets::card_entry::CardEntry;
@ -49,9 +50,7 @@ impl ObjectImpl for MemoryCardsEditScene {
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()));
new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |_| { new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |_| {
let path = Rc::new(String::from("")); gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win)));
gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win), Rc::clone(&path)));
println!("{}", path); // this is empty cuz it's not waiting for user
})); }));
new_win.present(); new_win.present();
})); }));
@ -63,14 +62,17 @@ impl ObjectImpl for MemoryCardsEditScene {
} }
} }
async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>, mut _p: Rc<String>) { async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
let dialog: FileDialog = gtk::FileDialog::builder().build(); let dialog: FileDialog = gtk::FileDialog::builder().build();
let answer = dialog.open_future(Some(&*window)).await; let answer = dialog.open_future(Some(&*window)).await;
let path = answer.unwrap().path().unwrap(); let path = answer.unwrap().path().unwrap();
let path = path.as_path().to_str(); let path = path.as_path().to_str();
// _p = Rc::new(path.into_os_string().into_string().unwrap());
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref()).downcast_ref().unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene 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); w.get_image_widget().set_file(path);
let conn = sqlite::open("test.db").unwrap();
conn.execute("CREATE TABLE test(field_one TEXT, field_two INTEGER); INSERT INTO test VALUES ('Suka', 15);").unwrap();
} }
impl WidgetImpl for MemoryCardsEditScene {} impl WidgetImpl for MemoryCardsEditScene {}