Skip to content

armgit / ente

Public Imported from GitHub
0

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

Merge branch 'main' into contacts_changes

Neeraj Gupta06c8d22dbc20
diff --cc mobile/apps/photos/lib/l10n/intl_en.arb
index 1922b9756d,51844a0026..c9cbbcedec
--- a/mobile/apps/photos/lib/l10n/intl_en.arb
+++ b/mobile/apps/photos/lib/l10n/intl_en.arb
@@@ -1882,6 -1868,6 +1868,8 @@@
"editPerson": "Edit person",
"enterDateOfBirthHint": "Enter date of birth",
"setAContactPhoto": "Set a contact photo",
++ "setSelectedPhoto": "Set selected photo",
++ "removeContactPhoto": "Remove contact photo",
"mergedPhotos": "Merged photos",
"orMergeWithExistingPerson": "Or merge with existing",
"enterDateOfBirth": "Birthday (optional)",
diff --cc mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dart
index 01f26d304b,01f26d304b..90f9f19d7e
--- a/mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dart
+++ b/mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dart
@@@ -18,9 -18,9 +18,13 @@@ String resolveDisplayName(User user)
}
String? resolveKnownEmail(User user) {
-- if (flagService.enableContact && user.id != null && user.id! > 0) {
++ if (flagService.enableContact) {
++ final contactUserId = _validContactUserId(user);
final savedEmail = _knownEmailOrNull(
-- PhotosContactsService.instance.getCachedResolvedEmailByUserId(user.id),
++ PhotosContactsService.instance.getCachedResolvedEmail(
++ contactUserId: contactUserId,
++ email: contactUserId == null ? user.email : null,
++ ),
);
if (savedEmail != null) {
return savedEmail;
@@@ -42,12 -42,12 +46,16 @@@ bool matchesResolvedContactQuery(User u
}
String? _savedContactName(User user) {
-- if (!flagService.enableContact || user.id == null || user.id! <= 0) {
++ if (!flagService.enableContact) {
return null;
}
++ final contactUserId = _validContactUserId(user);
final savedName = PhotosContactsService.instance
-- .getCachedSavedNameByUserId(user.id)
++ .getCachedSavedName(
++ contactUserId: contactUserId,
++ email: contactUserId == null ? user.email : null,
++ )
?.trim();
if (savedName == null || savedName.isEmpty) {
return null;
@@@ -55,6 -55,6 +63,11 @@@
return savedName;
}
++int? _validContactUserId(User user) {
++ final userId = user.id;
++ return userId != null && userId > 0 ? userId : null;
++}
++
String? _knownEmailOrNull(String? email) {
if (email == null) {
return null;
diff --cc mobile/apps/photos/lib/services/photos_contacts_service.dart
index 2cfb0f4633,b9424c2ea6..a91808bc2e
--- a/mobile/apps/photos/lib/services/photos_contacts_service.dart
+++ b/mobile/apps/photos/lib/services/photos_contacts_service.dart
@@@ -137,25 -116,22 +137,50 @@@ class PhotosContactsService
}
contacts.ContactRecord? getCachedContactByUserId(int? contactUserId) {
- if (contactUserId == null || _sessionKey == null) {
- return null;
- }
- final contact = _contactsByUserId[contactUserId];
- if (contact == null || contact.isDeleted) {
- return null;
- }
- return contact;
+ return _getCachedContact(contactUserId: contactUserId);
+ }
+
++ contacts.ContactRecord? getCachedContactByEmail(String? email) {
++ return _getCachedContact(email: email);
++ }
++
++ contacts.ContactRecord? getCachedContact({
++ int? contactUserId,
++ String? email,
++ }) {
++ return _getCachedContact(contactUserId: contactUserId, email: email);
+ }
+
String? getCachedSavedNameByUserId(int? contactUserId) {
- final savedName = getCachedContactByUserId(contactUserId)?.data?.name;
- return getCachedContactByUserId(contactUserId)?.data?.name;
++ return getCachedSavedName(contactUserId: contactUserId);
++ }
++
++ String? getCachedSavedName({int? contactUserId, String? email}) {
++ final savedName = getCachedContact(
++ contactUserId: contactUserId,
++ email: email,
++ )?.data?.name;
+ if (savedName == null) {
+ return null;
+ }
+ final trimmed = savedName.trim();
+ return trimmed.isEmpty ? null : trimmed;
}
String? getCachedResolvedEmailByUserId(int? contactUserId) {
- final resolved = getCachedContactByUserId(contactUserId)?.email;
- return getCachedContactByUserId(contactUserId)?.email;
++ return getCachedResolvedEmail(contactUserId: contactUserId);
++ }
++
++ String? getCachedResolvedEmail({int? contactUserId, String? email}) {
++ final resolved = getCachedContact(
++ contactUserId: contactUserId,
++ email: email,
++ )?.email;
+ if (resolved == null) {
+ return null;
+ }
+ final trimmed = resolved.trim();
+ return trimmed.isEmpty ? null : trimmed;
}
Uint8List? getCachedProfilePictureBytesByUserId(int? contactUserId) {
@@@ -278,10 -242,10 +303,22 @@@
_cacheContact(contact);
_profilePictureBytesByUserId[contact.contactUserId] = bytes;
_resolvedProfilePictureUserIds.add(contact.contactUserId);
+ _notifyChanged(contact);
+ return contact;
+ }
+
++ Future<contacts.ContactRecord> deleteProfilePicture({
++ required String contactId,
++ }) async {
++ final contactsService = await _ensureReadyForWrite();
++ final contact = await contactsService.deleteProfilePicture(contactId);
++ _cacheContact(contact);
++ _profilePictureBytesByUserId.remove(contact.contactUserId);
++ _resolvedProfilePictureUserIds.add(contact.contactUserId);
+ _notifyChanged(contact);
+ return contact;
+ }
+
@visibleForTesting
Future<void> debugOpenAndSync(contacts.ContactsSession session) async {
final sessionKey = _buildSessionKey(session);
diff --cc mobile/apps/photos/lib/services/search_service.dart
index a4243afb1c,20065c62bf..e07a0c56c5
--- a/mobile/apps/photos/lib/services/search_service.dart
+++ b/mobile/apps/photos/lib/services/search_service.dart
@@@ -108,9 -108,9 +108,14 @@@ class SearchService
User user,
List<Collection> collections,
) {
++ final contactUserId = user.id != null && user.id! > 0 ? user.id : null;
++ final cachedContact = PhotosContactsService.instance.getCachedContact(
++ contactUserId: contactUserId,
++ email: contactUserId == null ? user.email : null,
++ );
final params = <String, dynamic>{
kPersonParamID: user.linkedPersonID,
-- kContactUserId: user.id,
++ kContactUserId: contactUserId ?? cachedContact?.contactUserId,
kContactEmail: resolveKnownEmail(user) ?? user.email,
kContactCollections: collections,
};
diff --cc mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart
index 59f7c1e0eb,59f7c1e0eb..2810e4001b
--- a/mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart
@@@ -1,119 -1,119 +1,161 @@@
++import "dart:math" as math;
++
++import "package:ente_components/ente_components.dart";
import "package:flutter/material.dart";
--import "package:modal_bottom_sheet/modal_bottom_sheet.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/file_load_result.dart";
import "package:photos/models/selected_files.dart";
import "package:photos/services/search_service.dart";
import "package:photos/theme/colors.dart";
--import "package:photos/theme/ente_theme.dart";
--import "package:photos/ui/components/bottom_of_title_bar_widget.dart";
--import "package:photos/ui/components/buttons/button_widget.dart";
--import "package:photos/ui/components/models/button_type.dart";
--import "package:photos/ui/components/title_bar_title_widget.dart";
import "package:photos/ui/viewer/gallery/gallery.dart";
import "package:photos/ui/viewer/gallery/state/gallery_files_inherited_widget.dart";
--Future<EnteFile?> showContactPhotoPickerSheet(BuildContext context) {
-- return showBarModalBottomSheet<EnteFile>(
++sealed class ContactPhotoPickerResult {
++ const ContactPhotoPickerResult();
++}
++
++class ContactPhotoPickerFile extends ContactPhotoPickerResult {
++ const ContactPhotoPickerFile(this.file);
++
++ final EnteFile file;
++}
++
++class ContactPhotoPickerRemove extends ContactPhotoPickerResult {
++ const ContactPhotoPickerRemove();
++}
++
++Future<ContactPhotoPickerResult?> showContactPhotoPickerSheet(
++ BuildContext context, {
++ required bool canRemovePhoto,
++}) {
++ return showBottomSheetComponent<ContactPhotoPickerResult>(
context: context,
-- builder: (context) => const _ContactPhotoPickerSheet(),
-- shape: const RoundedRectangleBorder(
-- side: BorderSide(width: 0),
-- borderRadius: BorderRadius.vertical(top: Radius.circular(5)),
-- ),
-- topControl: const SizedBox.shrink(),
-- backgroundColor: getEnteColorScheme(context).backgroundElevated,
barrierColor: backdropFaintDark,
enableDrag: true,
++ builder: (context) =>
++ _ContactPhotoPickerSheet(canRemovePhoto: canRemovePhoto),
);
}
--class _ContactPhotoPickerSheet extends StatelessWidget {
-- const _ContactPhotoPickerSheet();
++class _ContactPhotoPickerSheet extends StatefulWidget {
++ const _ContactPhotoPickerSheet({required this.canRemovePhoto});
++
++ final bool canRemovePhoto;
++
++ @override
++ State<_ContactPhotoPickerSheet> createState() =>
++ _ContactPhotoPickerSheetState();
++}
++
++class _ContactPhotoPickerSheetState extends State<_ContactPhotoPickerSheet> {
++ late final ValueNotifier<bool> _isFileSelected;
++ late final SelectedFiles _selectedFiles;
++
++ @override
++ void initState() {
++ super.initState();
++ _isFileSelected = ValueNotifier(false);
++ _selectedFiles = SelectedFiles()..addListener(_handleSelectionChanged);
++ }
++
++ @override
++ void dispose() {
++ _selectedFiles
++ ..removeListener(_handleSelectionChanged)
++ ..dispose();
++ _isFileSelected.dispose();
++ super.dispose();
++ }
++
++ void _handleSelectionChanged() {
++ _isFileSelected.value = _selectedFiles.files.isNotEmpty;
++ }
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
-- final isFileSelected = ValueNotifier(false);
-- final selectedFiles = SelectedFiles();
-- selectedFiles.addListener(() {
-- isFileSelected.value = selectedFiles.files.isNotEmpty;
-- });
++ final colors = context.componentColors;
++ final screenHeight = MediaQuery.sizeOf(context).height;
++ final sheetHeight = math.min(screenHeight * 0.78, screenHeight - 80);
-- return Padding(
-- padding: const EdgeInsets.fromLTRB(0, 32, 0, 8),
-- child: SizedBox(
-- width: double.infinity,
++ return Container(
++ height: sheetHeight,
++ clipBehavior: Clip.antiAlias,
++ decoration: BoxDecoration(
++ color: colors.backgroundBase,
++ borderRadius: const BorderRadius.vertical(
++ top: Radius.circular(Radii.bottomSheet),
++ ),
++ border: Border.all(color: colors.strokeDark),
++ ),
++ child: SafeArea(
++ top: false,
child: Column(
-- mainAxisSize: MainAxisSize.max,
children: [
++ _ContactPhotoSheetHeader(title: l10n.setAContactPhoto),
Expanded(
-- child: Column(
-- children: [
-- BottomOfTitleBarWidget(
-- title: TitleBarTitleWidget(title: l10n.setAContactPhoto),
-- showCloseButton: true,
-- ),
-- Expanded(
-- child: GalleryFilesState(
-- child: Gallery(
-- asyncLoader:
-- (
-- creationStartTime,
-- creationEndTime, {
-- limit,
-- asc,
-- }) async {
-- final files = await SearchService.instance
-- .getAllFilesForContactPhotoPicker();
-- return FileLoadResult(files, false);
-- },
-- tagPrefix: "pick_contact_photo_gallery",
-- selectedFiles: selectedFiles,
-- limitSelectionToOne: true,
-- showSelectAll: false,
-- disablePinnedGroupHeader: true,
-- disableVerticalPaddingForScrollbar: true,
-- ),
-- ),
-- ),
-- ],
++ child: GalleryFilesState(
++ child: Gallery(
++ asyncLoader:
++ (creationStartTime, creationEndTime, {limit, asc}) async {
++ final files = await SearchService.instance
++ .getAllFilesForContactPhotoPicker();
++ return FileLoadResult(files, false);
++ },
++ tagPrefix: "pick_contact_photo_gallery",
++ selectedFiles: _selectedFiles,
++ limitSelectionToOne: true,
++ showSelectAll: false,
++ disablePinnedGroupHeader: true,
++ disableVerticalPaddingForScrollbar: true,
++ ),
),
),
-- SafeArea(
-- child: Container(
-- decoration: BoxDecoration(
-- border: Border(
-- top: BorderSide(
-- color: getEnteColorScheme(context).strokeFaint,
-- ),
-- ),
-- ),
-- child: Center(
-- child: ConstrainedBox(
-- constraints: const BoxConstraints(maxWidth: 428),
-- child: Padding(
-- padding: const EdgeInsets.fromLTRB(16, 15, 16, 8),
-- child: ValueListenableBuilder<bool>(
-- valueListenable: isFileSelected,
-- builder: (context, value, _) {
-- return ButtonWidget(
-- key: ValueKey(value),
-- isDisabled: !value,
-- buttonType: ButtonType.neutral,
-- labelText: AppLocalizations.of(
-- context,
-- ).useSelectedPhoto,
-- onTap: () async {
-- Navigator.pop(context, selectedFiles.files.first);
-- },
++ Padding(
++ padding: const EdgeInsets.fromLTRB(
++ Spacing.xl,
++ Spacing.lg,
++ Spacing.xl,
++ Spacing.xl,
++ ),
++ child: ValueListenableBuilder<bool>(
++ valueListenable: _isFileSelected,
++ builder: (context, value, _) {
++ return Column(
++ mainAxisSize: MainAxisSize.min,
++ children: [
++ _ContactPhotoSheetButton(
++ label: l10n.setSelectedPhoto,
++ isDisabled: !value,
++ backgroundColor: colors.primary,
++ disabledBackgroundColor: colors.fillDark,
++ foregroundColor: colors.specialWhite,
++ disabledForegroundColor: colors.textLightest,
++ onTap: () {
++ Navigator.pop(
++ context,
++ ContactPhotoPickerFile(_selectedFiles.files.first),
);
},
),
-- ),
-- ),
-- ),
++ if (widget.canRemovePhoto) ...[
++ const SizedBox(height: Spacing.md),
++ _ContactPhotoSheetButton(
++ label: l10n.removeContactPhoto,
++ backgroundColor: colors.fillDark,
++ foregroundColor: colors.textBase,
++ onTap: () {
++ Navigator.pop(
++ context,
++ const ContactPhotoPickerRemove(),
++ );
++ },
++ ),
++ ],
++ ],
++ );
++ },
),
),
],
@@@ -122,3 -122,3 +164,98 @@@
);
}
}
++
++class _ContactPhotoSheetHeader extends StatelessWidget {
++ const _ContactPhotoSheetHeader({required this.title});
++
++ final String title;
++
++ @override
++ Widget build(BuildContext context) {
++ final colors = context.componentColors;
++ return Padding(
++ padding: const EdgeInsets.fromLTRB(
++ Spacing.xl,
++ Spacing.xl,
++ Spacing.xl,
++ Spacing.lg,
++ ),
++ child: Row(
++ children: [
++ Expanded(
++ child: Text(
++ title,
++ maxLines: 1,
++ overflow: TextOverflow.ellipsis,
++ style: TextStyles.h1.copyWith(color: colors.textBase),
++ ),
++ ),
++ GestureDetector(
++ behavior: HitTestBehavior.opaque,
++ onTap: () => Navigator.pop(context),
++ child: Container(
++ width: 36,
++ height: 36,
++ decoration: BoxDecoration(
++ color: colors.fillLight,
++ shape: BoxShape.circle,
++ ),
++ child: Icon(Icons.close, color: colors.textLightest, size: 20),
++ ),
++ ),
++ ],
++ ),
++ );
++ }
++}
++
++class _ContactPhotoSheetButton extends StatelessWidget {
++ const _ContactPhotoSheetButton({
++ required this.label,
++ required this.backgroundColor,
++ required this.foregroundColor,
++ required this.onTap,
++ this.disabledBackgroundColor,
++ this.disabledForegroundColor,
++ this.isDisabled = false,
++ });
++
++ final String label;
++ final Color backgroundColor;
++ final Color foregroundColor;
++ final Color? disabledBackgroundColor;
++ final Color? disabledForegroundColor;
++ final VoidCallback onTap;
++ final bool isDisabled;
++
++ @override
++ Widget build(BuildContext context) {
++ final enabled = !isDisabled;
++ final effectiveBackground = enabled
++ ? backgroundColor
++ : disabledBackgroundColor ?? backgroundColor;
++ final effectiveForeground = enabled
++ ? foregroundColor
++ : disabledForegroundColor ?? foregroundColor;
++ return GestureDetector(
++ behavior: HitTestBehavior.opaque,
++ onTap: enabled ? onTap : null,
++ child: Container(
++ width: double.infinity,
++ height: 48,
++ alignment: Alignment.center,
++ padding: const EdgeInsets.symmetric(horizontal: Spacing.xxl),
++ decoration: BoxDecoration(
++ color: effectiveBackground,
++ borderRadius: BorderRadius.circular(Radii.button),
++ ),
++ child: Text(
++ label,
++ maxLines: 1,
++ overflow: TextOverflow.ellipsis,
++ style: TextStyles.body.copyWith(color: effectiveForeground),
++ ),
++ ),
++ );
++ }
++}
diff --cc mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart
index 7d5da6e8ee,374ee83be0..03e2f63094
--- 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
@@@ -80,8 -95,11 +80,12 @@@ class _EditContactPageState extends Sta
bool get _canSave => !_isSaving && _nameController.text.trim().isNotEmpty;
String get _initialName => (widget.existingContact?.data?.name ?? "").trim();
- String? get _initialBirthDate => widget.existingContact?.data?.birthDate;
bool get _hasUnsavedChanges =>
- _nameController.text.trim() != _initialName ||
- _selectedBirthDate != _initialBirthDate ||
- _photoDirty;
+ _nameController.text.trim() != _initialName || _photoDirty;
++ bool get _hasContactPhoto =>
++ _draftPhotoBytes != null ||
++ (!_photoDirty &&
++ widget.existingContact?.profilePictureAttachmentId != null);
@override
Widget build(BuildContext context) {
@@@ -256,10 -329,10 +260,23 @@@
}
Future<void> _pickContactPhoto() async {
-- final selectedFile = await showContactPhotoPickerSheet(context);
-- if (selectedFile == null) {
++ final result = await showContactPhotoPickerSheet(
++ context,
++ canRemovePhoto: _hasContactPhoto,
++ );
++ if (result == null) {
++ return;
++ }
++ if (result is ContactPhotoPickerRemove) {
++ _photoLoadGeneration++;
++ setState(() {
++ _draftPhotoBytes = null;
++ _isLoadingPhoto = false;
++ _photoDirty = true;
++ });
return;
}
++ final selectedFile = (result as ContactPhotoPickerFile).file;
setState(() {
_isLoadingPhoto = true;
});
@@@ -307,13 -418,13 +324,20 @@@
var saved = await PhotosContactsService.instance.createOrUpdateContact(
contactUserId: widget.contactUserId,
name: _nameController.text.trim(),
- birthDate: _selectedBirthDate,
+ birthDate: _birthDateToPreserve,
);
-- if (_photoDirty && _draftPhotoBytes != null) {
-- saved = await PhotosContactsService.instance.setProfilePicture(
-- contactId: saved.id,
-- bytes: _draftPhotoBytes!,
-- );
++ if (_photoDirty) {
++ final draftPhotoBytes = _draftPhotoBytes;
++ if (draftPhotoBytes != null) {
++ saved = await PhotosContactsService.instance.setProfilePicture(
++ contactId: saved.id,
++ bytes: draftPhotoBytes,
++ );
++ } else if (widget.existingContact?.profilePictureAttachmentId != null) {
++ saved = await PhotosContactsService.instance.deleteProfilePicture(
++ contactId: saved.id,
++ );
++ }
}
if (!mounted) {
return;
diff --cc mobile/apps/photos/lib/ui/viewer/search_tab/contacts_section.dart
index e7ccc33223,e7ccc33223..84e5440636
--- a/mobile/apps/photos/lib/ui/viewer/search_tab/contacts_section.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search_tab/contacts_section.dart
@@@ -6,11 -6,11 +6,13 @@@ import "package:flutter/material.dart"
import "package:flutter_svg/flutter_svg.dart";
import "package:photos/events/event.dart";
import "package:photos/generated/l10n.dart";
++import "package:photos/models/api/collection/user.dart";
import "package:photos/models/search/generic_search_result.dart";
import "package:photos/models/search/recent_searches.dart";
import "package:photos/models/search/search_constants.dart";
import "package:photos/models/search/search_types.dart";
import "package:photos/service_locator.dart" show isLocalGalleryMode;
++import "package:photos/services/contacts/contact_identity_resolver.dart";
import "package:photos/services/search_service.dart";
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/common/loading_widget.dart";
@@@ -217,9 -217,9 +219,10 @@@ class _ContactRecommendationState exten
widget.contactSearchResult.params[kContactUserId] as int?;
final contactEmail =
widget.contactSearchResult.params[kContactEmail] as String;
++ final displayName = _displayName(contactUserId, contactEmail);
return GestureDetector(
onTap: () {
-- RecentSearches().add(widget.contactSearchResult.name());
++ RecentSearches().add(displayName);
if (widget.contactSearchResult.onResultTap != null) {
widget.contactSearchResult.onResultTap!(context);
} else {
@@@ -246,7 -246,7 +249,7 @@@
),
const SizedBox(height: 10),
Text(
-- widget.contactSearchResult.name(),
++ displayName,
style: TextStyles.mini.copyWith(color: colors.textBase),
maxLines: 1,
overflow: TextOverflow.ellipsis,
@@@ -257,6 -257,6 +260,16 @@@
),
);
}
++
++ String _displayName(int? contactUserId, String contactEmail) {
++ final contactUser = User(id: contactUserId, email: contactEmail);
++ final resolvedName = resolveDisplayName(contactUser);
++ final resolvedEmail = resolveKnownEmail(contactUser) ?? contactEmail;
++ if (resolvedName == resolvedEmail || resolvedName == "Someone") {
++ return widget.contactSearchResult.name();
++ }
++ return resolvedName;
++ }
}
class ContactCTA extends StatelessWidget {
diff --cc mobile/apps/photos/test/services/photos_contacts_service_test.dart
index a0728e6efc,1c9e8be5ad..b9019da635
--- a/mobile/apps/photos/test/services/photos_contacts_service_test.dart
+++ b/mobile/apps/photos/test/services/photos_contacts_service_test.dart
@@@ -66,9 -66,8 +66,14 @@@ void main()
expect(service.hasHydratedCache, isTrue);
expect(service.getCachedContactByUserId(7), isNotNull);
+ expect((await service.getContact(email: '[email protected]'))?.id, 'ct_1');
expect(service.getCachedSavedNameByUserId(7), 'Alice');
++ expect(service.getCachedSavedName(email: '[email protected]'), 'Alice');
expect(service.getCachedResolvedEmailByUserId(7), '[email protected]');
++ expect(
++ service.getCachedResolvedEmail(email: '[email protected]'),
++ );
expect(contactsService.openCalls, 1);
expect(contactsService.syncCalls, 1);
});