save [wip]

This commit is contained in:
Matthew Fosse 2024-08-28 14:54:47 -04:00
parent c92fc24a66
commit 4d5e3b7f0b
3 changed files with 79 additions and 40 deletions

View file

@ -102,6 +102,10 @@
<category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="nano-gpt" /> <data android:scheme="nano-gpt" />
</intent-filter> </intent-filter>
<service
android:name="id.flutter.flutter_background_service.BackgroundService"
android:foregroundServiceType="dataSync"
/>
</activity> </activity>
<meta-data <meta-data
android:name="flutterEmbedding" android:name="flutterEmbedding"

View file

@ -14,6 +14,7 @@ import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart'; import 'package:workmanager/workmanager.dart';
import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/main.dart';
@ -178,6 +179,24 @@ void callbackDispatcher() {
}); });
} }
Future<void> initializeService() async {
final service = FlutterBackgroundService();
await service.configure(
// iosConfiguration: IosConfiguration(
// autoStart: true,
// onForeground: onStart,
// onBackground: onIosBackground,
// ),
androidConfiguration: AndroidConfiguration(
autoStart: true,
onStart: onStart,
isForegroundMode: false,
autoStartOnBoot: true,
),
);
}
class BackgroundTasks { class BackgroundTasks {
void registerSyncTask({bool changeExisting = false}) async { void registerSyncTask({bool changeExisting = false}) async {
try { try {
@ -206,39 +225,54 @@ class BackgroundTasks {
return; return;
} }
await Workmanager().initialize( // await service.configure(
callbackDispatcher, // androidConfiguration: AndroidConfiguration(
isInDebugMode: true, // // this will be executed when app is in foreground or background in separated isolate
); // onStart: onStart,
final inputData = <String, dynamic>{"sync_all": syncAll}; // // auto start service
final constraints = Constraints( // autoStart: true,
networkType: // isForegroundMode: true,
syncMode.type == SyncType.unobtrusive ? NetworkType.unmetered : NetworkType.connected,
requiresBatteryNotLow: syncMode.type == SyncType.unobtrusive,
requiresCharging: syncMode.type == SyncType.unobtrusive,
requiresDeviceIdle: syncMode.type == SyncType.unobtrusive,
);
if (Platform.isIOS && syncMode.type == SyncType.unobtrusive) { // notificationChannelId: notificationChannelId, // this must match with notification channel you created above.
// await Workmanager().registerOneOffTask( // initialNotificationTitle: 'AWESOME SERVICE',
// moneroSyncTaskKey, // initialNotificationContent: 'Initializing',
// moneroSyncTaskKey, // foregroundServiceNotificationId: notificationId,
// initialDelay: syncMode.frequency, // );
// existingWorkPolicy: ExistingWorkPolicy.replace,
// inputData: inputData, // await Workmanager().initialize(
// constraints: constraints, // callbackDispatcher,
// ); // isInDebugMode: true,
await Workmanager().registerOneOffTask( // );
mwebSyncTaskKey,
mwebSyncTaskKey, // final inputData = <String, dynamic>{"sync_all": syncAll};
initialDelay: Duration(seconds: 30), // final constraints = Constraints(
existingWorkPolicy: ExistingWorkPolicy.replace, // networkType:
inputData: inputData, // syncMode.type == SyncType.unobtrusive ? NetworkType.unmetered : NetworkType.connected,
constraints: constraints, // requiresBatteryNotLow: syncMode.type == SyncType.unobtrusive,
); // requiresCharging: syncMode.type == SyncType.unobtrusive,
return; // requiresDeviceIdle: syncMode.type == SyncType.unobtrusive,
} // );
// if (Platform.isIOS && syncMode.type == SyncType.unobtrusive) {
// // await Workmanager().registerOneOffTask(
// // moneroSyncTaskKey,
// // moneroSyncTaskKey,
// // initialDelay: syncMode.frequency,
// // existingWorkPolicy: ExistingWorkPolicy.replace,
// // inputData: inputData,
// // constraints: constraints,
// // );
// await Workmanager().registerOneOffTask(
// mwebSyncTaskKey,
// mwebSyncTaskKey,
// initialDelay: Duration(seconds: 30),
// existingWorkPolicy: ExistingWorkPolicy.replace,
// inputData: inputData,
// constraints: constraints,
// );
// return;
// }
// await Workmanager().registerPeriodicTask( // await Workmanager().registerPeriodicTask(
// moneroSyncTaskKey, // moneroSyncTaskKey,
@ -249,15 +283,15 @@ class BackgroundTasks {
// inputData: inputData, // inputData: inputData,
// constraints: constraints, // constraints: constraints,
// ); // );
await Workmanager().registerPeriodicTask( // await Workmanager().registerPeriodicTask(
mwebSyncTaskKey, // mwebSyncTaskKey,
mwebSyncTaskKey, // mwebSyncTaskKey,
initialDelay: syncMode.frequency, // initialDelay: syncMode.frequency,
frequency: syncMode.frequency, // frequency: syncMode.frequency,
existingWorkPolicy: changeExisting ? ExistingWorkPolicy.replace : ExistingWorkPolicy.keep, // existingWorkPolicy: changeExisting ? ExistingWorkPolicy.replace : ExistingWorkPolicy.keep,
inputData: inputData, // inputData: inputData,
constraints: constraints, // constraints: constraints,
); // );
} catch (error, stackTrace) { } catch (error, stackTrace) {
print(error); print(error);
print(stackTrace); print(stackTrace);

View file

@ -65,6 +65,7 @@ dependencies:
url: https://github.com/cake-tech/device_display_brightness.git url: https://github.com/cake-tech/device_display_brightness.git
ref: master ref: master
workmanager: ^0.5.1 workmanager: ^0.5.1
flutter_background_service: ^5.0.9
wakelock_plus: ^1.2.5 wakelock_plus: ^1.2.5
flutter_mailer: ^2.0.2 flutter_mailer: ^2.0.2
device_info_plus: ^9.1.0 device_info_plus: ^9.1.0