From 5809687116423476b4956ae01d97d2ce1bd19f5c Mon Sep 17 00:00:00 2001
From: sneurlax <sneurlax@gmail.com>
Date: Tue, 17 Oct 2023 22:38:51 -0500
Subject: [PATCH] hook back and close buttons up to fusion's stop

---
 lib/pages/cashfusion/cashfusion_view.dart     | 32 ++++++++++++-------
 .../cashfusion/sub_widgets/fusion_dialog.dart | 22 ++++++++++++-
 2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/lib/pages/cashfusion/cashfusion_view.dart b/lib/pages/cashfusion/cashfusion_view.dart
index 2439890b9..9ec2171d7 100644
--- a/lib/pages/cashfusion/cashfusion_view.dart
+++ b/lib/pages/cashfusion/cashfusion_view.dart
@@ -85,17 +85,6 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
 
   @override
   void dispose() {
-    // Stop the fusion process on dispose.
-    //
-    // Hack to stop fusion when a mobile user clicks back.
-    final fusionWallet = ref
-        .read(walletsChangeNotifierProvider)
-        .getManager(widget.walletId)
-        .wallet as FusionWalletInterface;
-
-    // We probably want to await this and showLoading while it stops.
-    unawaited(fusionWallet.stop());
-
     serverController.dispose();
     portController.dispose();
     fusionRoundController.dispose();
@@ -116,7 +105,9 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
               Theme.of(context).extension<StackColors>()!.background,
           appBar: AppBar(
             automaticallyImplyLeading: false,
-            leading: const AppBarBackButton(),
+            leading: AppBarBackButton(onPressed: () {
+              _stop();
+            }),
             title: Text(
               "CashFusion",
               style: STextStyles.navBarTitle(context),
@@ -457,4 +448,21 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
       ),
     );
   }
+
+  /// Stops the fusion process.
+  ///
+  /// This is called when the user presses the back button.
+  void _stop() async {
+    final fusionWallet = ref
+        .read(walletsChangeNotifierProvider)
+        .getManager(widget.walletId)
+        .wallet as FusionWalletInterface;
+
+    await fusionWallet.stop();
+    // TODO await successful cancellation and showLoading while it stops.
+
+    if (mounted) {
+      Navigator.of(context).pop();
+    }
+  }
 }
diff --git a/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart b/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart
index 6e0399232..975bb6176 100644
--- a/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart
+++ b/lib/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart
@@ -55,7 +55,9 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
                     ),
                   ),
                   DesktopDialogCloseButton(
-                    onPressedOverride: () => Navigator.of(context).pop(true),
+                    onPressedOverride: () {
+                      _stop();
+                    },
                   ),
                 ],
               ),
@@ -107,4 +109,22 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
       ),
     );
   }
+
+  /// Stops the fusion process.
+  ///
+  /// This is called when the user presses the back button.
+  void _stop() async {
+    print(12121212);
+    final fusionWallet = ref
+        .read(walletsChangeNotifierProvider)
+        .getManager(widget.walletId)
+        .wallet as FusionWalletInterface;
+
+    await fusionWallet.stop();
+    // TODO await successful cancellation and showLoading while it stops.
+
+    if (mounted) {
+      Navigator.of(context).pop();
+    }
+  }
 }