π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Preserve device identity and refresh picker metadata
r4khul8e90c691c5fd
diff --git a/mobile/apps/photos/lib/ui/device_picker/device_picker_gallery.dart b/mobile/apps/photos/lib/ui/device_picker/device_picker_gallery.dartindex 70ea3ea434..596b2d63db 100644--- a/mobile/apps/photos/lib/ui/device_picker/device_picker_gallery.dart+++ b/mobile/apps/photos/lib/ui/device_picker/device_picker_gallery.dart@@ -1,5 +1,6 @@import "dart:async";+import "package:collection/collection.dart";import "package:flutter/foundation.dart";import "package:flutter/material.dart";import "package:logging/logging.dart";@@ -46,6 +47,7 @@ class _DevicePickerGalleryState extends State<DevicePickerGallery> {item.id,() => fileFromAsset(widget.model.album?.name ?? '', item),);+ _selection.register(file, item.id);if (file.duration == null || file.duration == 0) {file.duration = item.duration;}@@ -64,20 +66,30 @@ class _DevicePickerGalleryState extends State<DevicePickerGallery> {void _updateFiles() {_snapshot = List.of(widget.model.media);- final available = [...widget.model.selection, ..._snapshot];- final ids = available.map((item) => item.id).toSet();+ final available = {+ for (final item in widget.model.selection) item.id: item,+ for (final item in _snapshot) item.id: item,+ };+ final ids = available.keys.toSet();_assets.removeWhere((id, _) => !ids.contains(id));_files.removeWhere((id, _) => !ids.contains(id));_resolvedIDs.retainAll(ids);- for (final item in available) {- if (_assets[item.id] != item) _files.remove(item.id);+ for (final item in available.values) {+ if (!identical(_assets[item.id], item)) {+ _files.remove(item.id);+ _resolvedIDs.remove(item.id);+ }_assets[item.id] = item;}}void _modelChanged() {_selection.locked = widget.model.confirming;- if (listEquals(_snapshot, widget.model.media)) return;+ if (const ListEquality<AssetEntity>(+ IdentityEquality(),+ ).equals(_snapshot, widget.model.media)) {+ return;+ }_updateFiles();_reload.add(FilesUpdatedEvent([]));}@@ -102,7 +114,15 @@ class _DevicePickerGalleryState extends State<DevicePickerGallery> {_files.addAll(files);_resolvedIDs.addAll(ids);_readSelection();- return FileLoadResult(snapshot.map(_fileFor).toList(), false);+ return FileLoadResult(+ snapshot+ .map(_fileFor)+ .sortedByCompare(+ (file) => file.creationTime ?? 0,+ (first, second) => second.compareTo(first),+ ),+ false,+ );}void _readSelection() {@@ -115,7 +135,9 @@ class _DevicePickerGalleryState extends State<DevicePickerGallery> {void _writeSelection() {if (_syncingSelection) return;widget.model.replaceSelection(- _selection.files.map((file) => _assets[file.localID]).nonNulls,+ _selection.files+ .map((file) => _assets[_selection.assetID(file)])+ .nonNulls,);}diff --git a/mobile/apps/photos/lib/ui/device_picker/device_picker_selection.dart b/mobile/apps/photos/lib/ui/device_picker/device_picker_selection.dartindex a14c80e7c9..3dcc07ed69 100644--- a/mobile/apps/photos/lib/ui/device_picker/device_picker_selection.dart+++ b/mobile/apps/photos/lib/ui/device_picker/device_picker_selection.dart@@ -1,11 +1,25 @@+import "dart:collection";+import "package:photos/core/constants.dart";import "package:photos/models/file/dummy_file.dart";import "package:photos/models/file/file.dart";import "package:photos/models/selected_files.dart";class DevicePickerSelectedFiles extends SelectedFiles {+ final _assetIDs = Expando<String>();+ late final _files = LinkedHashSet<EnteFile>(+ equals: (first, second) => assetID(first) == assetID(second),+ hashCode: (file) => assetID(file).hashCode,+ );bool locked = false;+ @override+ Set<EnteFile> get files => _files;++ void register(EnteFile file, String id) => _assetIDs[file] ??= id;++ String? assetID(EnteFile file) => _assetIDs[file] ?? file.localID;+@overridebool isFileSelected(EnteFile file) => files.contains(file);@@ -32,7 +46,12 @@ class DevicePickerSelectedFiles extends SelectedFiles {@overridevoid unSelectAll(Set<EnteFile> filesToUnselect, {bool skipNotify = false}) {- if (!locked) super.unSelectAll(filesToUnselect, skipNotify: skipNotify);+ if (!locked) {+ super.unSelectAll(+ filesToUnselect.map(files.lookup).nonNulls.toSet(),+ skipNotify: skipNotify,+ );+ }}@overridediff --git a/mobile/apps/photos/test/ui/device_picker/device_picker_page_test.dart b/mobile/apps/photos/test/ui/device_picker/device_picker_page_test.dartindex 335d55f623..a53bec9fe7 100644--- a/mobile/apps/photos/test/ui/device_picker/device_picker_page_test.dart+++ b/mobile/apps/photos/test/ui/device_picker/device_picker_page_test.dart@@ -196,6 +196,64 @@ void main() {await tester.pumpAndSettle();});+ testWidgets('gallery dates stay ordered when stored dates differ', (+ tester,+ ) async {+ library.items = library.items.take(3).toList();+ for (final (index, day) in [3, 1, 2].indexed) {+ final asset = library.items[index];+ library.knownFiles[asset.id] = fileFromAsset('Camera', asset)+ ..creationTime = DateTime(2026, 10, day).microsecondsSinceEpoch;+ }+ await mount(tester);+ expect(+ tester+ .widgetList<GalleryFileWidget>(find.byType(GalleryFileWidget))+ .map((tile) => tile.file.localID),+ ['0', '2', '1'],+ );+ library.knownFiles.clear();+ library.items = [+ for (final (index, asset) in library.items.indexed)+ asset.copyWith(+ createDateSecond:+ DateTime(2026, 10, index + 1).millisecondsSinceEpoch ~/ 1000,+ ),+ ];+ await tester+ .widget<DevicePickerGallery>(find.byType(DevicePickerGallery))+ .model+ .refresh();+ await _settleImages(tester);+ expect(+ tester+ .widgetList<GalleryFileWidget>(find.byType(GalleryFileWidget))+ .map((tile) => tile.file.localID),+ ['2', '1', '0'],+ );+ await tester.pumpWidget(const SizedBox.shrink());+ await tester.pumpAndSettle();+ });++ testWidgets('cleared local IDs still return the original device assets', (+ tester,+ ) async {+ final asset = library.items.first;+ final file = fileFromAsset('Camera', asset)..generatedID = 1;+ library.knownFiles[asset.id] = file;+ await mount(tester);+ file.localID = null;+ await tester.tap(find.byType(GalleryFileWidget).first);+ await tester.pumpAndSettle();+ expect(find.text('1/50'), findsOneWidget);+ await tester.tap(find.text('Add selected'));+ await _settleImages(tester);+ expect(await result, [asset]);+ expect(tester.takeException(), isNull);+ await tester.pumpWidget(const SizedBox.shrink());+ await tester.pumpAndSettle();+ });+testWidgets('Add selected in preview returns the remaining original assets', (tester,) async {@@ -295,7 +353,9 @@ void main() {expect(model.media.length, 96);await tester.tap(find.byType(GalleryFileWidget).first);await tester.pumpAndSettle();- model.replaceSelection(library.items.take(50));+ model.replaceSelection(+ library.items.take(50).map((item) => item.copyWith()),+ );await tester.pumpAndSettle();expect(library.fileLookupBatches, hasLength(1));final scroll = testerdiff --git a/mobile/apps/photos/test/ui/device_picker/device_picker_selection_test.dart b/mobile/apps/photos/test/ui/device_picker/device_picker_selection_test.dartindex b2c07827be..6b94e53a6c 100644--- a/mobile/apps/photos/test/ui/device_picker/device_picker_selection_test.dart+++ b/mobile/apps/photos/test/ui/device_picker/device_picker_selection_test.dart@@ -50,6 +50,30 @@ void main() {expect(selection.files.any((file) => file is DummyFile), isFalse);});+ test('uploaded files stay selected when their local ID is cleared', () {+ final selection = DevicePickerSelectedFiles();+ addTearDown(selection.dispose);+ final file = EnteFile()+ ..generatedID = 1+ ..uploadedFileID = 10+ ..localID = 'photo';+ selection.register(file, 'photo');+ selection.toggleSelection(file);+ file.localID = null;+ expect(selection.isFileSelected(file), isTrue);+ selection.toggleSelection(file);+ expect(selection.files, isEmpty);+ selection.toggleSelection(file);+ expect(selection.files.single, same(file));+ final refreshed = EnteFile()+ ..generatedID = 2+ ..localID = 'photo';+ selection.register(refreshed, 'photo');+ expect(selection.isFileSelected(refreshed), isTrue);+ selection.toggleGroupSelection({refreshed});+ expect(selection.files, isEmpty);+ });+test('confirmation locks gestures and clearing stays inside the picker',() async {