From 4e6816ec5e2f3b869333499d7e617ee125cd54ee Mon Sep 17 00:00:00 2001 From: sdkjfhsdkjhfsdlkjhfsdf Date: Fri, 22 Dec 2017 13:33:15 -0600 Subject: [PATCH] Code cleanup // FREEBIE --- SignalMessaging/profiles/OWSProfileManager.m | 28 ++++++++----------- SignalMessaging/profiles/OWSUserProfile.h | 7 ----- SignalMessaging/profiles/OWSUserProfile.m | 29 ++++++-------------- 3 files changed, 21 insertions(+), 43 deletions(-) diff --git a/SignalMessaging/profiles/OWSProfileManager.m b/SignalMessaging/profiles/OWSProfileManager.m index a8b351335..5bc3f644d 100644 --- a/SignalMessaging/profiles/OWSProfileManager.m +++ b/SignalMessaging/profiles/OWSProfileManager.m @@ -729,16 +729,15 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; return; } - [userProfile updateWithProfileName:nil - profileKey:profileKey - avatarUrlPath:nil - avatarFileName:nil - dbConnection:self.dbConnection - completion:^{ - [ProfileFetcherJob runWithRecipientId:recipientId - networkManager:self.networkManager - ignoreThrottling:YES]; - }]; + [userProfile clearWithProfileKey:profileKey + dbConnection:self.dbConnection + completion:^{ + dispatch_async(dispatch_get_main_queue(), ^(void) { + [ProfileFetcherJob runWithRecipientId:recipientId + networkManager:self.networkManager + ignoreThrottling:YES]; + }); + }]; }); } @@ -911,6 +910,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ OWSUserProfile *userProfile = [OWSUserProfile getOrBuildUserProfileForRecipientId:recipientId dbConnection:self.dbConnection]; + if (!userProfile.profileKey) { return; } @@ -918,8 +918,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; NSString *_Nullable profileName = [self decryptProfileNameData:profileNameEncrypted profileKey:userProfile.profileKey]; - BOOL isAvatarSame = [self isNullableStringEqual:userProfile.avatarUrlPath toString:avatarUrlPath]; - [userProfile updateWithProfileName:profileName avatarUrlPath:avatarUrlPath avatarFileName:userProfile.avatarFileName // use existing file name if already downloaded @@ -944,10 +942,8 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; completion:nil]; } - if (!isAvatarSame) { - if (avatarUrlPath) { - [self downloadAvatarForUserProfile:userProfile]; - } + if (userProfile.avatarUrlPath.length > 0 && userProfile.avatarFileName.length == 0) { + [self downloadAvatarForUserProfile:userProfile]; } }); } diff --git a/SignalMessaging/profiles/OWSUserProfile.h b/SignalMessaging/profiles/OWSUserProfile.h index 1410981ca..c58e59e79 100644 --- a/SignalMessaging/profiles/OWSUserProfile.h +++ b/SignalMessaging/profiles/OWSUserProfile.h @@ -41,13 +41,6 @@ extern NSString *const kLocalProfileUniqueId; #pragma mark - Update With... Methods -- (void)updateWithProfileName:(nullable NSString *)profileName - profileKey:(OWSAES256Key *)profileKey - avatarUrlPath:(nullable NSString *)avatarUrlPath - avatarFileName:(nullable NSString *)avatarFileName - dbConnection:(YapDatabaseConnection *)dbConnection - completion:(nullable OWSUserProfileCompletion)completion; - - (void)updateWithProfileName:(nullable NSString *)profileName avatarUrlPath:(nullable NSString *)avatarUrlPath avatarFileName:(nullable NSString *)avatarFileName diff --git a/SignalMessaging/profiles/OWSUserProfile.m b/SignalMessaging/profiles/OWSUserProfile.m index 83826db4e..2e4294bad 100644 --- a/SignalMessaging/profiles/OWSUserProfile.m +++ b/SignalMessaging/profiles/OWSUserProfile.m @@ -105,7 +105,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; dbConnection:(YapDatabaseConnection *)dbConnection completion:(nullable OWSUserProfileCompletion)completion { - id beforeSnapshot = self.dictionaryValue; + NSDictionary *beforeSnapshot = self.dictionaryValue; changeBlock(self); @@ -116,12 +116,19 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; if (latestInstance) { changeBlock(latestInstance); - id afterSnapshot = latestInstance.dictionaryValue; + NSDictionary *afterSnapshot = latestInstance.dictionaryValue; if ([beforeSnapshot isEqual:afterSnapshot]) { DDLogVerbose( @"%@ Ignoring redundant update in %s: %@", self.logTag, functionName, self.debugDescription); didChange = NO; } else { + NSString *_Nullable oldAvatarUrlPath = beforeSnapshot[@"avatarUrlPath"]; + if (!latestInstance.avatarUrlPath || ![latestInstance.avatarUrlPath isEqual:oldAvatarUrlPath]) { + // If the avatarURL changed, the avatarFileName can't be valid. + // Clear it. + latestInstance.avatarFileName = nil; + } + [latestInstance saveWithTransaction:transaction]; } } else { @@ -198,24 +205,6 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; completion:completion]; } -- (void)updateWithProfileName:(nullable NSString *)profileName - profileKey:(OWSAES256Key *)profileKey - avatarUrlPath:(nullable NSString *)avatarUrlPath - avatarFileName:(nullable NSString *)avatarFileName - dbConnection:(YapDatabaseConnection *)dbConnection - completion:(nullable OWSUserProfileCompletion)completion -{ - [self applyChanges:^(OWSUserProfile *userProfile) { - [userProfile setProfileName:[profileName ows_stripped]]; - [userProfile setProfileKey:profileKey]; - [userProfile setAvatarUrlPath:avatarUrlPath]; - [userProfile setAvatarFileName:avatarFileName]; - } - functionName:__PRETTY_FUNCTION__ - dbConnection:dbConnection - completion:completion]; -} - - (void)updateWithAvatarUrlPath:(nullable NSString *)avatarUrlPath avatarFileName:(nullable NSString *)avatarFileName dbConnection:(YapDatabaseConnection *)dbConnection