فهرست منبع

Configure background fetch during app widget init

vishnukvmd 3 سال پیش
والد
کامیت
01f9b673aa
2فایلهای تغییر یافته به همراه42 افزوده شده و 31 حذف شده
  1. 38 1
      lib/app.dart
  2. 4 30
      lib/main.dart

+ 38 - 1
lib/app.dart

@@ -1,7 +1,9 @@
+import 'package:background_fetch/background_fetch.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_easyloading/flutter_easyloading.dart';
 import 'package:flutter_easyloading/flutter_easyloading.dart';
 import 'package:flutter_gen/gen_l10n/app_localizations.dart';
 import 'package:flutter_gen/gen_l10n/app_localizations.dart';
 import 'package:flutter_localizations/flutter_localizations.dart';
 import 'package:flutter_localizations/flutter_localizations.dart';
+import 'package:logging/logging.dart';
 import 'package:photos/core/network.dart';
 import 'package:photos/core/network.dart';
 import 'package:photos/l10n/l10n.dart';
 import 'package:photos/l10n/l10n.dart';
 import 'package:photos/services/app_lifecycle_service.dart';
 import 'package:photos/services/app_lifecycle_service.dart';
@@ -42,17 +44,27 @@ final themeData = ThemeData(
 class EnteApp extends StatefulWidget {
 class EnteApp extends StatefulWidget {
   static const _homeWidget = HomeWidget();
   static const _homeWidget = HomeWidget();
 
 
-  EnteApp({Key key}) : super(key: key);
+  final Future<void> Function(String) runBackgroundTask;
+  final Future<void> Function(String) killBackgroundTask;
+
+  EnteApp(
+    this.runBackgroundTask,
+    this.killBackgroundTask, {
+    Key key,
+  }) : super(key: key);
 
 
   @override
   @override
   _EnteAppState createState() => _EnteAppState();
   _EnteAppState createState() => _EnteAppState();
 }
 }
 
 
 class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
 class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
+  final _logger = Logger("EnteApp");
+
   @override
   @override
   void initState() {
   void initState() {
     super.initState();
     super.initState();
     WidgetsBinding.instance.addObserver(this);
     WidgetsBinding.instance.addObserver(this);
+    _configureBackgroundFetch();
   }
   }
 
 
   @override
   @override
@@ -83,4 +95,29 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
       AppLifecycleService.instance.onAppInBackground();
       AppLifecycleService.instance.onAppInBackground();
     }
     }
   }
   }
+
+  void _configureBackgroundFetch() {
+    BackgroundFetch.configure(
+        BackgroundFetchConfig(
+          minimumFetchInterval: 15,
+          forceAlarmManager: false,
+          stopOnTerminate: false,
+          startOnBoot: true,
+          enableHeadless: true,
+          requiresBatteryNotLow: false,
+          requiresCharging: false,
+          requiresStorageNotLow: false,
+          requiresDeviceIdle: false,
+          requiredNetworkType: NetworkType.NONE,
+        ), (String taskId) async {
+      await widget.runBackgroundTask(taskId);
+    }, (taskId) {
+      _logger.info("BG task timeout");
+      widget.killBackgroundTask(taskId);
+    }).then((int status) {
+      _logger.info('[BackgroundFetch] configure success: $status');
+    }).catchError((e) {
+      _logger.info('[BackgroundFetch] configure ERROR: $e');
+    });
+  }
 }
 }

+ 4 - 30
lib/main.dart

@@ -56,9 +56,8 @@ Future<void> _runInForeground() async {
     _logger.info("Starting app in foreground");
     _logger.info("Starting app in foreground");
     await _init(false);
     await _init(false);
     _scheduleFGSync();
     _scheduleFGSync();
-    _configureBackgroundFetch();
     runApp(AppLock(
     runApp(AppLock(
-      builder: (args) => EnteApp(),
+      builder: (args) => EnteApp(_runBackgroundTask, _killBGTask),
       lockScreen: LockScreen(),
       lockScreen: LockScreen(),
       enabled: Configuration.instance.shouldShowLockScreen(),
       enabled: Configuration.instance.shouldShowLockScreen(),
       themeData: themeData,
       themeData: themeData,
@@ -69,14 +68,14 @@ Future<void> _runInForeground() async {
 Future _runInBackground(String taskId) async {
 Future _runInBackground(String taskId) async {
   if (_initializationStatus == null) {
   if (_initializationStatus == null) {
     _runWithLogs(() async {
     _runWithLogs(() async {
-      _backgroundTask(taskId);
+      _runBackgroundTask(taskId);
     }, prefix: "[bg]");
     }, prefix: "[bg]");
   } else {
   } else {
-    _backgroundTask(taskId);
+    _runBackgroundTask(taskId);
   }
   }
 }
 }
 
 
-void _backgroundTask(String taskId) async {
+Future<void> _runBackgroundTask(String taskId) async {
   await Future.delayed(Duration(seconds: 3));
   await Future.delayed(Duration(seconds: 3));
   if (await _isRunningInForeground()) {
   if (await _isRunningInForeground()) {
     _logger.info("FG task running, skipping BG task");
     _logger.info("FG task running, skipping BG task");
@@ -239,28 +238,3 @@ void _scheduleSuicide(Duration duration, [String taskID]) {
     _killBGTask(taskID);
     _killBGTask(taskID);
   });
   });
 }
 }
-
-void _configureBackgroundFetch() {
-  BackgroundFetch.configure(
-      BackgroundFetchConfig(
-        minimumFetchInterval: 15,
-        forceAlarmManager: false,
-        stopOnTerminate: false,
-        startOnBoot: true,
-        enableHeadless: true,
-        requiresBatteryNotLow: false,
-        requiresCharging: false,
-        requiresStorageNotLow: false,
-        requiresDeviceIdle: false,
-        requiredNetworkType: NetworkType.NONE,
-      ), (String taskId) async {
-    await _runInBackground(taskId);
-  }, (taskId) {
-    _logger.info("BG task timeout");
-    _killBGTask(taskId);
-  }).then((int status) {
-    _logger.info('[BackgroundFetch] configure success: $status');
-  }).catchError((e) {
-    _logger.info('[BackgroundFetch] configure ERROR: $e');
-  });
-}