bg-sync testing

This commit is contained in:
Matthew Fosse 2024-07-24 17:21:52 -05:00
parent c78243f802
commit 1b8218f970
7 changed files with 149 additions and 49 deletions

View file

@ -13,7 +13,9 @@ class CwMweb {
options: const ChannelOptions(
credentials: ChannelCredentials.insecure(),
keepAlive: ClientKeepAliveOptions(permitWithoutCalls: true),
)),
), channelShutdownHandler: () {
print("SHUTDOWN HANDLER CALLED @@@@@@@@@@@@@@@@@");
}),
);
}

View file

@ -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()

View file

@ -5,6 +5,7 @@
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.fotolockr.cakewallet.monero_sync_task</string>
<string>com.fotolockr.cakewallet.mweb_sync_task</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>

View file

@ -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<WalletListItem> ltcWallets = getIt
.get<WalletListViewModel>()
.wallets
.where((element) => [WalletType.litecoin].contains(element.type))
.toList();
if (ltcWallets.isEmpty) {
return Future.error("No ltc wallets found");
}
final walletLoadingService = getIt.get<WalletLoadingService>();
var wallet =
await walletLoadingService.load(ltcWallets.first.type, ltcWallets.first.name);
await wallet.startSync();
for (int i = 0;; i++) {
await Future<void>.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<WalletListViewModel>()
.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,

View file

@ -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<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);
List<ReceivePageOption> 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<String, int> getIncomingPayments(Object wallet);
void clearIncomingPayments(Object wallet);
String lightningTransactionPriorityWithLabel(TransactionPriority priority, int rate, {int? customRate});
List<TransactionPriority> getTransactionPriorities();
TransactionPriority getLightningTransactionPriorityCustom();
int getFeeRate(Object wallet, TransactionPriority priority);
int getMaxCustomFeeRate(Object wallet);
Future<void> fetchFees(Object wallet);
Future<int> calculateEstimatedFeeAsync(Object wallet, TransactionPriority? priority, int? amount);
Future<int> getEstimatedFeeWithFeeRate(Object wallet, int feeRate, int? amount);
TransactionPriority getDefaultTransactionPriority();
TransactionPriority deserializeLightningTransactionPriority({required int raw});
String getBreezApiKey();
}

View file

@ -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<SyncMode>(
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<SyncMode>(
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<void>(
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<void>(
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),
),

View file

@ -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;
}