瀏覽代碼

Show cities search in location section

Neeraj Gupta 1 年之前
父節點
當前提交
3080dcf9f1

+ 1 - 0
lib/models/search/search_types.dart

@@ -25,6 +25,7 @@ enum ResultType {
   collection,
   collection,
   file,
   file,
   location,
   location,
+  locationSuggestion,
   month,
   month,
   year,
   year,
   fileType,
   fileType,

+ 23 - 0
lib/services/search_service.dart

@@ -708,6 +708,7 @@ class SearchService {
       final locationTagEntities =
       final locationTagEntities =
           (await LocationService.instance.getLocationTags());
           (await LocationService.instance.getLocationTags());
       final allFiles = await getAllFiles();
       final allFiles = await getAllFiles();
+      final List<EnteFile> filesWithNoLocTag = [];
 
 
       for (int i = 0; i < locationTagEntities.length; i++) {
       for (int i = 0; i < locationTagEntities.length; i++) {
         if (limit != null && i >= limit) break;
         if (limit != null && i >= limit) break;
@@ -716,15 +717,20 @@ class SearchService {
 
 
       for (EnteFile file in allFiles) {
       for (EnteFile file in allFiles) {
         if (file.hasLocation) {
         if (file.hasLocation) {
+          bool hasLocationTag = false;
           for (LocalEntity<LocationTag> tag in tagToItemsMap.keys) {
           for (LocalEntity<LocationTag> tag in tagToItemsMap.keys) {
             if (isFileInsideLocationTag(
             if (isFileInsideLocationTag(
               tag.item.centerPoint,
               tag.item.centerPoint,
               file.location!,
               file.location!,
               tag.item.radius,
               tag.item.radius,
             )) {
             )) {
+              hasLocationTag = true;
               tagToItemsMap[tag]!.add(file);
               tagToItemsMap[tag]!.add(file);
             }
             }
           }
           }
+          if (!hasLocationTag) {
+            filesWithNoLocTag.add(file);
+          }
         }
         }
       }
       }
 
 
@@ -753,6 +759,23 @@ class SearchService {
           );
           );
         }
         }
       }
       }
+      if (limit == null || tagSearchResults.length < limit) {
+        final results = await LocationService.instance
+            .getFilesInCity(filesWithNoLocTag, '');
+        final List<City> sortedByResultCount = results.keys.toList()
+          ..sort((a, b) => results[b]!.length.compareTo(results[a]!.length));
+        for (final city in sortedByResultCount) {
+          if (results[city]!.length <= 1) continue;
+          // If the location tag already exists for a city, don't add it again
+          tagSearchResults.add(
+            GenericSearchResult(
+              ResultType.locationSuggestion,
+              city.city,
+              results[city]!,
+            ),
+          );
+        }
+      }
       return tagSearchResults;
       return tagSearchResults;
     } catch (e) {
     } catch (e) {
       _logger.severe("Error in getAllLocationTags", e);
       _logger.severe("Error in getAllLocationTags", e);

+ 2 - 0
lib/ui/viewer/search/result/search_result_widget.dart

@@ -131,6 +131,8 @@ class SearchResultWidget extends StatelessWidget {
         return "Day";
         return "Day";
       case ResultType.location:
       case ResultType.location:
         return "Location";
         return "Location";
+      case ResultType.locationSuggestion:
+        return "Add Location";
       case ResultType.fileType:
       case ResultType.fileType:
         return "Type";
         return "Type";
       case ResultType.fileExtension:
       case ResultType.fileExtension:

+ 10 - 3
lib/ui/viewer/search/result/search_section_all_page.dart

@@ -3,6 +3,7 @@ import "dart:async";
 import "package:flutter/material.dart";
 import "package:flutter/material.dart";
 import "package:flutter_animate/flutter_animate.dart";
 import "package:flutter_animate/flutter_animate.dart";
 import "package:photos/events/event.dart";
 import "package:photos/events/event.dart";
+import "package:photos/extensions/list.dart";
 import "package:photos/models/search/album_search_result.dart";
 import "package:photos/models/search/album_search_result.dart";
 import "package:photos/models/search/generic_search_result.dart";
 import "package:photos/models/search/generic_search_result.dart";
 import "package:photos/models/search/recent_searches.dart";
 import "package:photos/models/search/recent_searches.dart";
@@ -83,8 +84,6 @@ class _SearchSectionAllPageState extends State<SearchSectionAllPage> {
                   builder: (context, snapshot) {
                   builder: (context, snapshot) {
                     if (snapshot.hasData) {
                     if (snapshot.hasData) {
                       final sectionResults = snapshot.data!;
                       final sectionResults = snapshot.data!;
-                      sectionResults
-                          .sort((a, b) => a.name().compareTo(b.name()));
                       return Text(sectionResults.length.toString())
                       return Text(sectionResults.length.toString())
                           .animate()
                           .animate()
                           .fadeIn(
                           .fadeIn(
@@ -109,7 +108,15 @@ class _SearchSectionAllPageState extends State<SearchSectionAllPage> {
                 future: sectionData,
                 future: sectionData,
                 builder: (context, snapshot) {
                 builder: (context, snapshot) {
                   if (snapshot.hasData) {
                   if (snapshot.hasData) {
-                    final sectionResults = snapshot.data!;
+                    List<SearchResult> sectionResults = snapshot.data!;
+                    sectionResults.sort((a, b) => a.name().compareTo(b.name()));
+                    if (widget.sectionType == SectionType.location) {
+                      final result = sectionResults.splitMatch(
+                        (e) => e.type() == ResultType.location,
+                      );
+                      sectionResults = result.matched;
+                      sectionResults.addAll(result.unmatched);
+                    }
                     return ListView.separated(
                     return ListView.separated(
                       itemBuilder: (context, index) {
                       itemBuilder: (context, index) {
                         if (sectionResults.length == index) {
                         if (sectionResults.length == index) {

+ 4 - 1
lib/ui/viewer/search/result/searchable_item.dart

@@ -77,7 +77,10 @@ class SearchableItemWidget extends StatelessWidget {
                         children: [
                         children: [
                           Text(
                           Text(
                             searchResult.name(),
                             searchResult.name(),
-                            style: textTheme.body,
+                            style: searchResult.type() ==
+                                    ResultType.locationSuggestion
+                                ? textTheme.bodyFaint
+                                : textTheme.body,
                             overflow: TextOverflow.ellipsis,
                             overflow: TextOverflow.ellipsis,
                           ),
                           ),
                           const SizedBox(
                           const SizedBox(