Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix: refactor code

Prateek Sunalb03bdfb9d665
diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart
index 1a68dc0338..b0275fbd71 100644
--- a/mobile/lib/main.dart
+++ b/mobile/lib/main.dart
@@ -85,7 +85,7 @@ void main() async {
}
Future<void> _runInForeground(AdaptiveThemeMode? savedThemeMode) async {
- return await _runWithLogs(() async {
+ return await runWithLogs(() async {
_logger.info("Starting app in foreground");
await _init(false, via: 'mainMethod');
final Locale? locale = await getLocale(noFallback: true);
@@ -125,32 +125,30 @@ Future<void> _homeWidgetSync([bool isBackground = false]) async {
}
}
-Future<void> runBackgroundTask(String taskId, {String mode = 'normal'}) async {
- await _runWithLogs(
- () async {
- try {
- final cancellableOp =
- CancelableOperation.fromFuture(_runMinimally(taskId));
-
- await Future.wait([
- if (Platform.isIOS)
- _scheduleSuicide(
- kBGTaskTimeout,
- taskId,
- cancellableOp,
- ),
- cancellableOp.valueOrCancellation(),
- ]);
- } catch (e, s) {
- _logger.severe("Error in background task", e, s);
- }
- },
- prefix: "[bg]",
- );
+Future<void> runBackgroundTask(
+ String taskId,
+ TimeLogger tlog, {
+ String mode = 'normal',
+}) async {
+ try {
+ final cancellableOp =
+ CancelableOperation.fromFuture(_runMinimally(taskId, tlog));
+
+ await Future.wait([
+ if (Platform.isIOS)
+ _scheduleSuicide(
+ kBGTaskTimeout,
+ taskId,
+ cancellableOp,
+ ),
+ cancellableOp.valueOrCancellation(),
+ ]);
+ } catch (e, s) {
+ _logger.severe("Error in background task", e, s);
+ }
}
-Future<void> _runMinimally(String taskId) async {
- final TimeLogger tlog = TimeLogger();
+Future<void> _runMinimally(String taskId, TimeLogger tlog) async {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
final SharedPreferences prefs = await SharedPreferences.getInstance();
@@ -347,7 +345,7 @@ Future<void> _sync(String caller) async {
}
}
-Future _runWithLogs(Function() function, {String prefix = ""}) async {
+Future runWithLogs(Function() function, {String prefix = ""}) async {
await SuperLogging.main(
LogConfig(
body: function,
@@ -418,7 +416,7 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
}
} else {
// App is dead
- _runWithLogs(
+ runWithLogs(
() async {
_logger.info("Background push received");
await _init(true, via: 'firebasePush');
diff --git a/mobile/lib/utils/bg_task_utils.dart b/mobile/lib/utils/bg_task_utils.dart
index 39452e8403..fe44b1174d 100644
--- a/mobile/lib/utils/bg_task_utils.dart
+++ b/mobile/lib/utils/bg_task_utils.dart
@@ -3,6 +3,7 @@ import "dart:io";
import "package:logging/logging.dart";
import "package:permission_handler/permission_handler.dart";
import "package:photos/db/upload_locks_db.dart";
+import "package:photos/extensions/stop_watch.dart";
import "package:photos/main.dart";
import "package:photos/service_locator.dart";
import "package:photos/utils/file_uploader.dart";
@@ -12,15 +13,30 @@ import "package:workmanager/workmanager.dart" as workmanager;
@pragma('vm:entry-point')
void callbackDispatcher() {
workmanager.Workmanager().executeTask((taskName, inputData) async {
- try {
- await runBackgroundTask(taskName);
- return true;
- } catch (e) {
- BgTaskUtils.$.info('Task error: $e');
- final prefs = await SharedPreferences.getInstance();
- await BgTaskUtils.releaseResourcesForKill(taskName, prefs);
- return Future.error(e.toString());
- }
+ Future<bool> result = Future.error("Task didn't run");
+
+ await runWithLogs(
+ () async {
+ try {
+ final TimeLogger tlog = TimeLogger();
+ BgTaskUtils.$.info('Task started $tlog');
+ await runBackgroundTask(taskName, tlog);
+ BgTaskUtils.$.info('Task run successful $tlog');
+ result = Future.value(true);
+ } catch (e) {
+ BgTaskUtils.$.warning('Task error: $e');
+ final prefs = await SharedPreferences.getInstance();
+ await BgTaskUtils.releaseResourcesForKill(taskName, prefs);
+ result = Future.error(e.toString());
+ }
+ },
+ prefix: "[bg]",
+ ).onError((_, __) {
+ result = Future.error("Didn't finished correctly!");
+ return;
+ });
+
+ return result;
});
}