Constantize ConversationColorName, map previous incorrect values

pull/1/head
Michael Kirk 7 years ago
parent d59e21e7f0
commit 2b805e4eae

@ -468,7 +468,7 @@ const CGFloat kIconViewLength = 24;
[mainSection [mainSection
addItem:[OWSTableItem addItem:[OWSTableItem
itemWithCustomCellBlock:^{ itemWithCustomCellBlock:^{
NSString *colorName = self.thread.conversationColorName; ConversationColorName colorName = self.thread.conversationColorName;
UIColor *currentColor = UIColor *currentColor =
[OWSConversationColor conversationColorOrDefaultForColorName:colorName].themeColor; [OWSConversationColor conversationColorOrDefaultForColorName:colorName].themeColor;
NSString *title = NSLocalizedString(@"CONVERSATION_SETTINGS_CONVERSATION_COLOR", NSString *title = NSLocalizedString(@"CONVERSATION_SETTINGS_CONVERSATION_COLOR",

@ -183,7 +183,7 @@ const CGFloat kContactCellAvatarTextMargin = 12;
return; return;
} }
NSString *colorName = ^{ ConversationColorName colorName = ^{
if (self.thread) { if (self.thread) {
return self.thread.conversationColorName; return self.thread.conversationColorName;
} else { } else {

@ -2,20 +2,21 @@
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import <SignalServiceKit/TSThread.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface OWSConversationColor : NSObject @interface OWSConversationColor : NSObject
@property (nonatomic, readonly) NSString *name; @property (nonatomic, readonly) ConversationColorName name;
@property (nonatomic, readonly) UIColor *primaryColor; @property (nonatomic, readonly) UIColor *primaryColor;
@property (nonatomic, readonly) UIColor *shadeColor; @property (nonatomic, readonly) UIColor *shadeColor;
@property (nonatomic, readonly) UIColor *tintColor; @property (nonatomic, readonly) UIColor *tintColor;
@property (nonatomic, readonly) UIColor *themeColor; @property (nonatomic, readonly) UIColor *themeColor;
+ (OWSConversationColor *)conversationColorWithName:(NSString *)name + (OWSConversationColor *)conversationColorWithName:(ConversationColorName)name
primaryColor:(UIColor *)primaryColor primaryColor:(UIColor *)primaryColor
shadeColor:(UIColor *)shadeColor shadeColor:(UIColor *)shadeColor
tintColor:(UIColor *)tintColor; tintColor:(UIColor *)tintColor;
@ -66,15 +67,15 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Conversation Colors #pragma mark - Conversation Colors
+ (nullable OWSConversationColor *)conversationColorForColorName:(NSString *)colorName + (nullable OWSConversationColor *)conversationColorForColorName:(ConversationColorName)colorName
NS_SWIFT_NAME(conversationColor(colorName:)); NS_SWIFT_NAME(conversationColor(colorName:));
// If the conversation color name is valid, return its colors. // If the conversation color name is valid, return its colors.
// Otherwise return the "default" conversation colors. // Otherwise return the "default" conversation colors.
+ (OWSConversationColor *)conversationColorOrDefaultForColorName:(NSString *)conversationColorName + (OWSConversationColor *)conversationColorOrDefaultForColorName:(ConversationColorName)conversationColorName
NS_SWIFT_NAME(conversationColorOrDefault(colorName:)); NS_SWIFT_NAME(conversationColorOrDefault(colorName:));
@property (class, readonly, nonatomic) NSArray<NSString *> *conversationColorNames; @property (class, readonly, nonatomic) NSArray<ConversationColorName> *conversationColorNames;
@end @end

@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface OWSConversationColor () @interface OWSConversationColor ()
@property (nonatomic) NSString *name; @property (nonatomic) ConversationColorName name;
@property (nonatomic) UIColor *primaryColor; @property (nonatomic) UIColor *primaryColor;
@property (nonatomic) UIColor *shadeColor; @property (nonatomic) UIColor *shadeColor;
@property (nonatomic) UIColor *tintColor; @property (nonatomic) UIColor *tintColor;
@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
@implementation OWSConversationColor @implementation OWSConversationColor
+ (OWSConversationColor *)conversationColorWithName:(NSString *)name + (OWSConversationColor *)conversationColorWithName:(ConversationColorName)name
primaryColor:(UIColor *)primaryColor primaryColor:(UIColor *)primaryColor
shadeColor:(UIColor *)shadeColor shadeColor:(UIColor *)shadeColor
tintColor:(UIColor *)tintColor tintColor:(UIColor *)tintColor
@ -246,51 +246,51 @@ NS_ASSUME_NONNULL_BEGIN
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
// Order here affects the order in the conversation color picker. // Order here affects the order in the conversation color picker.
allConversationColors = @[ allConversationColors = @[
[OWSConversationColor conversationColorWithName:@"crimson" [OWSConversationColor conversationColorWithName:ConversationColorNameCrimson
primaryColor:self.ows_crimsonColor primaryColor:self.ows_crimsonColor
shadeColor:self.ows_crimsonShadeColor shadeColor:self.ows_crimsonShadeColor
tintColor:self.ows_crimsonTintColor], tintColor:self.ows_crimsonTintColor],
[OWSConversationColor conversationColorWithName:@"vermilion" [OWSConversationColor conversationColorWithName:ConversationColorNameVermilion
primaryColor:self.ows_vermilionColor primaryColor:self.ows_vermilionColor
shadeColor:self.ows_vermilionShadeColor shadeColor:self.ows_vermilionShadeColor
tintColor:self.ows_vermilionTintColor], tintColor:self.ows_vermilionTintColor],
[OWSConversationColor conversationColorWithName:@"burlap" [OWSConversationColor conversationColorWithName:ConversationColorNameBurlap
primaryColor:self.ows_burlapColor primaryColor:self.ows_burlapColor
shadeColor:self.ows_burlapShadeColor shadeColor:self.ows_burlapShadeColor
tintColor:self.ows_burlapTintColor], tintColor:self.ows_burlapTintColor],
[OWSConversationColor conversationColorWithName:@"forest" [OWSConversationColor conversationColorWithName:ConversationColorNameForest
primaryColor:self.ows_forestColor primaryColor:self.ows_forestColor
shadeColor:self.ows_forestShadeColor shadeColor:self.ows_forestShadeColor
tintColor:self.ows_forestTintColor], tintColor:self.ows_forestTintColor],
[OWSConversationColor conversationColorWithName:@"wintergreen" [OWSConversationColor conversationColorWithName:ConversationColorNameWintergreen
primaryColor:self.ows_wintergreenColor primaryColor:self.ows_wintergreenColor
shadeColor:self.ows_wintergreenShadeColor shadeColor:self.ows_wintergreenShadeColor
tintColor:self.ows_wintergreenTintColor], tintColor:self.ows_wintergreenTintColor],
[OWSConversationColor conversationColorWithName:@"teal" [OWSConversationColor conversationColorWithName:ConversationColorNameTeal
primaryColor:self.ows_tealColor primaryColor:self.ows_tealColor
shadeColor:self.ows_tealShadeColor shadeColor:self.ows_tealShadeColor
tintColor:self.ows_tealTintColor], tintColor:self.ows_tealTintColor],
[OWSConversationColor conversationColorWithName:@"blue" [OWSConversationColor conversationColorWithName:ConversationColorNameBlue
primaryColor:self.ows_blueColor primaryColor:self.ows_blueColor
shadeColor:self.ows_blueShadeColor shadeColor:self.ows_blueShadeColor
tintColor:self.ows_blueTintColor], tintColor:self.ows_blueTintColor],
[OWSConversationColor conversationColorWithName:@"indigo" [OWSConversationColor conversationColorWithName:ConversationColorNameIndigo
primaryColor:self.ows_indigoColor primaryColor:self.ows_indigoColor
shadeColor:self.ows_indigoShadeColor shadeColor:self.ows_indigoShadeColor
tintColor:self.ows_indigoTintColor], tintColor:self.ows_indigoTintColor],
[OWSConversationColor conversationColorWithName:@"violet" [OWSConversationColor conversationColorWithName:ConversationColorNameViolet
primaryColor:self.ows_violetColor primaryColor:self.ows_violetColor
shadeColor:self.ows_violetShadeColor shadeColor:self.ows_violetShadeColor
tintColor:self.ows_violetTintColor], tintColor:self.ows_violetTintColor],
[OWSConversationColor conversationColorWithName:@"plum" [OWSConversationColor conversationColorWithName:ConversationColorNamePlum
primaryColor:self.ows_plumColor primaryColor:self.ows_plumColor
shadeColor:self.ows_plumShadeColor shadeColor:self.ows_plumShadeColor
tintColor:self.ows_plumTintColor], tintColor:self.ows_plumTintColor],
[OWSConversationColor conversationColorWithName:@"taupe" [OWSConversationColor conversationColorWithName:ConversationColorNameTaupe
primaryColor:self.ows_taupeColor primaryColor:self.ows_taupeColor
shadeColor:self.ows_taupeShadeColor shadeColor:self.ows_taupeShadeColor
tintColor:self.ows_taupeTintColor], tintColor:self.ows_taupeTintColor],
[OWSConversationColor conversationColorWithName:@"steel" [OWSConversationColor conversationColorWithName:ConversationColorNameSteel
primaryColor:self.ows_steelColor primaryColor:self.ows_steelColor
shadeColor:self.ows_steelShadeColor shadeColor:self.ows_steelShadeColor
tintColor:self.ows_steelTintColor], tintColor:self.ows_steelTintColor],
@ -300,12 +300,12 @@ NS_ASSUME_NONNULL_BEGIN
return allConversationColors; return allConversationColors;
} }
+ (NSDictionary<NSString *, OWSConversationColor *> *)conversationColorMap + (NSDictionary<ConversationColorName, OWSConversationColor *> *)conversationColorMap
{ {
static NSDictionary<NSString *, OWSConversationColor *> *colorMap; static NSDictionary<ConversationColorName, OWSConversationColor *> *colorMap;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
NSMutableDictionary<NSString *, OWSConversationColor *> *mutableColorMap = [NSMutableDictionary new]; NSMutableDictionary<ConversationColorName, OWSConversationColor *> *mutableColorMap = [NSMutableDictionary new];
for (OWSConversationColor *conversationColor in self.allConversationColors) { for (OWSConversationColor *conversationColor in self.allConversationColors) {
mutableColorMap[conversationColor.name] = conversationColor; mutableColorMap[conversationColor.name] = conversationColor;
} }
@ -315,22 +315,22 @@ NS_ASSUME_NONNULL_BEGIN
return colorMap; return colorMap;
} }
+ (NSArray<NSString *> *)conversationColorNames + (NSArray<ConversationColorName> *)conversationColorNames
{ {
NSMutableArray<NSString *> *names = [NSMutableArray new]; NSMutableArray<ConversationColorName> *names = [NSMutableArray new];
for (OWSConversationColor *conversationColor in self.allConversationColors) { for (OWSConversationColor *conversationColor in self.allConversationColors) {
[names addObject:conversationColor.name]; [names addObject:conversationColor.name];
} }
#ifdef DEBUG #ifdef DEBUG
NSSet<NSString *> *colorNameSet = [NSSet setWithArray:names]; NSSet<ConversationColorName> *colorNameSet = [NSSet setWithArray:names];
// These constants are duplicated in two places. So this canary exists to make sure they stay in sync. // These constants are duplicated in two places. So this canary exists to make sure they stay in sync.
NSSet<NSString *> *threadColorNameSet = [NSSet setWithArray:TSThread.conversationColorNames]; NSSet<ConversationColorName> *threadColorNameSet = [NSSet setWithArray:TSThread.conversationColorNames];
OWSAssertDebug([colorNameSet isEqual:threadColorNameSet]); OWSAssertDebug([colorNameSet isEqual:threadColorNameSet]);
#endif #endif
return [names copy]; return [names copy];
} }
+ (nullable OWSConversationColor *)conversationColorForColorName:(NSString *)conversationColorName + (nullable OWSConversationColor *)conversationColorForColorName:(ConversationColorName)conversationColorName
{ {
OWSConversationColor *_Nullable result = self.conversationColorMap[conversationColorName]; OWSConversationColor *_Nullable result = self.conversationColorMap[conversationColorName];
@ -340,7 +340,7 @@ NS_ASSUME_NONNULL_BEGIN
return result; return result;
} }
+ (OWSConversationColor *)conversationColorOrDefaultForColorName:(NSString *)conversationColorName + (OWSConversationColor *)conversationColorOrDefaultForColorName:(ConversationColorName)conversationColorName
{ {
OWSConversationColor *_Nullable conversationColor = [self conversationColorForColorName:conversationColorName]; OWSConversationColor *_Nullable conversationColor = [self conversationColorForColorName:conversationColorName];
if (conversationColor) { if (conversationColor) {
@ -351,7 +351,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (OWSConversationColor *)defaultConversationColor + (OWSConversationColor *)defaultConversationColor
{ {
return [self conversationColorForColorName:kTSThread_DefaultConversationColorName]; return [self conversationColorForColorName:kConversationColorName_Default];
} }
@end @end

@ -29,7 +29,7 @@ typedef void (^OWSAvatarDrawBlock)(CGContextRef context);
OWSAvatarBuilder *avatarBuilder; OWSAvatarBuilder *avatarBuilder;
if ([thread isKindOfClass:[TSContactThread class]]) { if ([thread isKindOfClass:[TSContactThread class]]) {
TSContactThread *contactThread = (TSContactThread *)thread; TSContactThread *contactThread = (TSContactThread *)thread;
NSString *colorName = thread.conversationColorName; ConversationColorName colorName = thread.conversationColorName;
avatarBuilder = [[OWSContactAvatarBuilder alloc] initWithSignalId:contactThread.contactIdentifier avatarBuilder = [[OWSContactAvatarBuilder alloc] initWithSignalId:contactThread.contactIdentifier
colorName:colorName colorName:colorName
diameter:diameter]; diameter:diameter];

@ -3,6 +3,7 @@
// //
#import "OWSAvatarBuilder.h" #import "OWSAvatarBuilder.h"
#import <SignalServiceKit/TSThread.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -13,7 +14,9 @@ NS_ASSUME_NONNULL_BEGIN
/** /**
* Build an avatar for a Signal recipient * Build an avatar for a Signal recipient
*/ */
- (instancetype)initWithSignalId:(NSString *)signalId colorName:(NSString *)colorName diameter:(NSUInteger)diameter; - (instancetype)initWithSignalId:(NSString *)signalId
colorName:(ConversationColorName)colorName
diameter:(NSUInteger)diameter;
/** /**
* Build an avatar for a non-Signal recipient * Build an avatar for a non-Signal recipient

@ -18,7 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSString *signalId; @property (nonatomic, readonly) NSString *signalId;
@property (nonatomic, readonly) NSString *contactName; @property (nonatomic, readonly) NSString *contactName;
@property (nonatomic, readonly) NSString *colorName; @property (nonatomic, readonly) ConversationColorName colorName;
@property (nonatomic, readonly) NSUInteger diameter; @property (nonatomic, readonly) NSUInteger diameter;
@end @end
@ -29,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithContactId:(NSString *)contactId - (instancetype)initWithContactId:(NSString *)contactId
name:(NSString *)name name:(NSString *)name
colorName:(NSString *)colorName colorName:(ConversationColorName)colorName
diameter:(NSUInteger)diameter diameter:(NSUInteger)diameter
{ {
self = [super init]; self = [super init];
@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN
} }
- (instancetype)initWithSignalId:(NSString *)signalId - (instancetype)initWithSignalId:(NSString *)signalId
colorName:(NSString *)colorName colorName:(ConversationColorName)colorName
diameter:(NSUInteger)diameter diameter:(NSUInteger)diameter
{ {
// Name for avatar initials. // Name for avatar initials.
@ -66,7 +66,7 @@ NS_ASSUME_NONNULL_BEGIN
colorSeed:(NSString *)colorSeed colorSeed:(NSString *)colorSeed
diameter:(NSUInteger)diameter diameter:(NSUInteger)diameter
{ {
NSString *colorName = [TSThread stableColorNameForNewConversationWithString:colorSeed]; ConversationColorName 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];
} }
@ -76,7 +76,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssertDebug(localNumber.length > 0); OWSAssertDebug(localNumber.length > 0);
OWSAssertDebug(diameter > 0); OWSAssertDebug(diameter > 0);
return [self initWithSignalId:localNumber colorName:kTSThread_DefaultConversationColorName diameter:diameter]; return [self initWithSignalId:localNumber colorName:kConversationColorName_Default diameter:diameter];
} }
#pragma mark - Dependencies #pragma mark - Dependencies

@ -10,7 +10,21 @@ NS_ASSUME_NONNULL_BEGIN
@class TSInteraction; @class TSInteraction;
@class TSInvalidIdentityKeyReceivingErrorMessage; @class TSInvalidIdentityKeyReceivingErrorMessage;
extern NSString *const kTSThread_DefaultConversationColorName; typedef NSString *ConversationColorName NS_STRING_ENUM;
extern ConversationColorName const ConversationColorNameCrimson;
extern ConversationColorName const ConversationColorNameVermilion;
extern ConversationColorName const ConversationColorNameBurlap;
extern ConversationColorName const ConversationColorNameForest;
extern ConversationColorName const ConversationColorNameWintergreen;
extern ConversationColorName const ConversationColorNameTeal;
extern ConversationColorName const ConversationColorNameBlue;
extern ConversationColorName const ConversationColorNameIndigo;
extern ConversationColorName const ConversationColorNameViolet;
extern ConversationColorName const ConversationColorNamePlum;
extern ConversationColorName const ConversationColorNameTaupe;
extern ConversationColorName const ConversationColorNameSteel;
extern ConversationColorName const kConversationColorName_Default;
/** /**
* TSThread is the superclass of TSContactThread and TSGroupThread * TSThread is the superclass of TSContactThread and TSGroupThread
@ -34,11 +48,12 @@ extern NSString *const kTSThread_DefaultConversationColorName;
*/ */
- (NSString *)name; - (NSString *)name;
@property (nonatomic, readonly) NSString *conversationColorName; @property (nonatomic, readonly) ConversationColorName conversationColorName;
- (void)updateConversationColorName:(NSString *)colorName transaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateConversationColorName:(ConversationColorName)colorName
+ (NSString *)stableColorNameForNewConversationWithString:(NSString *)colorSeed; transaction:(YapDatabaseReadWriteTransaction *)transaction;
@property (class, nonatomic, readonly) NSArray<NSString *> *conversationColorNames; + (ConversationColorName)stableColorNameForNewConversationWithString:(NSString *)colorSeed;
@property (class, nonatomic, readonly) NSArray<ConversationColorName> *conversationColorNames;
/** /**
* @returns * @returns

@ -19,7 +19,20 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSString *const kTSThread_DefaultConversationColorName = @"steel"; ConversationColorName const ConversationColorNameCrimson = @"red";
ConversationColorName const ConversationColorNameVermilion = @"orange";
ConversationColorName const ConversationColorNameBurlap = @"brown";
ConversationColorName const ConversationColorNameForest = @"green";
ConversationColorName const ConversationColorNameWintergreen = @"light_green";
ConversationColorName const ConversationColorNameTeal = @"teal";
ConversationColorName const ConversationColorNameBlue = @"blue";
ConversationColorName const ConversationColorNameIndigo = @"indigo";
ConversationColorName const ConversationColorNameViolet = @"purple";
ConversationColorName const ConversationColorNamePlum = @"pink";
ConversationColorName const ConversationColorNameTaupe = @"blue_grey";
ConversationColorName const ConversationColorNameSteel = @"grey";
ConversationColorName const kConversationColorName_Default = ConversationColorNameSteel;
@interface TSThread () @interface TSThread ()
@ -78,19 +91,26 @@ NSString *const kTSThread_DefaultConversationColorName = @"steel";
} }
// To be consistent with colors synced to desktop // To be consistent with colors synced to desktop
NSString *colorName = [self.class stableColorNameForLegacyConversationWithString:colorSeed]; ConversationColorName colorName = [self.class stableColorNameForLegacyConversationWithString:colorSeed];
OWSAssertDebug(colorName); OWSAssertDebug(colorName);
_conversationColorName = colorName; _conversationColorName = colorName;
} else if (![[[self class] conversationColorNames] containsObject:_conversationColorName]) { } else if (![[[self class] conversationColorNames] containsObject:_conversationColorName]) {
// If we'd persisted a non-mapped color name // If we'd persisted a non-mapped color name
NSString *_Nullable mappedColorName = self.class.legacyConversationColorMap[_conversationColorName]; ConversationColorName _Nullable mappedColorName = self.class.legacyConversationColorMap[_conversationColorName];
if (mappedColorName) {
_conversationColorName = mappedColorName; if (!mappedColorName) {
} else { // We previously used the wrong values for the new colors, it's possible we persited them.
// map them to the proper value
mappedColorName = self.class.legacyFixupConversationColorMap[_conversationColorName];
}
if (!mappedColorName) {
OWSFailDebug(@"failure: unexpected unmappable conversationColorName: %@", _conversationColorName); OWSFailDebug(@"failure: unexpected unmappable conversationColorName: %@", _conversationColorName);
_conversationColorName = kTSThread_DefaultConversationColorName; mappedColorName = kConversationColorName_Default;
} }
_conversationColorName = mappedColorName;
} }
return self; return self;
@ -456,30 +476,37 @@ NSString *const kTSThread_DefaultConversationColorName = @"steel";
#pragma mark - Conversation Color #pragma mark - Conversation Color
+ (NSArray<NSString *> *)colorNamesForNewConversation - (ConversationColorName)conversationColorName
{
OWSAssertDebug([self.class.conversationColorNames containsObject:_conversationColorName]);
return _conversationColorName;
}
+ (NSArray<ConversationColorName> *)colorNamesForNewConversation
{ {
// all conversation colors except "steel" // all conversation colors except "steel"
return @[ return @[
@"crimson", ConversationColorNameCrimson,
@"vermilion", ConversationColorNameVermilion,
@"burlap", ConversationColorNameBurlap,
@"forest", ConversationColorNameForest,
@"wintergreen", ConversationColorNameWintergreen,
@"teal", ConversationColorNameTeal,
@"blue", ConversationColorNameBlue,
@"indigo", ConversationColorNameIndigo,
@"violet", ConversationColorNameViolet,
@"plum", ConversationColorNamePlum,
@"taupe" ConversationColorNameTaupe,
]; ];
} }
+ (NSArray<NSString *> *)conversationColorNames + (NSArray<ConversationColorName> *)conversationColorNames
{ {
return [self.colorNamesForNewConversation arrayByAddingObject:kTSThread_DefaultConversationColorName]; return [self.colorNamesForNewConversation arrayByAddingObject:kConversationColorName_Default];
} }
+ (NSString *)stableConversationColorNameForString:(NSString *)colorSeed colorNames:(NSArray<NSString *> *)colorNames + (ConversationColorName)stableConversationColorNameForString:(NSString *)colorSeed
colorNames:(NSArray<ConversationColorName> *)colorNames
{ {
NSData *contactData = [colorSeed dataUsingEncoding:NSUTF8StringEncoding]; NSData *contactData = [colorSeed dataUsingEncoding:NSUTF8StringEncoding];
@ -496,22 +523,22 @@ NSString *const kTSThread_DefaultConversationColorName = @"steel";
return [colorNames objectAtIndex:index]; return [colorNames objectAtIndex:index];
} }
+ (NSString *)stableColorNameForNewConversationWithString:(NSString *)colorSeed + (ConversationColorName)stableColorNameForNewConversationWithString:(NSString *)colorSeed
{ {
return [self stableConversationColorNameForString:colorSeed colorNames:self.colorNamesForNewConversation]; 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 // After introducing new conversation colors, we want to try to maintain as close as possible to the old color for an
// existing thread. // existing thread.
+ (NSString *)stableColorNameForLegacyConversationWithString:(NSString *)colorSeed + (ConversationColorName)stableColorNameForLegacyConversationWithString:(NSString *)colorSeed
{ {
NSString *legacyColorName = NSString *legacyColorName =
[self stableConversationColorNameForString:colorSeed colorNames:self.legacyConversationColorNames]; [self stableConversationColorNameForString:colorSeed colorNames:self.legacyConversationColorNames];
NSString *mappedColorName = self.class.legacyConversationColorMap[legacyColorName]; ConversationColorName _Nullable mappedColorName = self.class.legacyConversationColorMap[legacyColorName];
if (!mappedColorName) { if (!mappedColorName) {
OWSFailDebug(@"failure: unexpected unmappable legacyColorName: %@", legacyColorName); OWSFailDebug(@"failure: unexpected unmappable legacyColorName: %@", legacyColorName);
return kTSThread_DefaultConversationColorName; return kConversationColorName_Default;
} }
return mappedColorName; return mappedColorName;
@ -533,38 +560,64 @@ NSString *const kTSThread_DefaultConversationColorName = @"steel";
]; ];
} }
+ (NSDictionary<NSString *, NSString *> *)legacyConversationColorMap + (NSDictionary<NSString *, ConversationColorName> *)legacyConversationColorMap
{
static NSDictionary<NSString *, ConversationColorName> *colorMap;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
colorMap = @{
@"red" : ConversationColorNameCrimson,
@"deep_orange" : ConversationColorNameCrimson,
@"orange" : ConversationColorNameVermilion,
@"amber" : ConversationColorNameVermilion,
@"brown" : ConversationColorNameBurlap,
@"yellow" : ConversationColorNameBurlap,
@"pink" : ConversationColorNamePlum,
@"purple" : ConversationColorNameViolet,
@"deep_purple" : ConversationColorNameViolet,
@"indigo" : ConversationColorNameIndigo,
@"blue" : ConversationColorNameBlue,
@"light_blue" : ConversationColorNameBlue,
@"cyan" : ConversationColorNameTeal,
@"teal" : ConversationColorNameTeal,
@"green" : ConversationColorNameForest,
@"light_green" : ConversationColorNameWintergreen,
@"lime" : ConversationColorNameWintergreen,
@"blue_grey" : ConversationColorNameTaupe,
@"grey" : ConversationColorNameSteel,
};
});
return colorMap;
}
// we temporarily used the wrong value for the new color names.
+ (NSDictionary<NSString *, ConversationColorName> *)legacyFixupConversationColorMap
{ {
static NSDictionary<NSString *, NSString *> *colorMap; static NSDictionary<NSString *, ConversationColorName> *colorMap;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
colorMap = @{ colorMap = @{
@"red" : @"crimson", @"crimson" : ConversationColorNameCrimson,
@"deep_orange" : @"crimson", @"vermilion" : ConversationColorNameVermilion,
@"orange" : @"vermilion", @"burlap" : ConversationColorNameBurlap,
@"amber" : @"vermilion", @"forest" : ConversationColorNameForest,
@"brown" : @"burlap", @"wintergreen" : ConversationColorNameWintergreen,
@"yellow" : @"burlap", @"teal" : ConversationColorNameTeal,
@"pink" : @"plum", @"blue" : ConversationColorNameBlue,
@"purple" : @"violet", @"indigo" : ConversationColorNameIndigo,
@"deep_purple" : @"violet", @"violet" : ConversationColorNameViolet,
@"indigo" : @"indigo", @"plum" : ConversationColorNamePlum,
@"blue" : @"blue", @"taupe" : ConversationColorNameTaupe,
@"light_blue" : @"blue", @"steel" : ConversationColorNameSteel,
@"cyan" : @"teal",
@"teal" : @"teal",
@"green" : @"forest",
@"light_green" : @"wintergreen",
@"lime" : @"wintergreen",
@"blue_grey" : @"taupe",
@"grey" : @"steel",
}; };
}); });
return colorMap; return colorMap;
} }
- (void)updateConversationColorName:(NSString *)colorName transaction:(YapDatabaseReadWriteTransaction *)transaction - (void)updateConversationColorName:(ConversationColorName)colorName
transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(TSThread *thread) { changeBlock:^(TSThread *thread) {

@ -44,7 +44,7 @@ extern NSString *const TSGroupThread_NotificationKey_UniqueId;
- (void)fireAvatarChangedNotification; - (void)fireAvatarChangedNotification;
+ (NSString *)defaultConversationColorNameForGroupId:(NSData *)groupId; + (ConversationColorName)defaultConversationColorNameForGroupId:(NSData *)groupId;
@end @end

@ -242,7 +242,7 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific
userInfo:userInfo]; userInfo:userInfo];
} }
+ (NSString *)defaultConversationColorNameForGroupId:(NSData *)groupId + (ConversationColorName)defaultConversationColorNameForGroupId:(NSData *)groupId
{ {
OWSAssertDebug(groupId.length > 0); OWSAssertDebug(groupId.length > 0);

@ -27,7 +27,6 @@ NS_ASSUME_NONNULL_BEGIN
[groupBuilder setName:group.groupName]; [groupBuilder setName:group.groupName];
[groupBuilder setMembers:group.groupMemberIds]; [groupBuilder setMembers:group.groupMemberIds];
[groupBuilder setColor:groupThread.conversationColorName]; [groupBuilder setColor:groupThread.conversationColorName];
OWSAssertDebug([TSThread.conversationColorNames containsObject:groupThread.conversationColorName]);
if ([OWSBlockingManager.sharedManager isGroupIdBlocked:group.groupId]) { if ([OWSBlockingManager.sharedManager isGroupIdBlocked:group.groupId]) {
[groupBuilder setBlocked:YES]; [groupBuilder setBlocked:YES];

@ -108,7 +108,6 @@ NS_ASSUME_NONNULL_BEGIN
conversationColorName = [TSThread stableColorNameForNewConversationWithString:signalAccount.recipientId]; conversationColorName = [TSThread stableColorNameForNewConversationWithString:signalAccount.recipientId];
} }
OWSAssertDebug([TSThread.conversationColorNames containsObject:conversationColorName]);
[contactsOutputStream writeSignalAccount:signalAccount [contactsOutputStream writeSignalAccount:signalAccount
recipientIdentity:recipientIdentity recipientIdentity:recipientIdentity
profileKeyData:profileKeyData profileKeyData:profileKeyData

Loading…
Cancel
Save