Skip to content

Commit 4bbaadf

Browse files
committed
add a new CollectionViewCell type - Aside type
1 parent c81aa93 commit 4bbaadf

10 files changed

+256
-12
lines changed

JSQMessages.xcodeproj/project.pbxproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
88A901B619F618B100F99777 /* JSQMediaItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A901B519F618B100F99777 /* JSQMediaItem.m */; };
106106
88C4583019F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C4582F19F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.m */; };
107107
94A4FA20C2FBD0D62614D5A8 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 97E6750B77E8A7042BA0754B /* libPods.a */; };
108+
BF94DB9C1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.m in Sources */ = {isa = PBXBuildFile; fileRef = BF94DB9B1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.m */; };
109+
BF94DB9E1A0A5B79008E54F6 /* JSQMessagesCollectionViewCellAside.xib in Resources */ = {isa = PBXBuildFile; fileRef = BF94DB9D1A0A5B79008E54F6 /* JSQMessagesCollectionViewCellAside.xib */; };
108110
/* End PBXBuildFile section */
109111

110112
/* Begin PBXContainerItemProxy section */
@@ -279,6 +281,9 @@
279281
88C4582E19F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSQMessagesMediaViewBubbleImageMasker.h; sourceTree = "<group>"; };
280282
88C4582F19F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQMessagesMediaViewBubbleImageMasker.m; sourceTree = "<group>"; };
281283
97E6750B77E8A7042BA0754B /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
284+
BF94DB9A1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSQMessagesCollectionViewCellAside.h; sourceTree = "<group>"; };
285+
BF94DB9B1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQMessagesCollectionViewCellAside.m; sourceTree = "<group>"; };
286+
BF94DB9D1A0A5B79008E54F6 /* JSQMessagesCollectionViewCellAside.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = JSQMessagesCollectionViewCellAside.xib; sourceTree = "<group>"; };
282287
FC5C727E4CCDA2B95A7BA30C /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
283288
/* End PBXFileReference section */
284289

@@ -356,8 +361,8 @@
356361
children = (
357362
886C33FE19F45E30006B4997 /* JSQMessagesViewController.podspec */,
358363
88A25F3E19D8E01A00924534 /* JSQMessagesViewController */,
359-
636A8663AEEE5C37B65C515D /* Frameworks */,
360364
88A25F2B19D8DF2500924534 /* JSQMessagesDemo */,
365+
636A8663AEEE5C37B65C515D /* Frameworks */,
361366
88A25F1E19D8DEC500924534 /* JSQMessagesTests */,
362367
2BBEF3CD91C31A49E5FF9E3C /* Pods */,
363368
88A25F0319D8DEC400924534 /* Products */,
@@ -549,6 +554,9 @@
549554
88A25F9119D8E01A00924534 /* JSQMessagesCollectionViewCellOutgoing.h */,
550555
88A25F9219D8E01A00924534 /* JSQMessagesCollectionViewCellOutgoing.m */,
551556
88A25F9319D8E01A00924534 /* JSQMessagesCollectionViewCellOutgoing.xib */,
557+
BF94DB9A1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.h */,
558+
BF94DB9B1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.m */,
559+
BF94DB9D1A0A5B79008E54F6 /* JSQMessagesCollectionViewCellAside.xib */,
552560
88A25F9419D8E01A00924534 /* JSQMessagesComposerTextView.h */,
553561
88A25F9519D8E01A00924534 /* JSQMessagesComposerTextView.m */,
554562
88A25F9619D8E01A00924534 /* JSQMessagesInputToolbar.h */,
@@ -745,6 +753,7 @@
745753
88A25FBC19D8E01A00924534 /* JSQMessagesViewController.xib in Resources */,
746754
88A25FD819D8E01A00924534 /* JSQMessagesTypingIndicatorFooterView.xib in Resources */,
747755
88A25F3919D8DF2500924534 /* Main.storyboard in Resources */,
756+
BF94DB9E1A0A5B79008E54F6 /* JSQMessagesCollectionViewCellAside.xib in Resources */,
748757
88A25FD419D8E01A00924534 /* JSQMessagesLoadEarlierHeaderView.xib in Resources */,
749758
);
750759
runOnlyForDeploymentPostprocessing = 0;
@@ -856,6 +865,7 @@
856865
886FFD2E19E9A65D00EB8485 /* UIDevice+JSQMessages.m in Sources */,
857866
88A25FB619D8E01A00924534 /* NSString+JSQMessages.m in Sources */,
858867
88A901B619F618B100F99777 /* JSQMediaItem.m in Sources */,
868+
BF94DB9C1A0A5B3B008E54F6 /* JSQMessagesCollectionViewCellAside.m in Sources */,
859869
88A25FCC19D8E01A00924534 /* JSQMessagesCollectionViewCellIncoming.m in Sources */,
860870
88A25FBE19D8E01A00924534 /* JSQMessagesBubbleImageFactory.m in Sources */,
861871
88A25FDF19D8E0C400924534 /* DemoMessagesViewController.m in Sources */,

