This repository has been archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewController+WMComments.m
304 lines (247 loc) · 9.2 KB
/
UIViewController+WMComments.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
//
// UIViewController+WMComments.m
// WhiteMochaUP
//
// Created by John Liedtke on 7/18/14.
//
//
#import "UIViewController+WMComments.h"
#import "MBProgressHUD.h"
#import "WMCommentsHeaderTableViewCell.h"
#import "WMCommentTableViewCell.h"
#import "WMAddCommentTableViewCell.h"
#import "WMCommentsViewController.h"
#import <objc/runtime.h>
@implementation UIViewController (WMComments)
static char recentCommentsKey;
static char commentParentKey;
static char enableComments;
static char commentCountKey;
- (void)setEnableComments:(NSNumber *)enabled
{
objc_setAssociatedObject(self, &enableComments,
enabled, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)enableComments
{
return objc_getAssociatedObject(self, &enableComments);
}
- (void)setRecentComments:(NSMutableArray *)recentComments
{
objc_setAssociatedObject(self, &recentCommentsKey,
recentComments, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSMutableArray *)recentComments
{
return objc_getAssociatedObject(self, &recentCommentsKey);
}
- (void)setCommentParent:(WMPointer *)parent
{
objc_setAssociatedObject(self, &commentParentKey,parent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)commentCount
{
return objc_getAssociatedObject(self, &commentCountKey);
}
- (void)setCommentCount:(NSNumber *)commentCount
{
objc_setAssociatedObject(self, &commentCountKey,commentCount, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (WMPointer *)parent
{
return objc_getAssociatedObject(self, &commentParentKey);
}
- (void)addComment:(NSString *)comment parent:(PFObject *)parent withSelectors:(NSArray *)selectors;
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Adding comment...";
hud.graceTime = 2.0;
[WMComment addComment:comment parent:parent withBlock:^(BOOL success, NSError *error) {
if (success) {
hud.labelText = @"Sucess! Commend added.";
[hud hide:YES afterDelay:0.5];
[self performSelectorsFromArray:selectors];
} else {
hud.labelText = @"ERROR";
[hud hide:YES afterDelay:0.5];
}
}];
}
- (void)performSelectorsFromArray:(NSArray *)stringSelectors
{
for (NSString *stringSelector in stringSelectors) {
SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@", stringSelector]);
if ([self respondsToSelector:selector]) {
[self performSelector:selector withObject:0];
}
}
}
- (void)fetchComments:(UITableView *)tableView
{
if (![self parent] || ![[self enableComments] boolValue]) return;
// PFQuery *query = [PFQuery queryWithClassName:@"WMComment"];
// [query includeKey:@"user"];
// [query whereKey:@"parent" equalTo:[self parent]];
// query.limit = 3;
// [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// if (!error) {
// if (![self recentComments]) {
// [self setRecentComments:[NSMutableArray array]];
// }
// [[self recentComments] removeAllObjects];
// [[self recentComments] addObjectsFromArray:objects];
// [tableView reloadData];
// } else {
// NSLog(@"Error with comments");
// }
// }];
if (![self recentComments]) {
[self setRecentComments:[NSMutableArray array]];
}
[PFCloud callFunctionInBackground:@"fetchRecentComments" withParameters:@{@"commentPointer" : [self parent].objectId}block:^(NSDictionary *dictionary, NSError *error) {
if (!error) {
[[self recentComments] removeAllObjects];
[[self recentComments] addObjectsFromArray:[dictionary objectForKey:@"comments"]];
[self setCommentCount:[NSNumber numberWithInt:[[dictionary objectForKey:@"count"] intValue]]];
[tableView reloadData];
} else {
NSLog(@"%@", error);
}
}];
}
- (void)deleteComment:(WMComment *)comment withSelectors:(NSArray *)selectors
{
if (!comment || selectors.count == 0 || ![[self enableComments] boolValue]) return;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Deleting comment...";
hud.graceTime = 2.0;
[comment deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
hud.labelText = @"Sucess! Commend deleted.";
[hud hide:YES afterDelay:0.5];
[self performSelectorsFromArray:selectors];
} else {
hud.labelText = @"ERROR DELETING COMMENT";
[hud hide:YES afterDelay:2.0];
}
}];
}
- (void)setUpCommentCells:(UITableView *)tableView
{
if (![[self enableComments] boolValue]) {
return;
}
// Comment Cells
[tableView registerNib:[UINib nibWithNibName:@"WMCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"WMCommentTableViewCell"];
[tableView registerNib:[UINib nibWithNibName:@"WMCommentsHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"WMCommentsHeaderTableViewCell"];
[tableView registerNib:[UINib nibWithNibName:@"WMAddCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"WMAddCommentTableViewCell"];
}
- (NSUInteger)numberOfCommentSections
{
if (![[self enableComments] boolValue]) {
return 0;
}
return 3;
}
- (NSUInteger)numberOfRowsInCommentSection:(UITableView *)tableView inSection:(NSUInteger)section
{
if (![[self enableComments] boolValue]) {
return 0;
}
if ([self isHeaderSection:tableView inSection:section]) {
return 1;
} else if ([self isCommentsSection:tableView inSection:section]) {
return [self recentComments].count;
} else if ([self isAddCommentsSection:tableView inSection:section]) {
return 1;
}
{ return 0; }
}
- (float)heightForCommentRow:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
if (![[self enableComments] boolValue]) {
return 0;
}
int height = 0;
if ([self isHeaderSection:tableView inSection:indexPath.section]) {
height = 45.0;
} else if ([self isCommentsSection:tableView inSection:indexPath.section]) { // Comments
height = [[[self recentComments] objectAtIndex:indexPath.row] commentHeight];
} else if ([self isAddCommentsSection:tableView inSection:indexPath.section]) {
height = 50;
}
return height;
}
- (float)heightForCommentFooter:(UITableView *)tableView inSection:(NSUInteger)section;
{
if (![[self enableComments] boolValue]) {
return 0;
}
if ([self isAddCommentsSection:tableView inSection:section]) {
return 25.0;
}
return 0;
}
- (void)didSelectCommentRow:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
if (![[self enableComments] boolValue]) {
return;
}
if ([self isHeaderSection:tableView inSection:indexPath.section]) {
[self showComments:NO];
} else if ([self isAddCommentsSection:tableView inSection:indexPath.section]) {
[self showComments:YES];
}
}
- (id)setUpCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
if (![[self enableComments] boolValue]) {
return nil;
}
id cell;
if ([self isHeaderSection:tableView inSection:indexPath.section]){
cell = (WMCommentsHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"WMCommentsHeaderTableViewCell" forIndexPath:indexPath];
[[cell titleLabel] setText:[NSString stringWithFormat:@"Comments (%d)", [[self commentCount] intValue]]];
[cell setDelegate:self];
} else if ([self isCommentsSection:tableView inSection:indexPath.section]) {
cell = (WMCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"WMCommentTableViewCell" forIndexPath:indexPath];
[cell setComment:[self recentComments][indexPath.row]];
} else if ([self isAddCommentsSection:tableView inSection:indexPath.section]) {
cell = (WMAddCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"WMAddCommentTableViewCell" forIndexPath:indexPath];
}
return cell;
}
- (void)showComments:(BOOL)showKeyboard
{
if (![[self enableComments] boolValue]) {
return;
}
if ([self parent]) {
WMCommentsViewController *commentsVC = [[WMCommentsViewController alloc] initWithParent:[self parent]];
[commentsVC setParent:[self parent]];
if (showKeyboard) {
[commentsVC.commentBar.textView becomeFirstResponder];
[commentsVC setShowKeyboardOnLoad:YES];
}
[self.navigationController pushViewController:commentsVC animated:YES];
}
}
- (void)viewAllPressed
{
[self showComments:NO];
}
- (BOOL)isHeaderSection:(UITableView *)tableView inSection:(NSUInteger)section
{
return section == [tableView numberOfSections] -3;
}
- (BOOL)isCommentsSection:(UITableView *)tableView inSection:(NSUInteger)section
{
return section == [tableView numberOfSections] -2;
}
- (BOOL)isAddCommentsSection:(UITableView *)tableView inSection:(NSUInteger)section
{
return section == [tableView numberOfSections] -1;
}
@end