forked from NikantVohra/HackerNewsClient-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentsViewController.swift
More file actions
209 lines (171 loc) · 7.33 KB
/
CommentsViewController.swift
File metadata and controls
209 lines (171 loc) · 7.33 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
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
//
// CommentsViewController.swift
// HackerNews
//
// Created by Vohra, Nikant on 30/03/15.
// Copyright (c) 2015 Vohra, Nikant. All rights reserved.
//
import UIKit
class CommentsViewController: UITableViewController,ReplyViewControllerDelegate,StoryTableViewCellDelegate, CommentTableViewCellDelegate {
var story:HNPost?
var isFirstTime = true
var transitionManager = TransitionManager()
var comments:[HNComment]! = [] {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 140
tableView.rowHeight = UITableViewAutomaticDimension
loadCommments()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
if isFirstTime {
view.showLoading()
isFirstTime = false
}
}
func reloadStory() {
view.showLoading()
self.loadCommments()
}
@IBAction func didPressShareButton(sender: AnyObject) {
var title = story!.Title
var url = story!.UrlString
let activityViewController = UIActivityViewController(activityItems: [title, url], applicationActivities: nil)
activityViewController.setValue(title, forKey: "subject")
activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop]
presentViewController(activityViewController, animated: true, completion: nil)
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments!.count + 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifer = indexPath.row == 0 ? "StoryCell" : "CommentCell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifer) as UITableViewCell
if let storyCell = cell as? StoryCell {
storyCell.configureWithStory(story)
storyCell.delegate = self
}
if let commentCell = cell as? CommentCell {
let comment = comments![indexPath.row - 1]
commentCell.configureWithComment(comment)
commentCell.delegate = self
}
refreshControl?.addTarget(self, action: "refreshComments", forControlEvents: UIControlEvents.ValueChanged)
return cell
}
func loadCommments() {
if (story != nil) {
HNManager.sharedManager().loadCommentsFromPost(story, completion: {[weak self](comments) -> Void in
if (comments != nil && self != nil) {
var s = self!
s.refreshControl?.endRefreshing()
s.comments = comments as? [HNComment]
s.view.hideLoading()
}
})
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "replySegue" {
let toView = segue.destinationViewController as ReplyViewController
if let cell = sender as? CommentCell {
let indexPath = tableView.indexPathForCell(cell)!
let comment = comments[indexPath.row - 1]
toView.comment = comment
}
if let cell = sender as? StoryCell {
toView.story = story
}
toView.delegate = self
toView.transitioningDelegate = transitionManager
}
if segue.identifier == "profileSegue" {
let toView = segue.destinationViewController as UserProfileViewController
if let cell = sender as? CommentCell {
let indexPath = tableView.indexPathForCell(cell)!
let comment = comments[indexPath.row - 1]
toView.userName = comment.Username
}
if let cell = sender as? StoryCell {
toView.userName = story?.Username
}
}
}
// MARK: ReplyViewControllerDelegate
func replyViewControllerDidSend(controller: ReplyViewController) {
reloadStory()
}
// MARK: CommentTableViewCellDelegate
func commentTableViewCellDidTouchUpvote(cell: CommentCell){
if HNManager.sharedManager().userIsLoggedIn() {
let indexPath = tableView.indexPathForCell(cell)!
let comment = comments[indexPath.row]
HNManager.sharedManager().voteOnPostOrComment(comment, direction: VoteDirection.Up, completion: { (success) -> Void in
if(success) {
HNManager.sharedManager().addHNObjectToVotedOnDictionary(comment, direction: VoteDirection.Up)
cell.configureWithComment(comment)
}
else {
self.showAlert()
}
})
cell.configureWithComment(comment)
} else {
performSegueWithIdentifier("loginSegue", sender: self)
}
}
func commentTableViewCellDidTouchComment(cell: CommentCell){
if !HNManager.sharedManager().userIsLoggedIn() {
performSegueWithIdentifier("loginSegue", sender: cell)
} else {
performSegueWithIdentifier("replySegue", sender: cell)
}
}
func commentTableViewCellDidTouchAuthorLabel(cell: CommentCell) {
performSegueWithIdentifier("profileSegue", sender: cell)
}
// MARK: StoryTableViewCellDelegate
func storyTableViewCellDidTouchUpvote(cell: StoryCell, sender: AnyObject) {
if HNManager.sharedManager().userIsLoggedIn(){
HNManager.sharedManager().voteOnPostOrComment(story, direction: VoteDirection.Up, completion: { (success) -> Void in
if(success) {
HNManager.sharedManager().addHNObjectToVotedOnDictionary(self.story, direction: VoteDirection.Up)
self.story!.Points = self.story!.Points + 1
cell.configureWithStory(self.story!)
}
else {
self.showAlert()
}
})
cell.configureWithStory(story)
} else {
performSegueWithIdentifier("loginSegue", sender: self)
}
}
func storyTableViewCellDidTouchAuthorLabel(cell: StoryCell, sender: AnyObject) {
performSegueWithIdentifier("profileSegue", sender: cell)
}
func storyTableViewCellDidTouchComment(cell: StoryCell, sender: AnyObject) {
if !HNManager.sharedManager().userIsLoggedIn() {
performSegueWithIdentifier("loginSegue", sender: self)
} else {
performSegueWithIdentifier("replySegue", sender: cell)
}
}
func refreshComments() {
self.refreshControl?.beginRefreshing()
view.showLoading()
loadCommments()
}
func showAlert() {
self.view.hideLoading()
var alert = UIAlertController(title: "Oh noes.", message: "Something went wrong. Your message wasn't sent. Try again and save your text just in case.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}