-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathPEPhotosDetailViewController.m
342 lines (290 loc) · 13.3 KB
/
PEPhotosDetailViewController.m
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// PEPhotosDetailViewController.m
// AMBTableViewController
//
// Created by Ernesto Rivera on 2014/05/06.
// Copyright (c) 2014-2017 CyberAgent Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "PEPhotosDetailViewController.h"
#import "PEPost.h"
@implementation PEPhotosDetailViewController
- (void)setPost:(PEPost *)post
{
_post = post;
[self updateAllSections];
[self reloadComments:self];
}
#pragma mark - Sections
- (void)awakeFromNib
{
[super awakeFromNib];
// Setup sections
__weak typeof(self) weakSelf = self;
self.sections = @[// A section with hideable cells
self.topSection,
// A section with a single row of one of two kinds
[AMBTableViewSection
sectionWithObjects:@[@"author_cell"]
sectionUpdateBlock:^(AMBTableViewSection * section)
{
[section reloadObjectAtIndex:0];
}
cellHeightBlock:NULL
cellIdentifierBlock:^NSString *(id object, NSIndexPath *indexPath)
{
BOOL ownPost = [weakSelf.post.authorName isEqualToString:@"Me"];
return ownPost ? @"author_self" : @"author_other";
}
cellConfigurationBlock:^(id object,
UITableViewCell * cell,
NSIndexPath * indexPath)
{
PEPhotosDetailAuthorCell * authorCell = (PEPhotosDetailAuthorCell *)cell;
authorCell.authorLabel.text = weakSelf.post.authorName;
}],
// A section with a single "static" cell hidden when post is nil
[AMBTableViewSection
sectionWithObjects:@[[AMBCellIdentifier identifierFromString:@"write_comment"]]
sectionUpdateBlock:^(AMBTableViewSection * section)
{
section.hidden = (weakSelf.post == nil);
}
cellHeightBlock:NULL
cellIdentifierBlock:NULL
cellConfigurationBlock:NULL],
// A section with a dynamic number of cells of dynamic height and a special "no content cell"
self.commentsSection,
// A section with a single "static" cell of custom height
[AMBTableViewSection
sectionWithObjects:@[[AMBCellIdentifier identifierFromString:@"footer"]]
sectionUpdateBlock:NULL
cellHeightBlock:^CGFloat(id object, NSIndexPath * indexPath) { return 120.0; }
cellIdentifierBlock:NULL
cellConfigurationBlock:NULL]];
[self goToNextPost:self];
}
- (AMBTableViewSection *)topSection
{
if (!_topSection)
{
__weak typeof(self) weakSelf = self;
NSArray * sectionObjects = @[[AMBCellIdentifier identifierFromString:@"title"], // 0
[AMBCellIdentifier identifierFromString:@"image"], // 1
[AMBCellIdentifier identifierFromString:@"tags"], // 2
[AMBCellIdentifier identifierFromString:@"recipe"]]; // 3
_topSection = [AMBTableViewSection
sectionWithObjects:sectionObjects
sectionUpdateBlock:^(AMBTableViewSection *section)
{
[section reloadObjectAtIndex:0];
}
cellHeightBlock:^CGFloat(id object,
NSIndexPath * indexPath)
{
switch ([sectionObjects indexOfObject:object]) // Shouldn't use indexPath.row because we hide/show rows
{
case 0:
return 40.0;
case 1:
return 160.0;
case 3:
return 170.0;
default:
return -1.0; // Table view's default height
}
}
cellIdentifierBlock:NULL
cellConfigurationBlock:^(id object,
UITableViewCell * cell,
NSIndexPath * indexPath)
{
switch ([sectionObjects indexOfObject:object]) // Shouldn't use indexPath.row because we hide/show rows
{
case 0:
{
PEPhotosDetailTitleCell * titleCell = (PEPhotosDetailTitleCell *)cell;
titleCell.titleLabel.text = weakSelf.post.title;
break;
}
case 1:
{
//PEPhotosDetailImageCell * imageCell = (PEPhotosDetailImageCell *)cell;
break;
}
}
}];
// Initial state
[_topSection setObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)]
hidden:YES];
}
return _topSection;
}
- (AMBTableViewSection *)commentsSection
{
if (!_commentsSection)
{
__weak typeof(self) weakSelf = self;
_commentsSection = [AMBTableViewSection
sectionWithObjects:@[[AMBCellIdentifier identifierFromString:@"loading_comments"]]
sectionUpdateBlock:NULL
cellHeightBlock:^CGFloat(id object,
NSIndexPath * indexPath)
{
if ([object isKindOfClass:[AMBCellIdentifier class]])
{
return -1.0; // Loading comments (default height)
}
if (!object)
{
return 88.0; // No content cell
}
// Dynamic height comments
return [weakSelf heightForResizableCellWithIdentifier:@"comment"
text:object
limitedToNumberOfLines:0];
}
cellIdentifierBlock:^NSString *(id object,
NSIndexPath * indexPath)
{
return (object ? @"comment" : // A comment
@"no_comments"); // No content cell
}
cellConfigurationBlock:^(id object,
UITableViewCell * cell,
NSIndexPath * indexPath)
{
if ([cell isKindOfClass:[PEPhotosDetailCommentCell class]] &&
[object isKindOfClass:[NSString class]])
{
((PEPhotosDetailCommentCell *)cell).bodyLabel.text = (NSString *)object;
}
}];
// Enable section title
_commentsSection.sectionTitleBlock = ^NSString *(AMBTableViewSection * section)
{
return @"Comments";
};
// Enable "no content cell"
_commentsSection.presentsNoContentCell = YES;
}
return _commentsSection;
}
#pragma mark - Actions
- (IBAction)goToNextPost:(id)sender
{
PEPost * nextPost = [PEPost new];
nextPost.authorName = [self.post.authorName isEqualToString:@"Me"] ? @"Another author" : @"Me";
static NSUInteger count = 1;
nextPost.title = [NSString stringWithFormat:@"Post %@ by %@", @(count++), nextPost.authorName];
self.post = nextPost;
}
- (IBAction)follow:(id)sender
{
NSLog(@"Follow tapped.");
}
#pragma mark - Toggling details
- (IBAction)toggleDetails:(id)sender
{
// Hide if all hiddeable rows are hidden, show all otherwise
[self.topSection setObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]
hidden:(![self.topSection isObjectAtIndexHidden:1] &&
![self.topSection isObjectAtIndexHidden:2] &&
![self.topSection isObjectAtIndexHidden:3])];
}
- (IBAction)hideDetail:(id)sender
{
NSIndexPath * indexPath = [self indexPathForRowWithSubview:sender];
AMBTableViewSection * section = self.sections[indexPath.section];
id objectToHide = section.visibleObjects[indexPath.row];
[section setObject:objectToHide
hidden:YES];
}
#pragma mark - Handling comments
- (IBAction)reloadComments:(id)sender
{
self.commentsSection.objects = @[[AMBCellIdentifier identifierFromString:@"loading_comments"]];
// Simulate async message loading
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
self.commentsSection.objects = @[@"Message 1",
@"Message 2",
@"Message 3",
[AMBCellIdentifier identifierFromString:@"load_more_comments"]];
});
}
- (IBAction)loadMoreComments:(id)sender
{
id loadingCommentsObject = [AMBCellIdentifier identifierFromString:@"loading_comments"];
[self combineChanges:^
{
// Remove "load more"
[self.commentsSection removeObjectAtIndex:self.commentsSection.objects.count - 1];
// Add "loading"
[self.commentsSection addObject:loadingCommentsObject];
}];
// Simulate async message loading
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
[self combineChanges:^
{
[self.commentsSection removeObject:loadingCommentsObject];
[self.commentsSection addObjects:@[@"Additional Message 1",
@"Additional Message 2",
@"Additional Message 3",
[AMBCellIdentifier identifierFromString:@"load_more_comments"]]];
}];
});
}
- (IBAction)writeComment:(id)sender
{
static NSUInteger count = 1;
NSString * comment = [NSString stringWithFormat:@"My message %@", @(count)];
for (NSUInteger i = 0; i < count; i++)
{
comment = [comment stringByAppendingString:@"\nbla bla bla"];
}
count++;
[self.commentsSection insertObject:comment
atIndex:0];
}
- (IBAction)deleteComment:(UIButton *)sender
{
NSIndexPath * indexPath = [self indexPathForRowWithSubview:sender];
AMBTableViewSection * section = self.sections[indexPath.section];
id objectToDelete = section.visibleObjects[indexPath.row];
NSLog(@"Deleting indexPath: %@ object %@", indexPath, objectToDelete);
// Update UI
[self combineChanges:^
{
[section removeObject:objectToDelete];
// Only "load more" left?
if (section.objects.count == 1)
{
// Remove it as well to let the no content cell appear
[section removeObjectAtIndex:0];
}
}];
}
@end
#pragma mark - Cells
@implementation PEPhotosDetailTitleCell
@end
@implementation PEPhotosDetailImageCell
@end
@implementation PEPhotosDetailAuthorCell
@end
@implementation PEPhotosDetailCommentCell
@synthesize resizableView = _resizableView;
@end