-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClothingTableViewController.swift
217 lines (186 loc) · 8.44 KB
/
ClothingTableViewController.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
import UIKit
import CloudKit
import MobileCoreServices
import CoreData
class ClothingTableViewController: UITableViewController {
//MARK: Properties
@IBAction func sumButton(_ sender: Any) {
setEditing(true, animated: true)
if sumButton.title == "Sum Weights" {
sumButton.title = "Sum: 0.0 g"
}
cancelSumButton.title = "Done"
}
@IBAction func cancelSumButton(_ sender: Any) {
setEditing(false, animated: true)
sumButton.title = "Sum Weights"
sumOfWeights = Float(0)
cancelSumButton.title = " "
}
var sumOfWeights = Float(0)
@IBOutlet weak var sumButton: UIBarButtonItem!
@IBOutlet weak var cancelSumButton: UIBarButtonItem!
var filterType = ""
var gear: [NSManagedObject] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cancelSumButton.title = ""
navigationController?.isToolbarHidden = false
if let index = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: index, animated: true)
}
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Gear")
fetchRequest.predicate = NSPredicate(format: "category == %@", filterType)
let sortDescriptor = NSSortDescriptor(key: "update", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
gear = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch CoreData gear. \(error), \(error.userInfo)")
}
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.allowsMultipleSelectionDuringEditing = true
navigationItem.title = filterType
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
}
// MARK: Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gear.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ClothingTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ClothingTableViewCell else{
fatalError("The dequeued cell is not an instance of ClothingTableViewCell")
}
//Fetches the appropriate clothing for the data source layout
let gearItem = gear[indexPath.row]
let details = gearItem.value(forKeyPath: "details") as? String
let selfWeight = gearItem.value(forKeyPath: "selfWeight") as? String
let manufWeight = gearItem.value(forKeyPath: "manufWeight") as? String
cell.clothingDetailsLabel.text = details
if selfWeight != "" && selfWeight != nil {
cell.clothingWeightLabel.text = selfWeight! + " g"
cell.clothingWeightLabel.textColor = UIColor.black
}
else if manufWeight != "" && manufWeight != nil {
cell.clothingWeightLabel.text = manufWeight! + " g"
cell.clothingWeightLabel.textColor = UIColor(red: 0.498, green: 0, blue: 0, alpha: 1)
}
else {
cell.clothingWeightLabel.text = "g"
cell.clothingWeightLabel.textColor = UIColor.black
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.isEditing{
let itemToSum = gear[indexPath.row]
let selfWeight = itemToSum.value(forKeyPath: "selfWeight") as? String
let manufWeight = itemToSum.value(forKeyPath: "manufWeight") as? String
if let weightOfItemToSum = Float(selfWeight!){
addWeight(weight: weightOfItemToSum)}
else if let weightOfItemToSum = Float(manufWeight!){
addWeight(weight: weightOfItemToSum)}
}
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if self.isEditing{
let itemToSubtract = gear[indexPath.row]
let selfWeight = itemToSubtract.value(forKeyPath: "selfWeight") as? String
let manufWeight = itemToSubtract.value(forKeyPath: "manufWeight") as? String
if let weightOfItemToSubtract = Float(selfWeight!){
subtractWeight(weight: weightOfItemToSubtract)}
else if let weightOfItemToSubtract = Float(manufWeight!){
subtractWeight(weight: weightOfItemToSubtract)}
}
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let selectedGear = gear[indexPath.row]
deleteGearFromCloud(gear: selectedGear)
managedContext.delete(selectedGear)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Gear")
fetchRequest.predicate = NSPredicate(format: "category == %@", filterType)
let sortDescriptor = NSSortDescriptor(key: "update", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
gear = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch CoreData gear after deletion. \(error), \(error.userInfo)")
}
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? ""){
case "AddClothing":
guard let clothingViewController = segue.destination as? ClothingViewController else{
fatalError("Unexpected destination: \(segue.destination)")
}
clothingViewController.category = filterType
clothingViewController.isEdit = false
case "ShowDetail":
guard let detailViewController = segue.destination as? ClothingViewController else{
fatalError("Unexpected destination: \(segue.destination)")
}
guard let selectedClothingCell = sender as? ClothingTableViewCell else {
fatalError("Unexpected sender")
}
guard let indexPath = tableView.indexPath(for: selectedClothingCell) else {
fatalError("The selected cell is not being displayed by the table")
}
if self.isEditing == false{
let selectedItem = gear[indexPath.row]
let gearID = selectedItem.value(forKeyPath: "gearID") as? String
detailViewController.gear = selectedItem
detailViewController.gearID = gearID
}
detailViewController.category = filterType
detailViewController.isEdit = true
default:
return
}
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return !self.isEditing
}
//MARK: Actions
@IBAction func unwindFromClothing(sender: UIStoryboardSegue){
return
}
//MARK: Private methods
override func setEditing (_ editing:Bool, animated:Bool)
{
super.setEditing(editing,animated:animated)
}
private func addWeight(weight: Float) {
sumOfWeights = sumOfWeights + weight
sumButton.title = "Sum: " + String(sumOfWeights) + " g"
}
private func subtractWeight(weight: Float) {
sumOfWeights = sumOfWeights - weight
sumButton.title = "Sum: " + String(sumOfWeights) + " g"
}
}