draft bookmark toggle action

This commit is contained in:
yggverse 2024-11-14 09:30:40 +02:00
parent feca899c5b
commit 232f67b9cc
4 changed files with 49 additions and 5 deletions

View File

@ -31,6 +31,7 @@ use std::{cell::RefCell, rc::Rc, time::Duration};
pub struct Page { pub struct Page {
id: GString, id: GString,
cancellable: RefCell<Cancellable>, cancellable: RefCell<Cancellable>,
profile: Rc<Profile>,
// Actions // Actions
browser_action: Rc<BrowserAction>, browser_action: Rc<BrowserAction>,
tab_action: Rc<TabAction>, tab_action: Rc<TabAction>,
@ -54,7 +55,7 @@ impl Page {
let content = Rc::new(Content::new((action.1.clone(), action.2.clone()))); let content = Rc::new(Content::new((action.1.clone(), action.2.clone())));
let navigation = Rc::new(Navigation::new( let navigation = Rc::new(Navigation::new(
profile, profile.clone(),
(action.0.clone(), action.1.clone(), action.2.clone()), (action.0.clone(), action.1.clone(), action.2.clone()),
)); ));
@ -73,6 +74,7 @@ impl Page {
Self { Self {
cancellable: RefCell::new(Cancellable::new()), cancellable: RefCell::new(Cancellable::new()),
id, id,
profile,
// Actions // Actions
browser_action: action.0, browser_action: action.0,
tab_action: action.2, tab_action: action.2,
@ -87,8 +89,11 @@ impl Page {
// Actions // Actions
/// Toggle bookmark for navigation request in profile database
pub fn bookmark(&self) { pub fn bookmark(&self) {
// @TODO self.navigation.request().widget().gobject().text() self.profile
.bookmark
.toggle(self.navigation.request().widget().gobject().text().as_str())
} }
/// Navigate home URL (parsed from current navigation entry) /// Navigate home URL (parsed from current navigation entry)

View File

@ -4,12 +4,13 @@ mod memory;
use database::Database; use database::Database;
use memory::Memory; use memory::Memory;
use gtk::glib::DateTime;
use sqlite::{Connection, Transaction}; use sqlite::{Connection, Transaction};
use std::{rc::Rc, sync::RwLock}; use std::{rc::Rc, sync::RwLock};
pub struct Bookmark { pub struct Bookmark {
database: Rc<Database>, database: Rc<Database>, // permanent storage
memory: Rc<Memory>, memory: Rc<Memory>, // fast search index
} }
impl Bookmark { impl Bookmark {
@ -43,7 +44,19 @@ impl Bookmark {
} }
} }
// @TODO add new record with index update /// Toggle record in bookmarks database, update emory index
pub fn toggle(&self, request: &str) {
let time = DateTime::now_local().unwrap();
if self.has_request(request, false) {
// @TODO
} else {
match self.database.add(time.clone(), request.into()) {
Ok(_) => self.memory.set(request.into(), time),
Err(_) => todo!(),
};
}
}
} }
// Tools // Tools

View File

@ -31,6 +31,26 @@ impl Database {
let tx = readable.unchecked_transaction().unwrap(); let tx = readable.unchecked_transaction().unwrap();
select(&tx, self.profile_id, request).unwrap() select(&tx, self.profile_id, request).unwrap()
} }
// Setters
pub fn add(&self, time: DateTime, request: String) -> Result<i64, ()> {
// Begin new transaction
let mut writable = self.connection.write().unwrap();
let tx = writable.transaction().unwrap();
// Create new record
insert(&tx, self.profile_id, time, request).unwrap();
// Hold insert ID for result
let id = last_insert_id(&tx);
// Done
match tx.commit() {
Ok(_) => Ok(id),
Err(_) => Err(()), // @TODO
}
}
} }
// Low-level DB API // Low-level DB API
@ -102,3 +122,7 @@ pub fn select(
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> { pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id]) tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id])
} }
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
}

View File

@ -9,6 +9,7 @@ pub struct Memory {
impl Memory { impl Memory {
// Constructors // Constructors
/// Create new `Self`
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
index: RefCell::new(HashMap::new()), index: RefCell::new(HashMap::new()),
@ -32,6 +33,7 @@ impl Memory {
index.insert(request, time); index.insert(request, time);
} }
/// Check request exist in memory index
pub fn is_exist(&self, request: &str) -> bool { pub fn is_exist(&self, request: &str) -> bool {
self.index.borrow().get(request).is_some() self.index.borrow().get(request).is_some()
} }