-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuViewController.m
More file actions
271 lines (228 loc) · 9.61 KB
/
MainMenuViewController.m
File metadata and controls
271 lines (228 loc) · 9.61 KB
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
//
// MainMenuViewController.m
// NBAPlayerTrack
//
// Created by Buv Sethia on 3/2/15.
// Copyright (c) 2015 ___Sethia___. All rights reserved.
//
#import "MainMenuViewController.h"
#import "SWRevealViewController.h"
#import "Player.h"
#import "Utility.h"
#import "PlayerTabBarController.h"
#import "PlayerCell.h"
@implementation MainMenuViewController
static NSMutableArray *userPlayers = nil;
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(self.revealViewController)
{
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
-(void)viewDidLoad
{
[super viewDidLoad];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.sidebarButton setTarget: self.revealViewController];
[self.sidebarButton setAction: @selector( revealToggle: )];
}
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor lightGrayColor];
//Removes horizontal lines from the table view
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//Let the table view use Player Cells
[self.tableView registerNib:[UINib nibWithNibName:@"PlayerCell" bundle:nil] forCellReuseIdentifier:@"menuCell"];
UIActivityIndicatorView *loadPlayersIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
loadPlayersIndicator.color = [UIColor blackColor];
loadPlayersIndicator.center = self.view.center;
[loadPlayersIndicator startAnimating];
[self.view addSubview:loadPlayersIndicator];
[self.view bringSubviewToFront:loadPlayersIndicator];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if(userPlayers == Nil && [[NSFileManager defaultManager] fileExistsAtPath:[MainMenuViewController userPlayersFilePath]])
{
userPlayers = [NSKeyedUnarchiver unarchiveObjectWithFile:[MainMenuViewController userPlayersFilePath]];
NSLog(@"Loading players from file");
//If there are players being followed by the user and they need to be updated
if(userPlayers.count > 0 && [self playersNeedUpdate])
{
//If we have internet, update the players
if([Utility haveInternet])
{
for(int i = 0; i < userPlayers.count; i++)
{
userPlayers[i] = [Utility generateObjectForPlayer:userPlayers[i]];
}
[MainMenuViewController saveUserPlayers];
}
}
}
else if(userPlayers != Nil)
{
NSLog(@"Players already loaded");
}
else
{
NSLog(@"userPlayersFile DNE");
}
dispatch_async(dispatch_get_main_queue(), ^{
[loadPlayersIndicator stopAnimating];
[loadPlayersIndicator removeFromSuperview];
[self.tableView reloadData];
//If we have players but they weren't updated b/c of internet connection, let the user know
if(userPlayers.count > 0 && [self playersNeedUpdate] && ![Utility haveInternet])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error connecting to the internet, so player information may not be up to date. Please turn on wifi or data and restart the app to update player information." delegate:Nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
}
});
});
}
-(BOOL)playersNeedUpdate
{
//Save ourselves a call to a service and the database and check in-app if stats for this player have already been updated today
//Since all the players will have the same update date in the app, grab the first player and check against his update date.
Player *player = [userPlayers objectAtIndex:0];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDateComponents *components = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[[NSDate alloc] init]];
NSInteger day = [components day];
NSInteger playerDay = [player.updateDate integerValue];
if(day == playerDay)
{
NSLog(@"Precheck results: No update to player necessary. Not calling updateDBMethod service.");
return NO;
}
else
{
return YES;
}
}
+(NSMutableArray*)userPlayers
{
if (userPlayers == nil) userPlayers = [[NSMutableArray alloc] init];
return userPlayers;
}
#pragma mark Table View Controller
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [userPlayers count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"menuCell";
PlayerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"PlayerCell" bundle:nil] forCellReuseIdentifier:@"menuCell"];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(PlayerCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a new Player Object
Player *player = [userPlayers objectAtIndex:indexPath.section];
cell.image.image = [[UIImage alloc] initWithData:player.playerImage];
cell.nameLabel.text = player.name;
cell.pointsLabel.text = [player.perGameStats objectForKey:@"PTS"];
cell.rebLabel.text = [player.perGameStats objectForKey:@"TRB"];
cell.astLabel.text = [player.perGameStats objectForKey:@"AST"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"PlayerDetailSegue" sender:tableView];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[userPlayers removeObjectAtIndex:indexPath.section];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
[MainMenuViewController saveUserPlayers];
}
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCell"];
return cell.bounds.size.height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 5.0f;
}
//http://stackoverflow.com/questions/12552785/resizing-image-to-fit-uiimageview
-(UIImage*)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
+(bool)containsPlayer:(NSString*)playerID
{
for(Player *p in userPlayers)
{
if([playerID isEqualToString:p.ID])
{
return YES;
}
}
return NO;
}
+(bool)saveUserPlayers
{
[NSKeyedArchiver archiveRootObject:userPlayers toFile:[MainMenuViewController userPlayersFilePath]];
if([[NSFileManager defaultManager] fileExistsAtPath:[MainMenuViewController userPlayersFilePath]])
{
NSLog(@"User players saved to file");
}
return YES;
}
+(NSString*)userPlayersFilePath
{
NSArray *initPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [initPath objectAtIndex:0];
NSString *path = [documentFolder stringByAppendingFormat:@"/userPlayers.plist"];
return path;
}
//Method used for debugging purposes only
+(void)removePlayerFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager removeItemAtPath:[MainMenuViewController userPlayersFilePath] error:&error];
if (success) {
/*UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];*/
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"PlayerDetailSegue"])
{
PlayerTabBarController *dest = (PlayerTabBarController*)segue.destinationViewController;
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
Player *p = [userPlayers objectAtIndex:selectedIndexPath.section];
dest.player = p;
}
}
@end