From 1577096b890eab523504896d878fddbe1357034a Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 4 Sep 2024 09:15:01 -0700 Subject: [PATCH] work on getting monero wallets synced --- lib/entities/background_tasks.dart | 63 +++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index 8ea3e4ddd..01f5ba2cb 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -2,18 +2,23 @@ import 'dart:async'; import 'dart:io'; import 'dart:ui'; +import 'package:cake_wallet/core/wallet_loading_service.dart'; +import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/utils/device_info.dart'; import 'package:cake_wallet/utils/feature_flag.dart'; import 'package:cake_wallet/view_model/settings/sync_mode.dart'; +import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart'; +import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/di.dart'; +import 'package:shared_preferences/shared_preferences.dart'; const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task"; const mwebSyncTaskKey = "com.fotolockr.cakewallet.mweb_sync_task"; @@ -88,6 +93,62 @@ Future onStart(ServiceInstance service) async { print("initialized app configs"); + final currentWallet = getIt.get().wallet; + // don't start syncing immediately: + await currentWallet?.stopSync(); + + final walletLoadingService = getIt.get(); + final typeRaw = getIt.get().getInt(PreferencesKey.currentWalletType); + + bool syncAll = true; + List syncingWallets = []; + + if (syncAll) { + /// get all Monero wallets of the user and sync them + final List moneroWallets = getIt + .get() + .wallets + .where((element) => [WalletType.monero, WalletType.wownero].contains(element.type)) + .toList(); + + for (int i = 0; i < moneroWallets.length; i++) { + final wallet = + await walletLoadingService.load(moneroWallets[i].type, moneroWallets[i].name); + final node = getIt.get().getCurrentNode(moneroWallets[i].type); + await wallet.connectToNode(node: node); + await wallet.startSync(); + syncingWallets.add(wallet); + } + + // get all litecoin wallets and sync them: + final List litecoinWallets = getIt + .get() + .wallets + .where((element) => element.type == WalletType.litecoin) + .toList(); + + // we only need to sync the first litecoin wallet since they share the same collection of blocks + if (litecoinWallets.isNotEmpty) { + var firstWallet = litecoinWallets.first; + final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name); + final node = getIt.get().getCurrentNode(firstWallet.type); + await wallet.connectToNode(node: node); + await wallet.startSync(); + syncingWallets.add(wallet); + } + } else { + // /// if the user chose to sync only active wallet + // /// if the current wallet is monero; sync it only + // if (typeRaw == WalletType.monero.index || typeRaw == WalletType.wownero.index) { + // final name = getIt.get().getString(PreferencesKey.currentWalletName); + // wallet = await walletLoadingService.load(WalletType.values[typeRaw!], name!); + // final node = getIt.get().getCurrentNode(WalletType.values[typeRaw]); + // await wallet.connectToNode(node: node); + // await wallet.startSync(); + // syncingWallets.add(wallet); + // } + } + _syncTimer?.cancel(); _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) { final wallet = getIt.get().wallet; @@ -96,7 +157,7 @@ Future onStart(ServiceInstance service) async { flutterLocalNotificationsPlugin.show( notificationId, "${syncProgress}% Synced", - 'Mweb background sync - ${DateTime.now()}', + 'Background sync - ${DateTime.now()}', const NotificationDetails( android: AndroidNotificationDetails( notificationChannelId,