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({
let tab = tab.clone();
move |position| tab.bookmark(position)
}); // @TODO
move |position| {
if tab.bookmark(position).is_err() {
todo!()
}
}
});
action.pin().connect_activate({
let tab = tab.clone();

View File

@ -1,8 +1,10 @@
mod database;
mod error;
mod item;
mod menu;
mod widget;
use error::Error;
use item::Item;
use menu::Menu;
use widget::Widget;
@ -157,14 +159,20 @@ impl Tab {
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(id) = page.keyword() {
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

View File

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

View File

@ -1,11 +1,13 @@
mod content;
mod database;
mod error;
mod input;
mod meta;
mod navigation;
mod widget;
use content::Content;
use error::Error;
use input::Input;
use meta::{Meta, Status};
use navigation::Navigation;
@ -89,12 +91,19 @@ impl Page {
// Actions
/// Toggle bookmark for navigation request in profile database
pub fn bookmark(&self) {
self.profile
/// Toggle bookmark for current `profile` by navigation request value
/// * return `true` on bookmark created, `false` on deleted
pub fn bookmark(&self) -> Result<bool, Error> {
let result = match self
.profile
.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();
result
}
/// 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
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
if let Ok(id) = self.get(request) {
match self.database.delete(id) {
Ok(_) => match self.memory.delete(request) {
Ok(_) => Ok(()),
Ok(_) => Ok(false),
Err(_) => Err(Error::MemoryDelete),
},
Err(_) => Err(Error::DatabaseDelete),
@ -63,7 +64,7 @@ impl Bookmark {
.add(DateTime::now_local().unwrap(), request.into())
{
Ok(id) => match self.memory.add(request.into(), id) {
Ok(_) => Ok(()),
Ok(_) => Ok(true),
Err(_) => Err(Error::MemoryAdd),
},
Err(_) => Err(Error::DatabaseAdd),