@@ -7,12 +7,16 @@
9import UIKit
9import UIKit
10+import TinyConstraints
11protocol UpdatableGroup:class {
12protocol UpdatableGroup:class {
12 func updateGroup(with group: Model.Group)
13 func updateGroup(with group: Model.Group)
15−class DialogViewController: UITableViewController, UpdatableGroup {
16+class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegate, UITableViewDataSource
19+ var tableView: UITableView = UITableView()
17 func updateGroup(with group: Model.Group){
21 func updateGroup(with group: Model.Group){
18 self.groupChat?.group = group
22 self.groupChat?.group = group
@@ -49,6 +53,12 @@ class DialogViewController: UITableViewController, UpdatableGroup {
50 override func viewDidLoad() {
54 override func viewDidLoad() {
51 super.viewDidLoad()
55 super.viewDidLoad()
58+ magicForKeyboardChanges()
60+ self.tableView.delegate = self
61+ self.tableView.dataSource = self
53 tabBarController?.tabBar.isHidden = true
63 tabBarController?.tabBar.isHidden = true
54 navigationItem.largeTitleDisplayMode = .never
64 navigationItem.largeTitleDisplayMode = .never
@@ -65,6 +75,97 @@ class DialogViewController: UITableViewController, UpdatableGroup {
78+ var messageSendView: UIView = {
80+ view.backgroundColor = .red
84+ var messageTextView: UITextView = {
85+ let textView = UITextView()
86+ textView.isEditable = true
87+ textView.isScrollEnabled = true
88+ textView.backgroundColor = .green
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)
100+ @objc func sendMessage()
102+ var destination: Model.MessageDestination?
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)
110+ if let destination = destination
112+ Model.sendMessage(message: Model.SendMessage(body: Model.Attachments(markdown: messageTextView.text), destination: destination)) { [unowned self] in
113+ self.getMessages(for: self.dialog!)
118+ var bottomConstraint: NSLayoutConstraint!
120+ func setConstraints(){
121+ self.view.addSubview(tableView)
122+ self.view.addSubview(messageSendView)
124+ messageSendView.addSubview(messageTextView)
125+ messageSendView.addSubview(sendButton)
127+ messageSendView.edgesToSuperview(excluding: [.top,.bottom])
129+ bottomConstraint = NSLayoutConstraint(item: messageSendView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
130+ view.addConstraint(bottomConstraint)
132+ messageSendView.height(40)
134+ sendButton.edgesToSuperview(excluding: .left)
135+ sendButton.width(60)
137+ messageTextView.edgesToSuperview(excluding: .right)
138+ messageTextView.rightToLeft(of: sendButton)
140+ tableView.edgesToSuperview(excluding: .bottom)
141+ tableView.bottomToTop(of: messageSendView)
143+ self.view.layoutSubviews()
146+ func magicForKeyboardChanges()
148+ NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillShowNotification, object: nil)
150+ NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillHideNotification, object: nil)
153+ @objc func handleKeyboardNotifications(notification: NSNotification){
154+ if let userInfo = notification.userInfo{
155+ // UIKeyboardFrameEndUserInfoKey
157+ let keyBoardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
159+ bottomConstraint.constant = notification.name == UIResponder.keyboardWillShowNotification ? -(keyBoardFrame.height) : 0
161+ UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
162+ self.view.layoutIfNeeded()
68 func setTitleForChat(userChat: Model.UserChat){
169 func setTitleForChat(userChat: Model.UserChat){
69 navigationItem.title = "🌌 \(users![userChat.user]!.fullName())"
170 navigationItem.title = "🌌 \(users![userChat.user]!.fullName())"
@@ -75,7 +176,7 @@ class DialogViewController: UITableViewController, UpdatableGroup {
77 @objc func moveToInfoVC(){
178 @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
80 vc.delegate = self
181 vc.delegate = self
81 vc.users = users!
182 vc.users = users!
@@ -97,11 +198,11 @@ class DialogViewController: UITableViewController, UpdatableGroup {
98 // MARK: - Table view data source
199 // MARK: - Table view data source
100− override func numberOfSections(in tableView: UITableView) -> Int {
201+ func numberOfSections(in tableView: UITableView) -> Int {
104− override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
205+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
105 return cellData.count
206 return cellData.count
@@ -128,7 +229,7 @@ class DialogViewController: UITableViewController, UpdatableGroup {
131− override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
232+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
133 let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DialogCell
234 let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DialogCell
@@ -143,3 +244,6 @@ class DialogViewController: UITableViewController, UpdatableGroup {
143 return cell
244 return cell