notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 32951249159bec08560168d448d109ed8050b4a3
parent f748b8b34a7331971cb3a986b0e8610c10918d5b
Author: kernelkind <kernelkind@gmail.com>
Date:   Tue, 17 Dec 2024 13:46:55 -0500

prepare AcquireKeyState for add column extern UI

Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mcrates/enostr/src/keypair.rs | 2+-
Mcrates/notedeck_columns/src/key_parsing.rs | 2+-
Mcrates/notedeck_columns/src/login_manager.rs | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Mcrates/notedeck_columns/src/ui/account_login_view.rs | 37+++----------------------------------
4 files changed, 64 insertions(+), 54 deletions(-)

diff --git a/crates/enostr/src/keypair.rs b/crates/enostr/src/keypair.rs @@ -5,7 +5,7 @@ use serde::Serialize; use crate::Pubkey; use crate::SecretKey; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub struct Keypair { pub pubkey: Pubkey, pub secret_key: Option<SecretKey>, diff --git a/crates/notedeck_columns/src/key_parsing.rs b/crates/notedeck_columns/src/key_parsing.rs @@ -7,7 +7,7 @@ use enostr::{Keypair, Pubkey, SecretKey}; use poll_promise::Promise; use serde::{Deserialize, Serialize}; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum AcquireKeyError { InvalidKey, Nip05Failed(String), diff --git a/crates/notedeck_columns/src/login_manager.rs b/crates/notedeck_columns/src/login_manager.rs @@ -46,7 +46,11 @@ impl<'a> AcquireKeyState { } pub fn is_awaiting_network(&self) -> bool { - self.promise_query.is_some() + if let Some((_, promise)) = &self.promise_query { + promise.ready().is_none() + } else { + false + } } /// Whether to indicate to the user that a login error occured @@ -62,23 +66,30 @@ impl<'a> AcquireKeyState { } /// Whether to indicate to the user that a successful login occured - pub fn check_for_successful_login(&mut self) -> Option<Keypair> { - if let Some((_, promise)) = &mut self.promise_query { - if promise.ready().is_some() { - if let Some((_, promise)) = self.promise_query.take() { - match promise.block_and_take() { - Ok(key) => { - return Some(key); - } - Err(e) => { - self.error = Some(e); - self.key_on_error = Some(self.desired_key.clone()); - } - }; - } + pub fn get_login_keypair(&mut self) -> Option<&Keypair> { + if let Some((_, promise)) = &self.promise_query { + match promise.poll() { + std::task::Poll::Ready(inner) => match inner { + Ok(kp) => Some(kp), + Err(e) => { + self.error = Some(e.clone()); + self.key_on_error = Some(self.desired_key.clone()); + None + } + }, + std::task::Poll::Pending => None, + } + } else { + None + } + } + + pub fn handle_input_change_after_acquire(&mut self) { + if let Some((query, _)) = &self.promise_query { + if *query != self.desired_key { + self.promise_query = None; } } - None } pub fn should_create_new(&mut self) { @@ -88,6 +99,36 @@ impl<'a> AcquireKeyState { pub fn check_for_create_new(&self) -> bool { self.should_create_new } + + pub fn loading_and_error_ui(&mut self, ui: &mut egui::Ui) { + ui.add_space(8.0); + + ui.vertical_centered(|ui| { + if self.is_awaiting_network() { + ui.add(egui::Spinner::new()); + } + }); + + if let Some(err) = self.check_for_error() { + show_error(ui, err); + } + + ui.add_space(8.0); + } +} + +fn show_error(ui: &mut egui::Ui, err: &AcquireKeyError) { + ui.horizontal(|ui| { + let error_label = match err { + AcquireKeyError::InvalidKey => egui::Label::new( + egui::RichText::new("Invalid key.").color(ui.visuals().error_fg_color), + ), + AcquireKeyError::Nip05Failed(e) => { + egui::Label::new(egui::RichText::new(e).color(ui.visuals().error_fg_color)) + } + }; + ui.add(error_label.truncate()); + }); } #[cfg(test)] @@ -134,8 +175,8 @@ mod tests { manager.apply_acquire(); } - if let Some(key) = manager.check_for_successful_login() { - assert_eq!(expected_key, key); + if let Some(key) = manager.get_login_keypair() { + assert_eq!(expected_key, key.clone()); return; } } diff --git a/crates/notedeck_columns/src/ui/account_login_view.rs b/crates/notedeck_columns/src/ui/account_login_view.rs @@ -1,4 +1,3 @@ -use crate::key_parsing::AcquireKeyError; use crate::login_manager::AcquireKeyState; use crate::ui::{Preview, PreviewConfig, View}; use egui::TextEdit; @@ -40,7 +39,7 @@ impl<'a> AccountLoginView<'a> { ui.vertical_centered_justified(|ui| { ui.add(login_textedit(self.manager)); - self.loading_and_error(ui); + self.manager.loading_and_error_ui(ui); if ui.add(login_button()).clicked() { self.manager.apply_acquire(); @@ -67,41 +66,11 @@ impl<'a> AccountLoginView<'a> { return Some(AccountLoginResponse::CreateNew); } - if let Some(keypair) = self.manager.check_for_successful_login() { - return Some(AccountLoginResponse::LoginWith(keypair)); + if let Some(keypair) = self.manager.get_login_keypair() { + return Some(AccountLoginResponse::LoginWith(keypair.clone())); } None } - - fn loading_and_error(&mut self, ui: &mut egui::Ui) { - ui.add_space(8.0); - - ui.vertical_centered(|ui| { - if self.manager.is_awaiting_network() { - ui.add(egui::Spinner::new()); - } - }); - - if let Some(err) = self.manager.check_for_error() { - show_error(ui, err); - } - - ui.add_space(8.0); - } -} - -fn show_error(ui: &mut egui::Ui, err: &AcquireKeyError) { - ui.horizontal(|ui| { - let error_label = match err { - AcquireKeyError::InvalidKey => { - egui::Label::new(RichText::new("Invalid key.").color(ui.visuals().error_fg_color)) - } - AcquireKeyError::Nip05Failed(e) => { - egui::Label::new(RichText::new(e).color(ui.visuals().error_fg_color)) - } - }; - ui.add(error_label.truncate()); - }); } fn login_title_text() -> RichText {