mitter.io
Search…
Channel Window
This ChannelWindowViewController contains a TableView to display a list of Messages, a UITextField to input a Message and a Send UIButton to send the Text Message.
To initialise the list of Messages, do the following:
1
// The channel id for this Channel. This will be set by ChannelListViewController
2
var channelId = String()
3
4
// The list of Messages in this channel, backing the messages TableView
5
var messages = [Message]()
6
7
override func viewDidLoad() {
8
// Get the AppDelegate, which contains the Mitter object
9
let appDelegate = UIApplication.shared.delegate as! AppDelegate
10
11
// TableView setup
12
...
13
14
// Hook up the Send button
15
sendButton.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
16
17
// Fetch all Messages in Channel
18
appDelegate.mitter.messaging.getMessagesInChannel(channelId) {
19
result in
20
switch result {
21
case .success(let fetchedMessages):
22
self.messages = fetchedMessages.reversed()
23
self.tableView.reloadData()
24
case .error:
25
print("Couldn't fetch messages")
26
}
27
}
28
}
29
}
Copied!
Set up the Send button click target:
1
@objc func buttonClicked() {
2
let appDelegate = UIApplication.shared.delegate as! AppDelegate
3
4
appDelegate.mitter.messaging.sendTextMessage(forChannel: channelId, inputText.text!) { result in
5
switch result {
6
case .success:
7
print("Message sent!")
8
case .error:
9
print("Couldn't send message")
10
}
11
}
12
}
Copied!
Add a public function to add a Message into the TableView. This will be used by AppDelegate when it receives an FCM message:
1
func newMessage(channelId: String, message: Message) {
2
if (self.channelId == channelId) {
3
messages.append(message)
4
tableView.reloadData()
5
}
6
}
Copied!
The controller also contains some View manipulation logic to align/colour Message bubbles based on who the sender is. Refer to the cellForRowAt delegate function for the same.
Last modified 2yr ago
Copy link