From d180c812e4adae1b962043539a1d23198d852ce0 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 14 May 2024 15:55:19 -0500 Subject: [PATCH] donate screen and separate methods for on-chain / lighting send --- src/bridge.rs | 2 +- src/components/sidebar.rs | 2 ++ src/main.rs | 51 +++++++++++++++++++++++++++------------ src/routes/donate.rs | 34 ++++++++++++++++++++++++++ src/routes/mod.rs | 4 +++ 5 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 src/routes/donate.rs diff --git a/src/bridge.rs b/src/bridge.rs index 0ff7b9d..0d743a2 100644 --- a/src/bridge.rs +++ b/src/bridge.rs @@ -60,7 +60,7 @@ impl UIHandle { self.ui_to_core_tx.send(msg).await.unwrap(); } - pub async fn send(&self, invoice: Bolt11Invoice) { + pub async fn send_lightning(&self, invoice: Bolt11Invoice) { self.msg_send(UICoreMsg::SendLightning(invoice)).await; } diff --git a/src/components/sidebar.rs b/src/components/sidebar.rs index 2bdae91..3499cf7 100644 --- a/src/components/sidebar.rs +++ b/src/components/sidebar.rs @@ -40,6 +40,8 @@ pub fn sidebar(harbor: &HarborWallet) -> Element { harbor.active_route ) .on_press(Message::Navigate(Route::Settings)), + sidebar_button("Donate", SvgIcon::Heart, Route::Donate, harbor.active_route) + .on_press(Message::Navigate(Route::Donate)), ] .spacing(8) .align_items(Alignment::Start), diff --git a/src/main.rs b/src/main.rs index 95a6ae3..bebc17e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,6 +68,7 @@ pub struct HarborWallet { receive_qr_data: Option, mint_invite_code_str: String, add_federation_failure_reason: Option, + donate_amount_str: String, } impl Default for HarborWallet { @@ -111,15 +112,16 @@ pub enum Message { SendAmountInputChanged(String), PasswordInputChanged(String), MintInviteCodeInputChanged(String), + DonateAmountChanged(String), CopyToClipboard(String), // Async commands we fire from the UI to core Noop, Send(String), - Receive(u64), GenerateInvoice, GenerateAddress, Unlock(String), AddFederation(String), + Donate, // Core messages we get from core CoreMessage(CoreUIMsg), } @@ -146,6 +148,7 @@ impl HarborWallet { receive_qr_data: None, mint_invite_code_str: String::new(), add_federation_failure_reason: None, + donate_amount_str: String::new(), } } @@ -153,9 +156,12 @@ impl HarborWallet { run_core() } - async fn async_send(ui_handle: Option>, invoice: Bolt11Invoice) { + async fn async_send_lightning( + ui_handle: Option>, + invoice: Bolt11Invoice, + ) { if let Some(ui_handle) = ui_handle { - ui_handle.clone().send(invoice).await; + ui_handle.clone().send_lightning(invoice).await; } else { panic!("UI handle is None"); } @@ -248,6 +254,10 @@ impl HarborWallet { self.mint_invite_code_str = input; Command::none() } + Message::DonateAmountChanged(input) => { + self.donate_amount_str = input; + Command::none() + } // Async commands we fire from the UI to core Message::Noop => Command::none(), Message::Send(invoice_str) => match self.send_status { @@ -255,9 +265,10 @@ impl HarborWallet { _ => { self.send_failure_reason = None; if let Ok(invoice) = Bolt11Invoice::from_str(&invoice_str) { - Command::perform(Self::async_send(self.ui_handle.clone(), invoice), |_| { - Message::Noop - }) + Command::perform( + Self::async_send_lightning(self.ui_handle.clone(), invoice), + |_| Message::Noop, + ) } else if let Ok(address) = Address::from_str(&invoice_str) { let amount = self.send_amount_input_str.parse::().unwrap(); // TODO: error handling Command::perform( @@ -270,16 +281,6 @@ impl HarborWallet { } } }, - Message::Receive(amount) => match self.send_status { - SendStatus::Sending => Command::none(), - _ => { - self.send_failure_reason = None; - Command::perform(Self::async_receive(self.ui_handle.clone(), amount), |_| { - // I don't know if this is the best way to do this but we don't really know anyting after we've fired the message - Message::Noop - }) - } - }, Message::GenerateInvoice => match self.receive_status { ReceiveStatus::Generating => Command::none(), _ => { @@ -306,6 +307,23 @@ impl HarborWallet { }) } }, + Message::Donate => match self.donate_amount_str.parse::() { + Ok(amount) => { + // TODO: don't hardcode this! + let hardcoded_donation_address = "tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v"; + let address = Address::from_str(hardcoded_donation_address).unwrap(); + + Command::perform( + Self::async_send_onchain(self.ui_handle.clone(), address, amount), + |_| Message::Noop, + ) + } + Err(e) => { + self.receive_amount_str = String::new(); + eprintln!("Error parsing amount: {e}"); + Command::none() + } + }, Message::Unlock(password) => match self.unlock_status { UnlockStatus::Unlocking => Command::none(), _ => { @@ -427,6 +445,7 @@ impl HarborWallet { Route::Receive => row![sidebar, crate::routes::receive(self)].into(), Route::Send => row![sidebar, crate::routes::send(self)].into(), Route::Mints => row![sidebar, crate::routes::mints(self)].into(), + Route::Donate => row![sidebar, crate::routes::donate(self)].into(), _ => row![sidebar, crate::routes::home(self)].into(), }; diff --git a/src/routes/donate.rs b/src/routes/donate.rs new file mode 100644 index 0000000..d296f73 --- /dev/null +++ b/src/routes/donate.rs @@ -0,0 +1,34 @@ +use iced::widget::{column, container, scrollable}; +use iced::Element; +use iced::{Length, Padding}; + +use crate::components::{h_button, h_header, h_input, SvgIcon}; +use crate::{HarborWallet, Message}; + +pub fn donate(harbor: &HarborWallet) -> Element { + let header = h_header("Donate", "Support Harbor development."); + + let donate_input = h_input( + "Amount", + "", + &harbor.donate_amount_str, + Message::DonateAmountChanged, + None, + false, + None, + Some("sats"), + ); + + let donate_button = h_button("Donate", SvgIcon::Heart, false).on_press(Message::Donate); + + let column = column![header, donate_input, donate_button].spacing(48); + + container(scrollable( + column + .spacing(48) + .width(Length::Fill) + .max_width(512) + .padding(Padding::new(48.)), + )) + .into() +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 2f09655..d7ffac2 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -16,6 +16,9 @@ pub use send::*; pub mod unlock; pub use unlock::*; +pub mod donate; +pub use donate::*; + #[derive(Default, PartialEq, Debug, Clone, Copy)] pub enum Route { #[default] @@ -27,4 +30,5 @@ pub enum Route { Settings, Receive, Send, + Donate, }