From 0290f176c00c350d6b081dabf89c72336f9f2b3f Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 15 Aug 2017 12:35:33 -0400 Subject: [PATCH 1/2] Use profile name/avatar when creating new contact // FREEBIE --- Signal/src/ViewControllers/ContactsViewHelper.h | 2 +- Signal/src/ViewControllers/ContactsViewHelper.m | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Signal/src/ViewControllers/ContactsViewHelper.h b/Signal/src/ViewControllers/ContactsViewHelper.h index 3101659ab..e963ba687 100644 --- a/Signal/src/ViewControllers/ContactsViewHelper.h +++ b/Signal/src/ViewControllers/ContactsViewHelper.h @@ -69,7 +69,7 @@ NS_ASSUME_NONNULL_BEGIN /** * NOTE: This method calls `[UIUtil applyDefaultSystemAppearence]`. - * When using this method, you must call `[UIUtil applySignalAppearence]` once contact editing is finished; + * When using this method, you must call `[UIUtil applySignalAppearence]` once contact editing is finished; */ - (void)presentContactViewControllerForRecipientId:(NSString *)recipientId fromViewController:(UIViewController *)fromViewController diff --git a/Signal/src/ViewControllers/ContactsViewHelper.m b/Signal/src/ViewControllers/ContactsViewHelper.m index 58f8f9981..e0b240206 100644 --- a/Signal/src/ViewControllers/ContactsViewHelper.m +++ b/Signal/src/ViewControllers/ContactsViewHelper.m @@ -5,6 +5,7 @@ #import "ContactsViewHelper.h" #import "ContactTableViewCell.h" #import "Environment.h" +#import "OWSProfileManager.h" #import "Signal-Swift.h" #import #import @@ -28,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) BOOL shouldNotifyDelegateOfUpdatedContacts; @property (nonatomic) BOOL hasUpdatedContactsAtLeastOnce; +@property (nonatomic) OWSProfileManager *profileManager; @end @@ -49,6 +51,7 @@ NS_ASSUME_NONNULL_BEGIN _blockedPhoneNumbers = [_blockingManager blockedPhoneNumbers]; _contactsManager = [Environment getCurrent].contactsManager; + _profileManager = [OWSProfileManager sharedManager]; // We don't want to notify the delegate in the `updateContacts`. self.shouldNotifyDelegateOfUpdatedContacts = YES; @@ -409,6 +412,13 @@ NS_ASSUME_NONNULL_BEGIN CNLabeledValue *labeledPhoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMain value:phoneNumber]; newContact.phoneNumbers = @[ labeledPhoneNumber ]; + newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId]; + + UIImage *_Nullable profileImage = [self.profileManager profileAvatarForRecipientId:recipientId]; + if (profileImage) { + // TODO get raw jpg data from profileManager to avoid recompress. + newContact.imageData = UIImageJPEGRepresentation(profileImage, 0.9); + } contactViewController = [CNContactViewController viewControllerForNewContact:newContact]; } From 9f72db44ac6057820baafc1f4d4796736bdde48c Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 15 Aug 2017 12:49:27 -0400 Subject: [PATCH 2/2] Avoid lossy re-encoding of profile image // FREEBIE --- Signal/src/Profiles/OWSProfileManager.h | 4 +++ Signal/src/Profiles/OWSProfileManager.m | 25 ++++++++++++++++--- .../src/ViewControllers/ContactsViewHelper.m | 8 ++---- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Signal/src/Profiles/OWSProfileManager.h b/Signal/src/Profiles/OWSProfileManager.h index d460c12b0..558d596fa 100644 --- a/Signal/src/Profiles/OWSProfileManager.h +++ b/Signal/src/Profiles/OWSProfileManager.h @@ -57,6 +57,10 @@ extern NSString *const kNSNotificationName_OtherUsersProfileDidChange; - (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId; +// Reads raw avatar data from disk if available. Uncached, so shouldn't be used frequently, +// but useful to get the raw image data for populating cnContact.imageData without lossily re-encoding. +- (nullable NSData *)profileAvatarDataForRecipientId:(NSString *)recipientId; + - (void)refreshProfileForRecipientId:(NSString *)recipientId; - (void)updateProfileForRecipientId:(NSString *)recipientId diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index f975be3a0..d4e560fa1 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -718,6 +718,15 @@ static const NSUInteger kOWSProfileManager_NameDataLength = 26; return userProfile.profileName; } +- (nullable NSData *)profileAvatarDataForRecipientId:(NSString *)recipientId +{ + UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId]; + if (userProfile.avatarFileName.length > 0) { + return [self loadProfileDataWithFilename:userProfile.avatarFileName]; + } + return nil; +} + - (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId { OWSAssert([NSThread isMainThread]); @@ -1001,12 +1010,20 @@ static const NSUInteger kOWSProfileManager_NameDataLength = 26; #pragma mark - Avatar Disk Cache -- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName +- (nullable NSData *)loadProfileDataWithFilename:(NSString *)filename { - OWSAssert(fileName.length > 0); + OWSAssert(filename.length > 0); - NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:fileName]; - UIImage *_Nullable image = [UIImage imageWithContentsOfFile:filePath]; + NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename]; + return [NSData dataWithContentsOfFile:filePath]; +} + +- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)filename +{ + OWSAssert(filename.length > 0); + + NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename]; + UIImage *_Nullable image = [UIImage imageWithData:[self loadProfileDataWithFilename:filename]]; return image; } diff --git a/Signal/src/ViewControllers/ContactsViewHelper.m b/Signal/src/ViewControllers/ContactsViewHelper.m index e0b240206..24c3e735c 100644 --- a/Signal/src/ViewControllers/ContactsViewHelper.m +++ b/Signal/src/ViewControllers/ContactsViewHelper.m @@ -412,13 +412,9 @@ NS_ASSUME_NONNULL_BEGIN CNLabeledValue *labeledPhoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMain value:phoneNumber]; newContact.phoneNumbers = @[ labeledPhoneNumber ]; - newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId]; - UIImage *_Nullable profileImage = [self.profileManager profileAvatarForRecipientId:recipientId]; - if (profileImage) { - // TODO get raw jpg data from profileManager to avoid recompress. - newContact.imageData = UIImageJPEGRepresentation(profileImage, 0.9); - } + newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId]; + newContact.imageData = [self.profileManager profileAvatarDataForRecipientId:recipientId]; contactViewController = [CNContactViewController viewControllerForNewContact:newContact]; }