-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetailViewController.swift
236 lines (170 loc) · 8.18 KB
/
DetailViewController.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
//
// DetailViewController.swift
//
//
// Created by Leon Yee on 6/18/20.
//
import UIKit
var id:Int = 0
//var tookClassArray:[String] = []
var tookClassArray = UserDefaults.standard.array(forKey: String(id)) ?? []
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var sportName: UILabel!
@IBOutlet weak var duration: UILabel!
@IBOutlet weak var tookClassTable: UITableView!
@IBOutlet weak var nextClassDate: UILabel!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var lastClass: UILabel!
var name:String = ""
var numClasses:Int = 0
var numClassesTaken:Int = 0
var interval:String = ""
override func viewDidLoad() {
tookClassArray = UserDefaults.standard.array(forKey: String(id)) ?? []
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "appbackground")!)
tookClassTable.backgroundColor = UIColor(white: 1, alpha: 0.1)
tookClassTable.layer.borderColor = UIColor.white.cgColor
tookClassTable.layer.borderWidth = 1.0
let calendar = UIBarButtonItem(image: UIImage(named: "date2"), style: .plain, target: self, action: #selector(goToCalendar(_:)))
navigationItem.rightBarButtonItem = calendar
//first time stuff
if #available(iOS 13.0, *){
} else {
//let temp = UserDefaults.standard.bool(forKey: "firstTime")
//if(temp == false){
let back = UIBarButtonItem(title: "< Classes", style: .plain, target: self, action: #selector(goBack(_:)))
navigationItem.leftBarButtonItem = back
//}
}
//tableview
self.tookClassTable.register(UITableViewCell.self, forCellReuseIdentifier: "tookClassCell")
tookClassTable.delegate = self
tookClassTable.dataSource = self
tookClassTable.reloadData()
//classes taken stuff
sportName.text = name
if(numClasses == numClassesTaken){
numClassesTaken = 0
classesTaken[id] = 0
UserDefaults.standard.removeObject(forKey: "classesTaken")
UserDefaults.standard.set(classesTaken, forKey: "classesTaken")
duration.text = String(numClasses)
} else {
duration.text = String(numClasses-numClassesTaken)
}
//next class stuff
if(tookClassArray.count > 0){
let format = DateFormatter()
format.dateFormat = "MM/dd/yyyy"
if(interval == "Daily"){
guard let date = format.date(from: tookClassArray[0] as! String) else {
fatalError()
}
var dateComponent = DateComponents()
dateComponent.day = 1
let added = Calendar.current.date(byAdding: dateComponent, to: date)!
nextClassDate.text = format.string(from: added)
} else if(interval == "Weekly"){
guard let date = format.date(from: tookClassArray[0] as! String) else {
fatalError()
}
var dateComponent = DateComponents()
dateComponent.weekOfYear = 1
let added = Calendar.current.date(byAdding: dateComponent, to: date)!
nextClassDate.text = format.string(from: added)
} else if(interval == "Biweekly"){
guard let date = format.date(from: tookClassArray[0] as! String) else {
fatalError()
}
var dateComponent = DateComponents()
dateComponent.weekOfYear = 2
let added = Calendar.current.date(byAdding: dateComponent, to: date)!
nextClassDate.text = format.string(from: added)
}
}
nextClassDate.layer.borderColor = UIColor.white.cgColor
nextClassDate.layer.borderWidth = 3.0
nextClassDate.backgroundColor = UIColor(white: 0, alpha: 0.25)
nextClassDate.layer.cornerRadius = 8
nextClassDate.layer.masksToBounds = true
//
// Do any additional setup after loading the view.
UserDefaults.standard.synchronize()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tookClassTable?.reloadData()
}
@objc func goBack(_ sender: Any){
performSegue(withIdentifier: "detailToList", sender: self)
}
@objc func goToCalendar(_ sender: Any){
let viewcont = secondDetailViewController()
viewcont.classdates = tookClassArray
self.navigationController?.pushViewController(viewcont, animated: true)
}
@IBAction func increaseClass(_ sender: Any) {
numClassesTaken+=1
let tempnum:Int = classesTaken[id] as! Int
classesTaken[id] = tempnum+1
UserDefaults.standard.removeObject(forKey: "classesTaken")
UserDefaults.standard.set(classesTaken, forKey: "classesTaken")
let format = DateFormatter()
format.dateFormat = "MM/dd/yyyy"
tookClassArray.insert(format.string(from: Date()), at: 0)
tookClassTable.beginUpdates()
tookClassTable.insertRows(at: [IndexPath(row: 0, section:0)], with: .top)
tookClassTable.endUpdates()
UserDefaults.standard.removeObject(forKey: String(id))
UserDefaults.standard.set(tookClassArray, forKey: String(id))
viewDidLoad()
}
// MARK: TABLE VIEW FUNCS
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tookClassArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tookClassTable.dequeueReusableCell(withIdentifier: "tookClassCell")!
lastClass.text = ""
cell.textLabel?.text = tookClassArray[indexPath.row] as? String
cell.layer.cornerRadius = 10
cell.layer.masksToBounds = true
cell.backgroundColor = UIColor(white: 1, alpha: 0.50)
if(tookClassArray.count >= numClasses){
let temp: String = ("Most recent class: " + (tookClassArray.last as! String))
lastClass.text! = temp
tookClassArray = []
UserDefaults.standard.removeObject(forKey: String(id))
UserDefaults.standard.set(tookClassArray, forKey: String(id))
}
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tookClassArray.remove(at: indexPath.row)
numClassesTaken -= 1
let num:Int = classesTaken[id] as! Int
classesTaken[id] = num - 1
UserDefaults.standard.removeObject(forKey: "classesTaken")
UserDefaults.standard.set(classesTaken, forKey: "classesTaken")
UserDefaults.standard.removeObject(forKey: String(id))
UserDefaults.standard.set(tookClassArray, forKey: String(id))
tableView.deleteRows(at: [indexPath], with: .fade)
viewDidLoad()
} 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 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}