-
Notifications
You must be signed in to change notification settings - Fork 0
/
GVInterestsViewController.m
executable file
·134 lines (97 loc) · 4.94 KB
/
GVInterestsViewController.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
//
// GVInterestsViewController.m
// Gifty Viewer
//
// Created by Mohamed Ayadi on 13/08/16.
// Copyright © 2016 Ayadi. All rights reserved.
//
#import "GVInterestsViewController.h"
#import "GVInterestsCollectionViewCell.h"
#import "GVCollectionViewFlowLayout.h"
#import "GVSwipeGiftsViewController.h"
#import "GVGift.h"
#import "GVGiftStore.h"
@interface GVInterestsViewController()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, assign) int numberOfInterestsSelected;
@property (weak, nonatomic) IBOutlet UILabel *counterLabel; // "You have selected..." label.
@property (copy, nonatomic) NSMutableArray *interestsSelected;
@end
@implementation GVInterestsViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.activityIndicator startAnimating];
self.activityIndicator.hidesWhenStopped = YES;
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController performSegueWithIdentifier:@"StartSegue" sender:self];
}
self.navigationController.navigationBarHidden = YES;
self.interestsCollectionView.delegate = self;
self.interestsCollectionView.dataSource = self;
[self.interestsCollectionView registerNib:[UINib nibWithNibName:@"GVInterestsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"tagCell"];
_interestsSelected = [[NSMutableArray alloc] init];
_numberOfInterestsSelected = 0;
self.interestsList = [[GVGiftStore sharedStore] interests];
[self.interestsCollectionView setAllowsMultipleSelection:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshInterests:) name:@"interestListUpdated" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.interestsList.count;
}
#pragma mark UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
GVInterestsCollectionViewCell *cell = (GVInterestsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"tagCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.layer.cornerRadius = 10;
cell.layer.masksToBounds = NO;
cell.interestLabel.text = [self.interestsList objectAtIndex:indexPath.row];
if ([self.interestsSelected containsObject:cell.interestLabel.text]) {
cell.selected = YES;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
GVInterestsCollectionViewCell *cell = (GVInterestsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
[self.interestsSelected addObject:cell.interestLabel.text];
self.numberOfInterestsSelected++;
[self updateCounter];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
GVInterestsCollectionViewCell *cell = (GVInterestsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
[self.interestsSelected removeObject:cell.interestLabel.text];
self.numberOfInterestsSelected--;
[self updateCounter];
}
#pragma mark UICollectionViewFlowLayoutDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [[self.interestsList objectAtIndex:indexPath.row] sizeWithAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:30]}];
}
#pragma mark Custom functions
- (void)updateCounter {
[self.counterLabel setText:[NSString stringWithFormat:@"You have selected %i interests.",self.numberOfInterestsSelected]];
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
GVSwipeGiftsViewController *vc = (GVSwipeGiftsViewController *)[segue destinationViewController];
vc.interestsSelected = self.interestsSelected;
}
- (void)refreshInterests:(NSNotification *)notification {
self.interestsList = [[GVGiftStore sharedStore] interests];
[self.activityIndicator stopAnimating];
[self.interestsCollectionView reloadData];
}
@end