Merge branch 'mkirk/fixup-color-for-new-conversation' into release/2.30.2

pull/1/head
Michael Kirk 7 years ago
commit 54c061fb90

@ -188,7 +188,7 @@ const CGFloat kContactCellAvatarTextMargin = 12;
return self.thread.conversationColorName; return self.thread.conversationColorName;
} else { } else {
OWSAssertDebug(self.recipientId); OWSAssertDebug(self.recipientId);
return [TSThread stableConversationColorNameForString:self.recipientId]; return [TSThread stableColorNameForNewConversationWithString:self.recipientId];
} }
}(); }();

@ -66,7 +66,7 @@ NS_ASSUME_NONNULL_BEGIN
colorSeed:(NSString *)colorSeed colorSeed:(NSString *)colorSeed
diameter:(NSUInteger)diameter diameter:(NSUInteger)diameter
{ {
NSString *colorName = [TSThread stableConversationColorNameForString:colorSeed]; NSString *colorName = [TSThread stableColorNameForNewConversationWithString:colorSeed];
return [self initWithContactId:colorSeed name:nonSignalName colorName:(NSString *)colorName diameter:diameter]; return [self initWithContactId:colorSeed name:nonSignalName colorName:(NSString *)colorName diameter:diameter];
} }

@ -36,7 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSString *conversationColorName; @property (nonatomic, readonly) NSString *conversationColorName;
- (void)updateConversationColorName:(NSString *)colorName transaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateConversationColorName:(NSString *)colorName transaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (NSString *)stableConversationColorNameForString:(NSString *)colorSeed; + (NSString *)stableColorNameForNewConversationWithString:(NSString *)colorSeed;
/** /**
* @returns * @returns

@ -52,9 +52,9 @@ NS_ASSUME_NONNULL_BEGIN
NSString *_Nullable contactId = self.contactIdentifier; NSString *_Nullable contactId = self.contactIdentifier;
if (contactId.length > 0) { if (contactId.length > 0) {
// To be consistent with colors synced to desktop // To be consistent with colors synced to desktop
_conversationColorName = [self.class stableConversationColorNameForString:contactId]; _conversationColorName = [self.class stableColorNameForNewConversationWithString:contactId];
} else { } 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; NSString *_Nullable contactId = self.contactIdentifier;
if (contactId.length > 0) { if (contactId.length > 0) {
// To be consistent with colors synced to desktop // To be consistent with colors synced to desktop
_conversationColorName = [self.class stableConversationColorNameForString:contactId]; _conversationColorName = [self.class stableColorNameForLegacyConversationWithString:contactId];
} else { } 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 #pragma mark - Conversation Color
+ (NSString *)randomConversationColorName + (NSArray<NSString *> *)colorNamesForNewConversation
{ {
NSUInteger count = self.conversationColorNames.count; // all conversation colors except "steel"
NSUInteger index = arc4random_uniform((uint32_t)count); return @[
return [self.conversationColorNames objectAtIndex:index]; @"crimson",
@"vermilion",
@"burlap",
@"forest",
@"wintergreen",
@"teal",
@"blue",
@"indigo",
@"violet",
@"plum",
@"taupe"
];
} }
+ (NSString *)stableConversationColorNameForString:(NSString *)colorSeed + (NSString *)stableConversationColorNameForString:(NSString *)colorSeed colorNames:(NSArray<NSString *> *)colorNames
{ {
NSData *contactData = [colorSeed dataUsingEncoding:NSUTF8StringEncoding]; NSData *contactData = [colorSeed dataUsingEncoding:NSUTF8StringEncoding];
@ -461,11 +472,23 @@ NS_ASSUME_NONNULL_BEGIN
OWSFailDebug(@"could not compute hash for color seed."); OWSFailDebug(@"could not compute hash for color seed.");
} }
NSUInteger index = (hash % [self.conversationColorNames count]); NSUInteger index = (hash % colorNames.count);
return [self.conversationColorNames objectAtIndex:index]; 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<NSString *> *)conversationColorNames + (NSArray<NSString *> *)legacyConversationColorNames
{ {
return @[ return @[
@"red", @"red",

@ -101,7 +101,7 @@ NS_ASSUME_NONNULL_BEGIN
if (contactThread) { if (contactThread) {
return contactThread.conversationColorName; return contactThread.conversationColorName;
} }
return [self stableConversationColorNameForString:recipientId]; return [self stableColorNameForNewConversationWithString:recipientId];
} }
@end @end

@ -246,7 +246,7 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific
{ {
OWSAssertDebug(groupId.length > 0); OWSAssertDebug(groupId.length > 0);
return [self.class stableConversationColorNameForString:[self threadIdFromGroupId:groupId]]; return [self.class stableColorNameForNewConversationWithString:[self threadIdFromGroupId:groupId]];
} }
@end @end

@ -105,7 +105,7 @@ NS_ASSUME_NONNULL_BEGIN
conversationColorName = contactThread.conversationColorName; conversationColorName = contactThread.conversationColorName;
disappearingMessagesConfiguration = [contactThread disappearingMessagesConfigurationWithTransaction:transaction]; disappearingMessagesConfiguration = [contactThread disappearingMessagesConfigurationWithTransaction:transaction];
} else { } else {
conversationColorName = [TSThread stableConversationColorNameForString:signalAccount.recipientId]; conversationColorName = [TSThread stableColorNameForNewConversationWithString:signalAccount.recipientId];
} }
[contactsOutputStream writeSignalAccount:signalAccount [contactsOutputStream writeSignalAccount:signalAccount

Loading…
Cancel
Save