use AcquireKeyState for AddColumn

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-11-02 22:16:04 -04:00
parent 529b76094c
commit 412ba9b565
6 changed files with 143 additions and 109 deletions

View File

@@ -1,13 +1,13 @@
use crate::app_style::NotedeckTextStyle;
use crate::key_parsing::LoginError;
use crate::login_manager::LoginState;
use crate::key_parsing::AcquireKeyError;
use crate::login_manager::AcquireKeyState;
use crate::ui::{Preview, PreviewConfig, View};
use egui::TextEdit;
use egui::{Align, Button, Color32, Frame, InnerResponse, Margin, RichText, Vec2};
use enostr::Keypair;
pub struct AccountLoginView<'a> {
manager: &'a mut LoginState,
manager: &'a mut AcquireKeyState,
}
pub enum AccountLoginResponse {
@@ -16,7 +16,7 @@ pub enum AccountLoginResponse {
}
impl<'a> AccountLoginView<'a> {
pub fn new(state: &'a mut LoginState) -> Self {
pub fn new(state: &'a mut AcquireKeyState) -> Self {
AccountLoginView { manager: state }
}
@@ -43,7 +43,7 @@ impl<'a> AccountLoginView<'a> {
self.loading_and_error(ui);
if ui.add(login_button()).clicked() {
self.manager.apply_login();
self.manager.apply_acquire();
}
});
@@ -90,13 +90,13 @@ impl<'a> AccountLoginView<'a> {
}
}
fn show_error(ui: &mut egui::Ui, err: &LoginError) {
fn show_error(ui: &mut egui::Ui, err: &AcquireKeyError) {
ui.horizontal(|ui| {
let error_label = match err {
LoginError::InvalidKey => {
AcquireKeyError::InvalidKey => {
egui::Label::new(RichText::new("Invalid key.").color(ui.visuals().error_fg_color))
}
LoginError::Nip05Failed(e) => {
AcquireKeyError::Nip05Failed(e) => {
egui::Label::new(RichText::new(e).color(ui.visuals().error_fg_color))
}
};
@@ -126,8 +126,8 @@ fn login_button() -> Button<'static> {
.min_size(Vec2::new(0.0, 40.0))
}
fn login_textedit(manager: &mut LoginState) -> TextEdit {
manager.get_login_textedit(|text| {
fn login_textedit(manager: &mut AcquireKeyState) -> TextEdit {
manager.get_acquire_textedit(|text| {
egui::TextEdit::singleline(text)
.hint_text(
RichText::new("Enter your public key (npub, nip05), or private key (nsec) here...")
@@ -143,7 +143,7 @@ mod preview {
use super::*;
pub struct AccountLoginPreview {
manager: LoginState,
manager: AcquireKeyState,
}
impl View for AccountLoginPreview {
@@ -157,7 +157,7 @@ mod preview {
fn preview(cfg: PreviewConfig) -> Self::Prev {
let _ = cfg;
let manager = LoginState::new();
let manager = AcquireKeyState::new();
AccountLoginPreview { manager }
}
}

View File

@@ -1,17 +1,23 @@
use egui::{pos2, vec2, Color32, FontId, ImageSource, Pos2, Rect, RichText, Separator, Ui};
use core::f32;
use std::collections::HashMap;
use egui::{
pos2, vec2, Align, Color32, FontId, Id, ImageSource, Margin, Pos2, Rect, RichText, Separator,
Ui, Vec2,
};
use nostrdb::Ndb;
use tracing::{error, info};
use tracing::error;
use crate::{
app_style::{get_font_size, NotedeckTextStyle},
key_parsing::perform_key_retrieval,
login_manager::AcquireKeyState,
timeline::{PubkeySource, Timeline, TimelineKind},
ui::anim::ICON_EXPANSION_MULTIPLE,
user_account::UserAccount,
Damus,
};
use super::anim::AnimationHelper;
use super::{anim::AnimationHelper, padding};
pub enum AddColumnResponse {
Timeline(Timeline),
@@ -67,13 +73,22 @@ impl AddColumnOption {
}
pub struct AddColumnView<'a> {
key_state_map: &'a mut HashMap<Id, AcquireKeyState>,
ndb: &'a Ndb,
cur_account: Option<&'a UserAccount>,
}
impl<'a> AddColumnView<'a> {
pub fn new(ndb: &'a Ndb, cur_account: Option<&'a UserAccount>) -> Self {
Self { ndb, cur_account }
pub fn new(
key_state_map: &'a mut HashMap<Id, AcquireKeyState>,
ndb: &'a Ndb,
cur_account: Option<&'a UserAccount>,
) -> Self {
Self {
key_state_map,
ndb,
cur_account,
}
}
pub fn ui(&mut self, ui: &mut Ui) -> Option<AddColumnResponse> {
@@ -105,48 +120,49 @@ impl<'a> AddColumnView<'a> {
}
fn external_notification_ui(&mut self, ui: &mut Ui) -> Option<AddColumnResponse> {
ui.label(RichText::new("External Notification").heading());
ui.label("Paste the user's npub that you would like to have a notifications column for:");
padding(16.0, ui, |ui| {
let id = ui.id().with("external_notif");
let key_state = self.key_state_map.entry(id).or_default();
let id = ui.id().with("external_notif");
let mut text = ui.ctx().data_mut(|data| {
let text = data.get_temp_mut_or_insert_with(id, String::new);
text.clone()
});
ui.text_edit_singleline(&mut text);
ui.ctx().data_mut(|d| d.insert_temp(id, text.clone()));
let text_edit = key_state.get_acquire_textedit(|text| {
egui::TextEdit::singleline(text)
.hint_text(
RichText::new("Enter the user's key (npub, hex, nip05) here...")
.text_style(NotedeckTextStyle::Body.text_style()),
)
.vertical_align(Align::Center)
.desired_width(f32::INFINITY)
.min_size(Vec2::new(0.0, 40.0))
.margin(Margin::same(12.0))
});
if ui.button("Validate").clicked() {
if let Some(payload) = perform_key_retrieval(&text).ready() {
match payload {
Ok(keypair) => {
info!(
"Successfully retrieved external notification keypair {}",
keypair.pubkey
);
if let Some(resp) =
AddColumnOption::Notification(PubkeySource::Explicit(keypair.pubkey))
.take_as_response(self.ndb, self.cur_account)
{
Some(resp)
} else {
error!("Failed to get timeline column");
None
}
}
Err(_) => {
info!("User did not enter a valid npub or nip05");
ui.colored_label(Color32::RED, "Please enter a valid npub or nip05");
None
}
}
} else {
ui.add(text_edit);
if ui.button("Add").clicked() {
key_state.apply_acquire();
}
if key_state.is_awaiting_network() {
ui.spinner();
}
if let Some(error) = key_state.check_for_error() {
error!("acquire key error: {}", error);
ui.colored_label(
Color32::RED,
"Please enter a valid npub, public hex key or nip05",
);
}
if let Some(keypair) = key_state.check_for_successful_login() {
key_state.should_create_new();
AddColumnOption::Notification(PubkeySource::Explicit(keypair.pubkey))
.take_as_response(self.ndb, self.cur_account)
} else {
None
}
} else {
None
}
})
.inner
}
fn column_option_ui(&mut self, ui: &mut Ui, data: ColumnOptionData) -> egui::Response {
@@ -326,16 +342,24 @@ pub fn render_add_column_routes(
route: &AddColumnRoute,
) {
let resp = match route {
AddColumnRoute::Base => {
AddColumnView::new(&app.ndb, app.accounts.get_selected_account()).ui(ui)
}
AddColumnRoute::UndecidedNotification => {
AddColumnView::new(&app.ndb, app.accounts.get_selected_account()).notifications_ui(ui)
}
AddColumnRoute::ExternalNotification => {
AddColumnView::new(&app.ndb, app.accounts.get_selected_account())
.external_notification_ui(ui)
}
AddColumnRoute::Base => AddColumnView::new(
&mut app.view_state.id_state_map,
&app.ndb,
app.accounts.get_selected_account(),
)
.ui(ui),
AddColumnRoute::UndecidedNotification => AddColumnView::new(
&mut app.view_state.id_state_map,
&app.ndb,
app.accounts.get_selected_account(),
)
.notifications_ui(ui),
AddColumnRoute::ExternalNotification => AddColumnView::new(
&mut app.view_state.id_state_map,
&app.ndb,
app.accounts.get_selected_account(),
)
.external_notification_ui(ui),
};
if let Some(resp) = resp {
@@ -382,7 +406,12 @@ mod preview {
impl View for AddColumnPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
AddColumnView::new(&self.app.ndb, self.app.accounts.get_selected_account()).ui(ui);
AddColumnView::new(
&mut self.app.view_state.id_state_map,
&self.app.ndb,
self.app.accounts.get_selected_account(),
)
.ui(ui);
}
}