mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-03-13 18:58:53 +01:00
Fix new album item replacing existing ones during conflict
This commit is contained in:
parent
be435f31f0
commit
9a111c1fb9
2 changed files with 30 additions and 22 deletions
|
@ -1,28 +1,10 @@
|
|||
import 'dart:math';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/ci_string.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
import 'package:nc_photos/list_extension.dart';
|
||||
import 'package:nc_photos/or_null.dart';
|
||||
import 'package:nc_photos/type.dart';
|
||||
|
||||
List<AlbumItem> makeDistinctAlbumItems(List<AlbumItem> items) =>
|
||||
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 {
|
||||
return Random().nextInt(0xFFFFFFFF);
|
||||
}
|
||||
});
|
||||
|
||||
abstract class AlbumItem with EquatableMixin {
|
||||
AlbumItem({
|
||||
required this.addedBy,
|
||||
|
|
|
@ -7,6 +7,7 @@ import 'package:nc_photos/entity/album/item.dart';
|
|||
import 'package:nc_photos/entity/album/provider.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
import 'package:nc_photos/entity/share.dart';
|
||||
import 'package:nc_photos/override_comparator.dart';
|
||||
import 'package:nc_photos/pref.dart';
|
||||
import 'package:nc_photos/use_case/create_share.dart';
|
||||
import 'package:nc_photos/use_case/list_share.dart';
|
||||
|
@ -24,10 +25,19 @@ class AddToAlbum {
|
|||
assert(album.provider is AlbumStaticProvider);
|
||||
// resync is needed to work out album cover and latest item
|
||||
final oldItems = await PreProcessAlbum(appDb)(account, album);
|
||||
final newItems = makeDistinctAlbumItems([
|
||||
...items,
|
||||
...oldItems,
|
||||
]);
|
||||
final itemSet = oldItems
|
||||
.map((e) => OverrideComparator<AlbumItem>(
|
||||
e, _isItemFileEqual, _getItemHashCode))
|
||||
.toSet();
|
||||
// find the items that are not having the same file as any existing ones
|
||||
final addItems = items
|
||||
.where((i) => itemSet.add(OverrideComparator<AlbumItem>(
|
||||
i, _isItemFileEqual, _getItemHashCode)))
|
||||
.toList();
|
||||
if (addItems.isEmpty) {
|
||||
return album;
|
||||
}
|
||||
final newItems = <AlbumItem>[...addItems, ...oldItems];
|
||||
var newAlbum = album.copyWith(
|
||||
provider: AlbumStaticProvider.of(album).copyWith(
|
||||
items: newItems,
|
||||
|
@ -98,3 +108,19 @@ class AddToAlbum {
|
|||
|
||||
static final _log = Logger("use_case.add_to_album.AddToAlbum");
|
||||
}
|
||||
|
||||
bool _isItemFileEqual(AlbumItem a, AlbumItem b) {
|
||||
if (a is! AlbumFileItem || b is! AlbumFileItem) {
|
||||
return false;
|
||||
} else {
|
||||
return a.file.compareServerIdentity(b.file);
|
||||
}
|
||||
}
|
||||
|
||||
int _getItemHashCode(AlbumItem a) {
|
||||
if (a is AlbumFileItem) {
|
||||
return a.file.path.hashCode;
|
||||
} else {
|
||||
return a.hashCode;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue