From 74470a788283f93bac5df3558545ed7a6235dcdd Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Mon, 10 May 2021 18:53:53 +0530 Subject: [PATCH] Smoothen the first load experience --- lib/services/sync_service.dart | 9 ++++++++- lib/utils/file_sync_util.dart | 29 ++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/services/sync_service.dart b/lib/services/sync_service.dart index eb3d9e4ec..36633f5b4 100644 --- a/lib/services/sync_service.dart +++ b/lib/services/sync_service.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:io'; +import 'package:computer/computer.dart'; import 'package:connectivity/connectivity.dart'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; @@ -33,6 +34,7 @@ class SyncService { final _uploader = FileUploader.instance; final _collectionsService = CollectionsService.instance; final _diffFetcher = DiffFetcher(); + final Computer _computer = Computer(); bool _syncStopRequested = false; bool _isBackground = false; Completer _existingSync; @@ -78,6 +80,7 @@ class SyncService { await PhotoManager.clearFileCache(); _logger.info("Cleared file cache"); } + await _computer.turnOn(workersCount: 1); } Future existingSync() async { @@ -211,6 +214,7 @@ class SyncService { } } final lastDBUpdationTime = _prefs.getInt(kDbUpdationTimeKey) ?? 0; + final startTime = DateTime.now().microsecondsSinceEpoch; if (lastDBUpdationTime != 0) { await _loadAndStorePhotos( lastDBUpdationTime, syncStartTime, existingLocalFileIDs); @@ -232,6 +236,9 @@ class SyncService { Bus.instance .fire(SyncStatusUpdate(SyncStatus.completed_first_gallery_import)); } + final endTime = DateTime.now().microsecondsSinceEpoch; + final duration = Duration(microseconds: endTime - startTime); + _logger.info("Load took " + duration.inMilliseconds.toString() + "ms"); } Future _loadAndStorePhotos( @@ -240,7 +247,7 @@ class SyncService { DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() + " to " + DateTime.fromMicrosecondsSinceEpoch(toTime).toString()); - final files = await getDeviceFiles(fromTime, toTime); + final files = await getDeviceFiles(fromTime, toTime, _computer); if (files.isNotEmpty) { _logger.info("Fetched " + files.length.toString() + " files."); final updatedFiles = diff --git a/lib/utils/file_sync_util.dart b/lib/utils/file_sync_util.dart index 0c2820eb5..1371c2eb0 100644 --- a/lib/utils/file_sync_util.dart +++ b/lib/utils/file_sync_util.dart @@ -1,24 +1,26 @@ import 'dart:math'; +import 'package:computer/computer.dart'; import 'package:logging/logging.dart'; import 'package:photo_manager/photo_manager.dart'; import 'package:photos/models/file.dart'; final _logger = Logger("FileSyncUtil"); -Future> getDeviceFiles(int fromTime, int toTime) async { +Future> getDeviceFiles( + int fromTime, int toTime, Computer computer) async { final pathEntities = await _getGalleryList(fromTime, toTime); - final files = List(); + List files = []; AssetPathEntity recents; for (AssetPathEntity pathEntity in pathEntities) { if (pathEntity.name == "Recent" || pathEntity.name == "Recents") { recents = pathEntity; } else { - await _addToPhotos(pathEntity, fromTime, files); + files = await _computeFiles(pathEntity, fromTime, files, computer); } } if (recents != null) { - await _addToPhotos(recents, fromTime, files); + files = await _computeFiles(recents, fromTime, files, computer); } files.sort( (first, second) => first.creationTime.compareTo(second.creationTime)); @@ -51,9 +53,21 @@ Future> _getGalleryList( return galleryList; } -Future _addToPhotos( - AssetPathEntity pathEntity, int fromTime, List files) async { - final assetList = await pathEntity.assetList; +Future> _computeFiles(AssetPathEntity pathEntity, int fromTime, + List files, Computer computer) async { + final args = Map(); + args["pathEntity"] = pathEntity; + args["assetList"] = await pathEntity.assetList; + args["fromTime"] = fromTime; + args["files"] = files; + return await computer.compute(_getFiles, param: args); +} + +Future> _getFiles(Map args) async { + final pathEntity = args["pathEntity"]; + final assetList = args["assetList"]; + final fromTime = args["fromTime"]; + final files = args["files"]; for (AssetEntity entity in assetList) { if (max(entity.createDateTime.microsecondsSinceEpoch, entity.modifiedDateTime.microsecondsSinceEpoch) > @@ -68,4 +82,5 @@ Future _addToPhotos( } } } + return files; }