-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
391 lines (306 loc) · 14.3 KB
/
ViewController.swift
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
//
// ViewController.swift
// scorer
//
// Created by Jacob Kohn on 11/30/15.
// Copyright © 2015 Jacob Kohn. All rights reserved.
//
// DELETING GAMES/PLAYERS WHEN DELETING TEAMS
// CAT AVERAGE STRINGS
import UIKit
import CoreData
import Foundation
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var pastGamesTable: UITableView!
let backToTeamsButton = UIButton()
let addGameButton = UIButton()
var allGames = [NSManagedObject]()
var games = [NSManagedObject]()
var players = [NSManagedObject]()
var playerNames = [String]()
var team = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureButtons()
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"Game")
let error: NSError?
var fetchedResults = [NSManagedObject]()
do {
fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
} catch let error as NSError {
print("Fetch failed: \(error.localizedDescription)")
}
allGames = fetchedResults
for(var i=0; i<allGames.count; i++) {
if(allGames[i].valueForKey("team") as! Int == team) {
games.insert(allGames[i], atIndex: games.count)
}
}
let fetchReq = NSFetchRequest(entityName:"Player")
let err: NSError?
var fetchedR = [NSManagedObject]()
do {
fetchedR = try managedContext.executeFetchRequest(fetchReq) as! [NSManagedObject]
} catch let err as NSError {
print("Fetch failed: \(err.localizedDescription)")
}
players = fetchedR
let fetchR = NSFetchRequest(entityName:"Joint")
let e: NSError?
var fR = [NSManagedObject]()
do {
fR = try managedContext.executeFetchRequest(fetchR) as! [NSManagedObject]
} catch let err as NSError {
print("Fetch failed: \(err.localizedDescription)")
}
let joint = fR
for(var l=0; l<joint.count; l++) {
if(joint[l].valueForKey("teamnum") as! Int == team) {
playerNames.insert(joint[l].valueForKey("playername") as! String, atIndex: playerNames.count)
}
}
pastGamesTable.dataSource = self
}
func configureButtons() {
backToTeamsButton.frame = CGRectMake(0, 0, self.view.frame.size.width / 2, 60)
backToTeamsButton.backgroundColor = UIColor.blueColor()
backToTeamsButton.setTitle("Back To Teams", forState: .Normal)
backToTeamsButton.addTarget(self, action: "returnToTeams:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(backToTeamsButton)
addGameButton.frame = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, 60)
addGameButton.addTarget(self, action: "newGame:", forControlEvents: UIControlEvents.TouchUpInside)
addGameButton.backgroundColor = UIColor.greenColor()
addGameButton.setTitle("+New Game+", forState: .Normal)
self.view.addSubview(addGameButton)
}
func returnToTeams(sender: UIButton) {
self.performSegueWithIdentifier("returnToTeams", sender: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func newGame(sender: UIButton) {
performSegueWithIdentifier("goToStatEntry", sender: nil)
}
// MARK: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return games.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel!.text = (games[indexPath.row].valueForKey("opponent") as! String) + " on " + (games[indexPath.row].valueForKey("date") as! String)
if((indexPath.row as Int) % 2 == 0 ) {
cell.backgroundColor = UIColor.lightGrayColor()
} else {
cell.backgroundColor = UIColor.whiteColor()
}
return cell
}
//remove item from list
func tableView(tableView: UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
//CoreData stuff
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Game",
inManagedObjectContext:
managedContext)
let gameID = games[indexPath.row].valueForKey("id") as! Int
managedContext.deleteObject(games[indexPath.row])
games.removeAtIndex(indexPath.row)
for(var i=0; i<players.count; i++) {
if(players[i].valueForKey("game") as! Int == indexPath.row) {
managedContext.deleteObject(players[i])
players.removeAtIndex(i)
i = i - 1
}
}
reduceGameIDsByOne(gameID)
reduceGameNumsByOne(indexPath.row)
repopulateGames(indexPath.row)
do {
try managedContext.save()
} catch _ {
}
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
func repopulateGames(row: Int) {
//CoreData stuff
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"Game")
let entity = NSEntityDescription.entityForName("Game",
inManagedObjectContext:
managedContext)
let error: NSError?
var fetchedResults = [NSManagedObject]()
do {
fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
} catch let error as NSError {
print("Fetch failed: \(error.localizedDescription)")
}
allGames = fetchedResults
games.removeAll()
for(var i=0; i<allGames.count; i++) {
if(allGames[i].valueForKey("team") as! Int == self.team) {
self.games.insert(allGames[i], atIndex: self.games.count)
}
}
}
func reduceGameNumsByOne(row: Int) {
//CoreData stuff
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Player",
inManagedObjectContext:
managedContext)
for(var i=0; i<players.count; i++) {
if let gamenum = players[i].valueForKey("game") {
if((gamenum as! Int) > row) {
players[i].setValue((gamenum as! Int) - 1, forKey: "game")
}
}
}
}
func reduceGameIDsByOne(row: Int) {
//CoreData stuff
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Game",
inManagedObjectContext:
managedContext)
for(var l=row; l<allGames.count; l++) {
if let id = allGames[l].valueForKey("id") {
allGames[l].setValue((id as! Int) - 1, forKey: "id")
}
}
}
func getDate() -> String {
//get the month
let getMonth = NSDateFormatter()
getMonth.dateFormat = "MM"
let monthNum = getMonth.stringFromDate(NSDate())
//get the day
let getDay = NSDateFormatter()
getDay.dateFormat = "dd"
var day = getDay.stringFromDate(NSDate())
if(Int(day) < 10) {
day = String(day[day.endIndex.predecessor()])
}
var month = ""
if(monthNum == "01") { month = "January" }
else if(monthNum == "02") { month = "February" }
else if(monthNum == "03") { month = "March" }
else if(monthNum == "04") { month = "April" }
else if(monthNum == "05") { month = "May" }
else if(monthNum == "06") { month = "June" }
else if(monthNum == "07") { month = "July" }
else if(monthNum == "08") { month = "August" }
else if(monthNum == "09") { month = "September" }
else if(monthNum == "10") { month = "October" }
else if(monthNum == "11") { month = "November" }
else if(monthNum == "12") { month = "December" }
let date = month + " " + day
return date
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "goToStatEntry" {
let newGameNum = allGames.count
let controller = segue.destinationViewController as! ScoringViewController
//CoreData stuff
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Game",
inManagedObjectContext:
managedContext)
let date = getDate()
//creates new swing object
let gameObject = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext:managedContext)
gameObject.setValue(newGameNum, forKey: "id")
gameObject.setValue(date, forKey: "date")
gameObject.setValue(team, forKey: "team")
var error: NSError?
do {
try managedContext.save()
} catch var error1 as NSError {
error = error1
print("Could not save \(error), \(error?.userInfo)")
}
games.insert(gameObject, atIndex: games.count)
for(var i=0; i<playerNames.count; i++) {
let e = NSEntityDescription.entityForName("Player",
inManagedObjectContext:
managedContext)
//creates new player object
let playerObject = NSManagedObject(entity: e!,
insertIntoManagedObjectContext:managedContext)
print(newGameNum)
playerObject.setValue(newGameNum, forKey: "game")
playerObject.setValue(playerNames[i], forKey: "name")
playerObject.setValue(false, forKey: "inlineup")
var error: NSError?
do {
try managedContext.save()
} catch var error1 as NSError {
error = error1
print("Could not save \(error), \(error?.userInfo)")
}
players.insert(playerObject, atIndex: i)
}
do {
try managedContext.save()
} catch _ {
}
controller.game = newGameNum
controller.firstTime = true
controller.players = players
controller.team = team
}
if(segue.identifier == "goToBoxScore") {
if let indexPath = self.pastGamesTable.indexPathForSelectedRow {
let controller = segue.destinationViewController as! BoxScoreViewController
print(allGames.count)
var gameID = 0
for(var i=0; i<allGames.count; i++) {
print(String(i) + " " + String(allGames[i].valueForKey("id") as! Int))
if(allGames[i].valueForKey("id") as! Int == games[indexPath.row].valueForKey("id") as! Int) {
controller.game = allGames[i].valueForKey("id") as! Int
gameID = allGames[i].valueForKey("id") as! Int
}
}
print("GAME: " + String(gameID))
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"Player")
let error: NSError?
var fetchedResults = [NSManagedObject]()
do {
fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
} catch let error as NSError {
print("Fetch failed: \(error.localizedDescription)")
}
let pList = fetchedResults
var playersFromGame = [NSManagedObject]()
for(var i=0; i<pList.count; i++) {
print(pList[i].valueForKey("game"))
if(pList[i].valueForKey("game") as! Int == gameID) {
playersFromGame.insert(pList[i], atIndex: playersFromGame.count)
}
}
controller.players = playersFromGame
controller.team = team
}
}
}
}