Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tvolk131 committed Sep 29, 2024
1 parent 6f84ef8 commit 8f1c69d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
15 changes: 8 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ pub enum Message {

DbDeleteAllData,

UpdateFederationViews {
views: BTreeMap<FederationId, FederationView>,
},
UpdateFederationViews(BTreeMap<FederationId, FederationView>),

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

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

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

while let Some(msg) = stream.next().await {
yield msg;
Expand Down
16 changes: 11 additions & 5 deletions src/fedimint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ impl Wallet {
Ok(())
}

pub async fn join_federation(&self, invite_code: InviteCode) -> anyhow::Result<()> {
pub async fn join_federation(
&self,
invite_code: InviteCode,
) -> anyhow::Result<BTreeMap<FederationId, FederationView>> {
// Note: We're intentionally locking the clients mutex earlier than
// necessary so that the lock is held while we're accessing the data directory.
let mut clients = self.clients.lock().await;
Expand All @@ -163,7 +166,7 @@ impl Wallet {

// Short-circuit if we're already connected to this federation.
if federation_data_dir.is_dir() {
return Ok(());
return Ok(Self::get_current_state(clients).await);
}

let db: Database = RocksDb::open(federation_data_dir)?.into();
Expand All @@ -172,14 +175,17 @@ impl Wallet {

clients.insert(federation_id, client);

Ok(())
Ok(Self::get_current_state(clients).await)
}

// TODO: Call `ClientModule::leave()` for every module.
// https://docs.rs/fedimint-client/0.4.2/fedimint_client/module/trait.ClientModule.html#method.leave
// Currently it isn't implemented for the `LightningClientModule`, so for now we're just checking
// that the client has a zero balance.
pub async fn leave_federation(&self, federation_id: FederationId) -> anyhow::Result<()> {
pub async fn leave_federation(
&self,
federation_id: FederationId,
) -> anyhow::Result<BTreeMap<FederationId, FederationView>> {
// Note: We're intentionally locking the clients mutex earlier than
// necessary so that the lock is held while we're accessing the data directory.
let mut clients = self.clients.lock().await;
Expand All @@ -206,7 +212,7 @@ impl Wallet {
}
}

Ok(())
Ok(Self::get_current_state(clients).await)
}

async fn get_current_state(
Expand Down
35 changes: 20 additions & 15 deletions src/routes/bitcoin_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ pub enum Message {
config_invite_code: InviteCode,
},

JoinFedimintFederation(InviteCode),
JoinFederation(InviteCode),
ConnectedToFederation,

LeaveFederation(FederationId),
LeftFederation(FederationId),
LeftFederation((FederationId, BTreeMap<FederationId, FederationView>)),
FailedToLeaveFederation((FederationId, Arc<anyhow::Error>)),

Send(send::Message),
Expand Down Expand Up @@ -161,15 +161,16 @@ impl Page {

Task::none()
}
Message::JoinFedimintFederation(invite_code) => {
Message::JoinFederation(invite_code) => {
let wallet = self.connected_state.wallet.clone();

Task::future(async move {
wallet.join_federation(invite_code).await.unwrap();
app::Message::Routes(super::Message::BitcoinWalletPage(
Message::ConnectedToFederation,
))
let federation_views = wallet.join_federation(invite_code).await.unwrap();
app::Message::UpdateFederationViews(federation_views)
})
.chain(Task::done(app::Message::Routes(
super::Message::BitcoinWalletPage(Message::ConnectedToFederation),
)))
}
Message::ConnectedToFederation => {
// TODO: Do something here, or remove `ConnectedToFederation` message variant.
Expand All @@ -181,21 +182,25 @@ impl Page {

Task::future(async move {
match wallet.leave_federation(federation_id).await {
Ok(()) => app::Message::Routes(super::Message::BitcoinWalletPage(
Message::LeftFederation(federation_id),
)),
Ok(federation_views) => {
app::Message::Routes(super::Message::BitcoinWalletPage(
Message::LeftFederation((federation_id, federation_views)),
))
}
Err(err) => app::Message::Routes(super::Message::BitcoinWalletPage(
Message::FailedToLeaveFederation((federation_id, Arc::from(err))),
)),
}
})
}
Message::LeftFederation(_federation_id) => {
Message::LeftFederation((_federation_id, federation_views)) => {
// TODO: Display toast message.

Task::done(app::Message::Routes(super::Message::Navigate(
RouteName::BitcoinWallet(SubrouteName::List),
)))
Task::done(app::Message::UpdateFederationViews(federation_views)).chain(Task::done(
app::Message::Routes(super::Message::Navigate(RouteName::BitcoinWallet(
SubrouteName::List,
))),
))
}
Message::FailedToLeaveFederation((_federation_id, _err)) => {
// TODO: Implement this (including displaying a toast message).
Expand Down Expand Up @@ -508,7 +513,7 @@ impl Add {
.on_press_maybe(self.parsed_federation_invite_code_state_or.as_ref().map(
|parsed_federation_invite_code_state| {
app::Message::Routes(super::Message::BitcoinWalletPage(
Message::JoinFedimintFederation(
Message::JoinFederation(
parsed_federation_invite_code_state.invite_code.clone(),
),
))
Expand Down

0 comments on commit 8f1c69d

Please sign in to comment.