This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| /// Subclass this Class with a non generic Type. | |
| /// - note: Please override the init function with a private | |
| /// access modifactor and create a singleton, so you | |
| /// can ensure that there are no more Queues as you | |
| /// are expecting. | |
| internal class QueueHandler<T> { | |
| private var queues: Set<Queue<T>> | |
| /// Checks if any Queue contains an Element waiting for execution. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| target 'IntiveChatApp' do | |
| pod 'Starscream', '~> 3.0.2' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MessagingViewController: UIViewController { | |
| @IBOutlet private var tableView: UITableView! | |
| @IBOutlet private var messageTextField: UITextView! | |
| /// Assigned by the conversations view controller when the segue in triggered. | |
| var dataSource: MessagingDataSource? | |
| override func viewDidLoad() { | |
| super.viewDidLoad() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var messages: [Message] { | |
| return dataStore.messagesForConversation(with: userId) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension MessagingDataSource: UITableViewDataSource { | |
| func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| return messages.count | |
| } | |
| func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
| let message = messages[indexPath.row] | |
| let identifier = cellIdentifier(for: message) | |
| let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! MessageCell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func send(_ message: String) { | |
| let newMessage = Message(text: message, receiverId: userId) | |
| dataStore.send(newMessage) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MessageDataStore: DataStore { | |
| /// A singleton object. | |
| static let shared = MessageDataStore() | |
| /// The web socket for network communication. | |
| private var webSocket: WebSocketClient? | |
| /// The delegate is informed whenever new messages arrive. | |
| weak var delegate: DataStoreDelegate? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func connectWebSocket() { | |
| let url = URL(string: "ws://localhost:8080/api/echo")! | |
| var urlRequest = URLRequest(url: url) | |
| guard let tokens = LoginSessionManager.shared.tokens else { | |
| // Handle connection failure | |
| return | |
| } | |
| urlRequest.setValue("Bearer \(tokens.access_token)", forHTTPHeaderField: "Authorization") | |
| webSocket = WebSocket(request: urlRequest) | |
| webSocket?.delegate = self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension MessageDataStore: WebSocketDelegate { | |
| func websocketDidReceiveData(socket: WebSocketClient, data: Data) { | |
| if let message = try? JSONDecoder().decode(Message.self, from: data) { | |
| add(message, direction: .received) | |
| } | |
| } | |
| // ... | |
| // Other delegate method implementations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func add(_ message: Message, direction: Message.CommunicationDirection) { | |
| let conversationId = conversationIdFor(message: message, direction: direction) | |
| if let existingConverstion = conversations[conversationId] { | |
| existingConverstion.append(message) | |
| } else { | |
| let newConversation = Conversation(user: conversationId) | |
| newConversation.append(message) | |
| conversations[conversationId] = newConversation | |
| } | |
| delegate?.dataStoreDidReceiveNewMessage(forConversationWith: conversationId) |
OlderNewer