prepare AcquireKeyState for add column extern UI
Signed-off-by: kernelkind <kernelkind@gmail.com> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
f748b8b34a
commit
3295124915
@@ -5,7 +5,7 @@ use serde::Serialize;
|
|||||||
use crate::Pubkey;
|
use crate::Pubkey;
|
||||||
use crate::SecretKey;
|
use crate::SecretKey;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
pub struct Keypair {
|
pub struct Keypair {
|
||||||
pub pubkey: Pubkey,
|
pub pubkey: Pubkey,
|
||||||
pub secret_key: Option<SecretKey>,
|
pub secret_key: Option<SecretKey>,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use enostr::{Keypair, Pubkey, SecretKey};
|
|||||||
use poll_promise::Promise;
|
use poll_promise::Promise;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum AcquireKeyError {
|
pub enum AcquireKeyError {
|
||||||
InvalidKey,
|
InvalidKey,
|
||||||
Nip05Failed(String),
|
Nip05Failed(String),
|
||||||
|
|||||||
@@ -46,7 +46,11 @@ impl<'a> AcquireKeyState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_awaiting_network(&self) -> bool {
|
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
|
/// Whether to indicate to the user that a login error occured
|
||||||
@@ -62,24 +66,31 @@ impl<'a> AcquireKeyState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Whether to indicate to the user that a successful login occured
|
/// Whether to indicate to the user that a successful login occured
|
||||||
pub fn check_for_successful_login(&mut self) -> Option<Keypair> {
|
pub fn get_login_keypair(&mut self) -> Option<&Keypair> {
|
||||||
if let Some((_, promise)) = &mut self.promise_query {
|
if let Some((_, promise)) = &self.promise_query {
|
||||||
if promise.ready().is_some() {
|
match promise.poll() {
|
||||||
if let Some((_, promise)) = self.promise_query.take() {
|
std::task::Poll::Ready(inner) => match inner {
|
||||||
match promise.block_and_take() {
|
Ok(kp) => Some(kp),
|
||||||
Ok(key) => {
|
|
||||||
return Some(key);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.error = Some(e);
|
self.error = Some(e.clone());
|
||||||
self.key_on_error = Some(self.desired_key.clone());
|
self.key_on_error = Some(self.desired_key.clone());
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn should_create_new(&mut self) {
|
pub fn should_create_new(&mut self) {
|
||||||
self.should_create_new = true;
|
self.should_create_new = true;
|
||||||
@@ -88,6 +99,36 @@ impl<'a> AcquireKeyState {
|
|||||||
pub fn check_for_create_new(&self) -> bool {
|
pub fn check_for_create_new(&self) -> bool {
|
||||||
self.should_create_new
|
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)]
|
#[cfg(test)]
|
||||||
@@ -134,8 +175,8 @@ mod tests {
|
|||||||
manager.apply_acquire();
|
manager.apply_acquire();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(key) = manager.check_for_successful_login() {
|
if let Some(key) = manager.get_login_keypair() {
|
||||||
assert_eq!(expected_key, key);
|
assert_eq!(expected_key, key.clone());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use crate::key_parsing::AcquireKeyError;
|
|
||||||
use crate::login_manager::AcquireKeyState;
|
use crate::login_manager::AcquireKeyState;
|
||||||
use crate::ui::{Preview, PreviewConfig, View};
|
use crate::ui::{Preview, PreviewConfig, View};
|
||||||
use egui::TextEdit;
|
use egui::TextEdit;
|
||||||
@@ -40,7 +39,7 @@ impl<'a> AccountLoginView<'a> {
|
|||||||
ui.vertical_centered_justified(|ui| {
|
ui.vertical_centered_justified(|ui| {
|
||||||
ui.add(login_textedit(self.manager));
|
ui.add(login_textedit(self.manager));
|
||||||
|
|
||||||
self.loading_and_error(ui);
|
self.manager.loading_and_error_ui(ui);
|
||||||
|
|
||||||
if ui.add(login_button()).clicked() {
|
if ui.add(login_button()).clicked() {
|
||||||
self.manager.apply_acquire();
|
self.manager.apply_acquire();
|
||||||
@@ -67,41 +66,11 @@ impl<'a> AccountLoginView<'a> {
|
|||||||
return Some(AccountLoginResponse::CreateNew);
|
return Some(AccountLoginResponse::CreateNew);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(keypair) = self.manager.check_for_successful_login() {
|
if let Some(keypair) = self.manager.get_login_keypair() {
|
||||||
return Some(AccountLoginResponse::LoginWith(keypair));
|
return Some(AccountLoginResponse::LoginWith(keypair.clone()));
|
||||||
}
|
}
|
||||||
None
|
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 {
|
fn login_title_text() -> RichText {
|
||||||
|
|||||||
Reference in New Issue
Block a user