Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

donate screen and separate methods for on-chain / lighting send #19

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/components/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub fn sidebar(harbor: &HarborWallet) -> Element<Message> {
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),
Expand Down
51 changes: 35 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct HarborWallet {
receive_qr_data: Option<Data>,
mint_invite_code_str: String,
add_federation_failure_reason: Option<String>,
donate_amount_str: String,
}

impl Default for HarborWallet {
Expand Down Expand Up @@ -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),
}
Expand All @@ -146,16 +148,20 @@ impl HarborWallet {
receive_qr_data: None,
mint_invite_code_str: String::new(),
add_federation_failure_reason: None,
donate_amount_str: String::new(),
}
}

fn subscription(&self) -> Subscription<Message> {
run_core()
}

async fn async_send(ui_handle: Option<Arc<bridge::UIHandle>>, invoice: Bolt11Invoice) {
async fn async_send_lightning(
ui_handle: Option<Arc<bridge::UIHandle>>,
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");
}
Expand Down Expand Up @@ -248,16 +254,21 @@ 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 {
SendStatus::Sending => Command::none(),
_ => {
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::<u64>().unwrap(); // TODO: error handling
Command::perform(
Expand All @@ -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(),
_ => {
Expand All @@ -306,6 +307,23 @@ impl HarborWallet {
})
}
},
Message::Donate => match self.donate_amount_str.parse::<u64>() {
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(),
_ => {
Expand Down Expand Up @@ -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(),
};

Expand Down
34 changes: 34 additions & 0 deletions src/routes/donate.rs
Original file line number Diff line number Diff line change
@@ -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<Message> {
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()
}
4 changes: 4 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -27,4 +30,5 @@ pub enum Route {
Settings,
Receive,
Send,
Donate,
}
Loading