@@ -7,12 +7,16 @@
77//
88
99import UIKit
10+import TinyConstraints
1011
1112protocol UpdatableGroup:class {
1213 func updateGroup(with group: Model.Group)
1314}
1415
15−class DialogViewController: UITableViewController, UpdatableGroup {
16+class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegate, UITableViewDataSource
17+{
18+
19+ var tableView: UITableView = UITableView()
1620
1721 func updateGroup(with group: Model.Group){
1822 self.groupChat?.group = group
@@ -49,6 +53,12 @@ class DialogViewController: UITableViewController, UpdatableGroup {
4953
5054 override func viewDidLoad() {
5155 super.viewDidLoad()
56+
57+ setConstraints()
58+ magicForKeyboardChanges()
59+
60+ self.tableView.delegate = self
61+ self.tableView.dataSource = self
5262
5363 tabBarController?.tabBar.isHidden = true
5464 navigationItem.largeTitleDisplayMode = .never
@@ -65,6 +75,97 @@ class DialogViewController: UITableViewController, UpdatableGroup {
6575 }
6676 }
6777
78+ var messageSendView: UIView = {
79+ let view = UIView()
80+ view.backgroundColor = .red
81+ return view
82+ }()
83+
84+ var messageTextView: UITextView = {
85+ let textView = UITextView()
86+ textView.isEditable = true
87+ textView.isScrollEnabled = true
88+ textView.backgroundColor = .green
89+ return textView
90+ }()
91+
92+ var sendButton: UIButton = {
93+ let button = UIButton()
94+ button.setTitle("Send", for: .normal)
95+ button.setTitleColor(.blue, for: .normal)
96+ button.addTarget(self, action: #selector(sendMessage), for: .touchUpInside)
97+ return button
98+ }()
99+
100+ @objc func sendMessage()
101+ {
102+ var destination: Model.MessageDestination?
103+
104+ if let group = groupChat{
105+ destination = Model.MessageDestination.groupChatDestination(group.group.id)
106+ } else if let user = userChat {
107+ destination = Model.MessageDestination.userChatDestination(user.user)
108+ }
109+
110+ if let destination = destination
111+ {
112+ Model.sendMessage(message: Model.SendMessage(body: Model.Attachments(markdown: messageTextView.text), destination: destination)) { [unowned self] in
113+ self.getMessages(for: self.dialog!)
114+ }
115+ }
116+ }
117+
118+ var bottomConstraint: NSLayoutConstraint!
119+
120+ func setConstraints(){
121+ self.view.addSubview(tableView)
122+ self.view.addSubview(messageSendView)
123+
124+ messageSendView.addSubview(messageTextView)
125+ messageSendView.addSubview(sendButton)
126+
127+ messageSendView.edgesToSuperview(excluding: [.top,.bottom])
128+
129+ bottomConstraint = NSLayoutConstraint(item: messageSendView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
130+ view.addConstraint(bottomConstraint)
131+
132+ messageSendView.height(40)
133+
134+ sendButton.edgesToSuperview(excluding: .left)
135+ sendButton.width(60)
136+
137+ messageTextView.edgesToSuperview(excluding: .right)
138+ messageTextView.rightToLeft(of: sendButton)
139+
140+ tableView.edgesToSuperview(excluding: .bottom)
141+ tableView.bottomToTop(of: messageSendView)
142+
143+ self.view.layoutSubviews()
144+ }
145+
146+ func magicForKeyboardChanges()
147+ {
148+ NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillShowNotification, object: nil)
149+
150+ NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillHideNotification, object: nil)
151+ }
152+
153+ @objc func handleKeyboardNotifications(notification: NSNotification){
154+ if let userInfo = notification.userInfo{
155+ // UIKeyboardFrameEndUserInfoKey
156+
157+ let keyBoardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
158+
159+ bottomConstraint.constant = notification.name == UIResponder.keyboardWillShowNotification ? -(keyBoardFrame.height) : 0
160+
161+ UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
162+ self.view.layoutIfNeeded()
163+ }) { (completed) in
164+
165+ }
166+ }
167+ }
168+
68169 func setTitleForChat(userChat: Model.UserChat){
69170 navigationItem.title = "🌌 \(users![userChat.user]!.fullName())"
70171 }
@@ -75,7 +176,7 @@ class DialogViewController: UITableViewController, UpdatableGroup {
75176 }
76177
77178 @objc func moveToInfoVC(){
78− let vc = storyboard?.instantiateViewController(withIdentifier: chatInfoViewController) as! ChatInfoViewController
179+ let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: chatInfoViewController) as! ChatInfoViewController
79180
80181 vc.delegate = self
81182 vc.users = users!
@@ -97,11 +198,11 @@ class DialogViewController: UITableViewController, UpdatableGroup {
97198
98199 // MARK: - Table view data source
99200
100− override func numberOfSections(in tableView: UITableView) -> Int {
201+ func numberOfSections(in tableView: UITableView) -> Int {
101202 return 1
102203 }
103204
104− override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
205+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
105206 return cellData.count
106207 }
107208
@@ -128,7 +229,7 @@ class DialogViewController: UITableViewController, UpdatableGroup {
128229 }
129230 }
130231
131− override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
232+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
132233 {
133234 let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DialogCell
134235
@@ -143,3 +244,6 @@ class DialogViewController: UITableViewController, UpdatableGroup {
143244 return cell
144245 }
145246}
247+
248+
249+