Skip to content

Commit

Permalink
start over after adding mints
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Jun 3, 2024
1 parent 187ff3a commit deb515e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 54 deletions.
4 changes: 4 additions & 0 deletions assets/icons/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/bridge.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +23,7 @@ pub enum UICoreMsg {
},
ReceiveOnChain,
GetFederationInfo(InviteCode),
AddFederation(InviteCode),
AddFederation(FederationId),
Unlock(String),
GetSeedWords,
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/components/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/components/mini_copy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use iced::{
widget::{
button::{self, Status}, Button,
button::{self, Status},
Button,
},
Border, Color, Length, Shadow, Theme,
};
Expand Down
10 changes: 4 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,15 @@ 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"));
}

let client = FedimintClient::new(
self.storage.clone(),
FederationInviteOrId::Invite(invite_code),
FederationInviteOrId::Id(id),
&self.mnemonic,
self.network,
self.stop.clone(),
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 36 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -219,10 +221,10 @@ impl HarborWallet {
async fn async_add_federation(
ui_handle: Option<Arc<bridge::UIHandle>>,
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");
}
Expand All @@ -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<Message> {
match message {
// Setup
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down
80 changes: 52 additions & 28 deletions src/routes/mints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,62 @@ fn mints_list(harbor: &HarborWallet) -> Element<Message> {
fn mints_add(harbor: &HarborWallet) -> Element<Message> {
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))
}
Expand Down

0 comments on commit deb515e

Please sign in to comment.