add LoginState to app

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-09-07 21:06:30 -04:00
committed by William Casarin
parent ee0029268f
commit dda7256f51
3 changed files with 48 additions and 47 deletions

View File

@@ -10,6 +10,7 @@ use crate::filter::FilterState;
use crate::frame_history::FrameHistory; use crate::frame_history::FrameHistory;
use crate::imgcache::ImageCache; use crate::imgcache::ImageCache;
use crate::key_storage::KeyStorageType; use crate::key_storage::KeyStorageType;
use crate::login_manager::LoginState;
use crate::note::NoteRef; use crate::note::NoteRef;
use crate::notecache::{CachedNote, NoteCache}; use crate::notecache::{CachedNote, NoteCache};
use crate::relay_pool_manager::RelayPoolManager; use crate::relay_pool_manager::RelayPoolManager;
@@ -57,6 +58,7 @@ pub struct Damus {
pub threads: Threads, pub threads: Threads,
pub img_cache: ImageCache, pub img_cache: ImageCache,
pub accounts: AccountManager, pub accounts: AccountManager,
pub login_state: LoginState,
pub subscriptions: Subscriptions, pub subscriptions: Subscriptions,
frame_history: crate::frame_history::FrameHistory, frame_history: crate::frame_history::FrameHistory,
@@ -700,6 +702,7 @@ impl Damus {
frame_history: FrameHistory::default(), frame_history: FrameHistory::default(),
show_account_switcher: false, show_account_switcher: false,
account_management_view_state: RoutableWidgetState::default(), account_management_view_state: RoutableWidgetState::default(),
login_state: LoginState::default(),
} }
} }
@@ -743,6 +746,7 @@ impl Damus {
frame_history: FrameHistory::default(), frame_history: FrameHistory::default(),
show_account_switcher: false, show_account_switcher: false,
account_management_view_state: RoutableWidgetState::default(), account_management_view_state: RoutableWidgetState::default(),
login_state: LoginState::default(),
} }
} }

View File

