[mob] Fix person entity update

This commit is contained in:
Neeraj Gupta 2024-04-10 10:59:27 +05:30
parent c920aacd1b
commit fac087c81b
4 changed files with 54 additions and 11 deletions

View file

@ -1,6 +1,8 @@
// PersonEntity represents information about a Person in the context of FaceClustering that is stored.
// On the remote server, the PersonEntity is stored as {Entity} with type person.
// On the device, this information is stored as [LocalEntityData] with type person.
import "package:flutter/foundation.dart";
class PersonEntity {
final String remoteID;
final PersonData data;
@ -80,6 +82,25 @@ class PersonData {
);
}
void logStats() {
if (kDebugMode == false) return;
// log number of assigned and rejected clusters and total number of faces in each cluster
final StringBuffer sb = StringBuffer();
sb.writeln('Person: $name');
int assignedCount = 0;
for (final a in (assigned ?? <ClusterInfo>[])) {
assignedCount += a.faces.length;
}
sb.writeln('Assigned: ${assigned?.length} withFaces $assignedCount');
sb.writeln('Rejected: ${rejected?.length}');
if (assigned != null) {
for (var cluster in assigned!) {
sb.writeln('Cluster: ${cluster.id} - ${cluster.faces.length}');
}
}
debugPrint(sb.toString());
}
// toJson
Map<String, dynamic> toJson() => {
'name': name,

View file

@ -111,6 +111,7 @@ class PersonService {
personID: personID,
clusterID: clusterID,
);
personData.logStats();
}
Future<void> removeClusterToPerson({
@ -129,6 +130,7 @@ class PersonService {
personID: personID,
clusterID: clusterID,
);
personData.logStats();
}
Future<void> deletePerson(String personID, {bool onlyMapping = true}) async {
@ -145,6 +147,7 @@ class PersonService {
id: personID,
);
await faceMLDataDB.removePerson(personID);
justName.data.logStats();
} else {
await entityService.deleteEntry(personID);
await faceMLDataDB.removePerson(personID);
@ -173,10 +176,9 @@ class PersonService {
}
clusterToPersonID[cluster.id] = e.id;
}
if(kDebugMode) {
if (kDebugMode) {
logger.info(
"Person ${e.id} ${personData.name} has ${personData.assigned!
.length} clusters with $faceCount faces",
"Person ${e.id} ${personData.name} has ${personData.assigned!.length} clusters with $faceCount faces",
);
}
}
@ -186,11 +188,33 @@ class PersonService {
await faceMLDataDB.bulkAssignClusterToPersonID(clusterToPersonID);
}
Future<void> updatePerson(PersonEntity updatePerson) async {
Future<void> updateAttributes(
String id, {
String? name,
String? avatarFaceId,
bool? isHidden,
int? version,
String? birthDate,
}) async {
final person = (await getPerson(id))!;
final updatedPerson = person.copyWith(
data: person.data.copyWith(
name: name,
avatarFaceId: avatarFaceId,
isHidden: isHidden,
version: version,
birthDate: birthDate,
),
);
await _updatePerson(updatedPerson);
}
Future<void> _updatePerson(PersonEntity updatePerson) async {
await entityService.addOrUpdate(
EntityType.person,
json.encode(updatePerson.data.toJson()),
id: updatePerson.remoteID,
);
updatePerson.data.logStats();
}
}

View file

@ -653,11 +653,10 @@ class _FileSelectionActionsWidgetState
Future<void> _setPersonCover() async {
final EnteFile file = widget.selectedFiles.files.first;
final PersonEntity newPerson = widget.person!.copyWith(
data: widget.person!.data
.copyWith(avatarFaceId: file.uploadedFileID.toString()),
await PersonService.instance.updateAttributes(
widget.person!.remoteID,
avatarFaceId: file.uploadedFileID.toString(),
);
await PersonService.instance.updatePerson(newPerson);
widget.selectedFiles.clearAll();
if (mounted) {
setState(() => {});

View file

@ -110,9 +110,8 @@ class _AppBarWidgetState extends State<PeopleAppBar> {
}
try {
final updatePerson = widget.person
.copyWith(data: widget.person.data.copyWith(name: text));
await PersonService.instance.updatePerson(updatePerson);
await PersonService.instance
.updateAttributes(widget.person.remoteID, name: text);
if (mounted) {
_appBarTitle = text;
setState(() {});