JSQMessagesDemo/DemoMessagesViewController.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,15 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
346346

347347
JSQMessage *message = [self.demoData.messages objectAtIndex:indexPath.item];
348348

349-
if ([message.senderId isEqualToString:self.senderId]) {
349+
if ([message.senderId length] == 0) {
350+
return nil;
351+
}
352+
else if ([message.senderId isEqualToString:self.senderId]) {
350353
return self.demoData.outgoingBubbleImageData;
351354
}
352-
353-
return self.demoData.incomingBubbleImageData;
355+
else {
356+
return self.demoData.incomingBubbleImageData;
357+
}
354358
}
355359

356360
- (id<JSQMessageAvatarImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
@@ -400,10 +404,10 @@ - (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionVi
400404
*
401405
* Show a timestamp for every 3rd message
402406
*/
403-
if (indexPath.item % 3 == 0) {
404-
JSQMessage *message = [self.demoData.messages objectAtIndex:indexPath.item];
405-
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
406-
}
407+
// if (indexPath.item % 3 == 0) {
408+
// JSQMessage *message = [self.demoData.messages objectAtIndex:indexPath.item];
409+
// return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
410+
// }
407411

408412
return nil;
409413
}
@@ -450,6 +454,9 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
450454
* Override point for customizing cells
451455
*/
452456
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
457+
if ([cell isKindOfClass:[JSQMessagesCollectionViewCellAside class]]) {
458+
cell.textView.backgroundColor = [UIColor blueColor];
459+
}
453460

454461
/**
455462
* Configure almost *anything* on the cell

JSQMessagesDemo/DemoModelData.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ - (void)loadFakeMessages
9898
*
9999
* You should have a mutable array or orderedSet, or something.
100100
*/
101+
JSQPhotoMediaItem *photoItem = [[JSQPhotoMediaItem alloc] initWithImage:[UIImage imageNamed:@"goldengate"]];
102+
JSQMediaMessage *photoMessage = [JSQMediaMessage messageWithSenderId:@""
103+
displayName:@""
104+
media:photoItem];
105+
101106
self.messages = [[NSMutableArray alloc] initWithObjects:
102107
[[JSQTextMessage alloc] initWithSenderId:kJSQDemoAvatarIdSquires
103108
senderDisplayName:kJSQDemoAvatarDisplayNameSquires
@@ -128,6 +133,16 @@ - (void)loadFakeMessages
128133
senderDisplayName:kJSQDemoAvatarDisplayNameSquires
129134
date:[NSDate date]
130135
text:@"Now with media messages!"],
136+
137+
[[JSQTextMessage alloc] initWithSenderId:@""
138+
senderDisplayName:@""
139+
date:[NSDate date]
140+
text:@"This is an aside text!"],
141+
[[JSQTextMessage alloc] initWithSenderId:@""
142+
senderDisplayName:@""
143+
date:[NSDate date]
144+
text:@"It is simple, elegant, and easy to use. There are super sweet default settings, but you can customize like crazy."],
145+
photoMessage,
131146
nil];
132147

133148
[self addPhotoMediaMessage];

JSQMessagesViewController/Controllers/JSQMessagesViewController.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@
143143
*/
144144
@property (copy, nonatomic) NSString *incomingMediaCellIdentifier;
145145

146+
/**
147+
* The collection view cell identifier to use for dequeuing aside message collection view cells
148+
* in the collectionView for text messages.
149+
*
150+
* @discussion This cell identifier is used for aside text message data items.
151+
* The default value is the string returned by `[JSQMessagesCollectionViewCellAside cellReuseIdentifier]`.
152+
* This value must not be `nil`.
153+
*
154+
* @see JSQMessagesCollectionViewCellAside.
155+
*
156+
* @warning Overriding this property's default value is *not* recommended.
157+
* You should only override this property's default value if you are proividing your own cell prototypes.
158+
* These prototypes must be registered with the collectionView for reuse and you are then responsible for
159+
* completely overriding many delegate and data source methods for the collectionView,
160+
* including `collectionView:cellForItemAtIndexPath:`.
161+
*/
162+
@property (copy, nonatomic) NSString *asideCellIdentifier;
163+
164+
/**
165+
* The collection view cell identifier to use for dequeuing aside message collection view cells
166+
* in the collectionView for media messages.
167+
*
168+
* @discussion This cell identifier is used for aside media message data items.
169+
* The default value is the string returned by `[JSQMessagesCollectionViewCellAside mediaCellReuseIdentifier]`.
170+
* This value must not be `nil`.
171+
*
172+
* @see JSQMessagesCollectionViewCellAside.
173+
*
174+
* @warning Overriding this property's default value is *not* recommended.
175+
* You should only override this property's default value if you are proividing your own cell prototypes.
176+
* These prototypes must be registered with the collectionView for reuse and you are then responsible for
177+
* completely overriding many delegate and data source methods for the collectionView,
178+
* including `collectionView:cellForItemAtIndexPath:`.
179+
*/
180+
@property (copy, nonatomic) NSString *asideMediaCellIdentifier;
181+
146182
/**
147183
* Specifies whether or not the view controller should show the typing indicator for an incoming message.
148184
*

JSQMessagesViewController/Controllers/JSQMessagesViewController.m

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#import "JSQMessagesCollectionViewCellIncoming.h"
3030
#import "JSQMessagesCollectionViewCellOutgoing.h"
31+
#import "JSQMessagesCollectionViewCellAside.h"
3132

3233
#import "JSQMessagesTypingIndicatorFooterView.h"
3334
#import "JSQMessagesLoadEarlierHeaderView.h"
@@ -142,6 +143,9 @@ - (void)jsq_configureMessagesViewController
142143
self.incomingCellIdentifier = [JSQMessagesCollectionViewCellIncoming cellReuseIdentifier];
143144
self.incomingMediaCellIdentifier = [JSQMessagesCollectionViewCellIncoming mediaCellReuseIdentifier];
144145

146+
self.asideCellIdentifier = [JSQMessagesCollectionViewCellAside cellReuseIdentifier];
147+
self.asideMediaCellIdentifier = [JSQMessagesCollectionViewCellAside mediaCellReuseIdentifier];
148+
145149
self.showTypingIndicator = NO;
146150

147151
self.showLoadEarlierMessagesHeader = NO;
@@ -172,7 +176,11 @@ - (void)dealloc
172176
_senderId = nil;
173177
_senderDisplayName = nil;
174178
_outgoingCellIdentifier = nil;
179+
_outgoingMediaCellIdentifier = nil;
175180
_incomingCellIdentifier = nil;
181+
_incomingMediaCellIdentifier = nil;
182+
_asideCellIdentifier = nil;
183+
_asideMediaCellIdentifier = nil;
176184

177185
[_keyboardController endListeningForKeyboard];
178186
_keyboardController = nil;
@@ -424,15 +432,20 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
424432
NSString *messageSenderId = [messageItem senderId];
425433
NSParameterAssert(messageSenderId != nil);
426434

435+
BOOL isAsideMessage = [messageSenderId length] == 0;
427436
BOOL isOutgoingMessage = [messageSenderId isEqualToString:self.senderId];
428437
BOOL isMediaMessage = [messageItem isMediaMessage];
429438

430439
NSString *cellIdentifier = nil;
431440
if (isMediaMessage) {
432-
cellIdentifier = isOutgoingMessage ? self.outgoingMediaCellIdentifier : self.incomingMediaCellIdentifier;
441+
cellIdentifier = isAsideMessage ? self.asideMediaCellIdentifier
442+
: (isOutgoingMessage ? self.outgoingMediaCellIdentifier
443+
: self.incomingMediaCellIdentifier);
433444
}
434445
else {
435-
cellIdentifier = isOutgoingMessage ? self.outgoingCellIdentifier : self.incomingCellIdentifier;
446+
cellIdentifier = isAsideMessage ? self.asideCellIdentifier
447+
: (isOutgoingMessage ? self.outgoingCellIdentifier
448+
: self.incomingCellIdentifier);
436449
}
437450

438451
JSQMessagesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
@@ -455,7 +468,10 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
455468
}
456469

457470
BOOL needsAvatar = YES;
458-
if (isOutgoingMessage && CGSizeEqualToSize(collectionView.collectionViewLayout.outgoingAvatarViewSize, CGSizeZero)) {
471+
if (isAsideMessage) {
472+
needsAvatar = NO;
473+
}
474+
else if (isOutgoingMessage && CGSizeEqualToSize(collectionView.collectionViewLayout.outgoingAvatarViewSize, CGSizeZero)) {
459475
needsAvatar = NO;
460476
}
461477
else if (!isOutgoingMessage && CGSizeEqualToSize(collectionView.collectionViewLayout.incomingAvatarViewSize, CGSizeZero)) {
@@ -477,7 +493,10 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
477493

478494
CGFloat bubbleTopLabelInset = (avatarImageDataSource != nil) ? 60.0f : 15.0f;
479495

480-
if (isOutgoingMessage) {
496+
if (isAsideMessage) {
497+
cell.messageBubbleTopLabel.textInsets = UIEdgeInsetsMake(0.0f, bubbleTopLabelInset, 0.0f, bubbleTopLabelInset);
498+
}
499+
else if (isOutgoingMessage) {
481500
cell.messageBubbleTopLabel.textInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, bubbleTopLabelInset);
482501
}
483502
else {

JSQMessagesViewController/JSQMessages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "JSQMessagesCollectionView.h"
2626
#import "JSQMessagesCollectionViewCellIncoming.h"
2727
#import "JSQMessagesCollectionViewCellOutgoing.h"
28+
#import "JSQMessagesCollectionViewCellAside.h"
2829
#import "JSQMessagesTypingIndicatorFooterView.h"
2930
#import "JSQMessagesLoadEarlierHeaderView.h"
3031

JSQMessagesViewController/Views/JSQMessagesCollectionView.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import "JSQMessagesCollectionViewFlowLayout.h"
2222
#import "JSQMessagesCollectionViewCellIncoming.h"
2323
#import "JSQMessagesCollectionViewCellOutgoing.h"
24+
#import "JSQMessagesCollectionViewCellAside.h"
2425

2526
#import "JSQMessagesTypingIndicatorFooterView.h"
2627
#import "JSQMessagesLoadEarlierHeaderView.h"
@@ -54,12 +55,18 @@ - (void)jsq_configureCollectionView
5455
[self registerNib:[JSQMessagesCollectionViewCellOutgoing nib]
5556
forCellWithReuseIdentifier:[JSQMessagesCollectionViewCellOutgoing cellReuseIdentifier]];
5657

58+
[self registerNib:[JSQMessagesCollectionViewCellAside nib]
59+
forCellWithReuseIdentifier:[JSQMessagesCollectionViewCellAside cellReuseIdentifier]];
60+
5761
[self registerNib:[JSQMessagesCollectionViewCellIncoming nib]
5862
forCellWithReuseIdentifier:[JSQMessagesCollectionViewCellIncoming mediaCellReuseIdentifier]];
5963

6064
[self registerNib:[JSQMessagesCollectionViewCellOutgoing nib]
6165
forCellWithReuseIdentifier:[JSQMessagesCollectionViewCellOutgoing mediaCellReuseIdentifier]];
6266

67+
[self registerNib:[JSQMessagesCollectionViewCellAside nib]
68+
forCellWithReuseIdentifier:[JSQMessagesCollectionViewCellAside mediaCellReuseIdentifier]];
69+
6370
[self registerNib:[JSQMessagesTypingIndicatorFooterView nib]
6471
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
6572
withReuseIdentifier:[JSQMessagesTypingIndicatorFooterView footerReuseIdentifier]];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// JSQMessagesCollectionViewCellAside.h
3+
// JSQMessages
4+
//
5+
// Created by Vincent on 14/11/5.
6+
// Copyright (c) 2014年 Hexed Bits. All rights reserved.
7+
//
8+
9+
#import "JSQMessagesCollectionViewCell.h"
10+
11+
/**
12+
* A `JSQMessagesCollectionViewCellAside` object is a concrete instance
13+
* of `JSQMessagesCollectionViewCell` that represents an aside message data item.
14+
*/
15+
@interface JSQMessagesCollectionViewCellAside : JSQMessagesCollectionViewCell
16+
17+
@end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// JSQMessagesCollectionViewCellAside.m
3+
// JSQMessages
4+
//
5+
// Created by Vincent on 14/11/5.
6+
// Copyright (c) 2014年 Hexed Bits. All rights reserved.
7+
//
8+
9+
#import "JSQMessagesCollectionViewCellAside.h"
10+
11+
@implementation JSQMessagesCollectionViewCellAside
12+
13+
#pragma mark - Overrides
14+
15+
- (void)awakeFromNib
16+
{
17+
[super awakeFromNib];
18+
self.messageBubbleTopLabel.textAlignment = NSTextAlignmentLeft;
19+
self.cellBottomLabel.textAlignment = NSTextAlignmentLeft;
20+
}
21+
22+
@end

0 commit comments

Comments
 (0)