Modernize init, dealloc, dicts

* Use NS_DESIGNATE_INTIALIZER to ensure we're setting all the variables we need to be in OWSCall
* no need to nil properties in dealloc on ARC
* use declarative dictionaries for legability

// FREEBIE

use declarative dictionaries for clarity
pull/1/head
Michael Kirk 9 years ago
parent f7f1b6877b
commit 1a4b38e34b

@ -53,7 +53,7 @@ typedef enum : NSUInteger {
callerDisplayName:(NSString *)callerDisplayName callerDisplayName:(NSString *)callerDisplayName
date:(NSDate *)date date:(NSDate *)date
status:(CallStatus)status status:(CallStatus)status
displayString:(NSString *)detailString; displayString:(NSString *)detailString NS_DESIGNATED_INITIALIZER;
- (NSString *)dateText; - (NSString *)dateText;

@ -9,6 +9,16 @@
#pragma mark - Initialzation #pragma mark - Initialzation
- (id)init
{
NSAssert(NO,
@"%s is not a valid initializer for %@. Use %@ instead",
__PRETTY_FUNCTION__,
[self class],
NSStringFromSelector(@selector(initWithCallerId:callerDisplayName:date:status:displayString:)));
return [self initWithCallerId:nil callerDisplayName:nil date:nil status:0 displayString:nil];
}
- (instancetype)initWithCallerId:(NSString *)senderId - (instancetype)initWithCallerId:(NSString *)senderId
callerDisplayName:(NSString *)senderDisplayName callerDisplayName:(NSString *)senderDisplayName
date:(NSDate *)date date:(NSDate *)date
@ -19,32 +29,21 @@
NSParameterAssert(senderDisplayName != nil); NSParameterAssert(senderDisplayName != nil);
self = [super init]; self = [super init];
if (self) { if (!self) {
_senderId = [senderId copy]; return self;
_senderDisplayName = [senderDisplayName copy];
_date = [date copy];
_status = status;
_messageType = TSCallAdapter;
_detailString = [detailString stringByAppendingFormat:@" "];
} }
return self;
}
- (id)init _senderId = [senderId copy];
{ _senderDisplayName = [senderDisplayName copy];
NSAssert(NO, _date = [date copy];
@"%s is not a valid initializer for %@. Use %@ instead", _status = status;
__PRETTY_FUNCTION__, _messageType = TSCallAdapter;
[self class],
NSStringFromSelector(@selector(initWithCallerId:callerDisplayName:date:status:displayString:)));
return nil;
}
- (void)dealloc // TODO interpret detailString from status. make sure it works for calls and
{ // our re-use of calls as group update display
_senderId = nil; _detailString = [detailString stringByAppendingFormat:@" "];
_senderDisplayName = nil;
_date = nil; return self;
} }
- (NSString *)dateText - (NSString *)dateText
@ -98,14 +97,17 @@
- (instancetype)initWithCoder:(NSCoder *)aDecoder - (instancetype)initWithCoder:(NSCoder *)aDecoder
{ {
self = [super init]; NSString *senderId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderId))];
if (self) { NSString *senderDisplayName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderDisplayName))];
_senderId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderId))]; NSDate *date = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(date))];
_senderDisplayName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderDisplayName))]; CallStatus status = (CallStatus)[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(status))];
_date = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(date))]; NSString *displayString = @""; // FIXME what should this be?
_status = (CallStatus)[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(status))];
} return [self initWithCallerId:senderId
return self; callerDisplayName:senderDisplayName
date:date
status:status
displayString:displayString];
} }
- (void)encodeWithCoder:(NSCoder *)aCoder - (void)encodeWithCoder:(NSCoder *)aCoder

@ -21,13 +21,14 @@
date:(NSDate *)date date:(NSDate *)date
{ {
self = [super init]; self = [super init];
if (!self) {
if (self) { return self;
_senderId = [senderId copy];
_senderDisplayName = [senderDisplayName copy];
_date = [date copy];
} }
_senderId = [senderId copy];
_senderDisplayName = [senderDisplayName copy];
_date = [date copy];
return self; return self;
} }

