2025-03-21 18:22:00 +01:00
|
|
|
import 'dart:async';
|
2025-04-24 19:06:43 +02:00
|
|
|
import 'dart:io';
|
2025-03-21 18:22:00 +01:00
|
|
|
|
|
|
|
import 'package:cake_wallet/core/key_service.dart';
|
|
|
|
import 'package:cake_wallet/core/wallet_loading_service.dart';
|
|
|
|
import 'package:cake_wallet/di.dart';
|
2025-04-24 19:06:43 +02:00
|
|
|
import 'package:cake_wallet/entities/preferences_key.dart';
|
2025-03-21 18:22:00 +01:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2025-04-24 19:06:43 +02:00
|
|
|
import 'package:cake_wallet/utils/feature_flag.dart';
|
2025-03-21 18:22:00 +01:00
|
|
|
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/sync_status.dart';
|
2025-04-24 19:06:43 +02:00
|
|
|
import 'package:cw_core/transaction_direction.dart';
|
2025-03-21 18:22:00 +01:00
|
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2025-04-24 19:06:43 +02:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2025-03-21 18:22:00 +01:00
|
|
|
|
|
|
|
class BackgroundSync {
|
2025-04-24 19:06:43 +02:00
|
|
|
final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
bool _isInitialized = false;
|
|
|
|
|
|
|
|
Future<void> _initializeNotifications() async {
|
|
|
|
if (_isInitialized) return;
|
|
|
|
|
|
|
|
const androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
|
|
|
|
|
|
|
|
const iosSettings = DarwinInitializationSettings(
|
|
|
|
requestAlertPermission: true,
|
|
|
|
requestBadgePermission: true,
|
|
|
|
requestSoundPermission: true,
|
|
|
|
);
|
|
|
|
|
|
|
|
const initializationSettings = InitializationSettings(
|
|
|
|
android: androidSettings,
|
|
|
|
iOS: iosSettings,
|
|
|
|
);
|
|
|
|
|
|
|
|
await _notificationsPlugin.initialize(initializationSettings);
|
|
|
|
_isInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> requestPermissions() async {
|
|
|
|
if (Platform.isIOS || Platform.isMacOS) {
|
|
|
|
return await _notificationsPlugin
|
|
|
|
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
|
|
|
|
?.requestPermissions(
|
|
|
|
alert: true,
|
|
|
|
badge: true,
|
|
|
|
sound: true,
|
|
|
|
) ?? false;
|
|
|
|
} else if (Platform.isAndroid) {
|
|
|
|
return await _notificationsPlugin
|
|
|
|
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
|
|
|
?.areNotificationsEnabled() ?? false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> showNotification(String title, String content) async {
|
|
|
|
await _initializeNotifications();
|
|
|
|
final hasPermission = await requestPermissions();
|
|
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
|
printV('Notification permissions not granted');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const androidDetails = AndroidNotificationDetails(
|
|
|
|
'transactions',
|
|
|
|
'Transactions',
|
|
|
|
channelDescription: 'Channel for notifications about transactions',
|
|
|
|
importance: Importance.defaultImportance,
|
|
|
|
priority: Priority.defaultPriority,
|
|
|
|
);
|
|
|
|
|
|
|
|
const iosDetails = DarwinNotificationDetails();
|
|
|
|
|
|
|
|
const notificationDetails = NotificationDetails(
|
|
|
|
android: androidDetails,
|
|
|
|
iOS: iosDetails,
|
|
|
|
);
|
|
|
|
|
|
|
|
await _notificationsPlugin.show(
|
|
|
|
DateTime.now().millisecondsSinceEpoch.hashCode,
|
|
|
|
title,
|
|
|
|
content,
|
|
|
|
notificationDetails,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-21 18:22:00 +01:00
|
|
|
Future<void> sync() async {
|
|
|
|
printV("Background sync started");
|
2025-04-24 19:06:43 +02:00
|
|
|
await _syncWallets();
|
2025-03-21 18:22:00 +01:00
|
|
|
printV("Background sync completed");
|
|
|
|
}
|
|
|
|
|
2025-04-24 19:06:43 +02:00
|
|
|
Future<void> _syncWallets() async {
|
2025-03-21 18:22:00 +01:00
|
|
|
final walletLoadingService = getIt.get<WalletLoadingService>();
|
|
|
|
final walletListViewModel = getIt.get<WalletListViewModel>();
|
|
|
|
final settingsStore = getIt.get<SettingsStore>();
|
|
|
|
|
|
|
|
|
|
|
|
final List<WalletListItem> moneroWallets = walletListViewModel.wallets
|
|
|
|
.where((element) => !element.isHardware)
|
2025-04-24 19:06:43 +02:00
|
|
|
.where((element) => ![WalletType.haven, WalletType.decred].contains(element.type))
|
2025-03-21 18:22:00 +01:00
|
|
|
.toList();
|
|
|
|
for (int i = 0; i < moneroWallets.length; i++) {
|
2025-04-24 19:06:43 +02:00
|
|
|
final wallet = await walletLoadingService.load(moneroWallets[i].type, moneroWallets[i].name, isBackground: true);
|
2025-03-21 18:22:00 +01:00
|
|
|
int syncedTicks = 0;
|
|
|
|
final keyService = getIt.get<KeyService>();
|
|
|
|
|
|
|
|
int stuckTicks = 0;
|
|
|
|
|
|
|
|
inner:
|
|
|
|
while (true) {
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
final syncStatus = wallet.syncStatus;
|
|
|
|
final progress = syncStatus.progress();
|
|
|
|
if (syncStatus is ConnectedSyncStatus || syncStatus is AttemptingSyncStatus || syncStatus is NotConnectedSyncStatus) {
|
|
|
|
stuckTicks++;
|
|
|
|
if (stuckTicks > 30) {
|
|
|
|
printV("${wallet.name} STUCK SYNCING");
|
|
|
|
break inner;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stuckTicks = 0;
|
|
|
|
}
|
|
|
|
if (syncStatus is NotConnectedSyncStatus) {
|
|
|
|
printV("${wallet.name} NOT CONNECTED");
|
|
|
|
final node = settingsStore.getCurrentNode(wallet.type);
|
|
|
|
await wallet.connectToNode(node: node);
|
|
|
|
await wallet.startBackgroundSync();
|
|
|
|
printV("STARTED SYNC");
|
|
|
|
continue inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progress > 0.999 || syncStatus is SyncedSyncStatus) {
|
|
|
|
syncedTicks++;
|
|
|
|
if (syncedTicks > 5) {
|
|
|
|
syncedTicks = 0;
|
|
|
|
printV("WALLET $i SYNCED");
|
|
|
|
try {
|
|
|
|
await wallet.stopBackgroundSync((await keyService.getWalletPassword(walletName: wallet.name)));
|
|
|
|
} catch (e) {
|
|
|
|
printV("error stopping sync: $e");
|
|
|
|
}
|
|
|
|
break inner;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
syncedTicks = 0;
|
|
|
|
}
|
2025-04-24 19:06:43 +02:00
|
|
|
if (FeatureFlag.hasDevOptions) {
|
2025-03-21 18:22:00 +01:00
|
|
|
if (syncStatus is SyncingSyncStatus) {
|
|
|
|
final blocksLeft = syncStatus.blocksLeft;
|
|
|
|
printV("$blocksLeft Blocks Left");
|
|
|
|
} else if (syncStatus is SyncedSyncStatus) {
|
|
|
|
printV("Synced");
|
|
|
|
} else if (syncStatus is SyncedTipSyncStatus) {
|
|
|
|
printV("Scanned Tip: ${syncStatus.tip}");
|
|
|
|
} else if (syncStatus is NotConnectedSyncStatus) {
|
|
|
|
printV("Still Not Connected");
|
|
|
|
} else if (syncStatus is AttemptingSyncStatus) {
|
|
|
|
printV("Attempting Sync");
|
|
|
|
} else if (syncStatus is StartingScanSyncStatus) {
|
|
|
|
printV("Starting Scan");
|
|
|
|
} else if (syncStatus is SyncronizingSyncStatus) {
|
|
|
|
printV("Syncronizing");
|
|
|
|
} else if (syncStatus is FailedSyncStatus) {
|
|
|
|
printV("Failed Sync");
|
|
|
|
} else if (syncStatus is ConnectingSyncStatus) {
|
|
|
|
printV("Connecting");
|
|
|
|
} else {
|
|
|
|
printV("Unknown Sync Status ${syncStatus.runtimeType}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-04-24 19:06:43 +02:00
|
|
|
final txs = wallet.transactionHistory;
|
|
|
|
final sortedTxs = txs.transactions.values.toList()..sort((a, b) => a.date.compareTo(b.date));
|
|
|
|
final sharedPreferences = await SharedPreferences.getInstance();
|
|
|
|
for (final tx in sortedTxs) {
|
|
|
|
final lastTriggerString = sharedPreferences.getString(PreferencesKey.backgroundSyncLastTrigger(wallet.name));
|
|
|
|
final lastTriggerDate = lastTriggerString != null
|
|
|
|
? DateTime.parse(lastTriggerString)
|
|
|
|
: DateTime.now();
|
|
|
|
final keys = sharedPreferences.getKeys();
|
|
|
|
if (tx.date.isBefore(lastTriggerDate)) {
|
|
|
|
printV("w: ${wallet.name}, tx: ${tx.date} is before $lastTriggerDate (lastTriggerString: $lastTriggerString) (k: ${keys.length})");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
await sharedPreferences.setString(PreferencesKey.backgroundSyncLastTrigger(wallet.name), tx.date.add(Duration(minutes: 1)).toIso8601String());
|
|
|
|
final action = tx.direction == TransactionDirection.incoming ? "Received" : "Sent";
|
|
|
|
if (sharedPreferences.getBool(PreferencesKey.backgroundSyncNotificationsEnabled) ?? false) {
|
|
|
|
await showNotification("$action ${wallet.currency.fullName} in ${wallet.name}", "${tx.amountFormatted()}");
|
|
|
|
}
|
|
|
|
printV("${wallet.currency.fullName} in ${wallet.name}: TX: ${tx.date} ${tx.amount} ${tx.direction}");
|
|
|
|
}
|
|
|
|
wallet.id;
|
2025-03-21 18:22:00 +01:00
|
|
|
await wallet.stopBackgroundSync(await keyService.getWalletPassword(walletName: wallet.name));
|
|
|
|
await wallet.close(shouldCleanup: true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|