Better way to compare if 2 Files are the same file on server

This commit is contained in:
Ming Ming 2021-10-23 12:39:50 +08:00
parent 743f9b79e1
commit d7a41161ed
3 changed files with 21 additions and 6 deletions

View file

@ -8,11 +8,13 @@ import 'package:nc_photos/or_null.dart';
import 'package:nc_photos/type.dart';
List<AlbumItem> makeDistinctAlbumItems(List<AlbumItem> items) =>
items.distinctIf(
(a, b) =>
a is AlbumFileItem &&
b is AlbumFileItem &&
a.file.path == b.file.path, (a) {
items.distinctIf((a, b) {
if (a is! AlbumFileItem || b is! AlbumFileItem) {
return false;
} else {
return a.file.compareServerIdentity(b.file);
}
}, (a) {
if (a is AlbumFileItem) {
return a.file.path.hashCode;
} else {

View file

@ -469,6 +469,18 @@ extension FileExtension on File {
String get filename => path_util.basename(path);
/// Compare the server identity of two Files
///
/// Return true if two Files point to the same file on server. Be careful that
/// this does NOT mean that the two Files are identical
bool compareServerIdentity(File other) {
if (fileId != null && other.fileId != null) {
return fileId == other.fileId;
} else {
return path == other.path;
}
}
static final _log = Logger("entity.file.FileExtension");
}

View file

@ -491,7 +491,8 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
if (AlbumStaticProvider.of(album)
.items
.whereType<AlbumFileItem>()
.containsIf(newItem, (a, b) => a.file.path == b.file.path)) {
.containsIf(
newItem, (a, b) => a.file.compareServerIdentity(b.file))) {
// already added, do nothing
_log.info("[_addToAlbum] File already in album: ${widget.file.path}");
SnackBarManager().showSnackBar(SnackBar(