@ -11,12 +11,13 @@
date:(NSDate *)date date:(NSDate *)date
{ {
self = [super initWithSenderId:senderId senderDisplayName:senderDisplayName date:date]; self = [super initWithSenderId:senderId senderDisplayName:senderDisplayName date:date];
if (!self) {
if (self) { return self;
_errorMessageType = messageType;
_messageType = TSErrorMessageAdapter;
} }
_errorMessageType = messageType;
_messageType = TSErrorMessageAdapter;
return self; return self;
} }

@ -23,5 +23,4 @@ typedef NS_ENUM(NSInteger, OWSInfoMessageType) {
- (NSString *)text; - (NSString *)text;
@end @end

@ -14,12 +14,13 @@
//@discussion: NSParameterAssert() ? //@discussion: NSParameterAssert() ?
self = [super initWithSenderId:senderId senderDisplayName:senderDisplayName date:date]; self = [super initWithSenderId:senderId senderDisplayName:senderDisplayName date:date];
if (!self) {
if (self) { return self;
_infoMessageType = messageType;
_messageType = TSInfoMessageAdapter;
} }
_infoMessageType = messageType;
_messageType = TSInfoMessageAdapter;
return self; return self;
} }

@ -826,20 +826,14 @@ typedef enum : NSUInteger {
NSString *allText = call.date != nil ? [text stringByAppendingString:[call dateText]] : text; NSString *allText = call.date != nil ? [text stringByAppendingString:[call dateText]] : text;
UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0f]; UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0f];
UIFont *regularFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
//TODO declarative dict
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:allText NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:allText
attributes:attrs]; attributes:@{ NSFontAttributeName: boldFont }];
if([call date]!=nil) { if([call date]!=nil) {
// Not a group meta message // Not a group meta message
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys: UIFont *regularFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
regularFont, NSFontAttributeName, nil]; const NSRange range = NSMakeRange([text length], [[call dateText] length]);
[attributedText setAttributes:@{ NSFontAttributeName: regularFont }
const NSRange range = NSMakeRange([text length],[[call dateText] length]); range:range];
[attributedText setAttributes:subAttrs range:range];
} }
callCell.cellLabel.attributedText = attributedText; callCell.cellLabel.attributedText = attributedText;
callCell.cellLabel.numberOfLines = 0; // uses as many lines as it needs callCell.cellLabel.numberOfLines = 0; // uses as many lines as it needs

@ -4,17 +4,12 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <JSQMessagesViewController/JSQMessagesCollectionViewCell.h> #import <JSQMessagesViewController/JSQMessagesCollectionViewCell.h>
#define kCallCellHeight 40.0f
#define kCallCellWidth 400.0f
@interface OWSCallCollectionViewCell : JSQMessagesCollectionViewCell @interface OWSCallCollectionViewCell : JSQMessagesCollectionViewCell
// TODO can we use an existing label from JSQMessagesCollectionViewCell?
@property (weak, nonatomic, readonly) JSQMessagesLabel *cellLabel; @property (weak, nonatomic, readonly) JSQMessagesLabel *cellLabel;
@property (weak, nonatomic, readonly) UIImageView *outgoingCallImageView; @property (weak, nonatomic, readonly) UIImageView *outgoingCallImageView;
@property (weak, nonatomic, readonly) UIImageView *incomingCallImageView; @property (weak, nonatomic, readonly) UIImageView *incomingCallImageView;
#pragma mark - Class methods #pragma mark - Class methods
+ (UINib *)nib; + (UINib *)nib;

@ -4,7 +4,6 @@
#import "OWSCallCollectionViewCell.h" #import "OWSCallCollectionViewCell.h"
#import <JSQMessagesViewController/UIView+JSQMessages.h> #import <JSQMessagesViewController/UIView+JSQMessages.h>
@interface OWSCallCollectionViewCell () @interface OWSCallCollectionViewCell ()
@property (weak, nonatomic) IBOutlet JSQMessagesLabel *cellLabel; @property (weak, nonatomic) IBOutlet JSQMessagesLabel *cellLabel;
@ -42,10 +41,6 @@
self.cellLabel.textColor = [UIColor lightGrayColor]; self.cellLabel.textColor = [UIColor lightGrayColor];
} }
- (void)dealloc
{
_cellLabel = nil;
}
#pragma mark - Collection view cell #pragma mark - Collection view cell

Loading…
Cancel
Save