diff --git a/cw_core/lib/wallet_base.dart b/cw_core/lib/wallet_base.dart index e3822ae2d..85f180a09 100644 --- a/cw_core/lib/wallet_base.dart +++ b/cw_core/lib/wallet_base.dart @@ -102,4 +102,6 @@ abstract class WalletBase verifyMessage(String message, String signature, {String? address = null}); bool isTestnet = false; + + Future closeWallet() async {} } diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index f4ecb9014..629778c8f 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -276,6 +276,19 @@ abstract class MoneroWalletBase monero_wallet.closeCurrentWallet(); } + @override + Future closeWallet() async { + printV("closing wallet"); + final currentWalletDirPath = await pathForWalletDir(name: name, type: type); + final wmaddr = wmPtr.address; + final waddr = openedWalletsByPath["$currentWalletDirPath/$name"]!.address; + await Isolate.run(() { + monero.WalletManager_closeWallet( + Pointer.fromAddress(wmaddr), Pointer.fromAddress(waddr), true); + }); + await init(); + } + @override Future createTransaction(Object credentials) async { final _credentials = credentials as MoneroTransactionCreationCredentials; diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index 1ca9a1536..ac3f696c9 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -519,7 +519,10 @@ Future onStart(ServiceInstance service) async { // if (lastAppState == AppLifecycleState.resumed && serviceState != "FOREGROUND") { // setForeground(); // } - if (lastAppStates.length < 5) return; + if (lastAppStates.length < 5) { + service.invoke("serviceState", {"state": serviceState}); + return; + } if (lastAppState == AppLifecycleState.paused && serviceState != "READY") { setReady(); } @@ -527,6 +530,7 @@ Future onStart(ServiceInstance service) async { if (lastAppStates.every((state) => state == AppLifecycleState.paused) && !bgSyncStarted) { setBackground(); } + service.invoke("serviceState", {"state": serviceState}); }); } @@ -607,8 +611,8 @@ class BackgroundTasks { FlutterBackgroundService bgService = FlutterBackgroundService(); FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); - Timer? _pingTimer; - String serviceState = "NOT_RUNNING"; + static Timer? _pingTimer; + static String serviceState = "NOT_RUNNING"; void serviceBackground() { bgService.invoke("setBackground"); @@ -628,7 +632,7 @@ class BackgroundTasks { final settingsStore = getIt.get(); bool showNotifications = settingsStore.showSyncNotification; bgService.invoke("stopService"); - await Future.delayed(const Duration(seconds: 3)); + await Future.delayed(const Duration(seconds: 5)); initializeService(bgService, showNotifications); bgService.invoke("setForeground"); } @@ -637,6 +641,12 @@ class BackgroundTasks { return await bgService.isRunning(); } + Future isBackgroundSyncing() async { + printV("serviceState: ${serviceState}"); + printV("isRunning: ${await bgService.isRunning()}"); + return await bgService.isRunning() && serviceState == "BACKGROUND"; + } + void serviceReady() { final settingsStore = getIt.get(); bool showNotifications = settingsStore.showSyncNotification; @@ -694,10 +704,12 @@ class BackgroundTasks { }); bgService.on("serviceState").listen((event) { + printV("UPDATING SERVICE STATE: ${event?["state"]}"); serviceState = event?["state"] as String; }); await initializeService(bgService, useNotifications); + bgService.invoke("setForeground"); } catch (error, stackTrace) { printV(error); printV(stackTrace); diff --git a/lib/src/screens/root/root.dart b/lib/src/screens/root/root.dart index 53005c63d..18892ae02 100644 --- a/lib/src/screens/root/root.dart +++ b/lib/src/screens/root/root.dart @@ -168,34 +168,30 @@ class RootState extends State with WidgetsBindingObserver { // background service handling: printV("state: $state"); + final appStore = widget.appStore; + final wallet = appStore.wallet; switch (state) { case AppLifecycleState.resumed: - // // restart the background service if it was running before: + bool isBackgroundSyncing = await getIt.get().isBackgroundSyncing(); + + printV("isBackgroundSyncing: $isBackgroundSyncing"); + + if (!isBackgroundSyncing) { + return; + } + + await wallet?.stopSync(isBackgroundSync: true); + // await wallet?.closeWallet(); + // restart the background service if it was running before: await getIt.get().serviceForeground(); - // _stateTimer?.cancel(); - // if (!wasInBackground) { - // return; - // } - // wasInBackground = false; - // final appStore = widget.appStore; - // final wallet = appStore.wallet; - // if (syncingWalletTypes.contains(wallet?.type)) { - // // wait a few seconds before starting the sync to make sure the background service is fully exited: - // Future.delayed(const Duration(seconds: 50), () async { - // final node = appStore.settingsStore.getCurrentNode(wallet!.type); - // await wallet.stopSync(); - // await wallet.init(); - // wallet.connectToNode(node: node); - // wallet.startSync(); - // }); - // } break; case AppLifecycleState.hidden: case AppLifecycleState.inactive: case AppLifecycleState.detached: break; case AppLifecycleState.paused: - widget.appStore.wallet?.stopSync(); + await wallet?.stopSync(); + await wallet?.close(); // getIt.get().serviceReady(); // // if (FeatureFlag.isBackgroundSyncEnabled && // // syncingWalletTypes.contains(widget.appStore.wallet?.type)) {