forked from NikantVohra/HackerNewsClient-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplyViewController.swift
More file actions
62 lines (53 loc) · 2.05 KB
/
ReplyViewController.swift
File metadata and controls
62 lines (53 loc) · 2.05 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
//
// ReplyViewController.swift
// HackerNews
//
// Created by Vohra, Nikant on 01/04/15.
// Copyright (c) 2015 Vohra, Nikant. All rights reserved.
//
import UIKit
protocol ReplyViewControllerDelegate: class {
func replyViewControllerDidSend(controller: ReplyViewController)
}
class ReplyViewController : UIViewController {
var story: HNPost?
var comment: HNComment?
weak var delegate: ReplyViewControllerDelegate?
@IBOutlet weak var replyTextView: UITextView!
@IBAction func didPressSendButton(sender: AnyObject) {
view.showLoading()
let body = replyTextView.text
if story != nil {
HNManager.sharedManager().replyToPostOrComment(story, withText: body, completion: { (success) -> Void in
if(success) {
self.dismissViewControllerAnimated(true, completion: nil)
self.delegate?.replyViewControllerDidSend(self)
}
else {
self.showAlert()
}
})
}
else if comment != nil {
HNManager.sharedManager().replyToPostOrComment(comment, withText: body, completion: { (success) -> Void in
if(success) {
self.dismissViewControllerAnimated(true, completion: nil)
self.delegate?.replyViewControllerDidSend(self)
}
else {
self.showAlert()
}
})
}
}
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)
}
override func viewDidLoad() {
super.viewDidLoad()
replyTextView.becomeFirstResponder()
}
}