Fix refresh for device path cover and count

This commit is contained in:
Neeraj Gupta 2022-08-23 11:08:18 +05:30
parent b396d420b0
commit cf84d21ac2
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
3 changed files with 42 additions and 23 deletions

View file

@ -6,11 +6,15 @@ import 'package:photos/models/device_folder.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_load_result.dart';
import 'package:sqflite/sqlite_api.dart';
import 'package:tuple/tuple.dart';
extension DeviceFiles on FilesDB {
static final Logger _logger = Logger("DeviceFilesDB");
Future<void> insertDeviceFiles(List<File> files) async {
Future<void> insertDeviceFiles(
List<File> files, {
ConflictAlgorithm conflictAlgorithm = ConflictAlgorithm.ignore,
}) async {
final startTime = DateTime.now();
final db = await database;
var batch = db.batch();
@ -34,7 +38,7 @@ extension DeviceFiles on FilesDB {
"id": file.localID,
"path_id": file.devicePathID,
},
conflictAlgorithm: ConflictAlgorithm.ignore,
conflictAlgorithm: conflictAlgorithm,
);
batchCounter++;
}
@ -83,6 +87,7 @@ extension DeviceFiles on FilesDB {
return result;
}
// todo: covert it to batch
Future<void> insertOrUpdatePathName(
List<AssetPathEntity> pathEntities,
) async {
@ -113,16 +118,32 @@ extension DeviceFiles on FilesDB {
}
Future<int> updateDeviceCoverWithCount(
AssetPathEntity pathEntity,
String localID,
List<Tuple2<AssetPathEntity, File>> devicePathInfo,
) async {
try {
final Database db = await database;
return db.rawUpdate(
"UPDATE device_path_collections SET name = ?, cover_id = ?, count"
" = ? where id = ?",
[pathEntity.name, localID, pathEntity.assetCount, pathEntity.id],
);
final Set<String> existingPathIds = await getDevicePathIDs();
for (Tuple2<AssetPathEntity, File> tup in devicePathInfo) {
AssetPathEntity pathEntity = tup.item1;
String localID = tup.item2.localID;
if (existingPathIds.contains(pathEntity.id)) {
await db.rawUpdate(
"UPDATE device_path_collections SET name = ?, cover_id = ?, count"
" = ? where id = ?",
[pathEntity.name, localID, pathEntity.assetCount, pathEntity.id],
);
} else {
await db.insert(
"device_path_collections",
{
"id": pathEntity.id,
"name": pathEntity.name,
"count": pathEntity.assetCount,
"cover_id": localID,
},
);
}
}
} catch (e) {
_logger.severe("failed to save path names", e);
rethrow;
@ -163,8 +184,7 @@ extension DeviceFiles on FilesDB {
);
final files = convertToFiles(fileRows);
final devicePathRows = await db.rawQuery(
'''SELECT * from
device_path_collections''',
'''SELECT * from device_path_collections''',
);
final List<DevicePathCollection> deviceCollections = [];
for (var row in devicePathRows) {

View file

@ -32,12 +32,12 @@ Future<Tuple2<List<AssetPathEntity>, List<File>>> getDeviceFiles(
}
// getDeviceFolderWithCountAndLatestFile returns a tuple of AssetPathEntity and
// latest file in the assetPath, along with modifiedAt time and total counts
// latest file in the assetPath, along with modifiedPath time and total counts
// of assets in a Asset Path. We use this result to update the latest thumbnail
// for any collection and also identify which AssetPath needs to be resynced
// for any collection and also identify which AssetPath needs to be re-synced
// again.
Future<List<Tuple2<AssetPathEntity, File>>>
getDeviceFolderWithCountAndLatestFile() async {
getDeviceFolderWithCountAndCoverFile() async {
List<Tuple2<AssetPathEntity, File>> result = [];
final pathEntities = await _getGalleryList(
needsTitle: false,
@ -46,6 +46,7 @@ Future<List<Tuple2<AssetPathEntity, File>>>
const OrderOption(type: OrderOptionType.createDate, asc: false),
);
for (AssetPathEntity pathEntity in pathEntities) {
//todo: test and handle empty album case
var latestEntity = await pathEntity.getAssetListPaged(
page: 0,
size: 1,

View file

@ -67,7 +67,6 @@ class LocalSyncService {
return;
}
}
refreshDeviceFolderCovers();
if (_existingSync != null) {
_logger.warning("Sync already in progress, skipping.");
return _existingSync.future;
@ -119,7 +118,7 @@ class LocalSyncService {
if (!_prefs.containsKey(kHasCompletedFirstImportKey) ||
!_prefs.getBool(kHasCompletedFirstImportKey)) {
await _prefs.setBool(kHasCompletedFirstImportKey, true);
await refreshDeviceFolderCovers();
await refreshDeviceFolderCountAndCover();
_logger.fine("first gallery import finished");
Bus.instance
.fire(SyncStatusUpdate(SyncStatus.completedFirstGalleryImport));
@ -131,13 +130,10 @@ class LocalSyncService {
_existingSync = null;
}
Future<void> refreshDeviceFolderCovers() async {
Future<void> refreshDeviceFolderCountAndCover() async {
List<Tuple2<AssetPathEntity, File>> result =
await getDeviceFolderWithCountAndLatestFile();
for (Tuple2<AssetPathEntity, File> tup in result) {
await FilesDB.instance
.updateDeviceCoverWithCount(tup.item1, tup.item2.localID);
}
await getDeviceFolderWithCountAndCoverFile();
await _db.updateDeviceCoverWithCount(result);
}
Future<bool> syncAll() async {
@ -152,8 +148,10 @@ class LocalSyncService {
d.inMilliseconds.toString() +
"ms",
);
await refreshDeviceFolderCovers();
await refreshDeviceFolderCountAndCover();
final existingIDs = await _db.getExistingLocalFileIDs();
final Map<String, Set<String>> pathToLocalIDs =
await _db.getDevicePathIDToLocalIDMap();
final invalidIDs = _getInvalidFileIDs().toSet();
final unsyncedFiles =
await getUnsyncedFiles(localAssets, existingIDs, invalidIDs, _computer);