Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Link contacts to unassigned faces

Neeraj Guptab6ea466a245b
diff --git a/mobile/apps/photos/lib/ui/viewer/search/result/contact_person_picker_page.dart b/mobile/apps/photos/lib/ui/viewer/search/result/contact_person_picker_page.dart
index 480e1e5e49..79b9227866 100644
--- a/mobile/apps/photos/lib/ui/viewer/search/result/contact_person_picker_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search/result/contact_person_picker_page.dart
@@ -4,18 +4,61 @@ import "package:flutter/material.dart";
import "package:hugeicons/hugeicons.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/models/ml/face/person.dart";
+import "package:photos/models/search/search_constants.dart";
import "package:photos/ui/viewer/people/face_thumbnail_squircle.dart";
import "package:photos/ui/viewer/people/person_face_widget.dart";
import "package:photos/utils/person_contact_linking_util.dart";
+sealed class ContactPersonPickerCandidate {
+ const ContactPersonPickerCandidate();
+}
+
+class ContactPersonPickerPersonCandidate extends ContactPersonPickerCandidate {
+ const ContactPersonPickerPersonCandidate(this.person);
+
+ final PersonEntity person;
+}
+
+class ContactPersonPickerClusterCandidate extends ContactPersonPickerCandidate {
+ const ContactPersonPickerClusterCandidate(this.clusterID);
+
+ final String clusterID;
+}
+
+List<ContactPersonPickerCandidate> buildContactPersonPickerCandidates({
+ required Iterable<Map<String, dynamic>> faceResultParams,
+ required Iterable<PersonEntity> persons,
+}) {
+ final personsByID = {for (final person in persons) person.remoteID: person};
+ final candidates = <ContactPersonPickerCandidate>[];
+
+ for (final params in faceResultParams) {
+ final personID = params[kPersonParamID] as String?;
+ if (personID != null) {
+ final person = personsByID[personID];
+ if (person != null) {
+ candidates.add(ContactPersonPickerPersonCandidate(person));
+ }
+ continue;
+ }
+
+ final clusterID = params[kClusterParamId] as String?;
+ if (clusterID != null) {
+ candidates.add(ContactPersonPickerClusterCandidate(clusterID));
+ }
+ }
+
+ return candidates;
+}
+
abstract class ContactPersonPickerResult {
const ContactPersonPickerResult();
}
class ContactPersonPickerSelected extends ContactPersonPickerResult {
- const ContactPersonPickerSelected(this.person);
+ const ContactPersonPickerSelected(this.candidate);
- final PersonEntity person;
+ final ContactPersonPickerCandidate candidate;
}
class ContactPersonPickerPickPhoto extends ContactPersonPickerResult {
@@ -26,13 +69,13 @@ class ContactPersonPickerPage extends StatefulWidget {
const ContactPersonPickerPage({
required this.contactUserId,
required this.contactEmail,
- required this.persons,
+ required this.candidates,
super.key,
});
final int contactUserId;
final String contactEmail;
- final List<PersonEntity> persons;
+ final List<ContactPersonPickerCandidate> candidates;
static const _horizontalPadding = 16.0;
static const _gridGap = 10.0;
@@ -49,26 +92,34 @@ class _ContactPersonPickerPageState extends State<ContactPersonPickerPage> {
Widget build(BuildContext context) {
final colors = context.componentColors;
final l10n = AppLocalizations.of(context);
- final persons =
- widget.persons
+ final personCandidates =
+ widget.candidates
+ .whereType<ContactPersonPickerPersonCandidate>()
.where(
- (person) => !isLinkedToDifferentContact(
- person,
+ (candidate) => !isLinkedToDifferentContact(
+ candidate.person,
contactUserId: widget.contactUserId,
email: widget.contactEmail,
),
)
.where(
- (person) => person.data.name.toLowerCase().contains(_searchQuery),
+ (candidate) => candidate.person.data.name.toLowerCase().contains(
+ _searchQuery,
+ ),
)
.toList()
..sort((first, second) {
final comparison = compareAsciiLowerCaseNatural(
- first.data.name,
- second.data.name,
+ first.person.data.name,
+ second.person.data.name,
);
return _sortAscending ? comparison : -comparison;
});
+ final candidates = <ContactPersonPickerCandidate>[
+ ...personCandidates,
+ if (_searchQuery.isEmpty)
+ ...widget.candidates.whereType<ContactPersonPickerClusterCandidate>(),
+ ];
return Scaffold(
backgroundColor: colors.backgroundBase,
@@ -96,7 +147,7 @@ class _ContactPersonPickerPageState extends State<ContactPersonPickerPage> {
),
sliver: SliverToBoxAdapter(child: _buildSearchRow(context)),
),
- _buildGrid(persons),
+ _buildGrid(candidates),
],
),
);
@@ -163,7 +214,7 @@ class _ContactPersonPickerPageState extends State<ContactPersonPickerPage> {
}
}
- Widget _buildGrid(List<PersonEntity> persons) {
+ Widget _buildGrid(List<ContactPersonPickerCandidate> candidates) {
return SliverPadding(
padding: const EdgeInsets.fromLTRB(
ContactPersonPickerPage._horizontalPadding,
@@ -186,10 +237,15 @@ class _ContactPersonPickerPageState extends State<ContactPersonPickerPage> {
childAspectRatio: tileSize / (tileSize + 28),
),
delegate: SliverChildBuilderDelegate(
- childCount: persons.length,
+ childCount: candidates.length,
(context, index) => _PersonTile(
- key: ValueKey(persons[index].remoteID),
- person: persons[index],
+ key: ValueKey(switch (candidates[index]) {
+ ContactPersonPickerPersonCandidate(:final person) =>
+ "person-${person.remoteID}",
+ ContactPersonPickerClusterCandidate(:final clusterID) =>
+ "cluster-$clusterID",
+ }),
+ candidate: candidates[index],
size: tileSize,
),
),
@@ -201,20 +257,28 @@ class _ContactPersonPickerPageState extends State<ContactPersonPickerPage> {
}
class _PersonTile extends StatelessWidget {
- const _PersonTile({super.key, required this.person, required this.size});
+ const _PersonTile({super.key, required this.candidate, required this.size});
- final PersonEntity person;
+ final ContactPersonPickerCandidate candidate;
final double size;
@override
Widget build(BuildContext context) {
final colors = context.componentColors;
final pixelWidth = (size * MediaQuery.devicePixelRatioOf(context)).round();
+ final person = switch (candidate) {
+ ContactPersonPickerPersonCandidate(:final person) => person,
+ ContactPersonPickerClusterCandidate() => null,
+ };
+ final clusterID = switch (candidate) {
+ ContactPersonPickerPersonCandidate() => null,
+ ContactPersonPickerClusterCandidate(:final clusterID) => clusterID,
+ };
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () =>
- Navigator.of(context).pop(ContactPersonPickerSelected(person)),
+ Navigator.of(context).pop(ContactPersonPickerSelected(candidate)),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
@@ -226,7 +290,8 @@ class _PersonTile extends StatelessWidget {
child: ColoredBox(
color: colors.strokeFaint,
child: PersonFaceWidget(
- personId: person.remoteID,
+ personId: person?.remoteID,
+ clusterID: clusterID,
keepAlive: true,
cachedPixelWidth: pixelWidth,
),
@@ -237,7 +302,7 @@ class _PersonTile extends StatelessWidget {
SizedBox(
width: double.infinity,
child: Text(
- person.data.name,
+ person?.data.name ?? " ",
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyles.body.copyWith(color: colors.textBase),
diff --git a/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart b/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart
index 398ee53750..f1fcaa42a0 100644
--- a/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart
@@ -1,3 +1,4 @@
+import "dart:async";
import "dart:typed_data";
import "package:ente_components/ente_components.dart";
@@ -11,9 +12,9 @@ import "package:photos/core/event_bus.dart";
import "package:photos/events/people_changed_event.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/models/ml/face/person.dart";
-import "package:photos/models/search/search_constants.dart";
import "package:photos/module/download/thumbnail.dart";
import "package:photos/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart";
+import "package:photos/services/machine_learning/face_ml/feedback/cluster_feedback.dart";
import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
import "package:photos/services/photos_contacts_service.dart";
import "package:photos/services/search_service.dart";
@@ -62,6 +63,7 @@ class _EditContactPageState extends State<EditContactPage> {
int _photoLoadGeneration = 0;
PersonEntity? _initialLinkedPerson;
PersonEntity? _draftLinkedPerson;
+ String? _draftUnassignedClusterID;
bool _linkDirty = false;
@override
@@ -90,7 +92,8 @@ class _EditContactPageState extends State<EditContactPage> {
String get _initialName => (widget.existingContact?.data?.name ?? "").trim();
bool get _hasUnsavedChanges =>
_nameController.text.trim() != _initialName || _photoDirty || _linkDirty;
- bool get _hasLinkedPersonDraft => _draftLinkedPerson != null;
+ bool get _hasLinkedPersonDraft =>
+ _draftLinkedPerson != null || _draftUnassignedClusterID != null;
bool get _hasContactPhoto =>
_draftPhotoBytes != null ||
(!_photoDirty &&
@@ -318,19 +321,16 @@ class _EditContactPageState extends State<EditContactPage> {
if (_isSaving) {
return;
}
- List<PersonEntity> persons;
+ List<ContactPersonPickerCandidate> candidates;
try {
final faceResults = await SearchService.instance.getAllFace(
null,
minClusterSize: kMinimumClusterSizeAllFaces,
);
- final personIdsWithFiles = faceResults
- .map((result) => result.params[kPersonParamID])
- .whereType<String>()
- .toSet();
- persons = (await PersonService.instance.getPersons())
- .where((person) => personIdsWithFiles.contains(person.remoteID))
- .toList();
+ candidates = buildContactPersonPickerCandidates(
+ faceResultParams: faceResults.map((result) => result.params),
+ persons: await PersonService.instance.getPersons(),
+ );
} catch (e, s) {
_logger.warning(
"Failed to load people before editing contact photo",
@@ -343,7 +343,7 @@ class _EditContactPageState extends State<EditContactPage> {
if (!mounted) {
return;
}
- if (persons.isEmpty) {
+ if (candidates.isEmpty) {
await _pickContactPhoto();
return;
}
@@ -353,7 +353,7 @@ class _EditContactPageState extends State<EditContactPage> {
ContactPersonPickerPage(
contactUserId: widget.contactUserId,
contactEmail: widget.email,
- persons: persons,
+ candidates: candidates,
),
);
if (!mounted || result == null) {
@@ -364,13 +364,19 @@ class _EditContactPageState extends State<EditContactPage> {
return;
}
if (result is ContactPersonPickerSelected) {
- await _draftSelectedPerson(result.person);
+ switch (result.candidate) {
+ case ContactPersonPickerPersonCandidate(:final person):
+ await _draftSelectedPerson(person);
+ case ContactPersonPickerClusterCandidate(:final clusterID):
+ await _draftSelectedCluster(clusterID);
+ }
}
}
void _draftUnlinkPerson() {
setState(() {
_draftLinkedPerson = null;
+ _draftUnassignedClusterID = null;
_linkDirty = _initialLinkedPerson != null;
});
}
@@ -388,15 +394,41 @@ class _EditContactPageState extends State<EditContactPage> {
_nameController.text = person.data.name;
setState(() {
_draftLinkedPerson = person;
+ _draftUnassignedClusterID = null;
_linkDirty =
_initialLinkedPerson?.remoteID != person.remoteID ||
_personNeedsContactLinkUpdate(person);
});
}
+ Future<void> _draftSelectedCluster(String clusterID) async {
+ setState(() {
+ _draftLinkedPerson = null;
+ _draftUnassignedClusterID = clusterID;
+ _linkDirty = true;
+ });
+ await _loadClusterPhotoDraft(clusterID, showError: true);
+ }
+
Future<void> _loadPersonPhotoDraft(
PersonEntity person, {
required bool showError,
+ }) => _loadFacePhotoDraft(
+ () => buildContactPhotoAttachmentBytesFromPerson(person),
+ showError: showError,
+ );
+
+ Future<void> _loadClusterPhotoDraft(
+ String clusterID, {
+ required bool showError,
+ }) => _loadFacePhotoDraft(
+ () => buildContactPhotoAttachmentBytesFromCluster(clusterID),
+ showError: showError,
+ );
+
+ Future<void> _loadFacePhotoDraft(
+ Future<Uint8List?> Function() loadPhotoBytes, {
+ required bool showError,
}) async {
final loadGeneration = ++_photoLoadGeneration;
setState(() {
@@ -404,9 +436,9 @@ class _EditContactPageState extends State<EditContactPage> {
});
Uint8List? photoBytes;
try {
- photoBytes = await buildContactPhotoAttachmentBytesFromPerson(person);
+ photoBytes = await loadPhotoBytes();
} catch (e, s) {
- _logger.warning("Failed to build contact photo from person", e, s);
+ _logger.warning("Failed to build contact photo from face", e, s);
}
if (!mounted || loadGeneration != _photoLoadGeneration) {
return;
@@ -487,6 +519,39 @@ class _EditContactPageState extends State<EditContactPage> {
});
}
+ Future<void> _createPersonForDraftCluster(String contactName) async {
+ final clusterID = _draftUnassignedClusterID;
+ if (clusterID == null) {
+ return;
+ }
+
+ final person = await PersonService.instance.addPerson(
+ name: contactName,
+ clusterID: clusterID,
+ );
+ unawaited(
+ ClusterFeedbackService.instance
+ .checkAndDoAutomaticMerges(person, personClusterID: clusterID)
+ .catchError((Object error, StackTrace stackTrace) {
+ _logger.warning(
+ "Failed to automatically merge clusters for new person",
+ error,
+ stackTrace,
+ );
+ return false;
+ }),
+ );
+ _draftLinkedPerson = person;
+ _draftUnassignedClusterID = null;
+ Bus.instance.fire(
+ PeopleChangedEvent(
+ type: PeopleEventType.saveOrEditPerson,
+ person: person,
+ source: "edit_contact_create_person",
+ ),
+ );
+ }
+
Future<void> _saveContact() async {
if (!_canSave) {
return;
@@ -497,6 +562,7 @@ class _EditContactPageState extends State<EditContactPage> {
try {
final contactName = _nameController.text.trim();
final contactNameChanged = contactName != _initialName;
+ await _createPersonForDraftCluster(contactName);
var saved = await PhotosContactsService.instance.createOrUpdateContact(
contactUserId: widget.contactUserId,
name: contactName,
diff --git a/mobile/apps/photos/lib/utils/contact_photo_util.dart b/mobile/apps/photos/lib/utils/contact_photo_util.dart
index 73462163d2..98362815fc 100644
--- a/mobile/apps/photos/lib/utils/contact_photo_util.dart
+++ b/mobile/apps/photos/lib/utils/contact_photo_util.dart
@@ -30,14 +30,39 @@ Future<Uint8List?> buildContactPhotoAttachmentBytesFromFace({
Future<Uint8List?> buildContactPhotoAttachmentBytesFromPerson(
PersonEntity person,
) async {
- final hiddenFileIds = await SearchService.instance.getHiddenFiles().then(
- (files) => files.map((file) => file.uploadedFileID).toSet(),
- );
final faceIds = await MLDataDB.instance.getFaceIDsForPersonOrderedByScore(
person.remoteID,
);
+ return _buildContactPhotoAttachmentBytesFromFaceGroup(
+ faceIds: faceIds,
+ avatarFaceId: person.data.avatarFaceID,
+ personID: person.remoteID,
+ );
+}
+
+Future<Uint8List?> buildContactPhotoAttachmentBytesFromCluster(
+ String clusterID,
+) async {
+ final faceIds = await MLDataDB.instance.getFaceIDsForClusterOrderedByScore(
+ clusterID,
+ );
+ return _buildContactPhotoAttachmentBytesFromFaceGroup(
+ faceIds: faceIds,
+ clusterID: clusterID,
+ );
+}
+
+Future<Uint8List?> _buildContactPhotoAttachmentBytesFromFaceGroup({
+ required List<String> faceIds,
+ String? avatarFaceId,
+ String? personID,
+ String? clusterID,
+}) async {
+ final hiddenFileIds = await SearchService.instance.getHiddenFiles().then(
+ (files) => files.map((file) => file.uploadedFileID).toSet(),
+ );
EnteFile? sourceFile;
- String? faceId = person.data.avatarFaceID;
+ String? faceId = avatarFaceId;
if (faceId != null) {
final fileId = getFileIdFromFaceId<int>(faceId);
@@ -67,7 +92,8 @@ Future<Uint8List?> buildContactPhotoAttachmentBytesFromPerson(
final face = await MLDataDB.instance.getCoverFaceForPerson(
recentFileID: sourceFile.uploadedFileID!,
avatarFaceId: faceId,
- personID: person.remoteID,
+ personID: personID,
+ clusterID: clusterID,
);
if (face == null) {
return null;
diff --git a/mobile/apps/photos/test/ui/viewer/search/result/contact_person_picker_page_test.dart b/mobile/apps/photos/test/ui/viewer/search/result/contact_person_picker_page_test.dart
new file mode 100644
index 0000000000..ddf1c488a8
--- /dev/null
+++ b/mobile/apps/photos/test/ui/viewer/search/result/contact_person_picker_page_test.dart
@@ -0,0 +1,28 @@
+import "package:flutter_test/flutter_test.dart";
+import "package:photos/models/ml/face/person.dart";
+import "package:photos/models/search/search_constants.dart";
+import "package:photos/ui/viewer/search/result/contact_person_picker_page.dart";
+
+void main() {
+ test("includes unassigned face clusters alongside existing people", () {
+ final person = PersonEntity("person-1", PersonData(name: "Alex"));
+
+ final candidates = buildContactPersonPickerCandidates(
+ faceResultParams: const [
+ {kPersonParamID: "person-1"},
+ {kClusterParamId: "cluster-1"},
+ ],
+ persons: [person],
+ );
+
+ expect(candidates, hasLength(2));
+ expect(
+ (candidates.first as ContactPersonPickerPersonCandidate).person,
+ same(person),
+ );
+ expect(
+ (candidates.last as ContactPersonPickerClusterCandidate).clusterID,
+ "cluster-1",
+ );
+ });
+}