element-ios/RiotSwiftUI/Modules/Template/TemplateAdvancedRoomsExample/TemplateRoomChat/ViewModel/TemplateRoomChatViewModel.swift

134 lines
4.9 KiB
Swift
Raw Normal View History

//
2021-09-12 13:57:45 +00:00
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
import Combine
@available(iOS 14, *)
typealias TemplateRoomChatViewModelType = StateStoreViewModel<TemplateRoomChatViewState,
TemplateRoomChatStateAction,
TemplateRoomChatViewAction>
@available(iOS 14, *)
class TemplateRoomChatViewModel: TemplateRoomChatViewModelType, TemplateRoomChatViewModelProtocol {
2021-09-12 13:57:45 +00:00
enum Constants {
static let maxTimeBeforeNewBubble: TimeInterval = 5*60
}
2021-09-12 13:57:45 +00:00
// MARK: - Properties
// MARK: Private
2021-09-12 13:57:45 +00:00
private let templateRoomChatService: TemplateRoomChatServiceProtocol
// MARK: Public
2021-09-12 13:57:45 +00:00
var completion: ((TemplateRoomChatViewModelResult) -> Void)?
// MARK: - Setup
init(templateRoomChatService: TemplateRoomChatServiceProtocol) {
2021-09-12 13:57:45 +00:00
self.templateRoomChatService = templateRoomChatService
super.init(initialViewState: Self.defaultState(templateRoomChatService: templateRoomChatService))
2021-09-12 13:57:45 +00:00
templateRoomChatService.chatMessagesSubject
.map(Self.makeBubbles(messages:))
.map(TemplateRoomChatStateAction.updateBubbles)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] action in
self?.dispatch(action:action)
})
.store(in: &cancellables)
}
private static func defaultState(templateRoomChatService: TemplateRoomChatServiceProtocol) -> TemplateRoomChatViewState {
let bubbles = makeBubbles(messages: templateRoomChatService.chatMessagesSubject.value)
let bindings = TemplateRoomChatViewModelBindings(messageInput: "")
return TemplateRoomChatViewState(roomName: templateRoomChatService.roomName, bubbles: bubbles, bindings: bindings)
2021-09-12 13:57:45 +00:00
}
private static func makeBubbles(messages: [TemplateRoomChatMessage]) -> [TemplateRoomChatBubble] {
2021-09-13 08:26:21 +00:00
var bubbleOrder = [String]()
var bubbleMap = [String:TemplateRoomChatBubble]()
2021-09-12 13:57:45 +00:00
messages.enumerated().forEach { i, message in
let messageItem = TemplateRoomChatBubbleItem(
id: message.id,
timestamp: message.timestamp,
content: .message(TemplateRoomChatBubbleMessageContent(body: message.body))
)
2021-09-13 08:26:21 +00:00
if i > 0,
let lastBubbleId = bubbleOrder.last,
var lastBubble = bubbleMap[lastBubbleId],
lastBubble.sender.id == message.sender.id,
let interveningTime = lastBubble.items.last?.timestamp.timeIntervalSince(message.timestamp),
abs(interveningTime) < Constants.maxTimeBeforeNewBubble
{
let item = TemplateRoomChatBubbleItem(
2021-09-13 08:26:21 +00:00
id: message.id,
timestamp: message.timestamp,
content: .message(TemplateRoomChatBubbleMessageContent(body: message.body))
2021-09-13 08:26:21 +00:00
)
lastBubble.items.append(item)
bubbleMap[lastBubble.id] = lastBubble
2021-09-12 13:57:45 +00:00
} else {
2021-09-13 08:26:21 +00:00
let bubble = TemplateRoomChatBubble(
id: message.id,
sender: message.sender,
items: [messageItem]
2021-09-12 13:57:45 +00:00
)
2021-09-13 08:26:21 +00:00
bubbleOrder.append(bubble.id)
bubbleMap[bubble.id] = bubble
2021-09-12 13:57:45 +00:00
}
}
2021-09-13 08:26:21 +00:00
return bubbleOrder.compactMap({ bubbleMap[$0] })
2021-09-12 13:57:45 +00:00
}
// MARK: - Public
override func process(viewAction: TemplateRoomChatViewAction) {
2021-09-12 13:57:45 +00:00
switch viewAction {
case .cancel:
cancel()
case .done:
done()
case .sendMessage:
templateRoomChatService.send(message: state.bindings.messageInput)
dispatch(action: .clearMessageInput)
2021-09-12 13:57:45 +00:00
}
}
override class func reducer(state: inout TemplateRoomChatViewState, action: TemplateRoomChatStateAction) {
2021-09-12 13:57:45 +00:00
switch action {
case .clearMessageInput:
state.bindings.messageInput = ""
2021-09-12 13:57:45 +00:00
case .updateBubbles(let bubbles):
state.bubbles = bubbles
}
UILog.debug("[TemplateRoomChatViewModel] reducer with action \(action) produced state: \(state)")
}
// MARK: - Private
2021-09-12 13:57:45 +00:00
private func done() {
completion?(.done)
}
private func cancel() {
completion?(.cancel)
}
}