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

144 lines
5.3 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-20 07:47:28 +00:00
var callback: ((TemplateRoomChatViewModelAction) -> Void)?
2021-09-12 13:57:45 +00:00
// MARK: - Setup
init(templateRoomChatService: TemplateRoomChatServiceProtocol) {
2021-09-12 13:57:45 +00:00
self.templateRoomChatService = templateRoomChatService
super.init(initialViewState: Self.defaultState(templateRoomChatService: templateRoomChatService))
setupMessageObserving()
setupRoomInitializationObserving()
}
2021-09-20 07:47:28 +00:00
private func setupRoomInitializationObserving() {
let initializationPublisher = templateRoomChatService
.roomInitializationStatus
.map(TemplateRoomChatStateAction.updateRoomInitializationStatus)
.eraseToAnyPublisher()
2021-09-20 07:47:28 +00:00
dispatch(actionPublisher: initializationPublisher)
}
private func setupMessageObserving() {
let messageActionPublisher = templateRoomChatService
.chatMessagesSubject
2021-09-12 13:57:45 +00:00
.map(Self.makeBubbles(messages:))
.map(TemplateRoomChatStateAction.updateBubbles)
.eraseToAnyPublisher()
dispatch(actionPublisher: messageActionPublisher)
2021-09-12 13:57:45 +00:00
}
private static func defaultState(templateRoomChatService: TemplateRoomChatServiceProtocol) -> TemplateRoomChatViewState {
let bindings = TemplateRoomChatViewModelBindings(messageInput: "")
return TemplateRoomChatViewState(roomInitializationStatus: .notInitialized, roomName: templateRoomChatService.roomName, 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
// New message content
let messageItem = TemplateRoomChatBubbleItem(
id: message.id,
timestamp: message.timestamp,
2021-09-20 07:47:28 +00:00
content: .message(message.content)
)
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
{
// if the last bubble's last message was within
// the last 5 minutes append
let item = TemplateRoomChatBubbleItem(
2021-09-13 08:26:21 +00:00
id: message.id,
timestamp: message.timestamp,
2021-09-20 07:47:28 +00:00
content: .message(message.content)
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 {
// else create a new bubble and add the message as the first item
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 .done:
done()
case .sendMessage:
2021-09-20 07:47:28 +00:00
templateRoomChatService.send(textMessage: 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 {
2021-09-20 07:47:28 +00:00
case .updateRoomInitializationStatus(let status):
state.roomInitializationStatus = status
case .clearMessageInput:
state.bindings.messageInput = ""
2021-09-12 13:57:45 +00:00
case .updateBubbles(let bubbles):
state.bubbles = bubbles
}
}
// MARK: - Private
2021-09-12 13:57:45 +00:00
private func done() {
2021-09-20 07:47:28 +00:00
callback?(.done)
2021-09-12 13:57:45 +00:00
}
}