handle bookmark toggle action results

This commit is contained in:
yggverse 2024-11-14 12:48:58 +02:00
parent a0c52f2ede
commit 0b44303023
6 changed files with 42 additions and 11 deletions

View File

@ -49,8 +49,12 @@ impl Window {
action.bookmark().connect_activate({ action.bookmark().connect_activate({
let tab = tab.clone(); let tab = tab.clone();
move |position| tab.bookmark(position) move |position| {
}); // @TODO if tab.bookmark(position).is_err() {
todo!()
}
}
});
action.pin().connect_activate({ action.pin().connect_activate({
let tab = tab.clone(); let tab = tab.clone();

View File

@ -1,8 +1,10 @@
mod database; mod database;
mod error;
mod item; mod item;
mod menu; mod menu;
mod widget; mod widget;
use error::Error;
use item::Item; use item::Item;
use menu::Menu; use menu::Menu;
use widget::Widget; use widget::Widget;
@ -157,14 +159,20 @@ impl Tab {
self.widget.close_all(); self.widget.close_all();
} }
pub fn bookmark(&self, page_position: Option<i32>) { /// Toggle bookmark for page at position or current page on `None`
/// * return `true` on bookmark created, `false` on deleted
pub fn bookmark(&self, page_position: Option<i32>) -> Result<bool, Error> {
if let Some(page) = self.widget.page(page_position) { if let Some(page) = self.widget.page(page_position) {
if let Some(id) = page.keyword() { if let Some(id) = page.keyword() {
if let Some(item) = self.index.borrow().get(&id) { if let Some(item) = self.index.borrow().get(&id) {
item.page().bookmark(); return match item.page().bookmark() {
Ok(result) => Ok(result),
Err(_) => Err(Error::Bookmark),
};
} }
} }
} }
Err(Error::PageNotFound)
} }
// Toggle pin status for active tab // Toggle pin status for active tab

View File

@ -0,0 +1,5 @@
#[derive(Debug)]
pub enum Error {
Bookmark,
PageNotFound,
}

View File

@ -1,11 +1,13 @@
mod content; mod content;
mod database; mod database;
mod error;
mod input; mod input;
mod meta; mod meta;
mod navigation; mod navigation;
mod widget; mod widget;
use content::Content; use content::Content;
use error::Error;
use input::Input; use input::Input;
use meta::{Meta, Status}; use meta::{Meta, Status};
use navigation::Navigation; use navigation::Navigation;
@ -89,12 +91,19 @@ impl Page {
// Actions // Actions
/// Toggle bookmark for navigation request in profile database /// Toggle bookmark for current `profile` by navigation request value
pub fn bookmark(&self) { /// * return `true` on bookmark created, `false` on deleted
self.profile pub fn bookmark(&self) -> Result<bool, Error> {
let result = match self
.profile
.bookmark .bookmark
.toggle(self.navigation.request().widget().gobject().text().as_str()); .toggle(self.navigation.request().widget().gobject().text().as_str())
{
Ok(result) => Ok(result),
Err(_) => Err(Error::Bookmark),
};
self.update(); self.update();
result
} }
/// Navigate home URL (parsed from current navigation entry) /// Navigate home URL (parsed from current navigation entry)

View File

@ -0,0 +1,4 @@
#[derive(Debug)]
pub enum Error {
Bookmark,
}

View File

@ -46,12 +46,13 @@ impl Bookmark {
} }
/// Toggle record in `database` and `memory` index /// Toggle record in `database` and `memory` index
pub fn toggle(&self, request: &str) -> Result<(), Error> { /// * return `true` on bookmark created, `false` on deleted
pub fn toggle(&self, request: &str) -> Result<bool, Error> {
// Delete record if exists // Delete record if exists
if let Ok(id) = self.get(request) { if let Ok(id) = self.get(request) {
match self.database.delete(id) { match self.database.delete(id) {
Ok(_) => match self.memory.delete(request) { Ok(_) => match self.memory.delete(request) {
Ok(_) => Ok(()), Ok(_) => Ok(false),
Err(_) => Err(Error::MemoryDelete), Err(_) => Err(Error::MemoryDelete),
}, },
Err(_) => Err(Error::DatabaseDelete), Err(_) => Err(Error::DatabaseDelete),
@ -63,7 +64,7 @@ impl Bookmark {
.add(DateTime::now_local().unwrap(), request.into()) .add(DateTime::now_local().unwrap(), request.into())
{ {
Ok(id) => match self.memory.add(request.into(), id) { Ok(id) => match self.memory.add(request.into(), id) {
Ok(_) => Ok(()), Ok(_) => Ok(true),
Err(_) => Err(Error::MemoryAdd), Err(_) => Err(Error::MemoryAdd),
}, },
Err(_) => Err(Error::DatabaseAdd), Err(_) => Err(Error::DatabaseAdd),