replace Option with Value enum

This commit is contained in:
yggverse 2024-11-20 17:28:25 +02:00
parent 69b879540d
commit f4cef86438
7 changed files with 57 additions and 43 deletions

View File

@ -1,5 +1,5 @@
mod widget;
use widget::Widget;
use widget::{form::list::item::value::Value, Widget};
use crate::app::browser::window::Action;
use crate::profile::Profile;
@ -27,10 +27,11 @@ impl Gemini {
let auth_url = auth_uri.to_string();
// Add new identity option
widget
.form
.list
.append(None, "Create new..", "Auto-generated certificate");
widget.form.list.append(
Value::CREATE_NEW_AUTH,
"Create new..",
"Auto-generated certificate",
);
// Collect additional options from database
match profile.identity.gemini.database.records() {
@ -44,7 +45,7 @@ impl Gemini {
// Append record option
widget.form.list.append(
Some(identity.id),
Value::PROFILE_IDENTITY_GEMINI_ID(identity.id),
&certificate.subject_name().unwrap().replace("CN=", ""), // trim prefix
&format!(
"valid: {} | auth: {}",
@ -76,10 +77,9 @@ impl Gemini {
move |response| {
// Get record ID depending of user selection
let profile_identity_gemini_id = match response {
// Use selected identity
Some(id) => id,
// Create new identity, get last insert ID
None => profile
Value::PROFILE_IDENTITY_GEMINI_ID(value) => value,
Value::REMOVE_CURRENT_AUTH => todo!(),
Value::CREATE_NEW_AUTH => profile
.identity
.gemini
.create(None, widget.form.name.value().as_deref())

View File

@ -1,6 +1,5 @@
mod form;
use form::Form;
pub mod form;
use form::{list::item::value::Value, Form};
use adw::{
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
@ -63,8 +62,8 @@ impl Widget {
// Actions
/// Callback wrapper for `apply` [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html)
/// * return `profile_identity_gemini_id` or new record request on `None`
pub fn on_apply(&self, callback: impl Fn(Option<i64>) + 'static) {
/// * return `Value` enum or new record request on `None`
pub fn on_apply(&self, callback: impl Fn(Value) + 'static) {
self.gobject.connect_response(Some(RESPONSE_APPLY.0), {
let form = self.form.clone();
move |_, _| callback(form.list.selected())

View File

@ -1,7 +1,7 @@
mod list;
pub mod list;
mod name;
use list::List;
use list::{item::value::Value, List};
use name::Name;
use gtk::{
@ -34,8 +34,12 @@ impl Form {
// Connect events
list.on_select({
let name = name.clone();
// Show name entry on new identity option selected
move |key| name.gobject.set_visible(key.is_none())
move |key| {
name.gobject.set_visible(match key {
Value::CREATE_NEW_AUTH => true,
_ => false,
})
}
});
// Return activated `Self`

View File

@ -1,5 +1,5 @@
mod item;
use item::Item;
pub mod item;
use item::{value::Value, Item};
use gtk::{
gio::{
@ -84,34 +84,33 @@ impl List {
// Actions
/// Append new item
pub fn append(&self, profile_identity_gemini_id: Option<i64>, title: &str, subtitle: &str) {
self.model
.append(&Item::new(profile_identity_gemini_id, title, subtitle));
pub fn append(&self, value: Value, title: &str, subtitle: &str) {
self.model.append(&Item::new(value, title, subtitle));
}
// Events
/// Run callback function on `connect_selected_notify` event
/// * return formatted `profile_identity_gemini_id` match selected item
pub fn on_select(&self, callback: impl Fn(Option<i64>) + 'static) {
/// * return `Value` enum match selected item
pub fn on_select(&self, callback: impl Fn(Value) + 'static) {
self.gobject.connect_selected_notify(move |list| {
callback(
list.selected_item()
.and_downcast::<Item>()
.unwrap()
.profile_identity_gemini_id_option(),
.value_enum(),
)
});
}
// Getters
/// Get formatted `profile_identity_gemini_id` **option** match selected item
pub fn selected(&self) -> Option<i64> {
/// Get formatted `value` match selected item
pub fn selected(&self) -> Value {
self.gobject
.selected_item()
.and_downcast::<Item>()
.unwrap()
.profile_identity_gemini_id_option()
.value_enum()
}
}

View File

@ -1,24 +1,29 @@
mod imp;
pub mod value;
use gtk::glib::{self, Object};
use value::Value;
glib::wrapper! {
pub struct Item(ObjectSubclass<imp::Item>);
}
// C-type conversion
const OPTION_NEW: i64 = -1;
// C-type property `value` conversion for `Item`
const CREATE_NEW_AUTH: i64 = 0;
const REMOVE_CURRENT_AUTH: i64 = -1;
impl Item {
// Constructors
/// Create new `GObject` with formatted properties
pub fn new(profile_identity_gemini_id: Option<i64>, title: &str, subtitle: &str) -> Self {
pub fn new(value: Value, title: &str, subtitle: &str) -> Self {
Object::builder()
.property(
"profile_identity_gemini_id",
match profile_identity_gemini_id {
Some(value) => value,
None => OPTION_NEW,
"value",
match value {
Value::CREATE_NEW_AUTH => CREATE_NEW_AUTH,
Value::REMOVE_CURRENT_AUTH => REMOVE_CURRENT_AUTH,
Value::PROFILE_IDENTITY_GEMINI_ID(value) => value,
},
)
.property("title", title)
@ -28,11 +33,12 @@ impl Item {
// Getters
/// Additional `profile_identity_gemini_id` wrapper with `Option` value support
pub fn profile_identity_gemini_id_option(&self) -> Option<i64> {
match self.profile_identity_gemini_id() {
OPTION_NEW => None,
value => Some(value),
/// Get `value` as enum `Value`
pub fn value_enum(&self) -> Value {
match self.value() {
CREATE_NEW_AUTH => Value::CREATE_NEW_AUTH,
REMOVE_CURRENT_AUTH => Value::REMOVE_CURRENT_AUTH,
value => Value::PROFILE_IDENTITY_GEMINI_ID(value),
}
}
}

View File

@ -12,7 +12,7 @@ use std::cell::{Cell, RefCell};
#[properties(wrapper_type = super::Item)]
pub struct Item {
#[property(get, set)]
profile_identity_gemini_id: Cell<i64>,
value: Cell<i64>,
#[property(get, set)]
title: RefCell<String>,
#[property(get, set)]

View File

@ -0,0 +1,6 @@
#[derive(Debug)]
pub enum Value {
CREATE_NEW_AUTH,
REMOVE_CURRENT_AUTH,
PROFILE_IDENTITY_GEMINI_ID(i64),
}