Skip to content

Commit

Permalink
Merge pull request #49 from nodetec/wallet_view_struct
Browse files Browse the repository at this point in the history
refactor: add WalletView struct
  • Loading branch information
tvolk131 authored Oct 3, 2024
2 parents 62e4ad3 + 3bfa13c commit 98288ba
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 53 deletions.
18 changes: 7 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{collections::BTreeMap, sync::Arc};
use std::sync::Arc;

use fedimint_core::config::FederationId;
use iced::{
futures::StreamExt,
widget::{column, container, row, scrollable, stack},
Expand All @@ -11,7 +10,7 @@ use nostr_sdk::PublicKey;

use crate::{
db::Database,
fedimint::{FederationView, Wallet},
fedimint::{Wallet, WalletView},
nostr::{NostrModuleMessage, NostrState},
routes::{self, bitcoin_wallet, unlock, Loadable, Route, RouteName},
ui_components::{sidebar, Toast, ToastManager, ToastStatus},
Expand All @@ -23,7 +22,7 @@ pub enum Message {

DbDeleteAllData,

UpdateFederationViews(BTreeMap<FederationId, FederationView>),
UpdateWalletView(WalletView),

NostrModule(NostrModuleMessage),
UpdateNostrState(NostrState),
Expand Down Expand Up @@ -73,16 +72,13 @@ impl App {

Task::none()
}
Message::UpdateFederationViews(federation_views) => {
Message::UpdateWalletView(wallet_view) => {
if let Some(connected_state) = self.page.get_connected_state_mut() {
connected_state.loadable_federation_views =
Loadable::Loaded(federation_views.clone());
connected_state.loadable_wallet_view = Loadable::Loaded(wallet_view.clone());
}

if let Route::BitcoinWallet(bitcoin_wallet) = &mut self.page {
bitcoin_wallet.update(bitcoin_wallet::Message::UpdateFederationViews(
federation_views,
))
bitcoin_wallet.update(bitcoin_wallet::Message::UpdateWalletView(wallet_view))
} else {
Task::none()
}
Expand Down Expand Up @@ -191,7 +187,7 @@ impl App {
async_stream::stream! {
let mut stream = wallet
.get_update_stream()
.map(Message::UpdateFederationViews);
.map(Message::UpdateWalletView);

while let Some(msg) = stream.next().await {
yield msg;
Expand Down
16 changes: 10 additions & 6 deletions src/fedimint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ pub enum LightningReceiveCompletion {
Failure,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WalletView {
pub federations: BTreeMap<FederationId, FederationView>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FederationView {
pub federation_id: FederationId,
Expand Down Expand Up @@ -82,8 +87,7 @@ impl Wallet {
// TODO: Optimize this. Repeated polling is not ideal.
pub fn get_update_stream(
&self,
) -> Pin<Box<dyn iced::futures::Stream<Item = BTreeMap<FederationId, FederationView>> + Send>>
{
) -> Pin<Box<dyn iced::futures::Stream<Item = WalletView> + Send>> {
let clients = self.clients.clone();
Box::pin(async_stream::stream! {
let mut last_state_or = None;
Expand Down Expand Up @@ -177,14 +181,14 @@ impl Wallet {

async fn get_current_state(
clients: MutexGuard<'_, HashMap<FederationId, ClientHandle>>,
) -> BTreeMap<FederationId, FederationView> {
let mut state = BTreeMap::new();
) -> WalletView {
let mut federations = BTreeMap::new();

for (federation_id, client) in clients.iter() {
let lightning_module = client.get_first_module::<LightningClientModule>();
let gateways = lightning_module.list_gateways().await;

state.insert(
federations.insert(
*federation_id,
FederationView {
federation_id: *federation_id,
Expand All @@ -200,7 +204,7 @@ impl Wallet {
);
}

state
WalletView { federations }
}

pub async fn pay_invoice(
Expand Down
27 changes: 14 additions & 13 deletions src/routes/bitcoin_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::BTreeMap, str::FromStr};
use std::str::FromStr;

use fedimint_core::{
config::{ClientConfig, FederationId, META_FEDERATION_NAME_KEY},
config::{ClientConfig, META_FEDERATION_NAME_KEY},
invite_code::InviteCode,
Amount,
};
Expand All @@ -14,7 +14,7 @@ use iced::{

use crate::{
app,
fedimint::FederationView,
fedimint::{FederationView, WalletView},
ui_components::{icon_button, PaletteColor, SvgIcon},
util::{format_amount, lighten, truncate_text},
};
Expand Down Expand Up @@ -45,7 +45,7 @@ pub enum Message {
Send(send::Message),
Receive(receive::Message),

UpdateFederationViews(BTreeMap<FederationId, FederationView>),
UpdateWalletView(WalletView),
}

pub struct Page {
Expand Down Expand Up @@ -185,12 +185,12 @@ impl Page {
Task::none()
}
}
Message::UpdateFederationViews(federation_views) => match &mut self.subroute {
Message::UpdateWalletView(wallet_view) => match &mut self.subroute {
Subroute::Send(send_page) => {
send_page.update(send::Message::UpdateFederationViews(federation_views))
send_page.update(send::Message::UpdateWalletView(wallet_view))
}
Subroute::Receive(receive_page) => {
receive_page.update(receive::Message::UpdateFederationViews(federation_views))
receive_page.update(receive::Message::UpdateWalletView(wallet_view))
}
_ => Task::none(),
},
Expand Down Expand Up @@ -266,17 +266,18 @@ impl List {
fn view<'a>(&self, connected_state: &ConnectedState) -> Column<'a, app::Message> {
let mut container = container("Wallet");

match &connected_state.loadable_federation_views {
match &connected_state.loadable_wallet_view {
Loadable::Loading => {
container = container.push(Text::new("Loading federations...").size(25));
}
Loadable::Loaded(views) => {
Loadable::Loaded(wallet_view) => {
container = container
.push(
Text::new(format_amount(Amount::from_msats(
views
.iter()
.map(|(_federation_id, view)| view.balance.msats)
wallet_view
.federations
.values()
.map(|view| view.balance.msats)
.sum::<u64>(),
)))
.size(35),
Expand All @@ -295,7 +296,7 @@ impl List {
])
.push(Text::new("Federations").size(25));

for view in views.values() {
for view in wallet_view.federations.values() {
let column: Column<_, Theme, _> = Column::new()
.push(
Text::new(
Expand Down
16 changes: 9 additions & 7 deletions src/routes/bitcoin_wallet/receive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, sync::Arc};
use std::sync::Arc;

use fedimint_core::{config::FederationId, Amount};
use fedimint_ln_common::bitcoin::Denomination;
Expand All @@ -10,7 +10,7 @@ use lightning_invoice::Bolt11Invoice;

use crate::{
app,
fedimint::{FederationView, LightningReceiveCompletion, Wallet},
fedimint::{FederationView, LightningReceiveCompletion, Wallet, WalletView},
routes::{self, container, Loadable, RouteName},
ui_components::{icon_button, PaletteColor, SvgIcon},
};
Expand All @@ -31,7 +31,7 @@ pub enum Message {
PaymentSuccess(Bolt11Invoice),
PaymentFailure(Bolt11Invoice),

UpdateFederationViews(BTreeMap<FederationId, FederationView>),
UpdateWalletView(WalletView),
}

pub struct Page {
Expand All @@ -57,9 +57,10 @@ impl Page {
denomination_combo_box_selected_denomination: Some(Denomination::Satoshi),
federation_combo_box_state: combo_box::State::new(
connected_state
.loadable_federation_views
.loadable_wallet_view
.as_ref_option()
.cloned()
.map(|wallet_view| wallet_view.federations)
.unwrap_or_default()
.into_values()
.collect(),
Expand Down Expand Up @@ -165,18 +166,19 @@ impl Page {

Task::none()
}
Message::UpdateFederationViews(federation_views) => {
Message::UpdateWalletView(wallet_view) => {
self.federation_combo_box_selected_federation = self
.federation_combo_box_selected_federation
.as_ref()
.and_then(|selected_federation| {
federation_views
wallet_view
.federations
.get(&selected_federation.federation_id)
.cloned()
});

self.federation_combo_box_state =
combo_box::State::new(federation_views.into_values().collect());
combo_box::State::new(wallet_view.federations.into_values().collect());

Task::none()
}
Expand Down
16 changes: 9 additions & 7 deletions src/routes/bitcoin_wallet/send.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, str::FromStr, sync::Arc};
use std::{str::FromStr, sync::Arc};

use fedimint_core::config::FederationId;
use iced::{
Expand All @@ -9,7 +9,7 @@ use lightning_invoice::Bolt11Invoice;

use crate::{
app,
fedimint::{FederationView, Wallet},
fedimint::{FederationView, Wallet, WalletView},
routes::{self, container, Loadable, RouteName},
ui_components::{icon_button, PaletteColor, SvgIcon, Toast, ToastStatus},
};
Expand All @@ -27,7 +27,7 @@ pub enum Message {
PayInvoiceSucceeded(Bolt11Invoice),
PayInvoiceFailed((Bolt11Invoice, Arc<anyhow::Error>)),

UpdateFederationViews(BTreeMap<FederationId, FederationView>),
UpdateWalletView(WalletView),
}

pub struct Page {
Expand All @@ -45,9 +45,10 @@ impl Page {
lightning_invoice_input: String::new(),
federation_combo_box_state: combo_box::State::new(
connected_state
.loadable_federation_views
.loadable_wallet_view
.as_ref_option()
.cloned()
.map(|wallet_view| wallet_view.federations)
.unwrap_or_default()
.into_values()
.collect(),
Expand Down Expand Up @@ -114,18 +115,19 @@ impl Page {
status: ToastStatus::Bad,
}))
}
Message::UpdateFederationViews(federation_views) => {
Message::UpdateWalletView(wallet_view) => {
self.federation_combo_box_selected_federation = self
.federation_combo_box_selected_federation
.as_ref()
.and_then(|selected_federation| {
federation_views
wallet_view
.federations
.get(&selected_federation.federation_id)
.cloned()
});

self.federation_combo_box_state =
combo_box::State::new(federation_views.into_values().collect());
combo_box::State::new(wallet_view.federations.into_values().collect());

Task::none()
}
Expand Down
11 changes: 3 additions & 8 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use std::{
collections::{BTreeMap, VecDeque},
fmt::Debug,
sync::Arc,
};
use std::{collections::VecDeque, fmt::Debug, sync::Arc};

use fedimint_core::config::FederationId;
use iced::{
widget::{column, row, text, Column, Text},
Alignment, Element, Task,
Expand All @@ -15,7 +10,7 @@ use nostr_sdk::PublicKey;
use crate::{
app,
db::Database,
fedimint::{FederationView, Wallet},
fedimint::{Wallet, WalletView},
nostr::{NostrModule, NostrState},
ui_components::{icon_button, PaletteColor, SvgIcon},
};
Expand All @@ -39,7 +34,7 @@ pub struct ConnectedState {
iced::futures::channel::oneshot::Sender<Nip46RequestApproval>,
)>,
>,
pub loadable_federation_views: Loadable<BTreeMap<FederationId, FederationView>>,
pub loadable_wallet_view: Loadable<WalletView>,
pub nostr_module: NostrModule,
pub nostr_state: NostrState,
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/unlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Page {
db,
wallet,
in_flight_nip46_requests: VecDeque::new(),
loadable_federation_views: Loadable::Loading,
loadable_wallet_view: Loadable::Loading,
nostr_module,
nostr_state: NostrState::default(),
}),
Expand Down

0 comments on commit 98288ba

Please sign in to comment.