π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Custom people sort in people list
laurenspriemc3e7d3cb6b6a
diff --git a/mobile/apps/photos/lib/events/people_sort_order_change_event.dart b/mobile/apps/photos/lib/events/people_sort_order_change_event.dartnew file mode 100644index 0000000000..ca1ae42810--- /dev/null+++ b/mobile/apps/photos/lib/events/people_sort_order_change_event.dart@@ -0,0 +1,3 @@+import "package:photos/events/event.dart";++class PeopleSortOrderChangeEvent extends Event {}diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arbindex 87fe59fddc..11f58623a1 100644--- a/mobile/apps/photos/lib/l10n/intl_en.arb+++ b/mobile/apps/photos/lib/l10n/intl_en.arb@@ -778,6 +778,7 @@"name": "Name","newest": "Newest","lastUpdated": "Last updated",+ "updated": "Updated","deleteEmptyAlbums": "Delete empty albums","deleteEmptyAlbumsWithQuestionMark": "Delete empty albums?","deleteAlbumsDialogBody": "This will delete all empty albums. This is useful when you want to reduce the clutter in your album list.",diff --git a/mobile/apps/photos/lib/services/search_service.dart b/mobile/apps/photos/lib/services/search_service.dartindex b49e42567c..849927ec89 100644--- a/mobile/apps/photos/lib/services/search_service.dart+++ b/mobile/apps/photos/lib/services/search_service.dart@@ -58,6 +58,7 @@ import "package:photos/ui/viewer/search/result/magic_result_screen.dart";import "package:photos/utils/cache_util.dart";import "package:photos/utils/file_util.dart";import "package:photos/utils/navigation_util.dart";+import "package:photos/utils/people_sort_util.dart";import 'package:photos/utils/standalone/date_time.dart';class SearchService {@@ -957,9 +958,6 @@ class SearchService {);for (final clusterId in sortedClusterIds) {- if (limit != null && facesResult.length >= limit) {- break;- }final files = clusterIdToFiles[clusterId]!;final String clusterName = clusterId;@@ -1019,6 +1017,14 @@ class SearchService {);}if (facesResult.isEmpty) return [];+ sortPeopleFaces(+ facesResult,+ PeopleSortConfig(+ sortKey: localSettings.peopleSortKey(),+ nameSortAscending: localSettings.peopleNameSortAscending,+ updatedSortAscending: localSettings.peopleUpdatedSortAscending,+ ),+ );if (limit != null) {return facesResult.sublist(0, min(limit, facesResult.length));} else {diff --git a/mobile/apps/photos/lib/states/all_sections_examples_state.dart b/mobile/apps/photos/lib/states/all_sections_examples_state.dartindex a652dcdcfb..982542e474 100644--- a/mobile/apps/photos/lib/states/all_sections_examples_state.dart+++ b/mobile/apps/photos/lib/states/all_sections_examples_state.dart@@ -7,6 +7,7 @@ import "package:photos/core/constants.dart";import "package:photos/core/event_bus.dart";import "package:photos/events/files_updated_event.dart";import "package:photos/events/people_changed_event.dart";+import "package:photos/events/people_sort_order_change_event.dart";import "package:photos/events/tab_changed_event.dart";import "package:photos/models/search/search_result.dart";import "package:photos/models/search/search_types.dart";@@ -33,6 +34,7 @@ class _AllSectionsExamplesProviderStatelate StreamSubscription<FilesUpdatedEvent> _filesUpdatedEvent;late StreamSubscription<PeopleChangedEvent> _onPeopleChangedEvent;+ late StreamSubscription<PeopleSortOrderChangeEvent> _peopleSortChangedEvent;late StreamSubscription<TabChangedEvent> _tabChangeEvent;bool hasPendingUpdate = false;bool isOnSearchTab = false;@@ -56,6 +58,10 @@ class _AllSectionsExamplesProviderStateBus.instance.on<PeopleChangedEvent>().listen((event) {onDataUpdate();});+ _peopleSortChangedEvent =+ Bus.instance.on<PeopleSortOrderChangeEvent>().listen((event) {+ onDataUpdate();+ });_tabChangeEvent = Bus.instance.on<TabChangedEvent>().listen((event) {if (event.source == TabChangedEventSource.pageView &&event.selectedIndex == 3) {@@ -123,6 +129,7 @@ class _AllSectionsExamplesProviderStatevoid dispose() {_onPeopleChangedEvent.cancel();_filesUpdatedEvent.cancel();+ _peopleSortChangedEvent.cancel();_tabChangeEvent.cancel();_debouncer.cancelDebounceTimer();super.dispose();diff --git a/mobile/apps/photos/lib/ui/viewer/search/result/people_section_all_page.dart b/mobile/apps/photos/lib/ui/viewer/search/result/people_section_all_page.dartindex 3492868bcc..e6d956df96 100644--- a/mobile/apps/photos/lib/ui/viewer/search/result/people_section_all_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/search/result/people_section_all_page.dart@@ -1,8 +1,10 @@import "dart:async";import 'package:flutter/material.dart';+import "package:photos/core/event_bus.dart";import "package:photos/events/event.dart";import "package:photos/events/people_changed_event.dart";+import "package:photos/events/people_sort_order_change_event.dart";import "package:photos/generated/l10n.dart";import "package:photos/models/file/file.dart";import "package:photos/models/ml/face/person.dart";@@ -12,11 +14,15 @@ import "package:photos/models/search/search_constants.dart";import "package:photos/models/search/search_result.dart";import "package:photos/models/search/search_types.dart";import "package:photos/models/selected_people.dart";+import "package:photos/service_locator.dart";import "package:photos/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart";import "package:photos/services/search_service.dart";+import "package:photos/theme/colors.dart";import "package:photos/theme/ente_theme.dart";+import "package:photos/theme/text_style.dart";import "package:photos/ui/common/loading_widget.dart";import "package:photos/ui/components/bottom_action_bar/people_bottom_action_bar_widget.dart";+import "package:photos/ui/components/buttons/icon_button_widget.dart";import "package:photos/ui/components/searchable_appbar.dart";import "package:photos/ui/viewer/file/no_thumbnail_widget.dart";import "package:photos/ui/viewer/file/thumbnail_widget.dart";@@ -28,7 +34,9 @@ import "package:photos/ui/viewer/people/person_gallery_suggestion.dart";import "package:photos/ui/viewer/people/pinned_person_badge.dart";import "package:photos/ui/viewer/search/result/search_result_page.dart";import "package:photos/ui/viewer/search_tab/people_section.dart";+import "package:photos/utils/local_settings.dart";import "package:photos/utils/navigation_util.dart";+import "package:photos/utils/people_sort_util.dart";class PeopleSectionAllPage extends StatefulWidget {const PeopleSectionAllPage({@@ -364,6 +372,12 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {bool userDismissedPersonGallerySuggestion = false;String _searchQuery = "";int _suggestionReloadToken = 0;+ late PeopleSortKey _sortKey;+ bool _nameSortAscending = true;+ bool _updatedSortAscending = false;++ static const double _sortMenuItemHeight = 52;+ static const double _sortMenuCornerRadius = 12;bool get _isSearching => _searchQuery.trim().isNotEmpty;@@ -406,6 +420,10 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {@overridevoid initState() {super.initState();+ final settings = localSettings;+ _sortKey = settings.peopleSortKey();+ _nameSortAscending = settings.peopleNameSortAscending;+ _updatedSortAscending = settings.peopleUpdatedSortAscending;sectionData = getResults(init: true);final streamsToListenTo = SectionType.face.viewAllUpdateEvents();@@ -455,6 +473,10 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {normalFaces = extraFaces;extraFaces = [];}+ if (widget.showSearchBar) {+ _sortFaces(normalFaces);+ _sortFaces(extraFaces);+ }final results =_showingAllFaces ? [...normalFaces, ...extraFaces] : normalFaces;@@ -486,6 +508,52 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {return results;}+ bool _isSortAscending(PeopleSortKey key) {+ switch (key) {+ case PeopleSortKey.name:+ return _nameSortAscending;+ case PeopleSortKey.lastUpdated:+ return _updatedSortAscending;+ case PeopleSortKey.mostPhotos:+ return false;+ }+ }++ bool _toggleSortDirection(PeopleSortKey key) {+ switch (key) {+ case PeopleSortKey.name:+ _nameSortAscending = !_nameSortAscending;+ return true;+ case PeopleSortKey.lastUpdated:+ _updatedSortAscending = !_updatedSortAscending;+ return true;+ case PeopleSortKey.mostPhotos:+ return false;+ }+ }++ bool _canToggleSortDirection(PeopleSortKey key) {+ return key == PeopleSortKey.name || key == PeopleSortKey.lastUpdated;+ }++ void _sortFaces(List<GenericSearchResult> faces) {+ sortPeopleFaces(+ faces,+ PeopleSortConfig(+ sortKey: _sortKey,+ nameSortAscending: _nameSortAscending,+ updatedSortAscending: _updatedSortAscending,+ ),+ );+ }++ Future<void> _persistSortPreferences() async {+ await localSettings.setPeopleSortKey(_sortKey);+ await localSettings.setPeopleNameSortAscending(_nameSortAscending);+ await localSettings.setPeopleUpdatedSortAscending(_updatedSortAscending);+ Bus.instance.fire(PeopleSortOrderChangeEvent());+ }+@overridevoid dispose() {for (var subscriptions in streamSubscriptions) {@@ -496,7 +564,9 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {@overrideWidget build(BuildContext context) {- final smallFontSize = getEnteTextTheme(context).small.fontSize!;+ final textTheme = getEnteTextTheme(context);+ final colorScheme = getEnteColorScheme(context);+ final smallFontSize = textTheme.small.fontSize!;final textScaleFactor =MediaQuery.textScalerOf(context).scale(smallFontSize) / smallFontSize;const horizontalEdgePadding = 20.0;@@ -515,6 +585,12 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {centerTitle: false,searchIconPadding:const EdgeInsets.fromLTRB(12, 12, horizontalEdgePadding, 12),+ actions: [+ Padding(+ padding: const EdgeInsets.only(right: horizontalEdgePadding),+ child: _buildSortMenu(context, textTheme, colorScheme),+ ),+ ],),];if (!_isLoaded &&@@ -726,6 +802,168 @@ class _PeopleSectionAllWidgetState extends State<PeopleSectionAllWidget> {);}+ Widget _buildSortMenu(+ BuildContext context,+ EnteTextTheme textTheme,+ EnteColorScheme colorScheme,+ ) {+ return Theme(+ data: Theme.of(context).copyWith(+ highlightColor: Colors.transparent,+ splashColor: Colors.transparent,+ ),+ child: GestureDetector(+ onTapDown: (TapDownDetails details) async {+ final l10n = AppLocalizations.of(context);+ const sortKeys = PeopleSortKey.values;+ final PeopleSortKey? selectedKey = await showMenu<PeopleSortKey>(+ color: colorScheme.backgroundElevated,+ context: context,+ elevation: 0,+ shape: RoundedRectangleBorder(+ side: BorderSide(+ width: 0.5,+ color: colorScheme.strokeFaint,+ ),+ borderRadius: BorderRadius.circular(_sortMenuCornerRadius),+ ),+ position: RelativeRect.fromLTRB(+ details.globalPosition.dx,+ details.globalPosition.dy,+ details.globalPosition.dx,+ details.globalPosition.dy + 50,+ ),+ items: List.generate(sortKeys.length, (index) {+ final key = sortKeys[index];+ return _buildSortMenuItem(+ key,+ index == sortKeys.length - 1,+ textTheme,+ colorScheme,+ l10n,+ );+ }),+ );+ if (!mounted || selectedKey == null) {+ return;+ }+ if (selectedKey == _sortKey &&+ !_canToggleSortDirection(selectedKey)) {+ return;+ }+ setState(() {+ if (selectedKey == _sortKey) {+ _toggleSortDirection(selectedKey);+ } else {+ _sortKey = selectedKey;+ }+ _sortFaces(normalFaces);+ _sortFaces(extraFaces);+ });+ unawaited(_persistSortPreferences());+ },+ child: IconButtonWidget(+ icon: Icons.sort_rounded,+ iconButtonType: IconButtonType.secondary,+ iconColor: colorScheme.textMuted,+ ),+ ),+ );+ }++ PopupMenuItem<PeopleSortKey> _buildSortMenuItem(+ PeopleSortKey key,+ bool isLast,+ EnteTextTheme textTheme,+ EnteColorScheme colorScheme,+ AppLocalizations l10n,+ ) {+ String label;+ switch (key) {+ case PeopleSortKey.mostPhotos:+ label = l10n.photos;+ break;+ case PeopleSortKey.name:+ label = l10n.name;+ break;+ case PeopleSortKey.lastUpdated:+ label = l10n.updated;+ break;+ }++ String detail;+ switch (key) {+ case PeopleSortKey.mostPhotos:+ detail = l10n.count;+ break;+ case PeopleSortKey.name:+ detail = _isSortAscending(key) ? "A-Z" : "Z-A";+ break;+ case PeopleSortKey.lastUpdated:+ detail =+ _isSortAscending(key) ? l10n.sortOldestFirst : l10n.sortNewestFirst;+ break;+ }++ final bool isSelected = _sortKey == key;+ final bool isAscending = _isSortAscending(key);+ final IconData directionIcon = key == PeopleSortKey.name+ ? (isAscending ? Icons.arrow_downward : Icons.arrow_upward)+ : (isAscending ? Icons.arrow_upward : Icons.arrow_downward);++ return PopupMenuItem<PeopleSortKey>(+ value: key,+ padding: EdgeInsets.zero,+ height: _sortMenuItemHeight,+ child: Container(+ width: double.infinity,+ height: _sortMenuItemHeight,+ padding: const EdgeInsets.symmetric(horizontal: 16),+ decoration: BoxDecoration(+ border: isLast+ ? null+ : Border(+ bottom: BorderSide(+ width: 0.5,+ color: colorScheme.strokeFaint,+ ),+ ),+ ),+ child: Row(+ mainAxisAlignment: MainAxisAlignment.start,+ children: [+ Text(+ label,+ style: textTheme.mini,+ ),+ if (isSelected) ...[+ const SizedBox(width: 8),+ Container(+ width: 4,+ height: 4,+ decoration: BoxDecoration(+ color: colorScheme.textMuted.withValues(alpha: 0.6),+ shape: BoxShape.circle,+ ),+ ),+ const SizedBox(width: 6),+ Text(+ detail,+ style: textTheme.miniMuted,+ ),+ const SizedBox(width: 4),+ Icon(+ directionIcon,+ size: 16,+ color: colorScheme.textMuted,+ ),+ ],+ ],+ ),+ ),+ );+ }+Widget _buildShowMoreOrLessButton(BuildContext context) {return Container(padding: const EdgeInsets.fromLTRB(20, 8, 20, 24),diff --git a/mobile/apps/photos/lib/utils/local_settings.dart b/mobile/apps/photos/lib/utils/local_settings.dartindex 1114d5de76..3d07add0f5 100644--- a/mobile/apps/photos/lib/utils/local_settings.dart+++ b/mobile/apps/photos/lib/utils/local_settings.dart@@ -22,6 +22,12 @@ enum AlbumViewType {list,}+enum PeopleSortKey {+ mostPhotos,+ name,+ lastUpdated,+}+class LocalSettings {static const kCollectionSortPref = "collection_sort_pref";static const kGalleryGroupType = "gallery_group_type";@@ -45,6 +51,9 @@ class LocalSettings {static const _kSwipeToSelectEnabled = "ls.swipe_to_select_enabled";static const kCollectionViewType = "collection_view_type";static const kCollectionSortDirection = "collection_sort_direction";+ static const kPeopleSortKey = "people_sort_key";+ static const kPeopleSortNameAscending = "people_sort_name_ascending";+ static const kPeopleSortUpdatedAscending = "people_sort_updated_ascending";static const kShowLocalIDOverThumbnails = "show_local_id_over_thumbnails";static const kEnableDatabaseLogging = "enable_db_logging";static const _kInternalUserDisabled = "ls.internal_user_disabled";@@ -83,6 +92,32 @@ class LocalSettings {return _prefs.setInt(kCollectionSortDirection, direction.index);}+ PeopleSortKey peopleSortKey() {+ final index = _prefs.getInt(kPeopleSortKey);+ if (index == null || index < 0 || index >= PeopleSortKey.values.length) {+ return PeopleSortKey.mostPhotos;+ }+ return PeopleSortKey.values[index];+ }++ Future<bool> setPeopleSortKey(PeopleSortKey key) {+ return _prefs.setInt(kPeopleSortKey, key.index);+ }++ bool get peopleNameSortAscending =>+ _prefs.getBool(kPeopleSortNameAscending) ?? true;++ Future<void> setPeopleNameSortAscending(bool value) async {+ await _prefs.setBool(kPeopleSortNameAscending, value);+ }++ bool get peopleUpdatedSortAscending =>+ _prefs.getBool(kPeopleSortUpdatedAscending) ?? false;++ Future<void> setPeopleUpdatedSortAscending(bool value) async {+ await _prefs.setBool(kPeopleSortUpdatedAscending, value);+ }+GroupType getGalleryGroupType() {final groupTypeString = _prefs.getString(kGalleryGroupType);if (groupTypeString != null) {diff --git a/mobile/apps/photos/lib/utils/people_sort_util.dart b/mobile/apps/photos/lib/utils/people_sort_util.dartnew file mode 100644index 0000000000..7e3c46704c--- /dev/null+++ b/mobile/apps/photos/lib/utils/people_sort_util.dart@@ -0,0 +1,91 @@+import "package:collection/collection.dart";+import "package:photos/models/search/generic_search_result.dart";+import "package:photos/models/search/search_constants.dart";+import "package:photos/utils/local_settings.dart";++class PeopleSortConfig {+ final PeopleSortKey sortKey;+ final bool nameSortAscending;+ final bool updatedSortAscending;++ const PeopleSortConfig({+ required this.sortKey,+ required this.nameSortAscending,+ required this.updatedSortAscending,+ });+}++void sortPeopleFaces(+ List<GenericSearchResult> faces,+ PeopleSortConfig config,+) {+ if (faces.isEmpty) {+ return;+ }++ final latestTimes = <GenericSearchResult, int>{};+ if (config.sortKey == PeopleSortKey.lastUpdated) {+ for (final face in faces) {+ latestTimes[face] = _latestAssignedTime(face);+ }+ }++ faces.sort((a, b) {+ final aHasPerson = _hasPersonId(a);+ final bHasPerson = _hasPersonId(b);+ if (aHasPerson != bHasPerson) {+ return aHasPerson ? -1 : 1;+ }++ final bool aPinned = a.params[kPersonPinned] as bool? ?? false;+ final bool bPinned = b.params[kPersonPinned] as bool? ?? false;+ if (aPinned != bPinned) {+ return aPinned ? -1 : 1;+ }++ int compareValue;+ switch (config.sortKey) {+ case PeopleSortKey.mostPhotos:+ compareValue = b.fileCount().compareTo(a.fileCount());+ break;+ case PeopleSortKey.name:+ compareValue = compareAsciiLowerCaseNatural(a.name(), b.name());+ if (!config.nameSortAscending) {+ compareValue = -compareValue;+ }+ break;+ case PeopleSortKey.lastUpdated:+ final aTime = latestTimes[a] ?? 0;+ final bTime = latestTimes[b] ?? 0;+ compareValue = aTime.compareTo(bTime);+ if (!config.updatedSortAscending) {+ compareValue = -compareValue;+ }+ break;+ }++ if (compareValue != 0) {+ return compareValue;+ }+ return compareAsciiLowerCaseNatural(a.name(), b.name());+ });+}++bool _hasPersonId(GenericSearchResult face) {+ final personId = face.params[kPersonParamID] as String?;+ return personId != null && personId.isNotEmpty;+}++int _latestAssignedTime(GenericSearchResult face) {+ var latestTime = 0;+ for (final file in face.resultFiles()) {+ int creationTime = 0;+ if (file.creationTime != null && file.creationTime! > 0) {+ creationTime = file.creationTime!;+ }+ if (creationTime > latestTime) {+ latestTime = creationTime;+ }+ }+ return latestTime;+}