-
Notifications
You must be signed in to change notification settings - Fork 20
/
BRCDetailViewController.m
357 lines (299 loc) · 14.1 KB
/
BRCDetailViewController.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
//
// BRCDetailViewController.m
// iBurn
//
// Created by David Chiles on 7/29/14.
// Copyright (c) 2014 Burning Man Earth. All rights reserved.
//
#import "BRCDetailViewController.h"
#import "BRCDetailCellInfo.h"
#import "BRCRelationshipDetailInfoCell.h"
#import "BRCDataObject.h"
#import "BRCDatabaseManager.h"
#import <MessageUI/MessageUI.h>
#import "RMMapView+iBurn.h"
#import "RMAnnotation+iBurn.h"
#import "RMUserLocation.h"
#import "RMMarker+iBurn.h"
#import <Mapbox_iOS_SDK/Mapbox.h>
#import "BRCDetailInfoTableViewCell.h"
#import "BRCDetailMapViewController.h"
#import "PureLayout.h"
#import "BRCEmbargo.h"
#import "BRCCampObject.h"
#import "BRCEventObject.h"
#import "BRCLocations.h"
#import "BRCAppDelegate.h"
#import <Parse/Parse.h>
#import "PFAnalytics+iBurn.h"
#import "BRCEventRelationshipDetailInfoCell.h"
#import "iBurn-Swift.h"
@import JTSImageViewController;
static CGFloat const kTableViewHeaderHeight = 200;
@interface BRCDetailViewController () <MFMailComposeViewControllerDelegate, RMMapViewDelegate>
@property (nonatomic, strong) BRCDataObject *dataObject;
@property (nonatomic, strong) NSArray *detailCellInfoArray;
@property (nonatomic, strong) UIBarButtonItem *favoriteBarButtonItem;
@property (nonatomic, strong) RMMapView *mapView;
@end
@implementation BRCDetailViewController
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithDataObject:(BRCDataObject *)dataObject
{
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
self.dataObject = dataObject;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(databaseExtensionRegistered:) name:BRCDatabaseExtensionRegisteredNotification object:[BRCDatabaseManager sharedInstance]];
}
return self;
}
- (void) databaseExtensionRegistered:(NSNotification*)notification {
NSString *extensionName = notification.userInfo[@"extensionName"];
if ([extensionName isEqualToString:[BRCDatabaseManager sharedInstance].relationships]) {
NSLog(@"databaseExtensionRegistered: %@", extensionName);
self.dataObject = self.dataObject; // retrigger info array population
[self.tableView reloadData];
}
}
- (void)setDataObject:(BRCDataObject *)dataObject
{
_dataObject = dataObject;
self.title = dataObject.title;
self.detailCellInfoArray = [BRCDetailCellInfo infoArrayForObject:self.dataObject];
[self setupMapViewWithObject:self.dataObject];
[self refreshFavoriteImage];
[self.tableView reloadData];
}
- (void) refreshFavoriteImage {
self.favoriteBarButtonItem.image = [self currentStarImage];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[PFAnalytics brc_trackEventInBackground:@"Detail" object:self.dataObject];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.estimatedRowHeight = 44.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView registerClass:[BRCDetailInfoTableViewCell class] forCellReuseIdentifier:[BRCDetailInfoTableViewCell cellIdentifier]];
if (self.mapView) {
CGRect headerFrame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), kTableViewHeaderHeight);
UIView *clearHeaderView = [[UIView alloc] initWithFrame:headerFrame];
clearHeaderView.backgroundColor = [UIColor clearColor];
CGFloat viewHeight = CGRectGetHeight(self.view.bounds);
self.mapView.frame = CGRectMake(0, -viewHeight+kTableViewHeaderHeight, CGRectGetWidth(self.view.bounds), viewHeight);
[clearHeaderView addSubview:self.mapView];
[clearHeaderView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMapContainerview:)]];
self.tableView.tableHeaderView = clearHeaderView;
}
self.favoriteBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self currentStarImage] style:UIBarButtonItemStylePlain target:self action:@selector(didTapFavorite:)];
self.favoriteBarButtonItem.tintColor = [UIColor colorWithRed: 254./255 green: 110./255 blue: 111./255 alpha: 1.0];
self.navigationItem.rightBarButtonItem = self.favoriteBarButtonItem;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect rect = self.tableView.tableHeaderView.bounds;
[self updateMapViewInRect:rect animated:NO];
}
- (void)updateMapViewInRect:(CGRect)rect animated:(BOOL)animated
{
CLLocation *objectLocation = self.dataObject.location;
if ([BRCEmbargo canShowLocationForObject:self.dataObject] && objectLocation) {
CLLocation *userLocation = self.mapView.userLocation.location;
if (userLocation) {
[self.mapView brc_zoomToIncludeCoordinate:objectLocation.coordinate andCoordinate:userLocation.coordinate inVisibleRect:rect animated:animated];
} else {
[self.mapView brc_zoomToIncludeCoordinate:objectLocation.coordinate andCoordinate:objectLocation.coordinate inVisibleRect:rect animated:animated];
}
} else {
[self.mapView setZoom:14.0 atCoordinate:[BRCLocations blackRockCityCenter] animated:animated];
}
}
- (UIImage*)imageIfFavorite:(BOOL)isFavorite {
UIImage *starImage = nil;
if (isFavorite) {
starImage = [UIImage imageNamed:@"BRCHeartFilledIcon"];
}
else {
starImage = [UIImage imageNamed:@"BRCHeartIcon"];
}
return starImage;
}
- (UIImage *)currentStarImage
{
UIImage *starImage = [self imageIfFavorite:self.dataObject.isFavorite];
return starImage;
}
- (void)didTapFavorite:(id)sender
{
if (!self.dataObject) {
return;
}
BRCDataObject *tempObject = [self.dataObject copy];
tempObject.isFavorite = !tempObject.isFavorite;
self.dataObject = tempObject;
if (self.dataObject.isFavorite) {
[PFAnalytics brc_trackEventInBackground:@"Favorite" object:self.dataObject];
}
[[BRCDatabaseManager sharedInstance].readWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:tempObject forKey:tempObject.uniqueID inCollection:[[tempObject class] collection]];
if ([tempObject isKindOfClass:[BRCEventObject class]]) {
BRCEventObject *event = (BRCEventObject*)tempObject;
[event refreshCalendarEntry:transaction];
}
}];
}
- (void)didTapMapContainerview:(id)sender
{
BRCDetailMapViewController *mapViewController = [[BRCDetailMapViewController alloc] initWithDataObject:self.dataObject];
mapViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:mapViewController animated:YES];
}
- (BRCDetailCellInfo *)cellInfoForIndexPath:(NSInteger)section
{
if ([self.detailCellInfoArray count] > section) {
return self.detailCellInfoArray[section];
}
return nil;
}
- (void)setupMapViewWithObject:(BRCDataObject *)dataObject
{
if (dataObject.location) {
self.mapView = [RMMapView brc_defaultMapViewWithFrame:CGRectMake(0, 0, 10, 150)];
self.mapView.delegate = self;
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
RMAnnotation *annotation = [RMAnnotation brc_annotationWithMapView:self.mapView dataObject:dataObject];
[self.mapView addAnnotation:annotation];
self.mapView.draggingEnabled = NO;
self.mapView.userInteractionEnabled = NO;
}
else {
self.mapView = nil;
}
}
#pragma - mark RMMapviewDelegate Methods
- (RMMapLayer*) mapView:(RMMapView *)mapView layerForAnnotation:(RMAnnotation *)annotation {
if (annotation.isUserLocationAnnotation || ![BRCEmbargo canShowLocationForObject:self.dataObject]) { // show default style
return nil;
}
if ([annotation.userInfo isKindOfClass:[BRCDataObject class]]) {
BRCDataObject *dataObject = annotation.userInfo;
return [RMMarker brc_defaultMarkerForDataObject:dataObject];
}
return nil;
}
#pragma - mark UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
BRCDetailCellInfo *cellInfo = [self cellInfoForIndexPath:indexPath.section];
if (cellInfo.cellType == BRCDetailCellInfoTypeImage) {
return 200;
}
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BRCDetailInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BRCDetailInfoTableViewCell cellIdentifier] forIndexPath:indexPath];
BRCDetailCellInfo *cellInfo = [self cellInfoForIndexPath:indexPath.section];
[cell setDetailCellInfo:cellInfo];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.detailCellInfoArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
BRCDetailCellInfo *cellInfo = [self cellInfoForIndexPath:section];
if (cellInfo) {
BRCDetailCellInfo *cellInfo = self.detailCellInfoArray[section];
title = cellInfo.displayName;
}
return title;
}
#pragma - mark UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BRCDetailCellInfo *cellInfo = [self cellInfoForIndexPath:indexPath.section];
if (cellInfo.cellType == BRCDetailCellInfoTypeURL) {
NSURL *url = cellInfo.value;
[BRCAppDelegate openURL:url fromViewController:self];
}
else if (cellInfo.cellType == BRCDetailCellInfoTypeEmail) {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate = self;
[mailComposeViewController setToRecipients:@[cellInfo.value]];
[self presentViewController:mailComposeViewController animated:YES completion:nil];
}
}
else if(cellInfo.cellType == BRCDetailCellInfoTypeRelationship)
{
// Go to correct camp page
BRCRelationshipDetailInfoCell *relationshipCellInfo = (BRCRelationshipDetailInfoCell *)cellInfo;
__block BRCDataObject *dataObject = nil;
[[BRCDatabaseManager sharedInstance].readConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
dataObject = [transaction objectForKey:relationshipCellInfo.dataObject.uniqueID inCollection:[[relationshipCellInfo.dataObject class]collection]];
}];
BRCDetailViewController *detailVC = [[BRCDetailViewController alloc] initWithDataObject:dataObject];
[self.navigationController pushViewController:detailVC animated:YES];
} else if(cellInfo.cellType == BRCDetailCellInfoTypeEventRelationship) {
// Shows events
BRCEventRelationshipDetailInfoCell *eventRelationshipCellInfo = (BRCEventRelationshipDetailInfoCell *)cellInfo;
BRCDataObject *relatedObject = eventRelationshipCellInfo.dataObject;
BRCEventsViewController *eventsVC = [[BRCEventsViewController alloc] initWithStyle:UITableViewStyleGrouped extensionName:[BRCDatabaseManager sharedInstance].relationships relatedObject:relatedObject];
[self.navigationController pushViewController:eventsVC animated:YES];
} else if (cellInfo.cellType == BRCDetailCellInfoTypeCoordinates) {
CLLocation *location = cellInfo.value;
NSString *coordinatesString = [NSString stringWithFormat:@"%f, %f", location.coordinate.latitude, location.coordinate.longitude];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:@[coordinatesString] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
} else if (cellInfo.cellType == BRCDetailCellInfoTypeImage) {
NSURL *imageURL = cellInfo.value;
UIImage *image = [UIImage imageWithContentsOfFile:imageURL.path];
if (image) {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
// Create image info
JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
imageInfo.image = image;
imageInfo.referenceRect = cell.frame;
imageInfo.referenceView = cell.superview;
// Setup view controller
JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
initWithImageInfo:imageInfo
mode:JTSImageViewControllerMode_Image
backgroundStyle:JTSImageViewControllerBackgroundOption_Scaled];
// Present the view controller.
[imageViewer showFromViewController:self transition:JTSImageViewControllerTransition_FromOriginalPosition];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma - mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat navBarY = CGRectGetMaxY(self.navigationController.navigationBar.frame);
CGFloat contentOffSetY = scrollView.contentOffset.y;
CGFloat headerViewHeight = CGRectGetHeight(self.tableView.tableHeaderView.frame);
CGFloat visibelHeight = headerViewHeight - (navBarY + contentOffSetY);
CGRect mapRect = CGRectZero;
if (visibelHeight > 0) {
mapRect = CGRectMake(0, headerViewHeight - visibelHeight, CGRectGetWidth(self.tableView.tableHeaderView.frame), visibelHeight);
}
self.mapView.frame = mapRect;
[self updateMapViewInRect:mapRect animated:NO];
}
#pragma - mark MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end