This repository was archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathUIBubbleTableView.m
More file actions
246 lines (191 loc) · 6.96 KB
/
UIBubbleTableView.m
File metadata and controls
246 lines (191 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// UIBubbleTableView.m
//
// Created by Alex Barinov
// Project home page: http://alexbarinov.github.com/UIBubbleTableView/
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
//
#import "UIBubbleTableView.h"
#import "NSBubbleData.h"
#import "UIBubbleHeaderTableViewCell.h"
#import "UIBubbleTypingTableViewCell.h"
@interface UIBubbleTableView ()
@property (nonatomic, retain) NSMutableArray *bubbleSection;
@end
@implementation UIBubbleTableView
@synthesize bubbleDataSource = _bubbleDataSource;
@synthesize snapInterval = _snapInterval;
@synthesize bubbleSection = _bubbleSection;
@synthesize typingBubble = _typingBubble;
@synthesize showAvatars = _showAvatars;
#pragma mark - Initializators
- (void)initializator
{
// UITableView properties
self.backgroundColor = [UIColor clearColor];
self.separatorStyle = UITableViewCellSeparatorStyleNone;
assert(self.style == UITableViewStylePlain);
self.delegate = self;
self.dataSource = self;
// UIBubbleTableView default properties
self.snapInterval = 120;
self.typingBubble = NSBubbleTypingTypeNobody;
}
- (id)init
{
self = [super init];
if (self) [self initializator];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) [self initializator];
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) [self initializator];
return self;
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:UITableViewStylePlain];
if (self) [self initializator];
return self;
}
#if !__has_feature(objc_arc)
- (void)dealloc
{
[_bubbleSection release];
_bubbleSection = nil;
_bubbleDataSource = nil;
[super dealloc];
}
#endif
#pragma mark - Override
- (void)reloadData
{
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
// Cleaning up
self.bubbleSection = nil;
// Loading new data
NSUInteger count = 0;
#if !__has_feature(objc_arc)
self.bubbleSection = [[[NSMutableArray alloc] init] autorelease];
#else
self.bubbleSection = [[NSMutableArray alloc] init];
#endif
if (self.bubbleDataSource && (count = [self.bubbleDataSource rowsForBubbleTable:self]) > 0)
{
#if !__has_feature(objc_arc)
NSMutableArray *bubbleData = [[[NSMutableArray alloc] initWithCapacity:count] autorelease];
#else
NSMutableArray *bubbleData = [[NSMutableArray alloc] initWithCapacity:count];
#endif
for (int i = 0; i < count; i++)
{
NSObject *object = [self.bubbleDataSource bubbleTableView:self dataForRow:i];
assert([object isKindOfClass:[NSBubbleData class]]);
[bubbleData addObject:object];
}
[bubbleData sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSBubbleData *bubbleData1 = (NSBubbleData *)obj1;
NSBubbleData *bubbleData2 = (NSBubbleData *)obj2;
return [bubbleData1.date compare:bubbleData2.date];
}];
NSDate *last = [NSDate dateWithTimeIntervalSince1970:0];
NSMutableArray *currentSection = nil;
for (int i = 0; i < count; i++)
{
NSBubbleData *data = (NSBubbleData *)[bubbleData objectAtIndex:i];
if ([data.date timeIntervalSinceDate:last] > self.snapInterval)
{
#if !__has_feature(objc_arc)
currentSection = [[[NSMutableArray alloc] init] autorelease];
#else
currentSection = [[NSMutableArray alloc] init];
#endif
[self.bubbleSection addObject:currentSection];
}
[currentSection addObject:data];
last = data.date;
}
}
[super reloadData];
}
#pragma mark - UITableViewDelegate implementation
#pragma mark - UITableViewDataSource implementation
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger result = [self.bubbleSection count];
if (self.typingBubble != NSBubbleTypingTypeNobody) result++;
return result;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// This is for now typing bubble
if (section >= [self.bubbleSection count]) return 1;
return [[self.bubbleSection objectAtIndex:section] count] + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Now typing
if (indexPath.section >= [self.bubbleSection count])
{
return MAX([UIBubbleTypingTableViewCell height], self.showAvatars ? 52 : 0);
}
// Header
if (indexPath.row == 0)
{
return [UIBubbleHeaderTableViewCell height];
}
NSBubbleData *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row - 1];
return MAX(data.insets.top + data.view.frame.size.height + data.insets.bottom, self.showAvatars ? 52 : 0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Now typing
if (indexPath.section >= [self.bubbleSection count])
{
static NSString *cellId = @"tblBubbleTypingCell";
UIBubbleTypingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) cell = [[UIBubbleTypingTableViewCell alloc] init];
cell.type = self.typingBubble;
cell.showAvatar = self.showAvatars;
return cell;
}
// Header with date and time
if (indexPath.row == 0)
{
static NSString *cellId = @"tblBubbleHeaderCell";
UIBubbleHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
NSBubbleData *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:0];
if (cell == nil) cell = [[UIBubbleHeaderTableViewCell alloc] init];
cell.date = data.date;
return cell;
}
// Standard bubble
static NSString *cellId = @"tblBubbleCell";
UIBubbleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
NSBubbleData *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row - 1];
if (cell == nil) cell = [[UIBubbleTableViewCell alloc] init];
cell.data = data;
cell.showAvatar = self.showAvatars;
return cell;
}
#pragma mark - Public interface
- (void) scrollBubbleViewToBottomAnimated:(BOOL)animated
{
NSInteger lastSectionIdx = [self numberOfSections] - 1;
if (lastSectionIdx >= 0)
{
[self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([self numberOfRowsInSection:lastSectionIdx] - 1) inSection:lastSectionIdx] atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}
}
@end