Skip to content

Instantly share code, notes, and snippets.

@mischa-hildebrand
mischa-hildebrand / QueueHandler.swift
Last active September 5, 2017 16:35 — forked from kayoslab/QueueHandler.swift
A simple Generic Queue with different priority Levels
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.
@mischa-hildebrand
mischa-hildebrand / Swift Chat - 1 - Podfile.rb
Last active October 14, 2018 22:13
Swift Chat - 1 - Podfile
target 'IntiveChatApp' do
pod 'Starscream', '~> 3.0.2'
end
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()
private var messages: [Message] {
return dataStore.messagesForConversation(with: userId)
}
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
func send(_ message: String) {
let newMessage = Message(text: message, receiverId: userId)
dataStore.send(newMessage)
}
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?
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
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
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)