This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
FavoritesViewController.m
859 lines (754 loc) · 40.7 KB
/
FavoritesViewController.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
* This project constitutes a work of the United States Government and is
* not subject to domestic copyright protection under 17 USC § 105.
*
* However, because the project utilizes code licensed from contributors
* and other third parties, it therefore is licensed under the MIT
* License. http://opensource.org/licenses/mit-license.php. Under that
* license, permission is granted free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the conditions that any appropriate copyright notices and this
* permission notice are included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//
// FavoritesViewController.m
// WhiteHouse
//
#import "FavoritesViewController.h"
#import "DetailViewController.h"
#import "Post.h"
#import "PostTableCell.h"
#import "PostCollectionCell.h"
#import "UIKit+AFNetworking.h"
#import "ActivityViewCustomActivity.h"
#import "GAI.h"
#import "GAITracker.h"
#import "GAIDictionaryBuilder.h"
#import "GAIFields.h"
#import "AppDelegate.h"
#import "LiveViewController.h"
#import "Social/Social.h"
#import "Constants.h"
@interface FavoritesViewController ()
@property (nonatomic, strong) NSString *favoritesFilePath;
@property (nonatomic, strong) NSIndexPath *colIndex;
@property (nonatomic, assign) const float heightCon;
@property (nonatomic, strong) UIView *baseView;
@property (weak, nonatomic) NSString *favoriteButtonTitle;
@property (weak, nonatomic) UIAlertView *alert;
@end
@implementation FavoritesViewController
Post *post;
BOOL alertShowing = NO;
- (void)viewDidLoad {
[super viewDidLoad];
_noFavoritesView.layer.zPosition = MAXFLOAT;
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self.tblFavorites setDelegate:self];
[self.tblFavorites setDataSource:self];
[self.collectBlogs setDelegate:self];
[self.collectBlogs setDataSource:self];
[self.collectBlogsPlus setDelegate:self];
[self.collectBlogsPlus setDataSource:self];
//self.tblFavorites.estimatedRowHeight = 255.0;
//self.tblFavorites.rowHeight = UITableViewAutomaticDimension;
[_tblFavorites setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
[_collectBlogsPlus setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
[_collectBlogs setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
self.edgesForExtendedLayout = UIRectEdgeNone;
[self createBanner];
_noFavoritesView.hidden = YES;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:true];
_placeholderImageIndex = 0;
[self refreshData];
}
- (void) refreshData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
self.favoritesFilePath = [docDirectory stringByAppendingPathComponent:@"favoritesdata"];
if ([[NSFileManager defaultManager] fileExistsAtPath:self.favoritesFilePath]) {
NSArray *dictsFromFile = [[NSMutableArray alloc] initWithContentsOfFile:self.favoritesFilePath];
NSMutableArray *obList = [[NSMutableArray alloc]init];
for (NSDictionary *dict in dictsFromFile) {
[obList addObject:[Post postFromDictionary:dict]];
}
_favorites = [NSMutableArray arrayWithArray:obList];
[self.tblFavorites reloadData];
_photos = [[NSMutableArray alloc]init];
for (Post *post in _favorites) {
MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:post.mobile2048]];
photo.caption = post.title;
[_photos addObject:photo];
}
}
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.tblFavorites reloadData];
[self.collectBlogs reloadData];
if (_favorites.count < 1){
_noFavoritesView.hidden = NO;
}else{
_noFavoritesView.hidden = YES;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)addFavoritesObject:(Post *)post{
NSDictionary *page = [[NSDictionary alloc]initWithDictionary:[Post dictionaryFromPost:post]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
self.favoritesFilePath = [docDirectory stringByAppendingPathComponent:@"favoritesdata"];
if ([[NSFileManager defaultManager] fileExistsAtPath:self.favoritesFilePath]) {
self.favorites = [[NSMutableArray alloc] initWithContentsOfFile:self.favoritesFilePath];
[self.favorites addObject:page];
if (![self.favorites writeToFile:self.favoritesFilePath atomically:YES]) {
NSLog(@"Couldn't save favorite");
}
}else{
self.favorites = [[NSMutableArray alloc] init];
[self.favorites addObject:page];
if (![self.favorites writeToFile:self.favoritesFilePath atomically:YES]) {
NSLog(@"Couldn't save favorite");
}
}
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"addedFavorite" // Event category (required)
action:@"button_press" // Event action (required)
label: post.link // Event label
value:nil] build]]; // Event value
}
-(void)removeFavoritesObject:(Post *)post{
NSDictionary *page = [[NSDictionary alloc]initWithDictionary:[Post dictionaryFromPost:post]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
self.favoritesFilePath = [docDirectory stringByAppendingPathComponent:@"favoritesdata"];
if ([[NSFileManager defaultManager] fileExistsAtPath:self.favoritesFilePath]) {
self.favorites = [[NSMutableArray alloc] initWithContentsOfFile:self.favoritesFilePath];
NSInteger theIndex = [self indexOfFavorite:page in:self.favorites];
if (theIndex != -1){
[self.favorites removeObjectAtIndex: theIndex];
if (![self.favorites writeToFile:self.favoritesFilePath atomically:YES]) {
NSLog(@"Couldn't save favorite");
}else{
NSLog(@"%ul", (unsigned int)_favorites.count);
}
if (self.favorites.count == 0){
_noFavoritesView.hidden = false;
}
}
}
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"removedFavorite" // Event category (required)
action:@"button_press" // Event action (required)
label: post.link // Event label
value:nil] build]]; // Event value
}
-(NSInteger) indexOfFavorite:(NSDictionary*)fav in:(NSArray*) favs{
NSInteger index = -1;
for (int i=0; i<favs.count; i+=1) {
if ([[[favs objectAtIndex:i] objectForKey:@"url"]isEqualToString:[fav objectForKey:@"url"]]){
index = i;
break;
}else{
index= -1;
}
}
return index;
}
-(BOOL)isFavorited:(Post*)post{
NSDictionary *page = [[NSDictionary alloc]initWithDictionary:[Post dictionaryFromPost:post]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
_favoritesFilePath = [docDirectory stringByAppendingPathComponent:@"favoritesdata"];
BOOL found = false;
if ([[NSFileManager defaultManager] fileExistsAtPath:self.favoritesFilePath]) {
NSArray *currentFavorites = [[NSMutableArray alloc] initWithContentsOfFile:self.favoritesFilePath];
if ([self indexOfFavorite:page in:currentFavorites] != -1){
found = true;
}
}
return found;
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.favorites.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITableViewCell *cell = nil;
Post *post = [_favorites objectAtIndex:indexPath.row];
PostTableCell *blogCell;
if(post.video){
cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
PostCollectionCell *blogCell = (PostCollectionCell *)cell;
[blogCell.backgroundImage setImageWithURL:[NSURL URLWithString:post.iPadThumbnail] placeholderImage:[UIImage imageNamed:@"WH_logo_3D_CMYK.png"]];
blogCell.titleLabel.text = post.title;
blogCell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@", post.getDate, post.getTime];
}else if (post.iPadThumbnail){
cell = [tableView dequeueReusableCellWithIdentifier:@"BlogCell" forIndexPath:indexPath];
blogCell = (PostTableCell *)cell;
[blogCell.backgroundImage setImageWithURL:[NSURL URLWithString:post.iPadThumbnail] placeholderImage: appDelegate.placeholderImages[_placeholderImageIndex]];
(_placeholderImageIndex == 3) ? _placeholderImageIndex = 0 : _placeholderImageIndex++;
blogCell.titleLabel.text = post.title;
blogCell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@", post.getDate, post.getTime];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:@"BlogCellNoImage" forIndexPath:indexPath];
blogCell = (PostTableCell *)cell;
blogCell.titleLabel.text = post.title;
blogCell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@", post.getDate, post.getTime];
}
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showActionSheet:)];
[cell addGestureRecognizer:longPress];
blogCell.card.layer.shadowColor = [UIColor blackColor].CGColor;
blogCell.card.layer.shadowRadius = 1;
blogCell.card.layer.shadowOpacity = 0.2;
blogCell.card.layer.shadowOffset = CGSizeMake(0.2, 2);
blogCell.card.layer.masksToBounds = NO;
[cell setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Post *post = [_favorites objectAtIndex:indexPath.row];
if ([post.type isEqualToString:@"photo"]){
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set options
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
browser.enableGrid = NO; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
[browser setCurrentPhotoIndex:indexPath.row];
[self.navigationController pushViewController:browser animated:YES];
}else{
if(post.video){
NSURL *fileURL = [NSURL URLWithString:post.video];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_moviePlayerController.controlStyle = MPMovieControlStyleDefault;
_moviePlayerController.shouldAutoplay = YES;
[self.view addSubview:_moviePlayerController.view];
[_moviePlayerController setFullscreen:YES animated:YES];
}else{
[self performSegueWithIdentifier:@"FavToDetailSegue" sender:self];
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
Post *post;
if (_tblFavorites.superview == self.view){
NSIndexPath *indexPath = [_tblFavorites indexPathForSelectedRow];
post = [_favorites objectAtIndex:indexPath.row];
}else{
post = [_favorites objectAtIndex:_colIndex.row];
}
DetailViewController *destViewController = segue.destinationViewController;
destViewController.post = post;
}
#pragma mark - collection view data source
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (IS_IPAD || IS_IPHONE_6P)
return _favorites.count;
else
return 0;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (IS_IPAD || IS_IPHONE_6P)
return 1;
else
return 0;
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UICollectionViewCell *cell = nil;
Post *post = [_favorites objectAtIndex:indexPath.row];
if(post.video){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoColCell" forIndexPath:indexPath];
PostCollectionCell *blogCell = (PostCollectionCell *)cell;
[blogCell.backgroundImage setImageWithURL:[NSURL URLWithString:post.iPadThumbnail] placeholderImage:[UIImage imageNamed:@"WH_logo_3D_CMYK.png"]];
blogCell.titleLabel.text = post.title;
blogCell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@", post.getDate, post.getTime];
}else if (post.iPadThumbnail){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BlogColCell" forIndexPath:indexPath];
PostCollectionCell *blogCell = (PostCollectionCell *)cell;
[blogCell.backgroundImage setImageWithURL:[NSURL URLWithString:post.iPadThumbnail] placeholderImage:appDelegate.placeholderImages[_placeholderImageIndex]];
(_placeholderImageIndex == 3) ? _placeholderImageIndex = 0 : _placeholderImageIndex++;
blogCell.titleLabel.text = post.title;
blogCell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@", post.getDate, post.getTime];
}else{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BlogColCellNoImage" forIndexPath:indexPath];
PostCollectionCell *blogCell = (PostCollectionCell *)cell;
blogCell.titleLabel.text = post.title;
blogCell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@", post.getDate, post.getTime];
blogCell.descriptionLabel.text = [Post stringByStrippingHTML:post.pageDescription];
}
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showActionSheet:)];
[cell addGestureRecognizer:longPress];
cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowRadius = 1;
cell.layer.shadowOpacity = 0.2;
cell.layer.shadowOffset = CGSizeMake(0.2, 2);
cell.layer.masksToBounds = NO;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
_colIndex = indexPath;
Post *post = [_favorites objectAtIndex:indexPath.row];
if ([post.type isEqualToString:@"photo"]){
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set options
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
browser.enableGrid = NO; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
[browser setCurrentPhotoIndex:indexPath.row];
[self.navigationController pushViewController:browser animated:YES];
}else{
if(post.video){
NSURL *fileURL = [NSURL URLWithString:post.video];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_moviePlayerController.controlStyle = MPMovieControlStyleDefault;
_moviePlayerController.shouldAutoplay = YES;
[self.view addSubview:_moviePlayerController.view];
[_moviePlayerController setFullscreen:YES animated:YES];
}else{
[self performSegueWithIdentifier:@"FavToDetailSegue" sender:self];
}
}
}
#pragma mark UICollectionViewDelegate layout
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
int totalGutterWidth = ([self cellsPerRow] + 1) * [self gutter];
int cellSize = (self.view.frame.size.width - totalGutterWidth) / [self cellsPerRow];
CGSize c = CGSizeMake(cellSize , cellSize);
return c;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake([self gutter],[self gutter],[self gutter],[self gutter]);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return [self gutter];
}
- (int)cellsPerRow{
return 2;
}
- (int)gutter{
return 25;
}
- (UICollectionViewFlowLayout *)flowLayout {
return (UICollectionViewFlowLayout *)self.collectBlogsPlus.collectionViewLayout;
}
#define NUMBER_OF_CELLS_PER_ROW 2
- (CGSize)itemSizeInCurrentOrientation {
CGFloat windowWidth = self.collectBlogsPlus.window.bounds.size.width;
CGFloat width = (windowWidth - (self.flowLayout.minimumInteritemSpacing * (NUMBER_OF_CELLS_PER_ROW - 1)) - self.flowLayout.sectionInset.left - self.flowLayout.sectionInset.right)/NUMBER_OF_CELLS_PER_ROW;
CGFloat height = 80.0f;
return CGSizeMake(width, height);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (IS_IPAD || IS_IPHONE_6P){
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.flowLayout invalidateLayout];
self.flowLayout.itemSize = [self itemSizeInCurrentOrientation];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (IS_IPAD || IS_IPHONE_6P){
[_collectBlogsPlus performBatchUpdates:nil completion:nil];
[_collectBlogs performBatchUpdates:nil completion:nil];
[_collectBlogsPlus reloadData];
} else
[_tblFavorites reloadData];
[self createBanner];
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _favorites.count;
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index {
NSArray *items = [NSArray arrayWithObjects: UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypeMail, nil];
ActivityViewCustomActivity *ca = [[ActivityViewCustomActivity alloc]init];
Post *post = [_favorites objectAtIndex:index];
NSString *activityName;
if ([self isFavorited:post]){
activityName = @"Unfavorite";
}else{
activityName = @"Favorite";
}
ca.activityTitle = activityName;
ca.post = post;
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:items
applicationActivities:[NSArray arrayWithObject:ca]];
activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo];
[photoBrowser presentViewController:activityVC animated:YES completion:nil];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ){
activityVC.popoverPresentationController.sourceView = self.view;
}
}
# pragma live event Banner
-(void)createBanner{
[self.baseView removeFromSuperview];
NSLog(@"%F", _heightCon);
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.livePosts){
NSMutableArray *happeningNow = [[NSMutableArray alloc]init];
for (NSDictionary *d in appDelegate.livePosts) {
Post *post = [Post postFromDictionary:d];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss ZZZ";
NSDate *postDate = [formatter dateFromString: post.pubDate];
NSDate *postDateEnd = [postDate dateByAddingTimeInterval:(+30*60)];
NSDate *timeNow = [NSDate date];
if([Post date:timeNow isBetweenDate:postDate andDate:postDateEnd]){
[happeningNow addObject:post];
}
}
if ([happeningNow count] > 0){
NSString *msg = [[NSString alloc] init];
UILabel *liveEventsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width, 20)];
if ([happeningNow count] == 1){
msg = [NSString stringWithFormat: @"Live: %@", [[happeningNow firstObject] title]];
liveEventsLabel.text = [NSString stringWithFormat:@"%@", msg];
}else {
msg = @"live events. Watch Live";
liveEventsLabel.text = [NSString stringWithFormat:@"%ld %@", (unsigned long)[happeningNow count], msg];
}
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
float frameWidth;
if (orientation == UIDeviceOrientationPortrait || IS_IOS_8_OR_LATER){
frameWidth = self.view.frame.size.width;
}else {
if (self.view.frame.size.width > self.view.frame.size.height)
frameWidth = self.view.frame.size.width;
else
frameWidth = self.view.frame.size.height;
}
if (IS_IOS_8_OR_LATER)
self.heightCon = (self.view.bounds.size.height > self.view.bounds.size.width)? 64 : 32;
else{
if(orientation == UIDeviceOrientationPortrait){
self.heightCon = 64;
}
else
self.heightCon = 52;
}
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad)
_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, frameWidth, 30)];
else
_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, _heightCon, frameWidth, 30)];
liveEventsLabel.textAlignment = NSTextAlignmentCenter;
liveEventsLabel.textColor = [UIColor whiteColor];
[_baseView addSubview:liveEventsLabel];
_baseView.backgroundColor = [UIColor colorWithRed:0.90 green:0.57 blue:0.22 alpha:0.9];
[self.view addSubview:_baseView];
[self.navigationController.view addSubview:_baseView];
_baseView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(presentLiveViewController)];
[_baseView addGestureRecognizer:tapGesture];
if (IS_IOS_8_OR_LATER){
[_tblFavorites setContentInset:UIEdgeInsetsMake(30,0,0,0)];
[_collectBlogs setContentInset:UIEdgeInsetsMake(5,0,0,0)];
}
else{
[_tblFavorites setContentInset:UIEdgeInsetsMake(22,0,0,0)];
[_collectBlogs setContentInset:UIEdgeInsetsMake(5,0,0,0)];
}
}
}
}
-(void)presentLiveViewController{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
bundle: nil];
LiveViewController *mainVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"LiveViewController"];
UINavigationController *navVC =[[UINavigationController alloc] initWithRootViewController:mainVC];
[self.revealViewController setFrontViewController:navVC];
}
# pragma long press for favoriting videos
- (IBAction)showActionSheet:(UILongPressGestureRecognizer *)gestureRecognizer {
NSIndexPath *indexPath;
//Post *post;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
CGPoint p = [gestureRecognizer locationInView:_collectBlogs];
indexPath = [_collectBlogs indexPathForItemAtPoint:p];
post = [_favorites objectAtIndex:indexPath.row];
}else{
CGPoint p = [gestureRecognizer locationInView:_tblFavorites];
indexPath = [_tblFavorites indexPathForRowAtPoint:p];
post = [_favorites objectAtIndex:indexPath.row];
}
FavoritesViewController * favoriteController = [[FavoritesViewController alloc] init];
if ([favoriteController isFavorited:post]){
_favoriteButtonTitle = @"Unfavorite";
}else{
_favoriteButtonTitle = @"Favorite";
}
if (IS_IOS_8_OR_LATER) {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Actions"
message:@"Share or save this page"
preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:alertController animated:YES completion:nil];
UIAlertAction *favoriteAction = [UIAlertAction
actionWithTitle:_favoriteButtonTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[self toggleFavorite:post];
}];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
}];
UIAlertAction *facebookAction = [UIAlertAction
actionWithTitle:@"Share on Facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *fbPostSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbPostSheet setInitialText:post.link];
[self presentViewController:fbPostSheet animated:YES completion:nil];
} else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't post right now, make sure your device has an internet connection and you have at least one facebook account setup"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
UIAlertAction *twitterAction = [UIAlertAction
actionWithTitle:@"Share on Twitter"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:post.link];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
UIAlertAction *emailAction = [UIAlertAction
actionWithTitle:@"Share via e-mail"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSString *emailTitle = @"Sent from the White House iOS App";
NSString *messageBody = post.link;
// NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
// [mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}];
[alertController addAction:favoriteAction];
[alertController addAction:facebookAction];
[alertController addAction:twitterAction];
[alertController addAction:emailAction];
[alertController addAction:cancelAction];
alertController.popoverPresentationController.barButtonItem = _sidebarButton;
}
else {
if (!alertShowing) {
UIAlertView *alertView =[[UIAlertView alloc ] initWithTitle:@"Actions"
message:@"Share or save this page"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles: nil];
[alertView addButtonWithTitle:_favoriteButtonTitle];
[alertView addButtonWithTitle:@"Share on Facebook"];
[alertView addButtonWithTitle:@"Share on Twitter"];
[alertView addButtonWithTitle:@"Share via Email"];
alertShowing = YES;
[alertView show];
}
}
}
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
alertShowing = NO;
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:_favoriteButtonTitle]){
[self toggleFavorite:post];
}else if([title isEqualToString:@"Share on Facebook"]) {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbPostSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbPostSheet setInitialText:post.link];
[self presentViewController:fbPostSheet animated:YES completion:nil];
} else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't post right now, make sure your device has an internet connection and you have at least one facebook account setup"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
} else if ([title isEqualToString:@"Share on Twitter"]) {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:post.link];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
} else if ( [title isEqualToString:@"Share via Email"]){
NSString *emailTitle = @"Sent from the White House iOS App";
NSString *messageBody = post.link;
//NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
// [mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
} else if ( [title isEqualToString:@"Cancel"]){
[self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void) toggleFavorite:(Post*)post{
if ([self isFavorited:post]){
[self removeFavoritesObject:post];
_favoriteButtonTitle = @"Favorite";
}else{
[self addFavoritesObject:post];
_favoriteButtonTitle = @"Unfavorite";
}
[self refreshData];
[_tblFavorites reloadData];
[_collectBlogs reloadData];
[_collectBlogsPlus reloadData];
}
-(CGFloat) tableView: (UITableView * ) tableView heightForRowAtIndexPath: (NSIndexPath * ) indexPath {
Post *post = [_favorites objectAtIndex:indexPath.row];
NSString *title = [Post stringByStrippingHTML:post.title];
//find the ratio of uppercase to totoal characters in string
int numberUpperCase = 0;
NSCharacterSet *upperCaseSet = [NSCharacterSet uppercaseLetterCharacterSet];
for (int x=0; x<title.length; x++) {
unichar aChar = [title characterAtIndex: x]; //get the first character from the string.
if ([upperCaseSet characterIsMember: aChar])
numberUpperCase++;
}
float a = numberUpperCase;
float b = title.length;
float upperToTotal = a/b;
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\?" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *string = [regex stringByReplacingMatchesInString:title options:0 range:NSMakeRange(0, [title length]) withTemplate:@""];
CGSize size = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:CGSizeMake(_tblFavorites.frame.size.width, 999) lineBreakMode:NSLineBreakByWordWrapping];
if (post.iPadThumbnail || post.video){
if (upperToTotal > 0.2)
return size.height + 270;
else
return size.height + 255;
}
else{
return size.height + 40;
}
}
@end