-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostTweetViewController.swift
More file actions
124 lines (95 loc) · 5.06 KB
/
PostTweetViewController.swift
File metadata and controls
124 lines (95 loc) · 5.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
//
// PostTweetViewController.swift
// Twitter
//
// Created by ZengJintao on 2/21/16.
// Copyright © 2016 ZengJintao. All rights reserved.
//
import UIKit
var newTweetCallback:Tweet?
class PostTweetViewController: UIViewController {
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var newTweetContent: UITextField!
@IBOutlet weak var charactarLimits: UILabel!
@IBOutlet weak var sendStatusView: UIView!
@IBOutlet weak var sendStatusToBottomConstraint: NSLayoutConstraint!
var replyTweetId:String?
override func viewDidLoad() {
super.viewDidLoad()
avatarImageView.setImageWithURL(NSURL(string: (User.currentUser?.profileImageUrl)!)!)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func cancelButtonPressed(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func sendButtonPressed(sender: UIButton) {
if newTweetContent.text == nil {
var alert = UIAlertController(title: "Please enter", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
} else if newTweetContent.text?.characters.count > 140 {
var alert = UIAlertController(title: "Please enter less than 140 charactars", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
} else {
if replyTweetId == nil {
print("new tweet start")
TwitterClient.sharedInstance.postNewTweet(["status":"\(newTweetContent.text!)"], completion: { (tweets, error) -> () in
newTweetCallback = tweets
self.performSegueWithIdentifier("unwindToHomeLineVC", sender: self)
// self.dismissViewControllerAnimated(true, completion: nil)
})
} else {
print("new reply start")
TwitterClient.sharedInstance.postNewTweet(["status":"\(newTweetContent.text!)", "in_reply_to_status_id":"\(replyTweetId!)"], completion: { (tweets, error) -> () in
newTweetCallback = tweets
// self.dismissViewControllerAnimated(true, completion: nil)
var alert = UIAlertController(title: "successful reply", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
// self.presentViewController(alert, animated: true, completion: nil)
// alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (UIAlertAction) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}))
self.presentViewController(alert, animated: true, completion: { () -> Void in
})
})
}
}
}
func keyboardWasShown(notification:NSNotification) {
let dict:NSDictionary = notification.userInfo!
let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let rect:CGRect = s.CGRectValue()
UIView.animateWithDuration(0.3, delay: 0, options: .CurveLinear, animations: {
self.sendStatusToBottomConstraint.constant = rect.height
}, completion: {
(finished:Bool) in
})
}
func keyboardWillHide(notification:NSNotification) {
let dict:NSDictionary = notification.userInfo!
let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let rect:CGRect = s.CGRectValue()
UIView.animateWithDuration(0.3, delay: 0, options: .CurveLinear, animations: {
self.sendStatusToBottomConstraint.constant = 0
}, completion: {
(finished:Bool) in
})
}
@IBAction func newTweetContentChanging(sender: AnyObject) {
var charLeft = 140 - (newTweetContent.text?.characters.count)!
charactarLimits.text = "\(charLeft)"
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}