use remoteDB for collection cover

This commit is contained in:
Neeraj Gupta
2025-04-23 12:00:21 +05:30
parent 7e2242dc69
commit d87e679650
5 changed files with 65 additions and 43 deletions

View File

@@ -1363,30 +1363,6 @@ class FilesDB with SqlDbBase {
return result;
}
// getCollectionFileFirstOrLast returns the first or last uploaded file in
// the collection based on the given collectionID and the order.
Future<EnteFile?> getCollectionFileFirstOrLast(
int collectionID,
bool sortAsc,
) async {
final db = await instance.sqliteAsyncDB;
final order = sortAsc ? 'ASC' : 'DESC';
final rows = await db.getAll(
'''
SELECT * FROM $filesTable
WHERE $columnCollectionID = ? AND ($columnUploadedFileID IS NOT NULL
AND $columnUploadedFileID IS NOT -1)
ORDER BY $columnCreationTime $order, $columnModificationTime $order
LIMIT 1;
''',
[collectionID],
);
if (rows.isEmpty) {
return null;
}
return convertToFiles(rows).first;
}
Future<void> markForReUploadIfLocationMissing(List<String> localIDs) async {
if (localIDs.isEmpty) {
return;

View File

@@ -54,4 +54,29 @@ extension CollectionFileRead on RemoteDB {
.map((row) => CollectionFileEntry.fromMap(row))
.toList(growable: false);
}
Future<CollectionFileEntry?> coverFile(
int collectionID,
int? fileID, {
bool sortInAsc = false,
}) async {
if (fileID != null) {
final row = await sqliteDB.getOptional(
"SELECT * FROM collection_files WHERE collection_id = ? AND file_id = ?",
[collectionID, fileID],
);
if (row != null) {
return CollectionFileEntry.fromMap(row);
}
}
final sortedRow = await sqliteDB.getOptional(
"SELECT * FROM collection_files join files on files.id= collection_files.file_id WHERE collection_id = ? ORDER BY files.creation_time ${sortInAsc ? 'ASC' : 'DESC'} LIMIT 1",
[collectionID],
);
if (sortedRow != null) {
return CollectionFileEntry.fromMap(sortedRow);
}
return null;
}
}

View File

@@ -126,6 +126,15 @@ class Collection {
return mMdVersion > 0 && (magicMetadata.visibility == hiddenVisibility);
}
int get visibility {
if (isHidden()) {
return hiddenVisibility;
} else if (isArchived() || hasShareeArchived()) {
return archiveVisibility;
}
return 0;
}
bool isDefaultHidden() {
return (magicMetadata.subType ?? 0) == subTypeDefaultHidden;
}

View File

@@ -321,18 +321,7 @@ class CollectionsService {
if (kDebugMode) {
debugPrint("getCover for collection ${c.id} ${c.displayName}");
}
if (c.hasCover) {
final coverID = c.pubMagicMetadata.coverID ?? 0;
final EnteFile? cover = await filesDB.getUploadedFile(coverID, c.id);
if (cover != null) {
_coverCache[coverKey] = cover;
return Future.value(cover);
}
}
final coverFile = await filesDB.getCollectionFileFirstOrLast(
c.id,
c.pubMagicMetadata.asc ?? false,
);
final coverFile = await remoteCache.getAlbumCover(c);
if (coverFile != null) {
_coverCache[coverKey] = coverFile;
return Future.value(coverFile);

View File

@@ -1,5 +1,7 @@
import "package:flutter/foundation.dart";
import "package:photos/db/remote/read/collection_files.dart";
import "package:photos/db/remote/schema.dart";
import "package:photos/models/collection/collection.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/file/remote/asset.dart";
import "package:photos/service_locator.dart";
@@ -10,13 +12,7 @@ class RemoteCache {
Future<List<EnteFile>> getCollectionFiles(FilterQueryParam? params) async {
final cf = await remoteDB.getCollectionFiles(params);
if (isLoaded == null) {
final assets = await remoteDB.getAllFiles();
for (final asset in assets) {
remoteAssets[asset.id] = asset;
}
isLoaded = true;
}
final _ = isLoaded ?? await _load();
final List<EnteFile> files = [];
for (final file in cf) {
final asset = remoteAssets[file.fileID];
@@ -26,4 +22,31 @@ class RemoteCache {
}
return files;
}
Future<void> _load() async {
if (isLoaded == null) {
final assets = await remoteDB.getAllFiles();
for (final asset in assets) {
remoteAssets[asset.id] = asset;
}
isLoaded = true;
}
}
Future<EnteFile?> getAlbumCover(Collection c) async {
final cf = await remoteDB.coverFile(
c.id,
c.pubMagicMetadata.coverID,
sortInAsc: c.pubMagicMetadata.asc ?? false,
);
final _ = isLoaded ?? await _load();
if (cf == null) {
return null;
}
final asset = remoteAssets[cf.fileID];
if (asset == null) {
return null;
}
return EnteFile.fromRemoteAsset(asset, cf);
}
}