-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommentsViewController.swift
More file actions
168 lines (131 loc) · 6.06 KB
/
CommentsViewController.swift
File metadata and controls
168 lines (131 loc) · 6.06 KB
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
//
// CommentsViewController.swift
// RocketPoll
//
// Created by Igor Kantor on 3/14/15.
//
//
import UIKit
class CommentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIBarPositioningDelegate , UITextFieldDelegate, UITextViewDelegate{
@IBOutlet weak var tableView: UITableView!
let cellIdentifier = "commentCell"
var question:Question!
var comments:[Comment] = []
@IBOutlet weak var commentTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let tap = UITapGestureRecognizer(target: self, action: "hideKeyboard")
self.view.addGestureRecognizer(tap)
loadComments()
self.title = "Comments"
hideCommentView()
}
func hideCommentView(){
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: "cancel")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Done, target: self, action: "showCommentView")
self.commentTextView.resignFirstResponder()
self.commentTextView.delegate = self
self.commentTextView.hidden = true
}
func showCommentView(){
self.commentTextView.becomeFirstResponder()
self.commentTextView.hidden = false
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: "hideCommentView")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Post", style: UIBarButtonItemStyle.Done, target: self, action: "postComment")
}
func hideKeyboard(){
self.commentTextView.resignFirstResponder()
}
func cancel(){
self.dismissViewControllerAnimated(true, completion: nil)
}
func postComment() {
if !self.commentTextView.text.isEmpty {
let comment = Comment(className: "Comment")
comment.text = self.commentTextView.text
comment.by = PFUser.currentUser()
comment.question = self.question
comment.saveEventually()
if self.question.askedBy != PFUser.currentUser() {
let data = ["alert":"Your question got a new comment from \(PFUser.currentUser().username): \"\(comment.text)\"",
"badge":"Increment"]
let push = PFPush()
push.setData(data)
push.setChannel("answers_to_\(question.askedBy.objectId)")
push.sendPushInBackgroundWithBlock(nil)
}
self.comments.insert(comment, atIndex:0)
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade)
self.commentTextView.text = ""
hideCommentView()
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func loadComments(){
let query = PFQuery(className: "Comment")
query.whereKey("question", equalTo: self.question!)
query.includeKey("answeredBy")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (comments, error) -> Void in
if error == nil {
self.comments = comments as! [Comment]!
}
else {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
UIAlertView(title: "Error", message: error.description, delegate: nil, cancelButtonTitle: "OK").show()
})
}
self.tableView.reloadData()
print("Found \(self.comments.count) comments")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.comments.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as! CommentsTableViewCell
cell.commentTextLabel.text = self.comments[indexPath.row].text
// todo: re-implement
// cell.commentDateLabel.text = self.comments[indexPath.row].createdAt?.timeAgo
cell.byTextLabel.text = self.comments[indexPath.row].by.username
if self.comments[indexPath.row].by.objectForKey("profile_picture") != nil {
let profilePictureFile = self.comments[indexPath.row].by.objectForKey("profile_picture") as! PFFile
profilePictureFile.getDataInBackgroundWithBlock({ (profilePicData, error) -> Void in
if error == nil {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
cell.profilePictureImageView.image = UIImage(data:profilePicData)
})
}
else {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
UIAlertView(title: "Error", message: error.description, delegate: nil, cancelButtonTitle: "OK").show()
})
}
})
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let text = self.comments[indexPath.row].text
let font = UIFont.systemFontOfSize(14)
let suggestedHeight = heightForView(
text,
font: font,
width: self.tableView.frame.width - 100// roughly what's configured in the auto layout contstraints
) // for top/bottom margin
return max(suggestedHeight, 100)
}
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
}