Resort to AppLifecycleState
This commit is contained in:
parent
8d4a763a65
commit
e398807f2e
4 changed files with 24 additions and 22 deletions
|
@ -126,7 +126,7 @@ void _backgroundTask(String taskId) async {
|
|||
}
|
||||
await _init(true);
|
||||
UpdateService.instance.showUpdateNotification();
|
||||
await _sync(isAppInBackground: true);
|
||||
await _sync();
|
||||
BackgroundFetch.finish(taskId);
|
||||
}
|
||||
|
||||
|
@ -143,8 +143,13 @@ Future<void> _init(bool isBackground) async {
|
|||
return _initializationStatus.future;
|
||||
}
|
||||
_initializationStatus = Completer<void>();
|
||||
_scheduleHeartBeat(isBackground);
|
||||
_logger.info("Initializing...");
|
||||
_scheduleHeartBeat(isBackground);
|
||||
if (isBackground) {
|
||||
AppLifecycleService.instance.onAppInBackground();
|
||||
} else {
|
||||
AppLifecycleService.instance.onAppInForeground();
|
||||
}
|
||||
InAppPurchaseConnection.enablePendingPurchases();
|
||||
CryptoUtil.init();
|
||||
await NotificationService.instance.init();
|
||||
|
@ -154,9 +159,9 @@ Future<void> _init(bool isBackground) async {
|
|||
await BillingService.instance.init();
|
||||
await CollectionsService.instance.init();
|
||||
await FileUploader.instance.init(isBackground);
|
||||
await LocalSyncService.instance.init(isBackground);
|
||||
await LocalSyncService.instance.init();
|
||||
await TrashSyncService.instance.init();
|
||||
await RemoteSyncService.instance.init(isBackground);
|
||||
await RemoteSyncService.instance.init();
|
||||
await SyncService.instance.init();
|
||||
await MemoriesService.instance.init();
|
||||
await LocalSettings.instance.init();
|
||||
|
@ -168,12 +173,12 @@ Future<void> _init(bool isBackground) async {
|
|||
_initializationStatus.complete();
|
||||
}
|
||||
|
||||
Future<void> _sync({bool isAppInBackground = false}) async {
|
||||
Future<void> _sync() async {
|
||||
if (SyncService.instance.isSyncInProgress()) {
|
||||
_logger.info("Sync is already in progress, skipping");
|
||||
return;
|
||||
}
|
||||
if (isAppInBackground) {
|
||||
if (!AppLifecycleService.instance.isForeground) {
|
||||
_logger.info("Syncing in background");
|
||||
}
|
||||
try {
|
||||
|
@ -322,13 +327,13 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|||
}
|
||||
await _init(true);
|
||||
if (PushService.shouldSync(message)) {
|
||||
await _sync(isAppInBackground: true);
|
||||
await _sync();
|
||||
}
|
||||
}, prefix: "[bg]");
|
||||
} else {
|
||||
_logger.info("Background push received when app is alive");
|
||||
if (PushService.shouldSync(message)) {
|
||||
await _sync(isAppInBackground: true);
|
||||
await _sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import 'package:photos/events/local_photos_updated_event.dart';
|
|||
import 'package:photos/models/collection.dart';
|
||||
import 'package:photos/models/collection_file_item.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
import 'package:photos/services/app_lifecycle_service.dart';
|
||||
import 'package:photos/services/remote_sync_service.dart';
|
||||
import 'package:photos/utils/crypto_util.dart';
|
||||
import 'package:photos/utils/file_download_util.dart';
|
||||
|
@ -67,14 +68,14 @@ class CollectionsService {
|
|||
});
|
||||
}
|
||||
|
||||
Future<List<Collection>> sync({bool isBackground = false}) async {
|
||||
Future<List<Collection>> sync() async {
|
||||
_logger.info("Syncing collections");
|
||||
final lastCollectionUpdationTime =
|
||||
_prefs.getInt(_collectionsSyncTimeKey) ?? 0;
|
||||
|
||||
// Might not have synced the collection fully
|
||||
final fetchedCollections =
|
||||
await _fetchCollections(lastCollectionUpdationTime ?? 0, isBackground);
|
||||
await _fetchCollections(lastCollectionUpdationTime ?? 0);
|
||||
final updatedCollections = <Collection>[];
|
||||
int maxUpdationTime = lastCollectionUpdationTime;
|
||||
final ownerID = _config.getUserID();
|
||||
|
@ -272,14 +273,13 @@ class CollectionsService {
|
|||
}
|
||||
}
|
||||
|
||||
Future<List<Collection>> _fetchCollections(
|
||||
int sinceTime, bool isBackground) async {
|
||||
Future<List<Collection>> _fetchCollections(int sinceTime) async {
|
||||
try {
|
||||
final response = await _dio.get(
|
||||
Configuration.instance.getHttpEndpoint() + "/collections",
|
||||
queryParameters: {
|
||||
"sinceTime": sinceTime,
|
||||
"source": isBackground ? "bg" : "fg",
|
||||
"source": AppLifecycleService.instance.isForeground ? "fg" : "bg",
|
||||
},
|
||||
options: Options(
|
||||
headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
||||
|
|
|
@ -10,6 +10,7 @@ import 'package:photos/db/files_db.dart';
|
|||
import 'package:photos/events/local_photos_updated_event.dart';
|
||||
import 'package:photos/events/sync_status_update_event.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
import 'package:photos/services/app_lifecycle_service.dart';
|
||||
import 'package:photos/utils/file_sync_util.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
|
@ -17,7 +18,6 @@ class LocalSyncService {
|
|||
final _logger = Logger("LocalSyncService");
|
||||
final _db = FilesDB.instance;
|
||||
final Computer _computer = Computer();
|
||||
bool _isBackground = false;
|
||||
SharedPreferences _prefs;
|
||||
|
||||
static const kDbUpdationTimeKey = "db_updation_time";
|
||||
|
@ -35,10 +35,9 @@ class LocalSyncService {
|
|||
static final LocalSyncService instance =
|
||||
LocalSyncService._privateConstructor();
|
||||
|
||||
Future<void> init(bool isBackground) async {
|
||||
_isBackground = isBackground;
|
||||
Future<void> init() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
if (_isBackground) {
|
||||
if (!AppLifecycleService.instance.isForeground) {
|
||||
await PhotoManager.setIgnorePermissionCheck(true);
|
||||
}
|
||||
await _computer.turnOn(workersCount: 1);
|
||||
|
@ -57,7 +56,7 @@ class LocalSyncService {
|
|||
_logger.info("Skipping local sync since permission has not been granted");
|
||||
return;
|
||||
}
|
||||
if (Platform.isAndroid && !_isBackground) {
|
||||
if (Platform.isAndroid && AppLifecycleService.instance.isForeground) {
|
||||
final permissionState = await PhotoManager.requestPermissionExtend();
|
||||
if (permissionState != PermissionState.authorized) {
|
||||
_logger.severe("sync requested with invalid permission",
|
||||
|
|
|
@ -31,7 +31,6 @@ class RemoteSyncService {
|
|||
final _diffFetcher = DiffFetcher();
|
||||
int _completedUploads = 0;
|
||||
SharedPreferences _prefs;
|
||||
bool _isBackground;
|
||||
Completer<void> _existingSync;
|
||||
|
||||
static const kHasSyncedArchiveKey = "has_synced_archive";
|
||||
|
@ -48,9 +47,8 @@ class RemoteSyncService {
|
|||
|
||||
RemoteSyncService._privateConstructor();
|
||||
|
||||
Future<void> init(bool isBackground) async {
|
||||
Future<void> init() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
_isBackground = isBackground;
|
||||
}
|
||||
|
||||
Future<void> sync({bool silently = false}) async {
|
||||
|
@ -65,7 +63,7 @@ class RemoteSyncService {
|
|||
_existingSync = Completer<void>();
|
||||
|
||||
bool isFirstSync = !_collectionsService.hasSyncedCollections();
|
||||
await _collectionsService.sync(isBackground: _isBackground);
|
||||
await _collectionsService.sync();
|
||||
|
||||
if (isFirstSync || _hasReSynced()) {
|
||||
await _syncUpdatedCollections(silently);
|
||||
|
|
Loading…
Reference in a new issue