-
Notifications
You must be signed in to change notification settings - Fork 1
/
SubViewController.swift
200 lines (171 loc) · 7.11 KB
/
SubViewController.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
//
// SubViewController.swift
// scorer
//
// Created by Jacob Kohn on 12/1/15.
// Copyright © 2015 Jacob Kohn. All rights reserved.
//
import UIKit
import CoreData
import Foundation
class SubViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var outTable: UITableView!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var inTable: UITableView!
var team = Int()
var lineup = [String]()
var bench = [String]()
var counter = 0
var game = Int()
var games = [NSManagedObject]()
var players = [NSManagedObject]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
outTable.dataSource = self
inTable.dataSource = self
doneButton.addTarget(self, action: "done:", forControlEvents: UIControlEvents.TouchUpInside)
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)")
}
games = fetchedResults
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
lineup = [String]()
bench = [String]()
for(var i=0; i<players.count; i++) {
if((players[i].valueForKey("game") as! Int) == game) {
print("Player's game: " + String(players[i].valueForKey("game") as! Int))
if((players[i].valueForKey("inlineup") as! Bool)) {
lineup.append(players[i].valueForKey("name") as! String)
} else {
bench.append(players[i].valueForKey("name") as! String)
}
}
}
}
func done(sender: UIButton) {
if(lineup.count == 5) {
performSegueWithIdentifier("returnToScoring", sender: nil)
} else {
let alert = UIAlertView()
alert.title = "Idiot:"
alert.message = "You need five players to play basketball"
alert.addButtonWithTitle("OK")
alert.show()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(tableView == self.outTable) {
return (bench.count)
} else {
return (lineup.count)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(tableView == self.inTable) {
let cell = tableView.dequeueReusableCellWithIdentifier("OutCell", forIndexPath: indexPath) as! subOutCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.nameLabel.text = lineup[indexPath.row]
cell.subButton.tag = indexPath.row
cell.subButton.addTarget(self, action: "subOut:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
} else {
let cell2 = tableView.dequeueReusableCellWithIdentifier("InCell", forIndexPath: indexPath) as! subInCell
cell2.selectionStyle = UITableViewCellSelectionStyle.None
cell2.nameLabel.text = bench[indexPath.row]
cell2.subButton.tag = indexPath.row
cell2.subButton.addTarget(self, action: "subIn:", forControlEvents: UIControlEvents.TouchUpInside)
return cell2
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "returnToScoring") {
let controller = segue.destinationViewController as! ScoringViewController
controller.game = game
controller.lineup = lineup
controller.bench = bench
controller.games = games
controller.players = players
controller.team = team
}
}
func subIn(sender: UIButton) {
let player = bench[sender.tag]
bench.removeAtIndex(sender.tag)
lineup.append(player)
for(var i=0; i<players.count; i++) {
if((players[i].valueForKey("name") as! String) == player) {
if((players[i].valueForKey("game") as! Int) == game) {
//CoreData stuff
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Player",
inManagedObjectContext:
managedContext)
players[i].setValue(true, forKey: "inlineup")
var error: NSError?
do {
try managedContext.save()
} catch var error1 as NSError {
error = error1
print("Could not save \(error), \(error?.userInfo)")
}
break;
}
}
}
self.outTable.reloadData()
self.inTable.reloadData()
}
func subOut(sender:UIButton) {
let player = lineup[sender.tag]
lineup.removeAtIndex(sender.tag)
bench.append(player)
for(var i=0; i<players.count; i++) {
if((players[i].valueForKey("name") as! String) == player) {
if((players[i].valueForKey("game") as! Int) == game) {
//CoreData stuff
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Player",
inManagedObjectContext:
managedContext)
players[i].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)")
}
break;
}
}
}
self.outTable.reloadData()
self.inTable.reloadData()
}
}