This commit is contained in:
Matthew Fosse 2025-01-10 17:52:01 -08:00
parent 582830de49
commit 840c15173d
4 changed files with 46 additions and 23 deletions

View file

@ -102,4 +102,6 @@ abstract class WalletBase<BalanceType extends Balance, HistoryType extends Trans
Future<bool> verifyMessage(String message, String signature, {String? address = null}); Future<bool> verifyMessage(String message, String signature, {String? address = null});
bool isTestnet = false; bool isTestnet = false;
Future<void> closeWallet() async {}
} }

View file

@ -276,6 +276,19 @@ abstract class MoneroWalletBase
monero_wallet.closeCurrentWallet(); monero_wallet.closeCurrentWallet();
} }
@override
Future<void> 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 @override
Future<PendingTransaction> createTransaction(Object credentials) async { Future<PendingTransaction> createTransaction(Object credentials) async {
final _credentials = credentials as MoneroTransactionCreationCredentials; final _credentials = credentials as MoneroTransactionCreationCredentials;

View file

@ -519,7 +519,10 @@ Future<void> onStart(ServiceInstance service) async {
// if (lastAppState == AppLifecycleState.resumed && serviceState != "FOREGROUND") { // if (lastAppState == AppLifecycleState.resumed && serviceState != "FOREGROUND") {
// setForeground(); // setForeground();
// } // }
if (lastAppStates.length < 5) return; if (lastAppStates.length < 5) {
service.invoke("serviceState", {"state": serviceState});
return;
}
if (lastAppState == AppLifecycleState.paused && serviceState != "READY") { if (lastAppState == AppLifecycleState.paused && serviceState != "READY") {
setReady(); setReady();
} }
@ -527,6 +530,7 @@ Future<void> onStart(ServiceInstance service) async {
if (lastAppStates.every((state) => state == AppLifecycleState.paused) && !bgSyncStarted) { if (lastAppStates.every((state) => state == AppLifecycleState.paused) && !bgSyncStarted) {
setBackground(); setBackground();
} }
service.invoke("serviceState", {"state": serviceState});
}); });
} }
@ -607,8 +611,8 @@ class BackgroundTasks {
FlutterBackgroundService bgService = FlutterBackgroundService(); FlutterBackgroundService bgService = FlutterBackgroundService();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
Timer? _pingTimer; static Timer? _pingTimer;
String serviceState = "NOT_RUNNING"; static String serviceState = "NOT_RUNNING";
void serviceBackground() { void serviceBackground() {
bgService.invoke("setBackground"); bgService.invoke("setBackground");
@ -628,7 +632,7 @@ class BackgroundTasks {
final settingsStore = getIt.get<SettingsStore>(); final settingsStore = getIt.get<SettingsStore>();
bool showNotifications = settingsStore.showSyncNotification; bool showNotifications = settingsStore.showSyncNotification;
bgService.invoke("stopService"); bgService.invoke("stopService");
await Future.delayed(const Duration(seconds: 3)); await Future.delayed(const Duration(seconds: 5));
initializeService(bgService, showNotifications); initializeService(bgService, showNotifications);
bgService.invoke("setForeground"); bgService.invoke("setForeground");
} }
@ -637,6 +641,12 @@ class BackgroundTasks {
return await bgService.isRunning(); return await bgService.isRunning();
} }
Future<bool> isBackgroundSyncing() async {
printV("serviceState: ${serviceState}");
printV("isRunning: ${await bgService.isRunning()}");
return await bgService.isRunning() && serviceState == "BACKGROUND";
}
void serviceReady() { void serviceReady() {
final settingsStore = getIt.get<SettingsStore>(); final settingsStore = getIt.get<SettingsStore>();
bool showNotifications = settingsStore.showSyncNotification; bool showNotifications = settingsStore.showSyncNotification;
@ -694,10 +704,12 @@ class BackgroundTasks {
}); });
bgService.on("serviceState").listen((event) { bgService.on("serviceState").listen((event) {
printV("UPDATING SERVICE STATE: ${event?["state"]}");
serviceState = event?["state"] as String; serviceState = event?["state"] as String;
}); });
await initializeService(bgService, useNotifications); await initializeService(bgService, useNotifications);
bgService.invoke("setForeground");
} catch (error, stackTrace) { } catch (error, stackTrace) {
printV(error); printV(error);
printV(stackTrace); printV(stackTrace);

View file

@ -168,34 +168,30 @@ class RootState extends State<Root> with WidgetsBindingObserver {
// background service handling: // background service handling:
printV("state: $state"); printV("state: $state");
final appStore = widget.appStore;
final wallet = appStore.wallet;
switch (state) { switch (state) {
case AppLifecycleState.resumed: case AppLifecycleState.resumed:
// // restart the background service if it was running before: bool isBackgroundSyncing = await getIt.get<BackgroundTasks>().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<BackgroundTasks>().serviceForeground(); await getIt.get<BackgroundTasks>().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; break;
case AppLifecycleState.hidden: case AppLifecycleState.hidden:
case AppLifecycleState.inactive: case AppLifecycleState.inactive:
case AppLifecycleState.detached: case AppLifecycleState.detached:
break; break;
case AppLifecycleState.paused: case AppLifecycleState.paused:
widget.appStore.wallet?.stopSync(); await wallet?.stopSync();
await wallet?.close();
// getIt.get<BackgroundTasks>().serviceReady(); // getIt.get<BackgroundTasks>().serviceReady();
// // if (FeatureFlag.isBackgroundSyncEnabled && // // if (FeatureFlag.isBackgroundSyncEnabled &&
// // syncingWalletTypes.contains(widget.appStore.wallet?.type)) { // // syncingWalletTypes.contains(widget.appStore.wallet?.type)) {