-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitialProfileViewController.swift
194 lines (167 loc) · 6.92 KB
/
InitialProfileViewController.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
//
// InitialProfileViewController.swift
// SCAM
//
// Created by Charles Leon on 3/4/17.
// Copyright © 2017 SCAM16. All rights reserved.
//
import DropDown
import Parse
import IQKeyboardManagerSwift
class InitialProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var user = PFUser.current()
@IBOutlet weak var minorsTable: UITableView!
@IBOutlet weak var classesTable: UITableView!
@IBOutlet weak var majorTextField: SCMTextField!
@IBOutlet weak var doubleMajorField: SCMTextField!
private var classFields = 4
private var minorFields = 2
private var knownMajors = SJSUInformation.majors
private var knownMinors = SJSUInformation.minors
private var knownClasses = SJSUInformation.classes
override func viewDidLoad() {
super.viewDidLoad()
user = PFUser.current()
if (user?["hasCompletedRequiredFields"] != nil) {
if (!(user?["hasCompletedRequiredFields"] as! Bool)) {
self.navigationItem.leftBarButtonItem = nil
}
} else {
self.navigationItem.leftBarButtonItem = nil
}
majorTextField.enableDropDown = true
majorTextField.setDataSource(newDataSource: knownMajors)
doubleMajorField.enableDropDown = true
doubleMajorField.setDataSource(newDataSource: knownMajors)
self.hideKeyboardWhenTappedAround()
setup()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func setup() {
self.majorTextField.text = user?["major"] as? String
self.doubleMajorField.text = user?["doubleMajor"] as? String
}
func keyboardDidShow(notification: NSNotification) {
self.classesTable.isScrollEnabled = false
}
func keyboardWillHide(notification: NSNotification) {
self.classesTable.isScrollEnabled = true
}
@IBAction func addClass(_ sender: Any) {
classFields += 1
classesTable.beginUpdates()
classesTable.insertRows(at: [IndexPath(row: classFields - 1, section: 0)], with: .automatic)
classesTable.endUpdates()
}
@IBAction func addMinor(_ sender: Any) {
minorFields += 1
minorsTable.beginUpdates()
minorsTable.insertRows(at: [IndexPath(row: minorFields - 1, section: 0)], with: .automatic)
minorsTable.endUpdates()
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (tableView == classesTable) {
classFields -= 1
classesTable.beginUpdates()
classesTable.deleteRows(at: [indexPath], with: .top)
classesTable.endUpdates()
} else {
minorFields -= 1
minorsTable.beginUpdates()
minorsTable.deleteRows(at: [indexPath], with: .top)
minorsTable.endUpdates()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (tableView == classesTable) {
return classFields
} else {
return minorFields
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "addClassCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! AddClassCell
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.classNameField.enableDropDown = true
if (tableView == classesTable) {
cell.classNameField.setDataSource(newDataSource: knownClasses)
if let classes = user?["classes"] as? [String] {
if (indexPath.row < classes.count) {
cell.classNameField.text = classes[indexPath.row]
}
}
} else {
cell.classNameField.setDataSource(newDataSource: knownMinors)
if let minors = user?["minors"] as? [String] {
if (indexPath.row < minors.count) {
cell.classNameField.text = minors[indexPath.row]
}
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! AddClassCell
cell.isSelected = false
}
@IBAction func saveInfo(_ sender: Any) {
var isValid = true
var major: String?
var doubleMajor: String?
var minors: [String] = []
var classes: [String] = []
if (!knownMajors.contains(majorTextField.text!)) {
majorTextField.shake()
isValid = false
} else {
major = majorTextField.text!
}
if (doubleMajorField.text != "" && !knownMajors.contains(doubleMajorField.text!)) {
doubleMajorField.shake()
} else {
doubleMajor = doubleMajorField.text!
}
for i in 0 ..< minorFields {
let cell = minorsTable.cellForRow(at: IndexPath(row: i, section: 0)) as! AddClassCell
let minor = cell.classNameField.text
if (minor != nil && minor != "") {
if (!knownMinors.contains(minor!)) {
cell.classNameField.text = ""
cell.classNameField.shake()
} else {
minors.append(minor!)
}
}
}
for i in 0 ..< classFields {
if let cell = classesTable.cellForRow(at: IndexPath(row: i, section: 0)) as? AddClassCell {
let course = cell.classNameField.text
if (course != nil && course != "") {
if (!knownClasses.contains(course!)) {
cell.classNameField.text = ""
cell.classNameField.shake()
} else {
classes.append(course!)
}
}
}
}
if (!isValid) {
return
}
user?["major"] = major
if (doubleMajor != nil && doubleMajor != "") {
user?["double"] = doubleMajor
}
user?["minors"] = minors
user?["classes"] = classes
let extraCurricVc = self.storyboard?.instantiateViewController(withIdentifier: "initialExtraCurricularViewController") as! InitialExtraCurricularProfileViewController
extraCurricVc.user = user
self.navigationController?.pushViewController(extraCurricVc, animated: true)
}
@IBAction func closeView(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}