瀏覽代碼

[mob][photos] Create new cluster when tapping unassigned face

laurenspriem 1 年之前
父節點
當前提交
811ffe0117
共有 2 個文件被更改,包括 100 次插入2 次删除
  1. 12 0
      mobile/lib/face/db.dart
  2. 88 2
      mobile/lib/ui/viewer/file_details/face_widget.dart

+ 12 - 0
mobile/lib/face/db.dart

@@ -348,6 +348,18 @@ class FaceMLDataDB {
     return result;
   }
 
+  Future<int?> getClusterIDForFaceID(String faceID) async {
+    final db = await instance.sqliteAsyncDB;
+    final List<Map<String, dynamic>> maps = await db.getAll(
+      'SELECT $fcClusterID FROM $faceClustersTable WHERE $fcFaceId = ?',
+      [faceID],
+    );
+    if (maps.isEmpty) {
+      return null;
+    }
+    return maps.first[fcClusterID] as int;
+  }
+
   Future<Map<int, Iterable<String>>> getAllClusterIdToFaceIDs() async {
     final db = await instance.sqliteAsyncDB;
     final Map<int, List<String>> result = {};

+ 88 - 2
mobile/lib/ui/viewer/file_details/face_widget.dart

@@ -4,6 +4,7 @@ import "dart:typed_data";
 import "package:flutter/cupertino.dart";
 import "package:flutter/foundation.dart" show kDebugMode;
 import "package:flutter/material.dart";
+import "package:photos/extensions/stop_watch.dart";
 import "package:photos/face/db.dart";
 import "package:photos/face/model/face.dart";
 import "package:photos/face/model/person.dart";
@@ -65,7 +66,50 @@ class _FaceWidgetState extends State<FaceWidget> {
                   name: "FaceWidget",
                 );
                 if (widget.person == null && widget.clusterID == null) {
-                  return;
+                  // Get faceID and double check that it doesn't belong to an existing clusterID. If it does, push that cluster page
+                  final w = (kDebugMode ? EnteWatch('FaceWidget') : null)
+                    ?..start();
+                  final existingClusterID = await FaceMLDataDB.instance
+                      .getClusterIDForFaceID(widget.face.faceID);
+                  w?.log('getting existing clusterID for faceID');
+                  if (existingClusterID != null) {
+                    final fileIdsToClusterIds =
+                        await FaceMLDataDB.instance.getFileIdToClusterIds();
+                    final files = await SearchService.instance.getAllFiles();
+                    final clusterFiles = files
+                        .where(
+                          (file) =>
+                              fileIdsToClusterIds[file.uploadedFileID]
+                                  ?.contains(existingClusterID) ??
+                              false,
+                        )
+                        .toList();
+                    await Navigator.of(context).push(
+                      MaterialPageRoute(
+                        builder: (context) => ClusterPage(
+                          clusterFiles,
+                          clusterID: existingClusterID,
+                        ),
+                      ),
+                    );
+                  }
+
+                  // Create new clusterID for the faceID and update DB to assign the faceID to the new clusterID
+                  final int newClusterID =
+                      DateTime.now().microsecondsSinceEpoch;
+                  await FaceMLDataDB.instance.updateFaceIdToClusterId(
+                    {widget.face.faceID: newClusterID},
+                  );
+
+                  // Push page for the new cluster
+                  await Navigator.of(context).push(
+                    MaterialPageRoute(
+                      builder: (context) => ClusterPage(
+                        [widget.file],
+                        clusterID: newClusterID,
+                      ),
+                    ),
+                  );
                 }
                 if (widget.person != null) {
                   await Navigator.of(context).push(
@@ -230,7 +274,49 @@ class _FaceWidgetState extends State<FaceWidget> {
                 name: "FaceWidget",
               );
               if (widget.person == null && widget.clusterID == null) {
-                return;
+                // Get faceID and double check that it doesn't belong to an existing clusterID. If it does, push that cluster page
+                final w = (kDebugMode ? EnteWatch('FaceWidget') : null)
+                  ?..start();
+                final existingClusterID = await FaceMLDataDB.instance
+                    .getClusterIDForFaceID(widget.face.faceID);
+                w?.log('getting existing clusterID for faceID');
+                if (existingClusterID != null) {
+                  final fileIdsToClusterIds =
+                      await FaceMLDataDB.instance.getFileIdToClusterIds();
+                  final files = await SearchService.instance.getAllFiles();
+                  final clusterFiles = files
+                      .where(
+                        (file) =>
+                            fileIdsToClusterIds[file.uploadedFileID]
+                                ?.contains(existingClusterID) ??
+                            false,
+                      )
+                      .toList();
+                  await Navigator.of(context).push(
+                    MaterialPageRoute(
+                      builder: (context) => ClusterPage(
+                        clusterFiles,
+                        clusterID: existingClusterID,
+                      ),
+                    ),
+                  );
+                }
+
+                // Create new clusterID for the faceID and update DB to assign the faceID to the new clusterID
+                final int newClusterID = DateTime.now().microsecondsSinceEpoch;
+                await FaceMLDataDB.instance.updateFaceIdToClusterId(
+                  {widget.face.faceID: newClusterID},
+                );
+
+                // Push page for the new cluster
+                await Navigator.of(context).push(
+                  MaterialPageRoute(
+                    builder: (context) => ClusterPage(
+                      [widget.file],
+                      clusterID: newClusterID,
+                    ),
+                  ),
+                );
               }
               if (widget.person != null) {
                 await Navigator.of(context).push(