Smoothen the first load experience

This commit is contained in:
Vishnu Mohandas 2021-05-10 18:53:53 +05:30
parent 24fa586085
commit 74470a7882
2 changed files with 30 additions and 8 deletions

View file

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:computer/computer.dart';
import 'package:connectivity/connectivity.dart'; import 'package:connectivity/connectivity.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
@ -33,6 +34,7 @@ class SyncService {
final _uploader = FileUploader.instance; final _uploader = FileUploader.instance;
final _collectionsService = CollectionsService.instance; final _collectionsService = CollectionsService.instance;
final _diffFetcher = DiffFetcher(); final _diffFetcher = DiffFetcher();
final Computer _computer = Computer();
bool _syncStopRequested = false; bool _syncStopRequested = false;
bool _isBackground = false; bool _isBackground = false;
Completer<bool> _existingSync; Completer<bool> _existingSync;
@ -78,6 +80,7 @@ class SyncService {
await PhotoManager.clearFileCache(); await PhotoManager.clearFileCache();
_logger.info("Cleared file cache"); _logger.info("Cleared file cache");
} }
await _computer.turnOn(workersCount: 1);
} }
Future<bool> existingSync() async { Future<bool> existingSync() async {
@ -211,6 +214,7 @@ class SyncService {
} }
} }
final lastDBUpdationTime = _prefs.getInt(kDbUpdationTimeKey) ?? 0; final lastDBUpdationTime = _prefs.getInt(kDbUpdationTimeKey) ?? 0;
final startTime = DateTime.now().microsecondsSinceEpoch;
if (lastDBUpdationTime != 0) { if (lastDBUpdationTime != 0) {
await _loadAndStorePhotos( await _loadAndStorePhotos(
lastDBUpdationTime, syncStartTime, existingLocalFileIDs); lastDBUpdationTime, syncStartTime, existingLocalFileIDs);
@ -232,6 +236,9 @@ class SyncService {
Bus.instance Bus.instance
.fire(SyncStatusUpdate(SyncStatus.completed_first_gallery_import)); .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<void> _loadAndStorePhotos( Future<void> _loadAndStorePhotos(
@ -240,7 +247,7 @@ class SyncService {
DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() + DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() +
" to " + " to " +
DateTime.fromMicrosecondsSinceEpoch(toTime).toString()); DateTime.fromMicrosecondsSinceEpoch(toTime).toString());
final files = await getDeviceFiles(fromTime, toTime); final files = await getDeviceFiles(fromTime, toTime, _computer);
if (files.isNotEmpty) { if (files.isNotEmpty) {
_logger.info("Fetched " + files.length.toString() + " files."); _logger.info("Fetched " + files.length.toString() + " files.");
final updatedFiles = final updatedFiles =

View file

@ -1,24 +1,26 @@
import 'dart:math'; import 'dart:math';
import 'package:computer/computer.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart'; import 'package:photo_manager/photo_manager.dart';
import 'package:photos/models/file.dart'; import 'package:photos/models/file.dart';
final _logger = Logger("FileSyncUtil"); final _logger = Logger("FileSyncUtil");
Future<List<File>> getDeviceFiles(int fromTime, int toTime) async { Future<List<File>> getDeviceFiles(
int fromTime, int toTime, Computer computer) async {
final pathEntities = await _getGalleryList(fromTime, toTime); final pathEntities = await _getGalleryList(fromTime, toTime);
final files = List<File>(); List<File> files = [];
AssetPathEntity recents; AssetPathEntity recents;
for (AssetPathEntity pathEntity in pathEntities) { for (AssetPathEntity pathEntity in pathEntities) {
if (pathEntity.name == "Recent" || pathEntity.name == "Recents") { if (pathEntity.name == "Recent" || pathEntity.name == "Recents") {
recents = pathEntity; recents = pathEntity;
} else { } else {
await _addToPhotos(pathEntity, fromTime, files); files = await _computeFiles(pathEntity, fromTime, files, computer);
} }
} }
if (recents != null) { if (recents != null) {
await _addToPhotos(recents, fromTime, files); files = await _computeFiles(recents, fromTime, files, computer);
} }
files.sort( files.sort(
(first, second) => first.creationTime.compareTo(second.creationTime)); (first, second) => first.creationTime.compareTo(second.creationTime));
@ -51,9 +53,21 @@ Future<List<AssetPathEntity>> _getGalleryList(
return galleryList; return galleryList;
} }
Future _addToPhotos( Future<List<File>> _computeFiles(AssetPathEntity pathEntity, int fromTime,
AssetPathEntity pathEntity, int fromTime, List<File> files) async { List<File> files, Computer computer) async {
final assetList = await pathEntity.assetList; final args = Map<String, dynamic>();
args["pathEntity"] = pathEntity;
args["assetList"] = await pathEntity.assetList;
args["fromTime"] = fromTime;
args["files"] = files;
return await computer.compute(_getFiles, param: args);
}
Future<List<File>> _getFiles(Map<String, dynamic> args) async {
final pathEntity = args["pathEntity"];
final assetList = args["assetList"];
final fromTime = args["fromTime"];
final files = args["files"];
for (AssetEntity entity in assetList) { for (AssetEntity entity in assetList) {
if (max(entity.createDateTime.microsecondsSinceEpoch, if (max(entity.createDateTime.microsecondsSinceEpoch,
entity.modifiedDateTime.microsecondsSinceEpoch) > entity.modifiedDateTime.microsecondsSinceEpoch) >
@ -68,4 +82,5 @@ Future _addToPhotos(
} }
} }
} }
return files;
} }