From 405cc31a38b671c85440de6f60c97ed473719274 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 4 Oct 2018 10:03:33 -0600 Subject: [PATCH] Apply new colors to new conversation --- SignalMessaging/Views/ContactCellView.m | 2 +- .../utils/OWSContactAvatarBuilder.m | 2 +- SignalServiceKit/src/Contacts/TSThread.h | 2 +- SignalServiceKit/src/Contacts/TSThread.m | 47 ++++++++++++++----- .../src/Contacts/Threads/TSContactThread.m | 2 +- .../src/Contacts/Threads/TSGroupThread.m | 2 +- .../DeviceSyncing/OWSSyncContactsMessage.m | 2 +- 7 files changed, 41 insertions(+), 18 deletions(-) diff --git a/SignalMessaging/Views/ContactCellView.m b/SignalMessaging/Views/ContactCellView.m index 6e44c049b..4067b0dd5 100644 --- a/SignalMessaging/Views/ContactCellView.m +++ b/SignalMessaging/Views/ContactCellView.m @@ -188,7 +188,7 @@ const CGFloat kContactCellAvatarTextMargin = 12; return self.thread.conversationColorName; } else { OWSAssertDebug(self.recipientId); - return [TSThread stableConversationColorNameForString:self.recipientId]; + return [TSThread stableColorNameForNewConversationWithString:self.recipientId]; } }(); diff --git a/SignalMessaging/utils/OWSContactAvatarBuilder.m b/SignalMessaging/utils/OWSContactAvatarBuilder.m index 8cb45b10b..9010e67e5 100644 --- a/SignalMessaging/utils/OWSContactAvatarBuilder.m +++ b/SignalMessaging/utils/OWSContactAvatarBuilder.m @@ -66,7 +66,7 @@ NS_ASSUME_NONNULL_BEGIN colorSeed:(NSString *)colorSeed diameter:(NSUInteger)diameter { - NSString *colorName = [TSThread stableConversationColorNameForString:colorSeed]; + NSString *colorName = [TSThread stableColorNameForNewConversationWithString:colorSeed]; return [self initWithContactId:colorSeed name:nonSignalName colorName:(NSString *)colorName diameter:diameter]; } diff --git a/SignalServiceKit/src/Contacts/TSThread.h b/SignalServiceKit/src/Contacts/TSThread.h index 207d5c9c8..475db8e4f 100644 --- a/SignalServiceKit/src/Contacts/TSThread.h +++ b/SignalServiceKit/src/Contacts/TSThread.h @@ -36,7 +36,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly) NSString *conversationColorName; - (void)updateConversationColorName:(NSString *)colorName transaction:(YapDatabaseReadWriteTransaction *)transaction; -+ (NSString *)stableConversationColorNameForString:(NSString *)colorSeed; ++ (NSString *)stableColorNameForNewConversationWithString:(NSString *)colorSeed; /** * @returns diff --git a/SignalServiceKit/src/Contacts/TSThread.m b/SignalServiceKit/src/Contacts/TSThread.m index 59f298972..c60986577 100644 --- a/SignalServiceKit/src/Contacts/TSThread.m +++ b/SignalServiceKit/src/Contacts/TSThread.m @@ -52,9 +52,9 @@ NS_ASSUME_NONNULL_BEGIN NSString *_Nullable contactId = self.contactIdentifier; if (contactId.length > 0) { // To be consistent with colors synced to desktop - _conversationColorName = [self.class stableConversationColorNameForString:contactId]; + _conversationColorName = [self.class stableColorNameForNewConversationWithString:contactId]; } else { - _conversationColorName = [self.class stableConversationColorNameForString:self.uniqueId]; + _conversationColorName = [self.class stableColorNameForNewConversationWithString:self.uniqueId]; } } @@ -72,9 +72,9 @@ NS_ASSUME_NONNULL_BEGIN NSString *_Nullable contactId = self.contactIdentifier; if (contactId.length > 0) { // To be consistent with colors synced to desktop - _conversationColorName = [self.class stableConversationColorNameForString:contactId]; + _conversationColorName = [self.class stableColorNameForLegacyConversationWithString:contactId]; } else { - _conversationColorName = [self.class stableConversationColorNameForString:self.uniqueId]; + _conversationColorName = [self.class stableColorNameForLegacyConversationWithString:self.uniqueId]; } } @@ -441,14 +441,25 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Conversation Color -+ (NSString *)randomConversationColorName ++ (NSArray *)colorNamesForNewConversation { - NSUInteger count = self.conversationColorNames.count; - NSUInteger index = arc4random_uniform((uint32_t)count); - return [self.conversationColorNames objectAtIndex:index]; + // all conversation colors except "steel" + return @[ + @"crimson", + @"vermilion", + @"burlap", + @"forest", + @"wintergreen", + @"teal", + @"blue", + @"indigo", + @"violet", + @"plum", + @"taupe" + ]; } -+ (NSString *)stableConversationColorNameForString:(NSString *)colorSeed ++ (NSString *)stableConversationColorNameForString:(NSString *)colorSeed colorNames:(NSArray *)colorNames { NSData *contactData = [colorSeed dataUsingEncoding:NSUTF8StringEncoding]; @@ -461,11 +472,23 @@ NS_ASSUME_NONNULL_BEGIN OWSFailDebug(@"could not compute hash for color seed."); } - NSUInteger index = (hash % [self.conversationColorNames count]); - return [self.conversationColorNames objectAtIndex:index]; + NSUInteger index = (hash % colorNames.count); + return [colorNames objectAtIndex:index]; +} + ++ (NSString *)stableColorNameForNewConversationWithString:(NSString *)colorSeed +{ + return [self stableConversationColorNameForString:colorSeed colorNames:self.colorNamesForNewConversation]; +} + +// After introducing new conversation colors, we want to try to maintain as close +// as possible to the old color for an existing thread. ++ (NSString *)stableColorNameForLegacyConversationWithString:(NSString *)colorSeed +{ + return [self stableConversationColorNameForString:colorSeed colorNames:self.legacyConversationColorNames]; } -+ (NSArray *)conversationColorNames ++ (NSArray *)legacyConversationColorNames { return @[ @"red", diff --git a/SignalServiceKit/src/Contacts/Threads/TSContactThread.m b/SignalServiceKit/src/Contacts/Threads/TSContactThread.m index d8c47a66b..1ea415e15 100644 --- a/SignalServiceKit/src/Contacts/Threads/TSContactThread.m +++ b/SignalServiceKit/src/Contacts/Threads/TSContactThread.m @@ -101,7 +101,7 @@ NS_ASSUME_NONNULL_BEGIN if (contactThread) { return contactThread.conversationColorName; } - return [self stableConversationColorNameForString:recipientId]; + return [self stableColorNameForNewConversationWithString:recipientId]; } @end diff --git a/SignalServiceKit/src/Contacts/Threads/TSGroupThread.m b/SignalServiceKit/src/Contacts/Threads/TSGroupThread.m index 248a5ae22..d1981c869 100644 --- a/SignalServiceKit/src/Contacts/Threads/TSGroupThread.m +++ b/SignalServiceKit/src/Contacts/Threads/TSGroupThread.m @@ -246,7 +246,7 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific { OWSAssertDebug(groupId.length > 0); - return [self.class stableConversationColorNameForString:[self threadIdFromGroupId:groupId]]; + return [self.class stableColorNameForNewConversationWithString:[self threadIdFromGroupId:groupId]]; } @end diff --git a/SignalServiceKit/src/Messages/DeviceSyncing/OWSSyncContactsMessage.m b/SignalServiceKit/src/Messages/DeviceSyncing/OWSSyncContactsMessage.m index 41ba71c74..ea22ac043 100644 --- a/SignalServiceKit/src/Messages/DeviceSyncing/OWSSyncContactsMessage.m +++ b/SignalServiceKit/src/Messages/DeviceSyncing/OWSSyncContactsMessage.m @@ -105,7 +105,7 @@ NS_ASSUME_NONNULL_BEGIN conversationColorName = contactThread.conversationColorName; disappearingMessagesConfiguration = [contactThread disappearingMessagesConfigurationWithTransaction:transaction]; } else { - conversationColorName = [TSThread stableConversationColorNameForString:signalAccount.recipientId]; + conversationColorName = [TSThread stableColorNameForNewConversationWithString:signalAccount.recipientId]; } [contactsOutputStream writeSignalAccount:signalAccount