This commit is contained in:
Matthew Fosse 2024-09-11 09:57:45 -07:00
parent c35908cdfb
commit 9f1b9548c3
4 changed files with 67 additions and 38 deletions

View file

@ -98,6 +98,7 @@ Future<void> onStart(ServiceInstance service) async {
await currentWallet?.stopSync();
final walletLoadingService = getIt.get<WalletLoadingService>();
final settingsStore = getIt.get<SettingsStore>();
final typeRaw = getIt.get<SharedPreferences>().getInt(PreferencesKey.currentWalletType);
bool syncAll = true;
@ -171,10 +172,10 @@ Future<void> onStart(ServiceInstance service) async {
return;
}
final wallet = syncingWallets.first!;
final syncProgress = ((wallet.syncStatus.progress() ?? 0) * 100).toStringAsPrecision(5);
// final wallet = syncingWallets.first!;
// final syncProgress = ((wallet.syncStatus.progress() ?? 0) * 100).toStringAsPrecision(5);
// String title = "${wallet!.name} ${syncProgress}% Synced";
String title = "";
for (int i = 0; i < syncingWallets.length; i++) {
@ -208,36 +209,38 @@ Future<bool> onIosBackground(ServiceInstance service) async {
return true;
}
Future<void> initializeService(FlutterBackgroundService bgService) async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
notificationChannelId,
notificationChannelName,
description: notificationChannelDescription,
importance: Importance.low,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
if (Platform.isIOS || Platform.isAndroid) {
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('ic_bg_service_small'),
),
Future<void> initializeService(FlutterBackgroundService bgService, bool useNotifications) async {
if (useNotifications) {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
notificationChannelId,
notificationChannelName,
description: notificationChannelDescription,
importance: Importance.low,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
if (Platform.isIOS || Platform.isAndroid) {
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('ic_bg_service_small'),
),
);
}
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
setNotificationStandby(flutterLocalNotificationsPlugin);
}
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
setNotificationStandby(flutterLocalNotificationsPlugin);
// notify the service that we are in the foreground:
bgService.invoke("setForeground");
@ -271,10 +274,10 @@ Future<void> initializeService(FlutterBackgroundService bgService) async {
class BackgroundTasks {
FlutterBackgroundService bgService = FlutterBackgroundService();
void updateServiceState(bool foreground) {
void updateServiceState(bool foreground, bool useNotifications) async {
if (foreground) {
bgService.invoke('stopService');
initializeService(bgService);
initializeService(bgService, useNotifications);
} else {
bgService.invoke("setBackground");
}
@ -292,12 +295,24 @@ class BackgroundTasks {
.wallets
.any((element) => element.type == WalletType.litecoin);
bool hasBitcoin = getIt
.get<WalletListViewModel>()
.wallets
.any((element) => element.type == WalletType.bitcoin);
final settingsStore = getIt.get<SettingsStore>();
if (!settingsStore.silentPaymentsAlwaysScan) {
hasBitcoin = false;
}
if (!settingsStore.mwebAlwaysScan) {
hasLitecoin = false;
}
/// if its not android nor ios, or the user has no monero wallets; exit
if (!DeviceInfo.instance.isMobile || (!hasMonero && !hasLitecoin)) {
if (!DeviceInfo.instance.isMobile || (!hasMonero && !hasLitecoin && !hasBitcoin)) {
return;
}
final settingsStore = getIt.get<SettingsStore>();
final SyncMode syncMode = settingsStore.currentSyncMode;
final bool syncAll = settingsStore.currentSyncAll;
@ -309,7 +324,7 @@ class BackgroundTasks {
bgService.invoke('stopService');
await initializeService(bgService);
await initializeService(bgService, settingsStore.showSyncNotification);
} catch (error, stackTrace) {
print(error);
print(stackTrace);

View file

@ -58,6 +58,7 @@ class PreferencesKey {
static const moneroWalletPasswordUpdateV1Base = 'monero_wallet_update_v1';
static const syncModeKey = 'sync_mode';
static const syncAllKey = 'sync_all';
static const showSyncNotificationKey = 'show_sync_notification';
static const lastPopupDate = 'last_popup_date';
static const lastAppReviewDate = 'last_app_review_date';
static const sortBalanceBy = 'sort_balance_by';

View file

@ -4,6 +4,7 @@ import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/core/totp_request_details.dart';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/entities/background_tasks.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/utils/device_info.dart';
import 'package:cake_wallet/view_model/link_view_model.dart';
import 'package:cw_core/wallet_base.dart';
@ -161,10 +162,11 @@ class RootState extends State<Root> with WidgetsBindingObserver {
}
// background service handling:
bool showNotifications = getIt.get<SettingsStore>().showSyncNotification;
switch (state) {
case AppLifecycleState.resumed:
// restart the background service if it was running before:
getIt.get<BackgroundTasks>().updateServiceState(true);
getIt.get<BackgroundTasks>().updateServiceState(true, showNotifications);
_stateTimer?.cancel();
if (!wasInBackground) {
return;
@ -178,7 +180,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
}
break;
case AppLifecycleState.paused:
getIt.get<BackgroundTasks>().updateServiceState(false);
getIt.get<BackgroundTasks>().updateServiceState(false, showNotifications);
case AppLifecycleState.inactive:
case AppLifecycleState.detached:
default:
@ -187,7 +189,7 @@ class RootState extends State<Root> with WidgetsBindingObserver {
_stateTimer?.cancel();
_stateTimer = Timer(const Duration(seconds: 10), () async {
wasInBackground = true;
getIt.get<BackgroundTasks>().updateServiceState(false);
getIt.get<BackgroundTasks>().updateServiceState(false, showNotifications);
});
break;
}

View file

@ -75,6 +75,7 @@ abstract class SettingsStoreBase with Store {
required String initialLanguageCode,
required SyncMode initialSyncMode,
required bool initialSyncAll,
required bool initialShowSyncNotification,
// required String initialCurrentLocale,
required this.appVersion,
required this.deviceName,
@ -169,6 +170,7 @@ abstract class SettingsStoreBase with Store {
initialShouldRequireTOTP2FAForAllSecurityAndBackupSettings,
currentSyncMode = initialSyncMode,
currentSyncAll = initialSyncAll,
showSyncNotification = initialShowSyncNotification,
priority = ObservableMap<WalletType, TransactionPriority>(),
defaultBuyProviders = ObservableMap<WalletType, ProviderType>(),
defaultSellProviders = ObservableMap<WalletType, ProviderType>() {
@ -389,7 +391,11 @@ abstract class SettingsStoreBase with Store {
reaction((_) => currentSyncAll, (bool syncAll) {
sharedPreferences.setBool(PreferencesKey.syncAllKey, syncAll);
_backgroundTasks.registerBackgroundService();
});
reaction((_) => showSyncNotification, (bool value) {
sharedPreferences.setBool(PreferencesKey.showSyncNotificationKey, value);
_backgroundTasks.registerBackgroundService();
});
@ -766,6 +772,9 @@ abstract class SettingsStoreBase with Store {
@observable
bool currentSyncAll;
@observable
bool showSyncNotification;
String appVersion;
String deviceName;
@ -1068,6 +1077,7 @@ abstract class SettingsStoreBase with Store {
return element.type.index == (sharedPreferences.getInt(PreferencesKey.syncModeKey) ?? 0);
});
final savedSyncAll = sharedPreferences.getBool(PreferencesKey.syncAllKey) ?? true;
final savedShowSyncNotification = sharedPreferences.getBool(PreferencesKey.showSyncNotificationKey) ?? false;
// migrated to secure:
final timeOutDuration = await SecureKey.getInt(
@ -1241,6 +1251,7 @@ abstract class SettingsStoreBase with Store {
backgroundTasks: backgroundTasks,
initialSyncMode: savedSyncMode,
initialSyncAll: savedSyncAll,
initialShowSyncNotification: savedShowSyncNotification,
shouldShowYatPopup: shouldShowYatPopup,
shouldShowRepWarning: shouldShowRepWarning,
);