Created
April 17, 2019 20:31
-
-
Save snowden2/86b060eab82ce30762affe29515eb91c to your computer and use it in GitHub Desktop.
Zendesk Sample By Goeffrey
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 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