From 5a2a207aecc779d02557b0746bf79a9260ea9e5d Mon Sep 17 00:00:00 2001
From: Matthew Fosse <matt@fosse.co>
Date: Tue, 16 Jul 2024 12:41:13 -0700
Subject: [PATCH] sync status fixes, potential fix for background state issue

---
 cw_bitcoin/lib/electrum_wallet.dart    | 14 +++++++++-----
 cw_mweb/ios/Classes/CwMwebPlugin.swift |  9 +++++++--
 ios/Runner/AppDelegate.swift           |  8 ++++++++
 lib/utils/exception_handler.dart       |  2 +-
 4 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart
index 2f49ae7b8..97d466ffe 100644
--- a/cw_bitcoin/lib/electrum_wallet.dart
+++ b/cw_bitcoin/lib/electrum_wallet.dart
@@ -408,7 +408,9 @@ abstract class ElectrumWalletBase
   @override
   Future<void> startSync() async {
     try {
-      syncStatus = SyncronizingSyncStatus();
+      if (this is! LitecoinWallet) {
+        syncStatus = SyncronizingSyncStatus();
+      }
 
       if (hasSilentPaymentsScanning) {
         await _setInitialHeight();
@@ -426,10 +428,12 @@ abstract class ElectrumWalletBase
       await updateFeeRates();
       Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates());
 
-      if (alwaysScan == true) {
-        _setListeners(walletInfo.restoreHeight);
-      } else {
-        syncStatus = SyncedSyncStatus();
+      if (this is! LitecoinWallet) {
+        if (alwaysScan == true) {
+          _setListeners(walletInfo.restoreHeight);
+        } else {
+          syncStatus = SyncedSyncStatus();
+        }
       }
     } catch (e, stacktrace) {
       print(stacktrace);
diff --git a/cw_mweb/ios/Classes/CwMwebPlugin.swift b/cw_mweb/ios/Classes/CwMwebPlugin.swift
index fa1ac39ad..bd9fff23e 100644
--- a/cw_mweb/ios/Classes/CwMwebPlugin.swift
+++ b/cw_mweb/ios/Classes/CwMwebPlugin.swift
@@ -18,7 +18,7 @@ public class CwMwebPlugin: NSObject, FlutterPlugin {
           result("iOS " + UIDevice.current.systemVersion)
       case "start":
           let args = call.arguments as? [String: String]
-          print("args: \(args)")
+        //   print("args: \(args)")
           let dataDir = args?["dataDir"]
           var error: NSError?
           
@@ -27,18 +27,22 @@ public class CwMwebPlugin: NSObject, FlutterPlugin {
               
               if let server = CwMwebPlugin.server {
                   do {
-                      print("starting server \(CwMwebPlugin.port)")
+                      print("starting server2 \(CwMwebPlugin.port)")
                       try server.start(0, ret0_: &CwMwebPlugin.port)
                       result(CwMwebPlugin.port)
                   } catch let startError as NSError {
+                      print("Server Start Error: \(startError.localizedDescription)")
                       result(FlutterError(code: "Server Start Error", message: startError.localizedDescription, details: nil))
                   }
               } else if let error = error {
+                  print("Server Creation Error: \(error.localizedDescription)")
                   result(FlutterError(code: "Server Creation Error", message: error.localizedDescription, details: nil))
               } else {
+                  print("Unknown Error: Failed to create server")
                   result(FlutterError(code: "Unknown Error", message: "Failed to create server", details: nil))
               }
           } else {
+            print("Server already running on port: \(CwMwebPlugin.port)")
 //              result(FlutterError(code: "Server Already Running", message: "The server is already running", details: nil))
               result(CwMwebPlugin.port)
           }
@@ -51,6 +55,7 @@ public class CwMwebPlugin: NSObject, FlutterPlugin {
   }
 
     deinit {
+        print("Stopping and cleaning up server")
         // Perform cleanup tasks
         CwMwebPlugin.server?.stop()
         CwMwebPlugin.server = nil
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index acdfa4346..26142c477 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -143,4 +143,12 @@ import workmanager
         }
     }
 
+    override func applicationDidBecomeActive(_ application: UIApplication) {
+        signal(SIGPIPE, SIG_IGN);
+    }
+    
+    override func applicationWillEnterForeground(_ application: UIApplication) {
+        signal(SIGPIPE, SIG_IGN);
+    }
+
 }
diff --git a/lib/utils/exception_handler.dart b/lib/utils/exception_handler.dart
index b19b1bb7e..c5c241d87 100644
--- a/lib/utils/exception_handler.dart
+++ b/lib/utils/exception_handler.dart
@@ -81,7 +81,7 @@ class ExceptionHandler {
   }
 
   static void onError(FlutterErrorDetails errorDetails) async {
-    if (kDebugMode) {
+    if (kDebugMode || kProfileMode) {
       FlutterError.presentError(errorDetails);
       debugPrint(errorDetails.toString());
       return;