Skip to content

Instantly share code, notes, and snippets.

@snowden2
Created April 17, 2019 20:31
Show Gist options
  • Save snowden2/86b060eab82ce30762affe29515eb91c to your computer and use it in GitHub Desktop.
Save snowden2/86b060eab82ce30762affe29515eb91c to your computer and use it in GitHub Desktop.
Zendesk Sample By Goeffrey
import ZDCChatAPI
struct ChatControllerDelegate: ChatViewControllerDelegate {
weak var client: APIClient?
func chatController(_ chatController: ChatViewController, sendMessage message: String) {
client?.sendMessage(message)
}
func chatController(_ chatController: ChatViewController, didSelectImage image: UIImage) {
client?.uploadImage(image)
}
}
/**
ChatDelegateDateSource connects the ChatViewController to Zopim using an APIClient
*/
final class ChatControllerDataSource: ChatViewControllerDataSource {
fileprivate weak var chatView: ChatView?
fileprivate weak var client: APIClient?
var chatLog: [ChatUIEvent] {
didSet {
chatView?.updateChatLog()
}
}
required init (withChatView chatView: ChatView, client: APIClient) {
self.chatView = chatView
self.client = client
chatLog = [ChatUIEvent]()
client.chatConnectedStatusUpdated = chatConnectedStatusUpdated
client.eventReceived = { [weak self] event in
self?.chatLog.append(event)
}
client.eventUpdated = { [weak self] event in
self?.updateCell(withId: event.id, updateBlock: { (updatedEvent: inout ChatUIEvent) in
updatedEvent = event
})
}
}
//MARK: APIClient delegate
fileprivate func chatConnectedStatusUpdated(_ state: Bool) {
chatView?.isChatConnected = state
}
fileprivate func updateCell(withId id: String, updateBlock: (inout ChatUIEvent) -> ()) {
guard let (index, item) = chatLog.enumerated().filter({ $1.id == id }).first else { return }
var retItem = item
updateBlock(&retItem)
chatLog[index] = retItem
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment