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});
bool isTestnet = false;
Future<void> closeWallet() async {}
}

View file

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

View file

@ -519,7 +519,10 @@ Future<void> 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<void> 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<SettingsStore>();
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<bool> isBackgroundSyncing() async {
printV("serviceState: ${serviceState}");
printV("isRunning: ${await bgService.isRunning()}");
return await bgService.isRunning() && serviceState == "BACKGROUND";
}
void serviceReady() {
final settingsStore = getIt.get<SettingsStore>();
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);

View file

@ -168,34 +168,30 @@ class RootState extends State<Root> 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<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();
// _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<BackgroundTasks>().serviceReady();
// // if (FeatureFlag.isBackgroundSyncEnabled &&
// // syncingWalletTypes.contains(widget.appStore.wallet?.type)) {