Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add isMine prop in Message, standalone 2 TableViewCells

  • Loading branch information...
commit f01d089314ac7dcf0614c37cb72e71ee593464df 1 parent 4bb31c8
@between40and2 authored
View
6 AcaniChat.xcodeproj/project.pbxproj
@@ -10,6 +10,7 @@
025167CF14C3BA1F00D2A951 /* ChatBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167CE14C3BA1F00D2A951 /* ChatBar.m */; };
025167D514C3CCE000D2A951 /* Defines.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167D414C3CCDF00D2A951 /* Defines.m */; };
025167D914C3F53800D2A951 /* TVCell_Date.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167D814C3F53800D2A951 /* TVCell_Date.m */; };
+ 025167DC14C4025C00D2A951 /* TVCell_Message.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167DB14C4025C00D2A951 /* TVCell_Message.m */; };
363AE19613EC32A000F3C4B1 /* ChatBubbleGray@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 363AE19413EC32A000F3C4B1 /* ChatBubbleGray@2x.png */; };
363AE19713EC32A000F3C4B1 /* ChatBubbleGreen@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 363AE19513EC32A000F3C4B1 /* ChatBubbleGreen@2x.png */; };
C92BF85013631710003B20F6 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C92BF84F13631710003B20F6 /* UIKit.framework */; };
@@ -39,6 +40,8 @@
025167D414C3CCDF00D2A951 /* Defines.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Defines.m; sourceTree = "<group>"; };
025167D714C3F53800D2A951 /* TVCell_Date.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TVCell_Date.h; path = TableViewCells/TVCell_Date.h; sourceTree = "<group>"; };
025167D814C3F53800D2A951 /* TVCell_Date.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TVCell_Date.m; path = TableViewCells/TVCell_Date.m; sourceTree = "<group>"; };
+ 025167DA14C4025C00D2A951 /* TVCell_Message.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TVCell_Message.h; path = TableViewCells/TVCell_Message.h; sourceTree = "<group>"; };
+ 025167DB14C4025C00D2A951 /* TVCell_Message.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TVCell_Message.m; path = TableViewCells/TVCell_Message.m; sourceTree = "<group>"; };
363AE19413EC32A000F3C4B1 /* ChatBubbleGray@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ChatBubbleGray@2x.png"; sourceTree = "<group>"; };
363AE19513EC32A000F3C4B1 /* ChatBubbleGreen@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ChatBubbleGreen@2x.png"; sourceTree = "<group>"; };
C92BF84B13631710003B20F6 /* AcaniChat.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AcaniChat.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -94,6 +97,8 @@
children = (
025167D714C3F53800D2A951 /* TVCell_Date.h */,
025167D814C3F53800D2A951 /* TVCell_Date.m */,
+ 025167DA14C4025C00D2A951 /* TVCell_Message.h */,
+ 025167DB14C4025C00D2A951 /* TVCell_Message.m */,
);
name = TableViewCells;
sourceTree = "<group>";
@@ -280,6 +285,7 @@
025167CF14C3BA1F00D2A951 /* ChatBar.m in Sources */,
025167D514C3CCE000D2A951 /* Defines.m in Sources */,
025167D914C3F53800D2A951 /* TVCell_Date.m in Sources */,
+ 025167DC14C4025C00D2A951 /* TVCell_Message.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
129 Application/Controllers/ChatViewController.m
@@ -4,11 +4,9 @@
#import "NSString+Additions.h"
-// Exact same color as native iPhone Messages app.
-// Achieved by taking a screen shot of the iPhone by pressing Home & Sleep buttons together.
-// Then, emailed the image to myself and used Mac's native DigitalColor Meter app.
-// Same => [UIColor colorWithRed:219.0/255.0 green:226.0/255.0 blue:237.0/255.0 alpha:1.0];
-#define CHAT_BACKGROUND_COLOR [UIColor colorWithRed:0.859f green:0.886f blue:0.929f alpha:1.0f]
+#import "TVCell_Date.h"
+#import "TVCell_Message.h"
+
#define ClearConversationButtonIndex 0
@@ -33,7 +31,6 @@
#define SECONDS_BETWEEN_MESSAGES (60*15)
-
@implementation ChatViewController
@synthesize receiveMessageSound;
@@ -336,6 +333,10 @@ - (NSUInteger)addMessage:(Message *)message
[cellMap addObject:message];
+ message.isMine = [NSNumber numberWithBool: (([cellMap count] %3) == 0)];
+
+// [message.managedObjectContext save: nil];
+
return numberOfObjectsAdded;
}
@@ -412,16 +413,12 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
return [cellMap count];
}
-#define SENT_DATE_TAG 101
-#define TEXT_TAG 102
-#define BACKGROUND_TAG 103
-
static NSString *kMessageCell = @"MessageCell";
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UILabel *msgSentDate;
+
UIImageView *msgBackground;
UILabel *msgText;
@@ -433,110 +430,34 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
// Handle sentDate (NSDate).
if ([object isKindOfClass:[NSDate class]]) {
static NSString *kSentDateCellId = @"SentDateCell";
- cell = [tableView dequeueReusableCellWithIdentifier:kSentDateCellId];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier:kSentDateCellId] autorelease];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
-
- // Create message sentDate lable
- msgSentDate = [[UILabel alloc] initWithFrame:
- CGRectMake(-2.0f, 0.0f,
- tableView.frame.size.width, kSentDateFontSize+5.0f)];
- msgSentDate.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- msgSentDate.clearsContextBeforeDrawing = NO;
- msgSentDate.tag = SENT_DATE_TAG;
- msgSentDate.font = [UIFont boldSystemFontOfSize:kSentDateFontSize];
- msgSentDate.lineBreakMode = UILineBreakModeTailTruncation;
- msgSentDate.textAlignment = UITextAlignmentCenter;
- msgSentDate.backgroundColor = CHAT_BACKGROUND_COLOR; // clearColor slows performance
- msgSentDate.textColor = [UIColor grayColor];
- [cell addSubview:msgSentDate];
- [msgSentDate release];
-// // Uncomment for view layout debugging.
-// cell.contentView.backgroundColor = [UIColor orangeColor];
-// msgSentDate.backgroundColor = [UIColor orangeColor];
- } else {
- msgSentDate = (UILabel *)[cell viewWithTag:SENT_DATE_TAG];
- }
-
- static NSDateFormatter *dateFormatter = nil;
- if (dateFormatter == nil) {
- dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Jan 1, 2010
- [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; // 1:43 PM
+ TVCell_Date * cell_date = nil;
+ cell_date = [tableView dequeueReusableCellWithIdentifier:kSentDateCellId];
+ if (cell_date == nil) {
- // TODO: Get locale from iPhone system prefs. Then, move this to viewDidAppear.
- NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
- [dateFormatter setLocale:usLocale];
- [usLocale release];
+ cell_date = [[TVCell_Date alloc] initWithReuseIdentifier: kSentDateCellId];
}
- msgSentDate.text = [dateFormatter stringFromDate:(NSDate *)object];
+ cell_date.date = (NSDate *)object;
- return cell;
+ return cell_date;
}
+
// Handle Message object.
- cell = [tableView dequeueReusableCellWithIdentifier:kMessageCell];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier:kMessageCell] autorelease];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
+ TVCell_Message * cell_message;
+ cell_message = [tableView dequeueReusableCellWithIdentifier:kMessageCell];
+ if (cell_message == nil) {
- // Create message background image view
- msgBackground = [[UIImageView alloc] init];
- msgBackground.clearsContextBeforeDrawing = NO;
- msgBackground.tag = BACKGROUND_TAG;
- msgBackground.backgroundColor = CHAT_BACKGROUND_COLOR; // clearColor slows performance
- [cell.contentView addSubview:msgBackground];
- [msgBackground release];
-
- // Create message text label
- msgText = [[UILabel alloc] init];
- msgText.clearsContextBeforeDrawing = NO;
- msgText.tag = TEXT_TAG;
- msgText.backgroundColor = [UIColor clearColor];
- msgText.numberOfLines = 0;
- msgText.lineBreakMode = UILineBreakModeWordWrap;
- msgText.font = [UIFont systemFontOfSize:kMessageFontSize];
- [cell.contentView addSubview:msgText];
- [msgText release];
+ cell_message = [[TVCell_Message alloc] initWithReuseIdentifier: kMessageCell];
+
} else {
- msgBackground = (UIImageView *)[cell.contentView viewWithTag:BACKGROUND_TAG];
- msgText = (UILabel *)[cell.contentView viewWithTag:TEXT_TAG];
+
}
- // Configure the cell to show the message in a bubble. Layout message cell & its subviews.
- CGSize size = [[(Message *)object text] sizeWithFont:[UIFont systemFontOfSize:kMessageFontSize]
- constrainedToSize:CGSizeMake(kMessageTextWidth, CGFLOAT_MAX)
- lineBreakMode:UILineBreakModeWordWrap];
- UIImage *bubbleImage;
- if (!([indexPath row] % 3)) { // right bubble
- CGFloat editWidth = tableView.editing ? 32.0f : 0.0f;
- msgBackground.frame = CGRectMake(tableView.frame.size.width-size.width-34.0f-editWidth,
- kMessageFontSize-13.0f, size.width+34.0f,
- size.height+12.0f);
- bubbleImage = [[UIImage imageNamed:@"ChatBubbleGreen.png"]
- stretchableImageWithLeftCapWidth:15 topCapHeight:13];
- msgText.frame = CGRectMake(tableView.frame.size.width-size.width-22.0f-editWidth,
- kMessageFontSize-9.0f, size.width+5.0f, size.height);
- msgBackground.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- msgText.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
-// // Uncomment for view layout debugging.
-// cell.contentView.backgroundColor = [UIColor blueColor];
- } else { // left bubble
- msgBackground.frame = CGRectMake(0.0f, kMessageFontSize-13.0f,
- size.width+34.0f, size.height+12.0f);
- bubbleImage = [[UIImage imageNamed:@"ChatBubbleGray.png"]
- stretchableImageWithLeftCapWidth:23 topCapHeight:15];
- msgText.frame = CGRectMake(22.0f, kMessageFontSize-9.0f, size.width+5.0f, size.height);
- msgBackground.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
- msgText.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
- }
- msgBackground.image = bubbleImage;
- msgText.text = [(Message *)object text];
+ //[cell_message setMessage: (Message *)object rightward: !([indexPath row] % 3)];
+ cell_message.message = (Message *) object;
+
// Mark message as read.
// Let's instead do this (asynchronously) from loadView and iterate over all messages
if (![(Message *)object read]) { // not read, so save as read
@@ -548,7 +469,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
}
}
- return cell;
+ return cell_message;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
View
6 Application/Controllers/Defines.h
@@ -11,7 +11,11 @@
#define BAR_BUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE\
style:UIBarButtonItemStylePlain target:self action:SELECTOR]
-
+// Exact same color as native iPhone Messages app.
+// Achieved by taking a screen shot of the iPhone by pressing Home & Sleep buttons together.
+// Then, emailed the image to myself and used Mac's native DigitalColor Meter app.
+// Same => [UIColor colorWithRed:219.0/255.0 green:226.0/255.0 blue:237.0/255.0 alpha:1.0];
+#define CHAT_BACKGROUND_COLOR [UIColor colorWithRed:0.859f green:0.886f blue:0.929f alpha:1.0f]
@interface Defines : NSObject
View
BIN  Application/Models/AcaniChat.xcdatamodeld/AcaniChat.xcdatamodel/elements
Binary file not shown
View
BIN  Application/Models/AcaniChat.xcdatamodeld/AcaniChat.xcdatamodel/layout
Binary file not shown
View
2  Application/Models/Message.h
@@ -6,4 +6,6 @@
@property (nonatomic, retain) NSNumber *read;
@property (nonatomic, retain) NSString *text;
+@property (nonatomic, retain) NSNumber * isMine;
+
@end
View
2  Application/Models/Message.m
@@ -6,4 +6,6 @@ @implementation Message
@dynamic read;
@dynamic text;
+@dynamic isMine;
+
@end
View
7 Application/TableViewCells/TVCell_Date.h
@@ -9,9 +9,16 @@
#import <UIKit/UIKit.h>
@interface TVCell_Date : UITableViewCell
+{
+ UILabel *msgSentDate;
+}
@property (nonatomic, retain) NSDate * date;
+
+
@property (nonatomic, retain) NSDateFormatter * dateFormatter;
+- (id) initWithReuseIdentifier:(NSString *)reuseIdentifier;
+
@end
View
59 Application/TableViewCells/TVCell_Date.m
@@ -8,20 +8,66 @@
#import "TVCell_Date.h"
+#import "ChatBar.h"
+#import "Defines.h"
+
+#define SENT_DATE_TAG 101
+
@implementation TVCell_Date
-{
-
-}
@synthesize date = _date;
@synthesize dateFormatter = _dateFormatter;
+- (void) setDate:(NSDate *)date {
+ _date = date;
+
+ msgSentDate.text = [[self dateFormatter] stringFromDate: _date];
+}
+
+- (NSDateFormatter*) dateFormatter {
+// static NSDateFormatter *dateFormatter = nil;
+ if (_dateFormatter == nil) {
+ _dateFormatter = [[NSDateFormatter alloc] init];
+ [_dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Jan 1, 2010
+ [_dateFormatter setTimeStyle:NSDateFormatterShortStyle]; // 1:43 PM
+
+ // TODO: Get locale from iPhone system prefs. Then, move this to viewDidAppear.
+ NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
+ [_dateFormatter setLocale:usLocale];
+ [usLocale release];
+ }
+
+ return _dateFormatter;
+}
+
+- (id) initWithReuseIdentifier:(NSString *)reuseIdentifier {
+ self = [self initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
+
+ return self;
+}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
- // Initialization code
+ self.selectionStyle = UITableViewCellSelectionStyleNone;
+
+ // Create message sentDate lable
+ msgSentDate = [[UILabel alloc] initWithFrame:
+ CGRectMake(-2.0f, 0.0f,
+ self.frame.size.width, kSentDateFontSize+5.0f)];
+ msgSentDate.autoresizingMask = UIViewAutoresizingFlexibleWidth;
+ msgSentDate.clearsContextBeforeDrawing = NO;
+ msgSentDate.tag = SENT_DATE_TAG;
+ msgSentDate.font = [UIFont boldSystemFontOfSize:kSentDateFontSize];
+ msgSentDate.lineBreakMode = UILineBreakModeTailTruncation;
+ msgSentDate.textAlignment = UITextAlignmentCenter;
+ msgSentDate.backgroundColor = CHAT_BACKGROUND_COLOR; // clearColor slows performance
+ msgSentDate.textColor = [UIColor grayColor];
+ [self addSubview:msgSentDate];
+ [msgSentDate release];
+
+
}
return self;
}
@@ -33,4 +79,9 @@ - (void)setSelected:(BOOL)selected animated:(BOOL)animated
// Configure the view for the selected state
}
+- (void) layoutSubviews {
+ [super layoutSubviews];
+
+}
+
@end
View
24 Application/TableViewCells/TVCell_Message.h
@@ -0,0 +1,24 @@
+//
+// TVCell_Message.h
+// AcaniChat
+//
+// Created by Juguang Xiao on 1/16/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "Message.h"
+@interface TVCell_Message : UITableViewCell
+{
+ UIImageView *msgBackground;
+ UILabel *msgText;
+}
+@property (nonatomic, retain) Message * message;
+@property (nonatomic, assign) BOOL rightward;
+
+- (void) setMessage:(Message *)message rightward: (BOOL) rightward;
+
+- (id) initWithReuseIdentifier:(NSString *)reuseIdentifier;
+
+
+@end
View
99 Application/TableViewCells/TVCell_Message.m
@@ -0,0 +1,99 @@
+//
+// TVCell_Message.m
+// AcaniChat
+//
+// Created by Juguang Xiao on 1/16/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "TVCell_Message.h"
+
+#import "ChatBar.h"
+#import "Defines.h"
+
+#define TEXT_TAG 102
+#define BACKGROUND_TAG 103
+
+
+@implementation TVCell_Message
+@synthesize message = _message;
+
+@synthesize rightward = _rightward;
+
+- (void) setMessage:(Message *)message {
+ [self setMessage: message rightward: [message.isMine boolValue]];
+}
+
+- (void) setMessage:(Message *)message rightward: (BOOL) rightward{
+ // Configure the cell to show the message in a bubble. Layout message cell & its subviews.
+ CGSize size = [[message text] sizeWithFont:[UIFont systemFontOfSize:kMessageFontSize]
+ constrainedToSize:CGSizeMake(kMessageTextWidth, CGFLOAT_MAX)
+ lineBreakMode:UILineBreakModeWordWrap];
+ UIImage *bubbleImage;
+ if (rightward) { // right bubble
+ CGFloat editWidth = self.editing ? 32.0f : 0.0f;
+ msgBackground.frame = CGRectMake(self.frame.size.width-size.width-34.0f-editWidth,
+ kMessageFontSize-13.0f, size.width+34.0f,
+ size.height+12.0f);
+ bubbleImage = [[UIImage imageNamed:@"ChatBubbleGreen.png"]
+ stretchableImageWithLeftCapWidth:15 topCapHeight:13];
+ msgText.frame = CGRectMake(self.frame.size.width-size.width-22.0f-editWidth,
+ kMessageFontSize-9.0f, size.width+5.0f, size.height);
+ msgBackground.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
+ msgText.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
+ // // Uncomment for view layout debugging.
+ // cell.contentView.backgroundColor = [UIColor blueColor];
+ } else { // left bubble
+ msgBackground.frame = CGRectMake(0.0f, kMessageFontSize-13.0f,
+ size.width+34.0f, size.height+12.0f);
+ bubbleImage = [[UIImage imageNamed:@"ChatBubbleGray.png"]
+ stretchableImageWithLeftCapWidth:23 topCapHeight:15];
+ msgText.frame = CGRectMake(22.0f, kMessageFontSize-9.0f, size.width+5.0f, size.height);
+ msgBackground.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
+ msgText.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
+ }
+ msgBackground.image = bubbleImage;
+ msgText.text = [message text];
+
+}
+
+- (id) initWithReuseIdentifier:(NSString *)reuseIdentifier{
+ self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier: reuseIdentifier];
+ return self;
+}
+- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
+{
+ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
+ if (self) {
+ self.selectionStyle = UITableViewCellSelectionStyleNone;
+
+ // Create message background image view
+ msgBackground = [[UIImageView alloc] init];
+ msgBackground.clearsContextBeforeDrawing = NO;
+ msgBackground.tag = BACKGROUND_TAG;
+ msgBackground.backgroundColor = CHAT_BACKGROUND_COLOR; // clearColor slows performance
+ [self.contentView addSubview:msgBackground];
+ [msgBackground release];
+
+ // Create message text label
+ msgText = [[UILabel alloc] init];
+ msgText.clearsContextBeforeDrawing = NO;
+ msgText.tag = TEXT_TAG;
+ msgText.backgroundColor = [UIColor clearColor];
+ msgText.numberOfLines = 0;
+ msgText.lineBreakMode = UILineBreakModeWordWrap;
+ msgText.font = [UIFont systemFontOfSize:kMessageFontSize];
+ [self.contentView addSubview:msgText];
+ [msgText release];
+ }
+ return self;
+}
+
+- (void)setSelected:(BOOL)selected animated:(BOOL)animated
+{
+ [super setSelected:selected animated:animated];
+
+ // Configure the view for the selected state
+}
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.