-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostViewController.swift
236 lines (193 loc) · 8.09 KB
/
PostViewController.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
//
// PostViewController.swift
// SCAM
//
// Created by Charles Leon on 5/19/17.
// Copyright © 2017 SCAM16. All rights reserved.
//
import Parse
import IQKeyboardManagerSwift
class PostViewController: UITableViewController {
var post: Post?
var comments: [Comment] = []
@IBOutlet weak var usefulCountLabel: UILabel!
@IBOutlet weak var creationTimeLabel: UILabel!
@IBOutlet weak var commentField: UITextView!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var postImageView: UIImageView!
@IBOutlet weak var usefulButton: UIButton!
@IBOutlet weak var commentButton: UIButton!
@IBOutlet weak var postBodyLabel: UILabel!
@IBOutlet weak var headerView: UIView!
@IBOutlet weak var hiddenCommentField: UITextField!
@IBOutlet weak var postImageViewHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.commentField.keyboardDistanceFromTextField = 250
tableView.estimatedRowHeight = 200;
tableView.rowHeight = UITableViewAutomaticDimension
let header = self.headerView!
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.groupTableViewBackground.cgColor
border.frame = CGRect(x: 0, y: header.frame.size.height - width, width: self.view.frame.width, height: header.frame.size.height)
border.borderWidth = width
self.headerView.layer.addSublayer(border)
self.headerView.layer.masksToBounds = true
let footer = self.tableView.tableFooterView!
let topBorder = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 2)
topBorder.backgroundColor = UIColor.groupTableViewBackground.cgColor
footer.layer.addSublayer(topBorder)
//Set up author
let author = post?.author as? Profile
usernameLabel.text = author?.name
if (author?.profileImage != nil) {
self.profileImageView.loadFromChacheThenParse(file: author!.profileImage!)
} else {
self.profileImageView.image = #imageLiteral(resourceName: "defaultAvatar")
}
self.postBodyLabel.sizeToFit()
if (post?.image != nil) {
self.post?.image?.getDataInBackground(block: { (data: Data?, error: Error?) in
if (data != nil) {
let image = UIImage(data: data!)
self.postImageView.image = image
self.postImageView.isHidden = false
let ratio = self.headerView.frame.width / (image?.size.height)!
self.postImageViewHeight.constant = (image?.size.height)! * ratio
}
})
} else {
self.postImageView.isHidden = true
self.postImageViewHeight.constant = 0
self.postImageView.image = nil
}
if (post?.helped != nil) {
for profile in post!.helped! {
if (profile.objectId! == User.currentProfile()?.objectId!) {
self.usefulButton.setTitle("Nevermind", for: .normal)
}
}
self.usefulCountLabel.text = post!.helped!.count.description + " People found this useful"
}
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
self.creationTimeLabel.text = formatter.string(from: post!.createdAt!)
postBodyLabel.sizeToFit()
self.loadObjects()
}
@IBAction func beginCommenting(_ sender: Any) {
self.hiddenCommentField.becomeFirstResponder()
self.commentField.becomeFirstResponder()
}
@IBAction func postComment(_ sender: Any) {
if (commentField.text == nil || commentField.text == "") {
return
}
let comment = Comment()
comment.body = commentField.text
comment.post = post
comment.saveInBackground { (success: Bool, error: Error?) in
if (error == nil) {
self.loadObjects()
} else {
print(error!)
}
}
}
@IBAction func clearComment(_ sender: Any) {
self.commentField.text = ""
self.commentField.resignFirstResponder()
}
@IBAction func findPostUseful(_ sender: Any) {
let params = [
"post": true,
"ID" : self.post!.objectId!
] as [String : Any]
PFCloud.callFunction(inBackground: "findUseful", withParameters:
params) { (newCount: Any?, error: Error?) in
if (error == nil) {
self.usefulButton.setTitle("Nevermind", for: .normal)
if(newCount != nil) {
self.usefulCountLabel.text = newCount as? String
}
} else {
print(error!)
}
}
}
@IBAction func findCommentUseful(_ sender: UIButton) {
let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as! GroupPostCell
let comment = comments[sender.tag]
let params = [
"post": false,
"ID" : comment.objectId!
] as [String : Any]
PFCloud.callFunction(inBackground: "findUseful", withParameters:
params) { (newCount: Any?, error: Error?) in
if (error == nil) {
sender.setTitle("Nevermind", for: .normal)
if(newCount != nil) {
cell.usefulCountLabel.text = newCount as? String
}
} else {
print(error!)
}
}
}
@objc func objectsDidLoad(error: Error? = nil) {
self.tableView.reloadData()
}
@objc func loadObjects() {
let query = Comment.query()!
query.whereKey("post", equalTo: self.post!)
query.includeKey("author")
query.order(byAscending: "createdAt")
query.findObjectsInBackground { (updatedComments: [PFObject]?, error: Error?) in
if (error == nil) {
self.commentField.text = ""
self.commentField.resignFirstResponder()
self.comments = updatedComments as! [Comment]
self.objectsDidLoad()
} else {
self.objectsDidLoad(error: error!)
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return comments.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell", for: indexPath) as! GroupPostCell
cell.indexPath = indexPath
let comment = comments[indexPath.row]
let author = comment.author as? Profile
cell.bodyLabel.text = comment.body
cell.nameLabel.text = author?.name
cell.setDate(date: comment.createdAt!)
if (author?.profileImage != nil) {
cell.setImage(file: author!.profileImage!, circular: false, indexPath: indexPath)
} else {
cell.profileImageView.image = #imageLiteral(resourceName: "defaultAvatar")
}
cell.bodyLabel.sizeToFit()
if (comment.helped != nil) {
for profile in comment.helped! {
if (profile.objectId! == User.currentProfile()?.objectId!) {
cell.findUsefulButton.setTitle("Nevermind", for: .normal)
}
}
cell.usefulCountLabel.text = comment.helped!.count.description + " People found this useful"
}
cell.findUsefulButton.tag = indexPath.row
return cell
}
}