From df70bb7a569401a2816f811cfa571b58d68e8829 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Mon, 6 Jan 2025 17:42:06 -0500 Subject: [PATCH] save --- cw_monero/lib/monero_wallet.dart | 36 +++++++++++++++++------------- lib/entities/background_tasks.dart | 19 +++++++++++++++- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index fc1b875ca..41378e29c 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -203,30 +203,34 @@ abstract class MoneroWalletBase @override Future startSync({bool isBackgroundSync = false}) async { - if (isBackgroundSync) { - try { - syncStatus = AttemptingSyncStatus(); - monero_wallet.startBackgroundSync(); - isBackgroundSyncing = true; - _setListeners(); - _listener?.start(); - return; - } catch (e) { - isBackgroundSyncing = false; - syncStatus = FailedSyncStatus(); - printV(e); - rethrow; - } - } + // if (isBackgroundSync) { + // try { + // syncStatus = AttemptingSyncStatus(); + // monero_wallet.startBackgroundSync(); + // isBackgroundSyncing = true; + // _setListeners(); + // _listener?.start(); + // return; + // } catch (e) { + // isBackgroundSyncing = false; + // syncStatus = FailedSyncStatus(); + // printV(e); + // rethrow; + // } + // } try { syncStatus = AttemptingSyncStatus(); - monero_wallet.startRefresh(); + // monero_wallet.startRefresh(); monero_wallet.setupBackgroundSync( backgroundSyncType: 2, walletPassword: password, backgroundCachePassword: "testing-cache-password", ); + monero_wallet.startBackgroundSync(); + if (isBackgroundSync) { + isBackgroundSyncing = true; + } _setListeners(); _listener?.start(); } catch (e) { diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index a22154806..bf005c9b7 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -156,6 +156,7 @@ Future onStart(ServiceInstance service) async { printV("error stopping sync: $e"); } // stop the service itself: + service.invoke("serviceState", {"state": "NOT_RUNNING"}); await service.stopSelf(); }); @@ -163,10 +164,15 @@ Future onStart(ServiceInstance service) async { printV(event); }); - service.on("setForeground").listen((event) async { + void setForeground() { bgSyncStarted = false; _syncTimer?.cancel(); setNotificationStandby(flutterLocalNotificationsPlugin); + } + + service.on("setForeground").listen((event) async { + setForeground(); + service.invoke("serviceState", {"state": "FOREGROUND"}); }); void setReady() { @@ -175,6 +181,7 @@ Future onStart(ServiceInstance service) async { service.on("setReady").listen((event) async { setReady(); + service.invoke("serviceState", {"state": "READY"}); }); service.on("foregroundPing").listen((event) async { @@ -488,6 +495,7 @@ Future onStart(ServiceInstance service) async { if (fgCount > 10) { fgCount = 0; setBackground(); + service.invoke("serviceState", {"state": "BACKGROUND"}); _bgTimer?.cancel(); } }); @@ -571,6 +579,7 @@ class BackgroundTasks { FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); Timer? _pingTimer; + String serviceState = "NOT_RUNNING"; void serviceBackground() { bgService.invoke("setBackground"); @@ -582,6 +591,10 @@ class BackgroundTasks { } Future serviceForeground() async { + if (serviceState == "FOREGROUND") { + return; + } + final settingsStore = getIt.get(); bool showNotifications = settingsStore.showSyncNotification; bgService.invoke("stopService"); @@ -645,6 +658,10 @@ class BackgroundTasks { getIt.get().foregroundPing(); }); + bgService.on("serviceState").listen((event) { + serviceState = event?["state"] as String; + }); + await initializeService(bgService, useNotifications); } catch (error, stackTrace) { printV(error);