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
/
WMCommentsViewController.m
384 lines (278 loc) · 12.4 KB
/
WMCommentsViewController.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// WMCommentsViewController.m
// WhiteMochaUP
//
// Created by John Liedtke on 6/22/14.
//
//
#import "WMCommentsViewController.h"
#import "WMCommentTableViewCell.h"
#import "WMComment.h"
#import "WMCommentBar.h"
#import "MBProgressHUD.h"
#import "UIColor+WMColors.h"
#import "UIViewController+ScrollingNavbar.h"
#import "UIViewController+WMComments.h"
@interface WMCommentsViewController ()
@property (nonatomic, readwrite) UIActionSheet *commentActionSheet;
@property (nonatomic, readwrite) NSIndexPath *actionIndexPath;
@property (nonatomic, readwrite) WMCommentBar *commentBar;
@property (nonatomic, readwrite) UILabel *noCommentsLabel;
@end
@implementation WMCommentsViewController
- (id)initWithParent:(PFObject *)parent
{
self.parent = parent;
self = [self initWithStyle:UITableViewStylePlain];
if (self) {
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
// The className to query on
self.parseClassName = @"WMComment";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 10;
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
UIColor *whiteKind = [[UIColor alloc] initWithRed:238.0/255.0
green:238.0/255
blue:238.0/255
alpha:1.0];
self.tableView.backgroundColor = whiteKind;
self.view.backgroundColor = whiteKind;
self.tableView.separatorColor = [UIColor clearColor];
self.navigationItem.title = @"Comments";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeFirstResponder)
name:UIKeyboardDidShowNotification
object:nil];
//[self followScrollView:self.tableView];
}
return self;
}
-(void)changeFirstResponder
{
[self.commentBar.commentTextViewBar.textView becomeFirstResponder];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)postButtonPressed:(NSString *)comment
{
[self addComment:comment parent:[self parent] withSelectors:@[@"hideKeyboard",@"loadObjects"]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < self.objects.count) {
return [[[self objects] objectAtIndex:indexPath.row] commentHeight];
} else return 70;
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setEnableComments:[NSNumber numberWithBool:YES]];
[self.tableView registerNib:[UINib nibWithNibName:@"WMCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"WMCommentTableViewCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"WMCommentLoadMoreCell" bundle:nil] forCellReuseIdentifier:@"WMCommentLoadMoreCell"];
// Hiding keyboard/toolbar
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:gestureRecognizer];
// Comment Bar
[_commentBar setDelegate:self];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //seconds
[self.tableView addGestureRecognizer:lpgr];
// UIToolbar *toolBar = self.navigationController.toolbar;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO animated:NO];
_commentBar = [[[UINib nibWithNibName:@"WMCommentBar" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
_commentBar.delegate = self;
[self.navigationController.toolbar addSubview:_commentBar];
// Show keyboard
if (_showKeyboardOnLoad) {
[_commentBar.textView becomeFirstResponder];
_showKeyboardOnLoad = NO;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self updateCommentsLabel];
}
- (void)updateCommentsLabel
{
if (self.objects.count == 0 && !self.isLoading) {
[UIView animateWithDuration:2.0 delay:10 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{
[self.view addSubview:_noCommentsLabel = [WMComment noCommentsLabel:CGRectMake(0.0, 150.0, self.view.bounds.size.width, 50)]];
} completion:^(BOOL finished) {
}];
} else {
[_noCommentsLabel removeFromSuperview];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_commentBar.textView resignFirstResponder];
[_commentBar.commentTextViewBar.textView resignFirstResponder];
[self.navigationController setToolbarHidden:YES animated:YES];
[[self.navigationController.toolbar subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self showNavBarAnimated:NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - PFQueryTableViewController
- (void)objectsWillLoad {
[super objectsWillLoad];
// This method is called before a PFQuery is fired to get more objects
}
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
// This method is called every time objects are loaded from Parse via the PFQuery
}
// Override to customize what kind of query to perform on the class. The default is to query for
// all objects ordered by createdAt descending.
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
if (self.parent) {
[query whereKey:@"parent" equalTo:_parent];
} else {
}
[query includeKey:@"user"];
// If Pull To Refresh is enabled, query against the network by default.
if (self.pullToRefreshEnabled) {
query.cachePolicy = kPFCachePolicyNetworkOnly;
}
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
query.cachePolicy = kPFCachePolicyNetworkOnly;
}
[query orderByDescending:@"createdAt"];
return query;
}
// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the textKey in the object,
// and the imageView being the imageKey in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
//static NSString *CellIdentifier = @"Cell";
// PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }
// Configure the cell
WMCommentTableViewCell *newCell = (WMCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"WMCommentTableViewCell"];
[newCell setComment:(WMComment *)object];
if (self.objects.count > 0) {
[self updateCommentsLabel];
}
return newCell;
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
if (_commentActionSheet) {
_commentActionSheet = nil;
} else {
CGPoint location = [longPress locationInView:self.tableView];
_actionIndexPath = [self.tableView indexPathForRowAtPoint:location];
WMComment *commentToDelete = [self objects][_actionIndexPath.row];
if ([commentToDelete.user.objectId isEqualToString:[PFUser currentUser].objectId]) {
_commentActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
// for (UIView *subview in _commentActionSheet.subviews) {
// if ([subview isKindOfClass:[UIButton class]]) {
// UIButton *button = (UIButton *)subview;
// if ([button.titleLabel.text isEqualToString:@"Cancel"]) {
// NSLog(@"wow");
// }
// [button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
// [[button titleLabel] setTextColor:[UIColor purpleColor]];
// }
// }
[_commentActionSheet showFromTabBar:self.tabBarController.tabBar];
}
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
WMComment *commentToDelete = [self objects][_actionIndexPath.row];
[self deleteComment:commentToDelete withSelectors:@[@"loadObjects"]];
}
}
/*
// Override if you need to change the ordering of objects in the table.
- (PFObject *)objectAtIndex:(NSIndexPath *)indexPath {
return [self.objects objectAtIndex:indexPath.row];
}
*/
// Override to customize the look of the cell that allows the user to load the next page of objects.
// The default implementation is a UITableViewCellStyleDefault cell with simple labels.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"WMCommentLoadMoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - UITableViewDataSource
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the object from Parse and reload the table view
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, and save it to Parse
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
NSLog(@"selected");
}
- (void)hideKeyboard
{
[self.commentBar.commentTextViewBar.textView resignFirstResponder];
[self.commentBar.commentTextViewBar.textView endEditing:YES];
[self.commentBar.textView resignFirstResponder];
}
@end