implement identity remove action

This commit is contained in:
yggverse 2024-11-29 05:44:40 +02:00
parent 00afa70d5d
commit 29083f50d9
8 changed files with 241 additions and 18 deletions

View file

@ -86,7 +86,7 @@ impl Gemini {
.gemini
.auth
.database
.records(None)
.records_scope(None)
.unwrap()
.iter()
.filter(|this| this.profile_identity_gemini_id == identity.id)
@ -100,7 +100,7 @@ impl Gemini {
.gemini
.auth
.database
.records(Some(auth_url.as_str()))
.records_scope(Some(auth_url.as_str()))
.unwrap()
.iter()
.filter(|this| this.profile_identity_gemini_id == identity.id)
@ -162,7 +162,8 @@ impl Gemini {
}
// Remove all identity auths for `auth_uri`
None => {
if let Err(reason) = profile.identity.gemini.auth.remove(auth_url.as_str())
if let Err(reason) =
profile.identity.gemini.auth.remove_scope(auth_url.as_str())
{
todo!("{}", reason.to_string())
};

View file

@ -1,8 +1,10 @@
mod drop;
mod file;
pub mod list;
mod name;
mod save;
use drop::Drop;
use file::File;
use list::{item::value::Value, List};
use name::Name;
@ -15,6 +17,7 @@ use std::rc::Rc;
pub struct Form {
// pub action: Rc<Action>,
// pub drop: Rc<Drop>,
pub file: Rc<File>,
pub list: Rc<List>,
pub name: Rc<Name>,
@ -28,6 +31,7 @@ impl Form {
/// Create new `Self`
pub fn new(profile: Rc<Profile>, action: Rc<Action>) -> Self {
// Init components
let drop = Rc::new(Drop::new(profile.clone(), action.clone()));
let file = Rc::new(File::new(action.clone()));
let list = Rc::new(List::new());
let name = Rc::new(Name::new(action.clone()));
@ -39,10 +43,12 @@ impl Form {
gobject.append(&list.gobject);
gobject.append(&name.gobject);
gobject.append(&file.gobject);
gobject.append(&drop.gobject);
gobject.append(&save.gobject);
// Connect events
list.on_select({
let drop = drop.clone();
let file = file.clone();
let name = name.clone();
let save = save.clone();
@ -54,12 +60,14 @@ impl Form {
// Change file choose button visibility
file.update(matches!(item, Value::ImportPem));
// Change export button visibility by update it holder value
// Change other components visibility by update it holder value
match item {
Value::ProfileIdentityGeminiId(value) => {
drop.update(Some(value));
save.update(Some(value));
}
_ => {
drop.update(None);
save.update(None);
}
}
@ -72,11 +80,12 @@ impl Form {
// Return activated `Self`
Self {
// action,
gobject,
// drop,
file,
list,
name,
// save,
gobject,
}
}

View file

@ -0,0 +1,128 @@
use super::Action;
use crate::profile::Profile;
use adw::{
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
AlertDialog, ResponseAppearance,
};
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::{cell::RefCell, rc::Rc};
// Defaults
const LABEL: &str = "Delete";
const TOOLTIP_TEXT: &str = "Drop selected identity from database";
const MARGIN: i32 = 8;
const HEADING: &str = "Delete";
const BODY: &str = "Permanently delete selected identity from database?";
const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel");
const RESPONSE_CONFIRM: (&str, &str) = ("confirm", "Confirm");
pub struct Drop {
profile_identity_gemini_id: Rc<RefCell<Option<i64>>>,
pub gobject: Button,
}
impl Drop {
// Constructors
/// Create new `Self`
pub fn new(profile: Rc<Profile>, action: Rc<Action>) -> Self {
// Init selected option holder
let profile_identity_gemini_id = Rc::new(RefCell::new(None::<i64>));
// Init `GObject`
let gobject = Button::builder()
.label(LABEL)
.margin_top(MARGIN)
.tooltip_text(TOOLTIP_TEXT)
.visible(false)
.build();
// Init events
gobject.connect_clicked({
let action = action.clone();
let gobject = gobject.clone();
let profile_identity_gemini_id = profile_identity_gemini_id.clone();
move |_| {
// Get selected identity from holder
match profile_identity_gemini_id.borrow().as_ref() {
Some(profile_identity_gemini_id) => {
// Init main `GObject`
let dialog = AlertDialog::builder()
.heading(HEADING)
.body(BODY)
.close_response(RESPONSE_CANCEL.0)
.default_response(RESPONSE_CANCEL.0)
.build();
// Set response variants
dialog.add_responses(&[RESPONSE_CANCEL, RESPONSE_CONFIRM]);
// Decorate default response preset
dialog.set_response_appearance(
RESPONSE_CONFIRM.0,
ResponseAppearance::Suggested,
);
dialog.set_response_appearance(
RESPONSE_CANCEL.0,
ResponseAppearance::Destructive,
);
// Connect confirmation event
dialog.connect_response(Some(RESPONSE_CONFIRM.0), {
let action = action.clone();
let profile = profile.clone();
let gobject = gobject.clone();
let profile_identity_gemini_id = profile_identity_gemini_id.clone();
move |_, _| {
match profile.identity.gemini.delete(profile_identity_gemini_id) {
Ok(_) => {
gobject.set_css_classes(&["success"]);
gobject.set_label("Identity successfully deleted")
}
Err(e) => {
gobject.set_css_classes(&["error"]);
gobject.set_label(&e.to_string())
}
}
action.update.activate()
}
});
// Show dialog
dialog.present(Some(&gobject))
}
None => todo!(), // unexpected
}
}
});
// Return activated `Self`
Self {
profile_identity_gemini_id,
gobject,
}
}
// Actions
/// Update `profile_identity_gemini_id` holder,
/// toggle visibility depending on given value
pub fn update(&self, profile_identity_gemini_id: Option<i64>) {
self.gobject.set_visible(match profile_identity_gemini_id {
Some(value) => {
self.profile_identity_gemini_id.replace(Some(value));
true
}
None => {
self.profile_identity_gemini_id.replace(None);
false
}
})
}
}