draft database features

This commit is contained in:
yggverse 2024-10-06 18:20:36 +03:00
parent 227cfb30a1
commit 3dffcb029a
5 changed files with 305 additions and 14 deletions

View file

@ -1,14 +1,18 @@
mod database;
mod tab;
mod widget;
use database::Database;
use sqlite::{Connection, Transaction};
use tab::Tab;
use widget::Widget;
use std::sync::Arc;
use std::sync::{Arc, RwLock};
use gtk::{gio::SimpleAction, glib::GString, Box};
pub struct Window {
database: Arc<Database>,
tab: Arc<Tab>,
widget: Arc<Widget>,
}
@ -16,14 +20,42 @@ pub struct Window {
impl Window {
// Construct
pub fn new(
// Extras
profile_database_connection: Arc<RwLock<Connection>>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
) -> Self {
// Init database
let database = {
// Init writable database connection
let mut connection = match profile_database_connection.write() {
Ok(connection) => connection,
Err(e) => todo!("{e}"),
};
// Init new transaction
let transaction = match connection.transaction() {
Ok(transaction) => transaction,
Err(e) => todo!("{e}"),
};
// Init database structure
match Database::init(&transaction) {
Ok(database) => match transaction.commit() {
Ok(_) => Arc::new(database),
Err(e) => todo!("{e}"),
},
Err(e) => todo!("{e}"),
}
};
// Init components
let tab = Arc::new(Tab::new(
profile_database_connection,
action_tab_page_navigation_base,
action_tab_page_navigation_history_back,
action_tab_page_navigation_history_forward,
@ -37,7 +69,11 @@ impl Window {
let widget = Arc::new(Widget::new(tab.gobject()));
// Init struct
Self { tab, widget }
Self {
database,
tab,
widget,
}
}
// Actions
@ -77,6 +113,58 @@ impl Window {
self.tab.update();
}
pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) {
match self.database.records(tx, app_browser_id) {
Ok(records) => {
for record in records {
match self.database.delete(tx, &record.id) {
Ok(_) => {
// Delegate clean action to childs
// @TODO
self.tab.clean(tx, &record.id);
// self.widget.clean(tx, &record.id);
}
Err(e) => todo!("{e}"),
}
}
}
Err(e) => todo!("{e}"),
}
}
pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) {
match self.database.records(tx, app_browser_id) {
Ok(records) => {
for record in records {
// Delegate restore action to childs
// @TODO
// self.header.restore(record.id);
// self.window.restore(record.id);
// self.widget.restore(tx, &record.id);
}
}
Err(e) => todo!("{e}"),
}
}
pub fn save(&self, tx: &Transaction, app_browser_id: &i64) {
match self.database.add(tx, app_browser_id) {
Ok(_) => {
// Delegate save action to childs
let id = self.database.last_insert_id(tx);
// @TODO
// self.header.save(id);
// self.window.save(id);
// self.widget.save(tx, &id);
}
Err(e) => todo!("{e}"),
}
}
// Getters
pub fn tab_page_title(&self) -> Option<GString> {
self.tab.page_title()