|
1 | 1 | use std::str::FromStr;
|
2 | 2 |
|
3 | 3 | use fedimint_core::{
|
4 |
| - config::{ClientConfig, META_FEDERATION_NAME_KEY}, |
| 4 | + config::{ClientConfig, FederationId, META_FEDERATION_NAME_KEY}, |
5 | 5 | invite_code::InviteCode,
|
6 | 6 | Amount,
|
7 | 7 | };
|
@@ -42,6 +42,9 @@ pub enum Message {
|
42 | 42 | JoinFederation(InviteCode),
|
43 | 43 | JoinedFederation(InviteCode),
|
44 | 44 |
|
| 45 | + LeaveFederation(FederationId), |
| 46 | + LeftFederation(FederationId), |
| 47 | + |
45 | 48 | Send(send::Message),
|
46 | 49 | Receive(receive::Message),
|
47 | 50 |
|
@@ -196,6 +199,44 @@ impl Page {
|
196 | 199 |
|
197 | 200 | Task::none()
|
198 | 201 | }
|
| 202 | + Message::LeaveFederation(federation_id) => { |
| 203 | + let wallet = self.connected_state.wallet.clone(); |
| 204 | + |
| 205 | + Task::stream(async_stream::stream! { |
| 206 | + match wallet.leave_federation(federation_id).await { |
| 207 | + Ok(()) => { |
| 208 | + yield app::Message::AddToast(Toast { |
| 209 | + title: "Left federation".to_string(), |
| 210 | + body: "You have successfully left the federation.".to_string(), |
| 211 | + status: ToastStatus::Good, |
| 212 | + }); |
| 213 | + |
| 214 | + yield app::Message::Routes(super::Message::BitcoinWalletPage( |
| 215 | + Message::LeftFederation(federation_id) |
| 216 | + )); |
| 217 | + } |
| 218 | + Err(err) => { |
| 219 | + yield app::Message::AddToast(Toast { |
| 220 | + title: "Failed to leave federation".to_string(), |
| 221 | + body: format!("Failed to leave the federation: {err}"), |
| 222 | + status: ToastStatus::Bad, |
| 223 | + }); |
| 224 | + } |
| 225 | + } |
| 226 | + }) |
| 227 | + } |
| 228 | + Message::LeftFederation(federation_id) => { |
| 229 | + // A verbose way of saying "if the user is currently on the FederationDetails page and the federation ID matches the one that was just left, navigate back to the List page". |
| 230 | + if let Subroute::FederationDetails(federation_details) = &self.subroute { |
| 231 | + if federation_details.view.federation_id == federation_id { |
| 232 | + return Task::done(app::Message::Routes(super::Message::Navigate( |
| 233 | + RouteName::BitcoinWallet(SubrouteName::List), |
| 234 | + ))); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + Task::none() |
| 239 | + } |
199 | 240 | Message::Send(send_message) => {
|
200 | 241 | if let Subroute::Send(send_page) = &mut self.subroute {
|
201 | 242 | send_page.update(send_message)
|
@@ -444,6 +485,27 @@ impl FederationDetails {
|
444 | 485 | );
|
445 | 486 | }
|
446 | 487 |
|
| 488 | + // TODO: Add a function to `Wallet` to check whether we can safely leave a federation. |
| 489 | + // Call it here rather and get rid of `has_zero_balance`. |
| 490 | + let has_zero_balance = self.view.balance.msats == 0; |
| 491 | + |
| 492 | + if !has_zero_balance { |
| 493 | + container = container.push( |
| 494 | + Text::new("Must have a zero balance in this federation in order to leave.") |
| 495 | + .size(20), |
| 496 | + ); |
| 497 | + } |
| 498 | + |
| 499 | + container = container.push( |
| 500 | + icon_button("Leave Federation", SvgIcon::Delete, PaletteColor::Danger).on_press_maybe( |
| 501 | + has_zero_balance.then(|| { |
| 502 | + app::Message::Routes(super::Message::BitcoinWalletPage( |
| 503 | + Message::LeaveFederation(self.view.federation_id), |
| 504 | + )) |
| 505 | + }), |
| 506 | + ), |
| 507 | + ); |
| 508 | + |
447 | 509 | container = container.push(
|
448 | 510 | icon_button("Back", SvgIcon::ArrowBack, PaletteColor::Background).on_press(
|
449 | 511 | app::Message::Routes(super::Message::Navigate(RouteName::BitcoinWallet(
|
|
0 commit comments