@@ -6,20 +6,22 @@ use poll_promise::Promise;
/// The UI view interface to log in to a nostr account. /// The UI view interface to log in to a nostr account.
#[derive(Default)] #[derive(Default)]
pub struct LoginManager { pub struct LoginState {
login_key: String, login_key: String,
promise_query: Option<(String, Promise<Result<Keypair, LoginError>>)>, promise_query: Option<(String, Promise<Result<Keypair, LoginError>>)>,
error: Option<LoginError>, error: Option<LoginError>,
key_on_error: Option<String>, key_on_error: Option<String>,
should_create_new: bool,
} }
impl<'a> LoginManager { impl<'a> LoginState {
pub fn new() -> Self { pub fn new() -> Self {
LoginManager { LoginState {
login_key: String::new(), login_key: String::new(),
promise_query: None, promise_query: None,
error: None, error: None,
key_on_error: None, key_on_error: None,
should_create_new: false,
} }
} }
@@ -85,6 +87,14 @@ impl<'a> LoginManager {
} }
None None
} }
pub fn should_create_new(&mut self) {
self.should_create_new = true;
}
pub fn check_for_create_new(&self) -> bool {
self.should_create_new
}
} }
#[cfg(test)] #[cfg(test)]
@@ -96,7 +106,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_retrieve_key() { async fn test_retrieve_key() {
let mut manager = LoginManager::new(); let mut manager = LoginState::new();
let expected_str = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"; let expected_str = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681";
let expected_key = Keypair::only_pubkey(Pubkey::from_hex(expected_str).unwrap()); let expected_key = Keypair::only_pubkey(Pubkey::from_hex(expected_str).unwrap());

View File

@@ -1,29 +1,32 @@
use crate::app_style::NotedeckTextStyle; use crate::app_style::NotedeckTextStyle;
use crate::key_parsing::LoginError; use crate::key_parsing::LoginError;
use crate::login_manager::LoginManager; use crate::login_manager::LoginState;
use crate::ui::{Preview, PreviewConfig, View}; use crate::ui::{Preview, PreviewConfig, View};
use egui::{Align, Button, Color32, Frame, Margin, Response, RichText, Ui, Vec2}; use egui::TextEdit;
use egui::{Image, TextEdit}; use egui::{Align, Button, Color32, Frame, InnerResponse, Margin, RichText, Vec2};
use enostr::Keypair;
pub struct AccountLoginView<'a> { pub struct AccountLoginView<'a> {
manager: &'a mut LoginManager, manager: &'a mut LoginState,
}
pub enum AccountLoginResponse {
CreateNew,
LoginWith(Keypair),
} }
impl<'a> AccountLoginView<'a> { impl<'a> AccountLoginView<'a> {
pub fn new(manager: &'a mut LoginManager) -> Self { pub fn new(state: &'a mut LoginState) -> Self {
AccountLoginView { manager } AccountLoginView { manager: state }
} }
pub fn ui(&mut self, ui: &mut egui::Ui) -> Response { pub fn ui(&mut self, ui: &mut egui::Ui) -> InnerResponse<Option<AccountLoginResponse>> {
Frame::none() Frame::none()
.outer_margin(12.0) .outer_margin(12.0)
.show(ui, |ui| { .show(ui, |ui| self.show(ui))
self.show(ui);
})
.response
} }
fn show(&mut self, ui: &mut egui::Ui) -> egui::Response { fn show(&mut self, ui: &mut egui::Ui) -> Option<AccountLoginResponse> {
ui.vertical(|ui| { ui.vertical(|ui| {
ui.vertical_centered(|ui| { ui.vertical_centered(|ui| {
ui.add_space(32.0); ui.add_space(32.0);
@@ -55,11 +58,19 @@ impl<'a> AccountLoginView<'a> {
.add(Button::new(RichText::new("Create Account")).frame(false)) .add(Button::new(RichText::new("Create Account")).frame(false))
.clicked() .clicked()
{ {
// TODO: navigate to 'create account' screen self.manager.should_create_new();
} }
}); });
}) });
.response
if self.manager.check_for_create_new() {
return Some(AccountLoginResponse::CreateNew);
}
if let Some(keypair) = self.manager.check_for_successful_login() {
return Some(AccountLoginResponse::LoginWith(keypair));
}
None
} }
fn loading_and_error(&mut self, ui: &mut egui::Ui) { fn loading_and_error(&mut self, ui: &mut egui::Ui) {
@@ -99,37 +110,12 @@ fn login_title_text() -> RichText {
.strong() .strong()
} }
fn login_info_text() -> RichText {
RichText::new("The best alternative to tweetDeck built in nostr protocol")
.text_style(NotedeckTextStyle::Heading3.text_style())
}
fn login_window_info_text(ui: &Ui) -> RichText {
RichText::new("Enter your private key to start using Notedeck")
.text_style(NotedeckTextStyle::Body.text_style())
.color(ui.visuals().noninteractive().fg_stroke.color)
}
fn login_textedit_info_text() -> RichText { fn login_textedit_info_text() -> RichText {
RichText::new("Enter your key") RichText::new("Enter your key")
.strong() .strong()
.text_style(NotedeckTextStyle::Body.text_style()) .text_style(NotedeckTextStyle::Body.text_style())
} }
fn logo_unformatted() -> Image<'static> {
let logo_gradient_data = egui::include_image!("../../assets/Logo-Gradient-2x.png");
return egui::Image::new(logo_gradient_data);
}
fn generate_info_text() -> RichText {
RichText::new("Quickly generate your keys. Make sure you save them safely.")
.text_style(NotedeckTextStyle::Body.text_style())
}
fn generate_keys_button() -> Button<'static> {
Button::new(RichText::new("Generate keys").text_style(NotedeckTextStyle::Body.text_style()))
}
fn login_button() -> Button<'static> { fn login_button() -> Button<'static> {
Button::new( Button::new(
RichText::new("Login now — let's do this!") RichText::new("Login now — let's do this!")
@@ -140,7 +126,7 @@ fn login_button() -> Button<'static> {
.min_size(Vec2::new(0.0, 40.0)) .min_size(Vec2::new(0.0, 40.0))
} }
fn login_textedit(manager: &mut LoginManager) -> TextEdit { fn login_textedit(manager: &mut LoginState) -> TextEdit {
manager.get_login_textedit(|text| { manager.get_login_textedit(|text| {
egui::TextEdit::singleline(text) egui::TextEdit::singleline(text)
.hint_text( .hint_text(
@@ -156,7 +142,7 @@ mod preview {
use super::*; use super::*;
pub struct AccountLoginPreview { pub struct AccountLoginPreview {
manager: LoginManager, manager: LoginState,
} }
impl View for AccountLoginPreview { impl View for AccountLoginPreview {
@@ -169,7 +155,8 @@ mod preview {
type Prev = AccountLoginPreview; type Prev = AccountLoginPreview;
fn preview(cfg: PreviewConfig) -> Self::Prev { fn preview(cfg: PreviewConfig) -> Self::Prev {
let manager = LoginManager::new(); let _ = cfg;
let manager = LoginState::new();
AccountLoginPreview { manager } AccountLoginPreview { manager }
} }
} }