From 1b8218f9708fbe679122383209b1d449edeeaa17 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 24 Jul 2024 17:21:52 -0500 Subject: [PATCH] bg-sync testing --- cw_mweb/lib/cw_mweb.dart | 4 +- ios/Runner/AppDelegate.swift | 1 + ios/Runner/InfoBase.plist | 1 + lib/entities/background_tasks.dart | 64 ++++++++++++-- lib/lightning/lightning.dart | 43 ++++++++++ .../settings/connection_sync_page.dart | 83 ++++++++++--------- lib/utils/feature_flag.dart | 2 +- 7 files changed, 149 insertions(+), 49 deletions(-) create mode 100644 lib/lightning/lightning.dart diff --git a/cw_mweb/lib/cw_mweb.dart b/cw_mweb/lib/cw_mweb.dart index 765ba7911..f11100f61 100644 --- a/cw_mweb/lib/cw_mweb.dart +++ b/cw_mweb/lib/cw_mweb.dart @@ -13,7 +13,9 @@ class CwMweb { options: const ChannelOptions( credentials: ChannelCredentials.insecure(), keepAlive: ClientKeepAliveOptions(permitWithoutCalls: true), - )), + ), channelShutdownHandler: () { + print("SHUTDOWN HANDLER CALLED @@@@@@@@@@@@@@@@@"); + }), ); } diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index acdfa4346..808d03e47 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -25,6 +25,7 @@ import workmanager } WorkmanagerPlugin.registerTask(withIdentifier: "com.fotolockr.cakewallet.monero_sync_task") + WorkmanagerPlugin.registerTask(withIdentifier: "com.fotolockr.cakewallet.mweb_sync_task") makeSecure() diff --git a/ios/Runner/InfoBase.plist b/ios/Runner/InfoBase.plist index aec00022b..6ba587922 100644 --- a/ios/Runner/InfoBase.plist +++ b/ios/Runner/InfoBase.plist @@ -5,6 +5,7 @@ BGTaskSchedulerPermittedIdentifiers com.fotolockr.cakewallet.monero_sync_task + com.fotolockr.cakewallet.mweb_sync_task CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index a373ca0ad..d7a4bbc9d 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -17,12 +17,42 @@ import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/di.dart'; const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task"; +const mwebSyncTaskKey = "com.fotolockr.cakewallet.mweb_sync_task"; @pragma('vm:entry-point') void callbackDispatcher() { Workmanager().executeTask((task, inputData) async { try { switch (task) { + case mwebSyncTaskKey: + final List ltcWallets = getIt + .get() + .wallets + .where((element) => [WalletType.litecoin].contains(element.type)) + .toList(); + + if (ltcWallets.isEmpty) { + return Future.error("No ltc wallets found"); + } + + final walletLoadingService = getIt.get(); + + var wallet = + await walletLoadingService.load(ltcWallets.first.type, ltcWallets.first.name); + await wallet.startSync(); + + for (int i = 0;; i++) { + await Future.delayed(const Duration(seconds: 1)); + if (wallet.syncStatus.progress() == 1.0) { + break; + } + if (i > 600) { + return Future.error("Synchronization Timed out"); + } + } + + break; + case moneroSyncTaskKey: /// The work manager runs on a separate isolate from the main flutter isolate. @@ -98,8 +128,13 @@ class BackgroundTasks { .wallets .any((element) => element.type == WalletType.monero); + bool hasLitecoin = getIt + .get() + .wallets + .any((element) => element.type == WalletType.litecoin); + /// if its not android nor ios, or the user has no monero wallets; exit - if (!DeviceInfo.instance.isMobile || !hasMonero) { + if (!DeviceInfo.instance.isMobile || (!hasMonero && !hasLitecoin)) { return; } @@ -128,10 +163,18 @@ class BackgroundTasks { ); if (Platform.isIOS) { + // await Workmanager().registerOneOffTask( + // moneroSyncTaskKey, + // moneroSyncTaskKey, + // initialDelay: syncMode.frequency, + // existingWorkPolicy: ExistingWorkPolicy.replace, + // inputData: inputData, + // constraints: constraints, + // ); await Workmanager().registerOneOffTask( - moneroSyncTaskKey, - moneroSyncTaskKey, - initialDelay: syncMode.frequency, + mwebSyncTaskKey, + mwebSyncTaskKey, + initialDelay: Duration(seconds: 10), existingWorkPolicy: ExistingWorkPolicy.replace, inputData: inputData, constraints: constraints, @@ -139,9 +182,18 @@ class BackgroundTasks { return; } + // await Workmanager().registerPeriodicTask( + // moneroSyncTaskKey, + // moneroSyncTaskKey, + // initialDelay: syncMode.frequency, + // frequency: syncMode.frequency, + // existingWorkPolicy: changeExisting ? ExistingWorkPolicy.replace : ExistingWorkPolicy.keep, + // inputData: inputData, + // constraints: constraints, + // ); await Workmanager().registerPeriodicTask( - moneroSyncTaskKey, - moneroSyncTaskKey, + mwebSyncTaskKey, + mwebSyncTaskKey, initialDelay: syncMode.frequency, frequency: syncMode.frequency, existingWorkPolicy: changeExisting ? ExistingWorkPolicy.replace : ExistingWorkPolicy.keep, diff --git a/lib/lightning/lightning.dart b/lib/lightning/lightning.dart new file mode 100644 index 000000000..1630994b2 --- /dev/null +++ b/lib/lightning/lightning.dart @@ -0,0 +1,43 @@ +import 'package:cw_core/transaction_priority.dart'; +import 'package:cw_core/wallet_info.dart'; +import 'package:cw_core/unspent_coins_info.dart'; +import 'package:cw_core/wallet_service.dart'; +import 'package:cw_core/receive_page_option.dart'; +import 'package:cw_core/crypto_amount_format.dart'; +import 'package:hive/hive.dart'; +import 'package:mobx/mobx.dart'; +import 'package:intl/intl.dart'; + + + +Lightning? lightning; + +abstract class Lightning { + String formatterLightningAmountToString({required int amount}); + double formatterLightningAmountToDouble({required int amount}); + int formatterStringDoubleToLightningAmount(String amount); + WalletService createLightningWalletService( + Box walletInfoSource, Box unspentCoinSource); + List getLightningReceivePageOptions(); + String satsToLightningString(int sats); + ReceivePageOption getOptionInvoice(); + ReceivePageOption getOptionOnchain(); + String bitcoinAmountToLightningString({required int amount}); + int bitcoinAmountToLightningAmount({required int amount}); + double bitcoinDoubleToLightningDouble({required double amount}); + double lightningDoubleToBitcoinDouble({required double amount}); + Map getIncomingPayments(Object wallet); + void clearIncomingPayments(Object wallet); + String lightningTransactionPriorityWithLabel(TransactionPriority priority, int rate, {int? customRate}); + List getTransactionPriorities(); + TransactionPriority getLightningTransactionPriorityCustom(); + int getFeeRate(Object wallet, TransactionPriority priority); + int getMaxCustomFeeRate(Object wallet); + Future fetchFees(Object wallet); + Future calculateEstimatedFeeAsync(Object wallet, TransactionPriority? priority, int? amount); + Future getEstimatedFeeWithFeeRate(Object wallet, int feeRate, int? amount); + TransactionPriority getDefaultTransactionPriority(); + TransactionPriority deserializeLightningTransactionPriority({required int raw}); + String getBreezApiKey(); +} + \ No newline at end of file diff --git a/lib/src/screens/settings/connection_sync_page.dart b/lib/src/screens/settings/connection_sync_page.dart index c4d85a3a5..6b8eb943f 100644 --- a/lib/src/screens/settings/connection_sync_page.dart +++ b/lib/src/screens/settings/connection_sync_page.dart @@ -44,52 +44,52 @@ class ConnectionSyncPage extends BasePage { : S.current.rescan, handler: (context) => Navigator.of(context).pushNamed(Routes.rescan), ), - if (DeviceInfo.instance.isMobile && FeatureFlag.isBackgroundSyncEnabled) ...[ - Observer(builder: (context) { - return SettingsPickerCell( - title: S.current.background_sync_mode, - items: SyncMode.all, - displayItem: (SyncMode syncMode) => syncMode.name, - selectedItem: dashboardViewModel.syncMode, - onItemSelected: (syncMode) async { - dashboardViewModel.setSyncMode(syncMode); + // if (DeviceInfo.instance.isMobile && FeatureFlag.isBackgroundSyncEnabled) ...[ + Observer(builder: (context) { + return SettingsPickerCell( + title: S.current.background_sync_mode, + items: SyncMode.all, + displayItem: (SyncMode syncMode) => syncMode.name, + selectedItem: dashboardViewModel.syncMode, + onItemSelected: (syncMode) async { + dashboardViewModel.setSyncMode(syncMode); - if (Platform.isIOS) return; + if (Platform.isIOS) return; - if (syncMode.type != SyncType.disabled) { - final isDisabled = await isBatteryOptimizationDisabled(); + if (syncMode.type != SyncType.disabled) { + final isDisabled = await isBatteryOptimizationDisabled(); - if (isDisabled) return; + if (isDisabled) return; - await showPopUp( - context: context, - builder: (BuildContext dialogContext) { - return AlertWithTwoActions( - alertTitle: S.current.disableBatteryOptimization, - alertContent: S.current.disableBatteryOptimizationDescription, - leftButtonText: S.of(context).cancel, - rightButtonText: S.of(context).ok, - actionLeftButton: () => Navigator.of(dialogContext).pop(), - actionRightButton: () async { - await requestDisableBatteryOptimization(); + await showPopUp( + context: context, + builder: (BuildContext dialogContext) { + return AlertWithTwoActions( + alertTitle: S.current.disableBatteryOptimization, + alertContent: S.current.disableBatteryOptimizationDescription, + leftButtonText: S.of(context).cancel, + rightButtonText: S.of(context).ok, + actionLeftButton: () => Navigator.of(dialogContext).pop(), + actionRightButton: () async { + await requestDisableBatteryOptimization(); - Navigator.of(dialogContext).pop(); - }, - ); - }, - ); - } - }); - }), - Observer(builder: (context) { - return SettingsSwitcherCell( - title: S.current.sync_all_wallets, - value: dashboardViewModel.syncAll, - onValueChange: (_, bool value) => dashboardViewModel.setSyncAll(value), - ); - }), - ], + Navigator.of(dialogContext).pop(); + }, + ); + }, + ); + } + }); + }), + Observer(builder: (context) { + return SettingsSwitcherCell( + title: S.current.sync_all_wallets, + value: dashboardViewModel.syncAll, + onValueChange: (_, bool value) => dashboardViewModel.setSyncAll(value), + ); + }), ], + // ], SettingsCellWithArrow( title: S.current.manage_nodes, handler: (context) => Navigator.of(context).pushNamed(Routes.manageNodes), @@ -109,7 +109,8 @@ class ConnectionSyncPage extends BasePage { }, ), if (isWalletConnectCompatibleChain(dashboardViewModel.wallet.type) && - !dashboardViewModel.wallet.isHardwareWallet) ...[ // ToDo: Remove this line once WalletConnect is implemented + !dashboardViewModel.wallet.isHardwareWallet) ...[ + // ToDo: Remove this line once WalletConnect is implemented WalletConnectTile( onTap: () => Navigator.of(context).pushNamed(Routes.walletConnectConnectionsListing), ), diff --git a/lib/utils/feature_flag.dart b/lib/utils/feature_flag.dart index f71e6b489..86579194b 100644 --- a/lib/utils/feature_flag.dart +++ b/lib/utils/feature_flag.dart @@ -2,5 +2,5 @@ class FeatureFlag { static const bool isCakePayEnabled = false; static const bool isExolixEnabled = true; static const bool isInAppTorEnabled = false; - static const bool isBackgroundSyncEnabled = false; + static const bool isBackgroundSyncEnabled = true; } \ No newline at end of file