Perf: Remove redundant calls via computer (#1043)
This commit is contained in:
commit
0d6a5fc435
2 changed files with 28 additions and 27 deletions
|
@ -16,7 +16,6 @@ const assetFetchPageSize = 2000;
|
||||||
Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
|
Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
|
||||||
int fromTime,
|
int fromTime,
|
||||||
int toTime,
|
int toTime,
|
||||||
Computer computer,
|
|
||||||
) async {
|
) async {
|
||||||
final pathEntities = await _getGalleryList(
|
final pathEntities = await _getGalleryList(
|
||||||
updateFromTime: fromTime,
|
updateFromTime: fromTime,
|
||||||
|
@ -31,7 +30,11 @@ Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
|
||||||
final List<File> uniqueFiles = [];
|
final List<File> uniqueFiles = [];
|
||||||
for (AssetPathEntity pathEntity in pathEntities) {
|
for (AssetPathEntity pathEntity in pathEntities) {
|
||||||
final List<AssetEntity> assetsInPath = await _getAllAssetLists(pathEntity);
|
final List<AssetEntity> assetsInPath = await _getAllAssetLists(pathEntity);
|
||||||
final Tuple2<Set<String>, List<File>> result = await computer.compute(
|
late Tuple2<Set<String>, List<File>> result;
|
||||||
|
if (assetsInPath.isEmpty) {
|
||||||
|
result = const Tuple2({}, []);
|
||||||
|
} else {
|
||||||
|
result = await Computer.shared().compute(
|
||||||
_getLocalIDsAndFilesFromAssets,
|
_getLocalIDsAndFilesFromAssets,
|
||||||
param: <String, dynamic>{
|
param: <String, dynamic>{
|
||||||
"pathEntity": pathEntity,
|
"pathEntity": pathEntity,
|
||||||
|
@ -44,6 +47,7 @@ Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
|
||||||
);
|
);
|
||||||
alreadySeenLocalIDs.addAll(result.item1);
|
alreadySeenLocalIDs.addAll(result.item1);
|
||||||
uniqueFiles.addAll(result.item2);
|
uniqueFiles.addAll(result.item2);
|
||||||
|
}
|
||||||
localPathAssets.add(
|
localPathAssets.add(
|
||||||
LocalPathAsset(
|
LocalPathAsset(
|
||||||
localIDs: result.item1,
|
localIDs: result.item1,
|
||||||
|
@ -120,14 +124,13 @@ Future<LocalDiffResult> getDiffWithLocal(
|
||||||
Set<String> existingIDs, // localIDs of files already imported in app
|
Set<String> existingIDs, // localIDs of files already imported in app
|
||||||
Map<String, Set<String>> pathToLocalIDs,
|
Map<String, Set<String>> pathToLocalIDs,
|
||||||
Set<String> invalidIDs,
|
Set<String> invalidIDs,
|
||||||
Computer computer,
|
|
||||||
) async {
|
) async {
|
||||||
final Map<String, dynamic> args = <String, dynamic>{};
|
final Map<String, dynamic> args = <String, dynamic>{};
|
||||||
args['assets'] = assets;
|
args['assets'] = assets;
|
||||||
args['existingIDs'] = existingIDs;
|
args['existingIDs'] = existingIDs;
|
||||||
args['invalidIDs'] = invalidIDs;
|
args['invalidIDs'] = invalidIDs;
|
||||||
args['pathToLocalIDs'] = pathToLocalIDs;
|
args['pathToLocalIDs'] = pathToLocalIDs;
|
||||||
final LocalDiffResult diffResult = await computer.compute(
|
final LocalDiffResult diffResult = await Computer.shared().compute(
|
||||||
_getLocalAssetsDiff,
|
_getLocalAssetsDiff,
|
||||||
param: args,
|
param: args,
|
||||||
taskName: "getLocalAssetsDiff",
|
taskName: "getLocalAssetsDiff",
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:computer/computer.dart';
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.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';
|
||||||
|
@ -24,7 +23,6 @@ import 'package:tuple/tuple.dart';
|
||||||
class LocalSyncService {
|
class LocalSyncService {
|
||||||
final _logger = Logger("LocalSyncService");
|
final _logger = Logger("LocalSyncService");
|
||||||
final _db = FilesDB.instance;
|
final _db = FilesDB.instance;
|
||||||
final Computer _computer = Computer.shared();
|
|
||||||
late SharedPreferences _prefs;
|
late SharedPreferences _prefs;
|
||||||
Completer<void>? _existingSync;
|
Completer<void>? _existingSync;
|
||||||
|
|
||||||
|
@ -194,7 +192,6 @@ class LocalSyncService {
|
||||||
existingLocalFileIDs,
|
existingLocalFileIDs,
|
||||||
pathToLocalIDs,
|
pathToLocalIDs,
|
||||||
invalidIDs,
|
invalidIDs,
|
||||||
_computer,
|
|
||||||
);
|
);
|
||||||
bool hasAnyMappingChanged = false;
|
bool hasAnyMappingChanged = false;
|
||||||
if (localDiffResult.newPathToLocalIDs?.isNotEmpty ?? false) {
|
if (localDiffResult.newPathToLocalIDs?.isNotEmpty ?? false) {
|
||||||
|
@ -299,17 +296,18 @@ class LocalSyncService {
|
||||||
required int toTime,
|
required int toTime,
|
||||||
}) async {
|
}) async {
|
||||||
final Tuple2<List<LocalPathAsset>, List<File>> result =
|
final Tuple2<List<LocalPathAsset>, List<File>> result =
|
||||||
await getLocalPathAssetsAndFiles(fromTime, toTime, _computer);
|
await getLocalPathAssetsAndFiles(fromTime, toTime);
|
||||||
|
|
||||||
|
final List<File> files = result.item2;
|
||||||
|
if (files.isNotEmpty) {
|
||||||
// Update the mapping for device path_id to local file id. Also, keep track
|
// Update the mapping for device path_id to local file id. Also, keep track
|
||||||
// of newly discovered device paths
|
// of newly discovered device paths
|
||||||
await FilesDB.instance.insertLocalAssets(
|
await FilesDB.instance.insertLocalAssets(
|
||||||
result.item1,
|
result.item1,
|
||||||
shouldAutoBackup: Configuration.instance.hasSelectedAllFoldersForBackup(),
|
shouldAutoBackup:
|
||||||
|
Configuration.instance.hasSelectedAllFoldersForBackup(),
|
||||||
);
|
);
|
||||||
|
|
||||||
final List<File> files = result.item2;
|
|
||||||
if (files.isNotEmpty) {
|
|
||||||
_logger.info(
|
_logger.info(
|
||||||
"Loaded ${files.length} photos from " +
|
"Loaded ${files.length} photos from " +
|
||||||
DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() +
|
DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() +
|
||||||
|
|
Loading…
Add table
Reference in a new issue