From deb515e24045a505d21cbed51dcdc9c032aa9955 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Thu, 30 May 2024 09:02:32 -0500 Subject: [PATCH] start over after adding mints --- assets/icons/eye.svg | 4 ++ src/bridge.rs | 8 ++-- src/components/icon.rs | 2 + src/components/mini_copy.rs | 3 +- src/core.rs | 10 ++--- src/main.rs | 51 ++++++++++++++++------- src/routes/mints.rs | 80 ++++++++++++++++++++++++------------- 7 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 assets/icons/eye.svg diff --git a/assets/icons/eye.svg b/assets/icons/eye.svg new file mode 100644 index 0000000..a0993af --- /dev/null +++ b/assets/icons/eye.svg @@ -0,0 +1,4 @@ + + + + diff --git a/src/bridge.rs b/src/bridge.rs index febb155..cddee41 100644 --- a/src/bridge.rs +++ b/src/bridge.rs @@ -1,7 +1,7 @@ use crate::components::{FederationItem, TransactionItem}; use bitcoin::{Address, Txid}; use fedimint_core::api::InviteCode; -use fedimint_core::config::ClientConfig; +use fedimint_core::config::{ClientConfig, FederationId}; use fedimint_core::Amount; use fedimint_ln_common::lightning_invoice::Bolt11Invoice; use tokio::sync::mpsc; @@ -23,7 +23,7 @@ pub enum UICoreMsg { }, ReceiveOnChain, GetFederationInfo(InviteCode), - AddFederation(InviteCode), + AddFederation(FederationId), Unlock(String), GetSeedWords, } @@ -128,9 +128,9 @@ impl UIHandle { .await; } - pub async fn add_federation(&self, id: Uuid, invite: InviteCode) { + pub async fn add_federation(&self, id: Uuid, federation_id: FederationId) { self.msg_send(UICoreMsgPacket { - msg: UICoreMsg::AddFederation(invite), + msg: UICoreMsg::AddFederation(federation_id), id, }) .await; diff --git a/src/components/icon.rs b/src/components/icon.rs index fe1c9ea..7e58d73 100644 --- a/src/components/icon.rs +++ b/src/components/icon.rs @@ -17,6 +17,7 @@ pub enum SvgIcon { SmallClose, Bolt, Chain, + Eye, } pub fn map_icon<'a>(icon: SvgIcon, width: f32, height: f32) -> Svg<'a, Theme> { @@ -37,6 +38,7 @@ pub fn map_icon<'a>(icon: SvgIcon, width: f32, height: f32) -> Svg<'a, Theme> { SvgIcon::SmallClose => Svg::from_path("assets/icons/small_close.svg"), SvgIcon::Bolt => Svg::from_path("assets/icons/bolt.svg"), SvgIcon::Chain => Svg::from_path("assets/icons/chain.svg"), + SvgIcon::Eye => Svg::from_path("assets/icons/eye.svg"), } .width(width) .height(height) diff --git a/src/components/mini_copy.rs b/src/components/mini_copy.rs index 6d9590f..c1f621c 100644 --- a/src/components/mini_copy.rs +++ b/src/components/mini_copy.rs @@ -1,6 +1,7 @@ use iced::{ widget::{ - button::{self, Status}, Button, + button::{self, Status}, + Button, }, Border, Color, Length, Shadow, Theme, }; diff --git a/src/core.rs b/src/core.rs index d5de2c7..2a1a7b5 100644 --- a/src/core.rs +++ b/src/core.rs @@ -314,9 +314,7 @@ impl HarborCore { Ok(config) } - async fn add_federation(&self, invite_code: InviteCode) -> anyhow::Result<()> { - let id = invite_code.federation_id(); - + async fn add_federation(&self, id: FederationId) -> anyhow::Result<()> { let mut clients = self.clients.write().await; if clients.get(&id).is_some() { return Err(anyhow!("Federation already added")); @@ -324,7 +322,7 @@ impl HarborCore { let client = FedimintClient::new( self.storage.clone(), - FederationInviteOrId::Invite(invite_code), + FederationInviteOrId::Id(id), &self.mnemonic, self.network, self.stop.clone(), @@ -556,8 +554,8 @@ async fn process_core(core_handle: &mut bridge::CoreHandle, core: &HarborCore) { } } } - UICoreMsg::AddFederation(invite_code) => { - if let Err(e) = core.add_federation(invite_code).await { + UICoreMsg::AddFederation(federation_id) => { + if let Err(e) = core.add_federation(federation_id).await { error!("Error adding federation: {e}"); core.msg(Some(msg.id), CoreUIMsg::AddFederationFailed(e.to_string())) .await; diff --git a/src/main.rs b/src/main.rs index 571e5ea..82a20de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use bitcoin::Address; use components::{FederationItem, Toast, ToastManager, ToastStatus, TransactionItem}; use core::run_core; use fedimint_core::api::InviteCode; +use fedimint_core::config::FederationId; use fedimint_ln_common::lightning_invoice::Bolt11Invoice; use iced::widget::qr_code::Data; use routes::Route; @@ -97,13 +98,14 @@ pub enum Message { ShowSeedWords(bool), AddToast(Toast), CloseToast(usize), + CancelAddFederation, // Async commands we fire from the UI to core Noop, Send(String), GenerateInvoice, GenerateAddress, Unlock(String), - AddFederation(String), + AddFederation(FederationId), PeekFederation(String), Donate, // Core messages we get from core @@ -219,10 +221,10 @@ impl HarborWallet { async fn async_add_federation( ui_handle: Option>, id: Uuid, - invite: InviteCode, + federation_id: FederationId, ) { if let Some(ui_handle) = ui_handle { - ui_handle.add_federation(id, invite).await; + ui_handle.add_federation(id, federation_id).await; } else { panic!("UI handle is None"); } @@ -248,6 +250,13 @@ impl HarborWallet { } } + fn clear_add_federation_state(&mut self) { + self.add_federation_failure_reason = None; + self.peek_federation_failure_reason = None; + self.peek_federation_item = None; + self.mint_invite_code_str = String::new(); + } + fn update(&mut self, message: Message) -> Command { match message { // Setup @@ -268,6 +277,17 @@ impl HarborWallet { self.settings_show_seed_words = false; self.active_route = route; } + // Reset the add federation state when leaving mints + Route::Mints(_) => match route { + // Staying in mints, don't reset + Route::Mints(_) => { + self.active_route = route; + } + _ => { + self.clear_add_federation_state(); + self.active_route = route; + } + }, _ => self.active_route = route, } Command::none() @@ -331,6 +351,12 @@ impl HarborWallet { self.toasts.remove(index); Command::none() } + Message::CancelAddFederation => { + self.clear_add_federation_state(); + self.active_route = Route::Mints(routes::MintSubroute::List); + + Command::none() + } // Async commands we fire from the UI to core Message::Noop => Command::none(), Message::Send(invoice_str) => match self.send_status { @@ -423,18 +449,12 @@ impl HarborWallet { ) } }, - Message::AddFederation(invite_code) => { - let invite = InviteCode::from_str(&invite_code); - if let Ok(invite) = invite { - let id = Uuid::new_v4(); // todo use this id somewhere - Command::perform( - Self::async_add_federation(self.ui_handle.clone(), id, invite), - |_| Message::Noop, - ) - } else { - self.add_federation_failure_reason = Some("Invalid invite code".to_string()); - Command::none() - } + Message::AddFederation(federation_id) => { + let id = Uuid::new_v4(); // todo use this id somewhere + Command::perform( + Self::async_add_federation(self.ui_handle.clone(), id, federation_id), + |_| Message::Noop, + ) } Message::PeekFederation(invite_code) => { let invite = InviteCode::from_str(&invite_code); @@ -563,6 +583,7 @@ impl HarborWallet { } CoreUIMsg::AddFederationSuccess => { self.mint_invite_code_str = String::new(); + self.active_route = Route::Mints(routes::MintSubroute::List); Command::none() } CoreUIMsg::FederationListUpdated(list) => { diff --git a/src/routes/mints.rs b/src/routes/mints.rs index 5ecf2d6..b1b2aa1 100644 --- a/src/routes/mints.rs +++ b/src/routes/mints.rs @@ -32,38 +32,62 @@ fn mints_list(harbor: &HarborWallet) -> Element { fn mints_add(harbor: &HarborWallet) -> Element { let header = h_header("Add Mint", "Add a new mint to your wallet."); - let mint_input = h_input( - "Mint Invite Code", - "", - &harbor.mint_invite_code_str, - Message::MintInviteCodeInputChanged, - None, - false, - None, - None, - ); + let column = match &harbor.peek_federation_item { + None => { + let mint_input = h_input( + "Mint Invite Code", + "", + &harbor.mint_invite_code_str, + Message::MintInviteCodeInputChanged, + None, + false, + None, + None, + ); - let add_mint_button = h_button("Add Mint", SvgIcon::Plus, false) - .on_press(Message::AddFederation(harbor.mint_invite_code_str.clone())); + let peek_mint_button = h_button("Preview", SvgIcon::Eye, false) + .on_press(Message::PeekFederation(harbor.mint_invite_code_str.clone())); - // let peek_mint_button = h_button("Peek Mint", SvgIcon::Squirrel, false) - // .on_press(Message::PeekFederation(harbor.mint_invite_code_str.clone())); + let column = column![header, mint_input, peek_mint_button].spacing(48); - let column = column![ - header, - mint_input, - // peek_mint_button, - add_mint_button - ] - .spacing(48); + let column = column.push_maybe( + harbor + .peek_federation_failure_reason + .as_ref() + .map(|r| text(r).size(18).color(Color::from_rgb8(255, 0, 0))), + ); - // TODO: better error styling - let column = column.push_maybe( - harbor - .add_federation_failure_reason - .as_ref() - .map(|r| text(r).size(18).color(Color::from_rgb8(255, 0, 0))), - ); + column + } + + Some(peek_federation_item) => { + let federation_preview = h_federation_item(peek_federation_item); + + let add_mint_button = h_button("Add Mint", SvgIcon::Plus, false) + .on_press(Message::AddFederation(peek_federation_item.id)); + + let start_over_button = h_button("Start Over", SvgIcon::Restart, false) + .on_press(Message::CancelAddFederation); + + let column = column![ + header, + federation_preview, + add_mint_button, + start_over_button + ] + .spacing(48); + + // TODO: better error styling + let column = column.push_maybe( + harbor + .add_federation_failure_reason + .as_ref() + .map(|r| text(r).size(18).color(Color::from_rgb8(255, 0, 0))), + ); + + column + } + }; basic_layout(column.spacing(48)) }