109 lines
4.1 KiB
Swift
109 lines
4.1 KiB
Swift
/*
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2015-present Badoo Trading Limited.
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
import Foundation
|
|
import Chatto
|
|
|
|
class FakeDataSource: ChatDataSourceProtocol {
|
|
var nextMessageId: Int = 0
|
|
let preferredMaxWindowSize = 500
|
|
|
|
var slidingWindow: SlidingDataSource<ChatItemProtocol>!
|
|
init(count: Int, pageSize: Int) {
|
|
self.slidingWindow = SlidingDataSource(count: count, pageSize: pageSize) { () -> ChatItemProtocol in
|
|
defer { self.nextMessageId += 1 }
|
|
return FakeMessageFactory.createChatItem("\(self.nextMessageId)")
|
|
}
|
|
}
|
|
|
|
init(messages: [ChatItemProtocol], pageSize: Int) {
|
|
self.slidingWindow = SlidingDataSource(items: messages, pageSize: pageSize)
|
|
}
|
|
|
|
lazy var messageSender: FakeMessageSender = {
|
|
let sender = FakeMessageSender()
|
|
sender.onMessageChanged = { [weak self] (message) in
|
|
guard let sSelf = self else { return }
|
|
sSelf.delegate?.chatDataSourceDidUpdate(sSelf)
|
|
}
|
|
return sender
|
|
}()
|
|
|
|
var hasMoreNext: Bool {
|
|
return self.slidingWindow.hasMore()
|
|
}
|
|
|
|
var hasMorePrevious: Bool {
|
|
return self.slidingWindow.hasPrevious()
|
|
}
|
|
|
|
var chatItems: [ChatItemProtocol] {
|
|
return self.slidingWindow.itemsInWindow
|
|
}
|
|
|
|
weak var delegate: ChatDataSourceDelegateProtocol?
|
|
|
|
func loadNext(completion: () -> Void) {
|
|
self.slidingWindow.loadNext()
|
|
self.slidingWindow.adjustWindow(focusPosition: 1, maxWindowSize: self.preferredMaxWindowSize)
|
|
completion()
|
|
}
|
|
|
|
func loadPrevious(completion: () -> Void) {
|
|
self.slidingWindow.loadPrevious()
|
|
self.slidingWindow.adjustWindow(focusPosition: 0, maxWindowSize: self.preferredMaxWindowSize)
|
|
completion()
|
|
}
|
|
|
|
func addTextMessage(text: String) {
|
|
let uid = "\(self.nextMessageId)"
|
|
self.nextMessageId += 1
|
|
let message = createTextMessageModel(uid, text: text, isIncoming: false)
|
|
self.messageSender.sendMessage(message)
|
|
self.slidingWindow.insertItem(message, position: .Bottom)
|
|
self.delegate?.chatDataSourceDidUpdate(self)
|
|
}
|
|
|
|
func addPhotoMessage(image: UIImage) {
|
|
let uid = "\(self.nextMessageId)"
|
|
self.nextMessageId += 1
|
|
let message = createPhotoMessageModel(uid, image: image, size: image.size, isIncoming: false)
|
|
self.messageSender.sendMessage(message)
|
|
self.slidingWindow.insertItem(message, position: .Bottom)
|
|
self.delegate?.chatDataSourceDidUpdate(self)
|
|
}
|
|
|
|
func addRandomIncomingMessage() {
|
|
let message = FakeMessageFactory.createChatItem("\(self.nextMessageId)", isIncoming: true)
|
|
self.nextMessageId += 1
|
|
self.slidingWindow.insertItem(message, position: .Bottom)
|
|
self.delegate?.chatDataSourceDidUpdate(self)
|
|
}
|
|
|
|
func adjustNumberOfMessages(preferredMaxCount preferredMaxCount: Int?, focusPosition: Double, completion:(didAdjust: Bool) -> Void) {
|
|
let didAdjust = self.slidingWindow.adjustWindow(focusPosition: focusPosition, maxWindowSize: preferredMaxCount ?? self.preferredMaxWindowSize)
|
|
completion(didAdjust: didAdjust)
|
|
}
|
|
}
|