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
date:(NSDate *)date
status:(CallStatus)status
displayString:(NSString *)detailString;
displayString:(NSString *)detailString NS_DESIGNATED_INITIALIZER;
- (NSString *)dateText;

@ -9,6 +9,16 @@
#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
callerDisplayName:(NSString *)senderDisplayName
date:(NSDate *)date
@ -19,32 +29,21 @@
NSParameterAssert(senderDisplayName != nil);
self = [super init];
if (self) {
_senderId = [senderId copy];
_senderDisplayName = [senderDisplayName copy];
_date = [date copy];
_status = status;
_messageType = TSCallAdapter;
_detailString = [detailString stringByAppendingFormat:@" "];
if (!self) {
return self;
}
return self;
}
- (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 nil;
}
_senderId = [senderId copy];
_senderDisplayName = [senderDisplayName copy];
_date = [date copy];
_status = status;
_messageType = TSCallAdapter;
- (void)dealloc
{
_senderId = nil;
_senderDisplayName = nil;
_date = nil;
// TODO interpret detailString from status. make sure it works for calls and
// our re-use of calls as group update display
_detailString = [detailString stringByAppendingFormat:@" "];
return self;
}
- (NSString *)dateText
@ -98,14 +97,17 @@
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_senderId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderId))];
_senderDisplayName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderDisplayName))];
_date = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(date))];
_status = (CallStatus)[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(status))];
}
return self;
NSString *senderId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderId))];
NSString *senderDisplayName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderDisplayName))];
NSDate *date = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(date))];
CallStatus status = (CallStatus)[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(status))];
NSString *displayString = @""; // FIXME what should this be?
return [self initWithCallerId:senderId
callerDisplayName:senderDisplayName
date:date
status:status
displayString:displayString];
}
- (void)encodeWithCoder:(NSCoder *)aCoder

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

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

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

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

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

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

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

Loading…
Cancel
Save