-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDTProfileViewController.m
429 lines (357 loc) · 19.5 KB
/
LDTProfileViewController.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// LDTProfileViewController.m
// Lets Do This
//
// Created by Aaron Schachter on 7/9/15.
// Copyright (c) 2015 Do Something. All rights reserved.
//
#import "LDTProfileViewController.h"
#import "LDTTheme.h"
#import "LDTCampaignDetailViewController.h"
#import "LDTSettingsViewController.h"
#import "LDTTabBarController.h"
#import "GAI+LDT.h"
#import "LDTProfileHeaderTableViewCell.h"
#import "LDTProfileCampaignTableViewCell.h"
#import "LDTProfileReportbackItemTableViewCell.h"
#import "LDTProfileNoSignupsTableViewCell.h"
#import "LDTActivityViewController.h"
#import "LDTEpicFailViewController.h"
#import "LDTAppDelegate.h"
@interface LDTProfileViewController ()<UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, LDTProfileHeaderTableViewCellDelegate, LDTProfileCampaignTableViewCellDelegate, LDTReportbackItemDetailViewDelegate, LDTEpicFailSubmitButtonDelegate>
@property (assign, nonatomic) BOOL isCurrentUserProfile;
@property (assign, nonatomic) BOOL isProfileLoaded;
@property (strong, nonatomic) LDTProfileReportbackItemTableViewCell *reportbackItemSizingCell;
@property (strong, nonatomic) NSMutableArray *campaignsDoing;
@property (strong, nonatomic) NSMutableArray *campaignsCompleted;
@property (strong, nonatomic) UIImagePickerController *imagePickerController;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
typedef NS_ENUM(NSInteger, LDTProfileSectionType) {
LDTProfileSectionTypeHeader,
LDTProfileSectionTypeCampaign
};
@implementation LDTProfileViewController
#pragma mark - NSObject
-(instancetype)initWithUser:(DSOUser *)user {
self = [super initWithNibName:@"LDTProfileView" bundle:nil];
if (self) {
_user = user;
}
return self;
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
if ([self.user isLoggedInUser] || !self.user) {
self.user = [DSOUserManager sharedInstance].user;
self.isCurrentUserProfile = YES;
self.isProfileLoaded = YES;
}
else {
self.isProfileLoaded = NO;
}
self.navigationItem.title = nil;
[self styleView];
[self.tableView registerNib:[UINib nibWithNibName:@"LDTProfileHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"headerCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"LDTProfileCampaignTableViewCell" bundle:nil] forCellReuseIdentifier:@"campaignCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"LDTProfileReportbackItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"reportbackItemCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"LDTProfileNoSignupsTableViewCell" bundle:nil] forCellReuseIdentifier:@"noSignupsCell"];
UINib *reportbackItemCellNib = [UINib nibWithNibName:@"LDTProfileReportbackItemTableViewCell" bundle:nil];
self.reportbackItemSizingCell = [[reportbackItemCellNib instantiateWithOwner:nil options:nil] firstObject];
self.tableView.estimatedRowHeight = 400.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
if (self.isCurrentUserProfile) {
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Settings Icon"] style:UIBarButtonItemStylePlain target:self action:@selector(settingsTapped:)];
self.navigationItem.rightBarButtonItem = settingsButton;
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.allowsEditing = YES;
}
else {
[self loadUserProfile];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController styleNavigationBar:LDTNavigationBarStyleClear];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self styleView];
self.navigationController.hidesBarsOnSwipe = YES;
[self.navigationController.barHideOnSwipeGestureRecognizer addTarget:self action:@selector(handleSwipeGestureRecognizer:)];
// Check for scenario where user may have logged out and then logged in as a different user.
if (self.isCurrentUserProfile && ![self.user isLoggedInUser]) {
self.user = [DSOUserManager sharedInstance].user;
}
NSString *trackingString;
if (self.isCurrentUserProfile) {
// Logged in user may have signed up, reported back, or logged in as a different user since this VC was first loaded.
[self.tableView reloadData];
trackingString = @"self";
}
else {
trackingString = self.user.userID;
}
[[GAI sharedInstance] trackScreenView:[NSString stringWithFormat:@"user-profile/%@", trackingString]];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.tableView.contentInset = UIEdgeInsetsMake(0,0,0,0);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
UIView *statusBarBackgroundView = ((LDTAppDelegate *)[UIApplication sharedApplication].delegate).statusBarBackgroundView;
statusBarBackgroundView.backgroundColor = UIColor.whiteColor;
self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
#pragma Mark - LDTUserProfileViewController
- (void)styleView {
self.view.backgroundColor = LDTTheme.ctaBlueColor;
self.tableView.backgroundColor = UIColor.clearColor;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self styleBackBarButton];
UIView *statusBarBackgroundView = ((LDTAppDelegate *)[UIApplication sharedApplication].delegate).statusBarBackgroundView;
statusBarBackgroundView.backgroundColor = LDTTheme.ctaBlueColor;
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
}
- (void)loadUserProfile {
[SVProgressHUD showWithStatus:@"Loading profile..."];
[[DSOUserManager sharedInstance] loadActiveCampaignSignupsForUser:self.user completionHandler:^{
[SVProgressHUD dismiss];
self.isProfileLoaded = YES;
[self.tableView reloadData];
} errorHandler:^(NSError *error) {
[SVProgressHUD dismiss];
// No connection or timeout:
if (error.code == -1001 || error.code == -1009) {
LDTEpicFailViewController *epicFailVC = [[LDTEpicFailViewController alloc] initWithTitle:[error readableTitle] subtitle:[error readableMessage]];
epicFailVC.delegate = self;
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:epicFailVC];
[navVC styleNavigationBar:LDTNavigationBarStyleNormal];
[self presentViewController:navVC animated:YES completion:nil];
}
else {
// @todo: Render specific EpicFail cell instead of NoSignups (GH #620)
// See -tableView:cellForRowAtIndexPath:
self.isProfileLoaded = YES;
[self.tableView reloadData];
}
}];
}
- (void)handleSwipeGestureRecognizer:(UISwipeGestureRecognizer *)recognizer {
[self setNeedsStatusBarAppearanceUpdate];
}
- (IBAction)settingsTapped:(id)sender {
LDTSettingsViewController *destVC = [[LDTSettingsViewController alloc] initWithNibName:@"LDTSettingsView" bundle:nil];
UINavigationController *destNavVC = [[UINavigationController alloc] initWithRootViewController:destVC];
[destNavVC styleNavigationBar:LDTNavigationBarStyleClear];
LDTTabBarController *tabBar = (LDTTabBarController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
[tabBar presentViewController:destNavVC animated:YES completion:nil];
}
- (void)didClickUserAvatarButtonForCell:(LDTProfileHeaderTableViewCell *)cell {
if (!self.isCurrentUserProfile) {
return;
}
[[GAI sharedInstance] trackEventWithCategory:@"account" action:@"change avatar" label:nil value:nil];
UIAlertController *avatarAlertController = [UIAlertController alertControllerWithTitle:@"Set your photo" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAlertAction;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
cameraAlertAction = [UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePickerController animated:YES completion:NULL];
}];
}
else {
cameraAlertAction = [UIAlertAction actionWithTitle:@"(Camera Unavailable)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[avatarAlertController dismissViewControllerAnimated:YES completion:nil];
}];
}
UIAlertAction *photoLibraryAlertAction = [UIAlertAction actionWithTitle:@"Choose From Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imagePickerController animated:YES completion:NULL];
}];
UIAlertAction *cancelAlertAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
[avatarAlertController dismissViewControllerAnimated:YES completion:nil];
}];
[avatarAlertController addAction:cameraAlertAction];
[avatarAlertController addAction:photoLibraryAlertAction];
[avatarAlertController addAction:cancelAlertAction];
[self presentViewController:avatarAlertController animated:YES completion:nil];
}
- (void)configureHeaderCell:(LDTProfileHeaderTableViewCell *)headerCell {
headerCell.delegate = self;
headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
headerCell.userAvatarImage = self.user.photo;
headerCell.userCountryNameText = self.user.countryName.uppercaseString;
headerCell.userDisplayNameText = self.user.displayName.uppercaseString;
}
- (void)configureCampaignCell:(LDTProfileCampaignTableViewCell *)campaignCell campaign:(DSOCampaign *)campaign{
campaignCell.delegate = self;
campaignCell.selectionStyle = UITableViewCellSelectionStyleNone;
campaignCell.campaign = campaign;
campaignCell.campaignTitleButtonTitle = campaign.title;
campaignCell.campaignTaglineText = campaign.tagline;
}
- (void)configureReportbackItemCell:(LDTProfileReportbackItemTableViewCell *)reportbackItemCell indexPath:(NSIndexPath *)indexPath{
reportbackItemCell.selectionStyle = UITableViewCellSelectionStyleNone;
reportbackItemCell.detailView.delegate = self;
DSOCampaignSignup *signup = self.user.campaignSignups[indexPath.row];
DSOReportbackItem *reportbackItem = signup.reportbackItem;
reportbackItemCell.detailView.reportbackItem = reportbackItem;
reportbackItemCell.detailView.campaignButtonTitle = reportbackItem.campaign.title;
BOOL isNewReportbackItem = NO;
if (self.isCurrentUserProfile) {
reportbackItemCell.detailView.displayShareButton = YES;
reportbackItemCell.detailView.shareButtonTitle = @"Share your photo".uppercaseString;
// If reportback was just submitted, we'll have an image property set.
if (reportbackItem.image) {
// Display the image we have in memory (its imageURL hasnt been set yet) -- refs GH issue #669
reportbackItemCell.detailView.reportbackItemImage = reportbackItem.image;
isNewReportbackItem = YES;
}
}
else {
reportbackItemCell.detailView.displayShareButton = NO;
}
if (!isNewReportbackItem) {
reportbackItemCell.detailView.reportbackItemImageURL = reportbackItem.imageURL;
}
reportbackItemCell.detailView.userAvatarImage = self.user.photo;
reportbackItemCell.detailView.userCountryNameLabelText = self.user.countryName;
reportbackItemCell.detailView.userDisplayNameButtonTitle = self.user.displayName;
reportbackItemCell.detailView.captionLabelText = reportbackItem.caption;
reportbackItemCell.detailView.quantityLabelText = [NSString stringWithFormat:@"%li %@ %@", (long)reportbackItem.quantity, reportbackItem.campaign.reportbackNoun, reportbackItem.campaign.reportbackVerb];
}
- (void)configureNoSignupsCell:(LDTProfileNoSignupsTableViewCell *)noSignupsCell {
if (self.user.isLoggedInUser) {
noSignupsCell.titleLabelText = @"You aren't doing anything right now!";
noSignupsCell.subtitleLabelText = @"We have 12 fresh ideas every month, check them out in the Actions tab.";
}
else {
noSignupsCell.titleLabelText = @"Oops, our bad.";
noSignupsCell.subtitleLabelText = @"There was a problem with that request.";
}
}
# pragma mark - LDTProfileCampaignTableViewCellDelegate
- (void)didClickCampaignTitleButtonForCell:(LDTProfileCampaignTableViewCell *)cell {
LDTCampaignDetailViewController *destVC = [[LDTCampaignDetailViewController alloc] initWithCampaign:cell.campaign];
[self.navigationController pushViewController:destVC animated:YES];
}
# pragma mark - LDTReportbackItemDetailViewDelegate
- (void)didClickCampaignTitleButtonForReportbackItemDetailView:(LDTReportbackItemDetailView *)reportbackItemDetailView {
LDTCampaignDetailViewController *destVC = [[LDTCampaignDetailViewController alloc] initWithCampaign:reportbackItemDetailView.reportbackItem.campaign];
[self.navigationController pushViewController:destVC animated:YES];
}
- (void)didClickShareButtonForReportbackItemDetailView:(LDTReportbackItemDetailView *)reportbackItemDetailView {
LDTActivityViewController *sharePhotoActivityViewController = [[LDTActivityViewController alloc] initWithReportbackItem:reportbackItemDetailView.reportbackItem image:reportbackItemDetailView.reportbackItemImage];
[self presentViewController:sharePhotoActivityViewController animated:YES completion:nil];
}
- (void)didClickOnReportbackItemUserForReportbackItemDetailView:(LDTReportbackItemDetailView *)reportbackItemDetailView {
return;
}
#pragma mark - LDTEpicFailSubmitButtonDelegate
- (void)didClickSubmitButton:(LDTEpicFailViewController *)vc {
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
[self loadUserProfile];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *selectedImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:^{
[SVProgressHUD showWithStatus:@"Uploading..."];
[[DSOAPI sharedInstance] postUserAvatarWithUserId:self.user.userID avatarImage:selectedImage completionHandler:^(id responseObject) {
self.user.photo = selectedImage;
// @todo: Could only refresh first section, not entire tableView.
[self.tableView reloadData];
[SVProgressHUD dismiss];
[LDTMessage displaySuccessMessageWithTitle:@"Hey good lookin'." subtitle:@"You've successfully changed your profile photo."];
NSLog(@"Successful user avatar upload: %@", responseObject);
} errorHandler:^(NSError * error) {
[SVProgressHUD dismiss];
[LDTMessage displayErrorMessageForError:error];
}];
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
# pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
viewController.title = @"Select photo".uppercaseString;
[viewController.navigationController styleNavigationBar:LDTNavigationBarStyleNormal];
[viewController styleBackBarButton];
[viewController styleRightBarButton];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == LDTProfileSectionTypeHeader) {
return 1;
}
if (self.isProfileLoaded) {
if (self.user.campaignSignups.count > 0) {
return self.user.campaignSignups.count;
}
return 1;
}
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == LDTProfileSectionTypeHeader) {
LDTProfileHeaderTableViewCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell"];
[self configureHeaderCell:headerCell];
return headerCell;
}
// @todo: If this isn't currentUserProfile and we're viewing someone's profile -- there must be an error, we wouldn't get to their profile unless they had reported back. This would probably be better as an EpicFail cell instead of the noSignupsCell (GH #620)
if (self.user.campaignSignups.count == 0) {
LDTProfileNoSignupsTableViewCell *noSignupsCell = [tableView dequeueReusableCellWithIdentifier:@"noSignupsCell"];
[self configureNoSignupsCell:noSignupsCell];
return noSignupsCell;
}
DSOCampaignSignup *signup = self.user.campaignSignups[indexPath.row];
if (signup.reportbackItem) {
LDTProfileReportbackItemTableViewCell *reportbackItemCell = [tableView dequeueReusableCellWithIdentifier:@"reportbackItemCell"];
[self configureReportbackItemCell:reportbackItemCell indexPath:indexPath];
return reportbackItemCell;
}
else {
DSOCampaign *campaign = [[DSOUserManager sharedInstance] activeCampaignWithId:signup.campaign.campaignID];
LDTProfileCampaignTableViewCell *campaignCell = [tableView dequeueReusableCellWithIdentifier:@"campaignCell"];
[self configureCampaignCell:campaignCell campaign:campaign];
return campaignCell;
}
return nil;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == LDTProfileSectionTypeCampaign) {
if (self.isProfileLoaded && self.user.campaignSignups.count == 0) {
// Create a sizing profileHeader cell to get its height.
UINib *profileHeaderCellNib = [UINib nibWithNibName:@"LDTProfileHeaderTableViewCell" bundle:nil];
LDTProfileHeaderTableViewCell *profileHeaderSizingCell = [[profileHeaderCellNib instantiateWithOwner:nil options:nil] firstObject];
[self configureHeaderCell:profileHeaderSizingCell];
profileHeaderSizingCell.frame = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(profileHeaderSizingCell.frame));
[profileHeaderSizingCell setNeedsLayout];
[profileHeaderSizingCell layoutIfNeeded];
CGFloat profileHeaderHeight = [profileHeaderSizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return self.tableView.bounds.size.height - profileHeaderHeight;
}
DSOCampaignSignup *signup = self.user.campaignSignups[indexPath.row];
if (signup.reportbackItem) {
[self configureReportbackItemCell:self.reportbackItemSizingCell indexPath:indexPath];
CGFloat detailViewHeight = [self.reportbackItemSizingCell.detailView heightForWidth:self.tableView.frame.size.width];
// Add bottom padding / border UIView:
return detailViewHeight + 16;
}
}
return UITableViewAutomaticDimension;
}
@end