mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-18 07:28:28 +02:00
Configured and applied SwiftFormat
This commit is contained in:
committed by
Stefan Ceriu
parent
ff2e6ddfa7
commit
43c28d23b7
@@ -1,4 +1,4 @@
|
||||
//
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -14,11 +14,10 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
class TemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
@@ -29,22 +28,23 @@ class TemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
private var eventBatch: [MXEvent]
|
||||
private var roomListenerReference: Any?
|
||||
|
||||
|
||||
// MARK: Public
|
||||
|
||||
private(set) var chatMessagesSubject: CurrentValueSubject<[TemplateRoomChatMessage], Never>
|
||||
private(set) var roomInitializationStatus: CurrentValueSubject<TemplateRoomChatRoomInitializationStatus, Never>
|
||||
|
||||
var roomName: String? {
|
||||
self.room.summary.displayname
|
||||
room.summary.displayname
|
||||
}
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(room: MXRoom) {
|
||||
self.room = room
|
||||
self.eventFormatter = EventFormatter(matrixSession: room.mxSession)
|
||||
self.chatMessagesSubject = CurrentValueSubject([])
|
||||
self.roomInitializationStatus = CurrentValueSubject(.notInitialized)
|
||||
self.eventBatch = [MXEvent]()
|
||||
eventFormatter = EventFormatter(matrixSession: room.mxSession)
|
||||
chatMessagesSubject = CurrentValueSubject([])
|
||||
roomInitializationStatus = CurrentValueSubject(.notInitialized)
|
||||
eventBatch = [MXEvent]()
|
||||
initializeRoom()
|
||||
}
|
||||
|
||||
@@ -54,14 +54,15 @@ class TemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
}
|
||||
|
||||
// MARK: Public
|
||||
|
||||
func send(textMessage: String) {
|
||||
var localEcho: MXEvent? = nil
|
||||
var localEcho: MXEvent?
|
||||
room.sendTextMessage(textMessage, threadId: nil, localEcho: &localEcho, completion: { _ in })
|
||||
}
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private func initializeRoom(){
|
||||
private func initializeRoom() {
|
||||
room.liveTimeline { [weak self] timeline in
|
||||
guard let self = self,
|
||||
let timeline = timeline
|
||||
@@ -70,21 +71,20 @@ class TemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
}
|
||||
self.timeline = timeline
|
||||
timeline.resetPagination()
|
||||
self.roomListenerReference = timeline.listenToEvents([.roomMessage], { [weak self] event, direction, roomState in
|
||||
self.roomListenerReference = timeline.listenToEvents([.roomMessage]) { [weak self] event, direction, _ in
|
||||
guard let self = self else { return }
|
||||
if direction == .backwards {
|
||||
self.eventBatch.append(event)
|
||||
} else {
|
||||
self.chatMessagesSubject.value += self.mapChatMessages(from: [event])
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
timeline.paginate(200, direction: .backwards, onlyFromStore: false) { result in
|
||||
guard result.isSuccess else {
|
||||
self.roomInitializationStatus.value = .failedToInitialize
|
||||
return
|
||||
}
|
||||
let sortedBatch = self.eventBatch.sorted(by: { $0.originServerTs < $1.originServerTs})
|
||||
let sortedBatch = self.eventBatch.sorted(by: { $0.originServerTs < $1.originServerTs })
|
||||
self.chatMessagesSubject.value = self.mapChatMessages(from: sortedBatch)
|
||||
self.roomInitializationStatus.value = .initialized
|
||||
}
|
||||
@@ -92,15 +92,14 @@ class TemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
}
|
||||
|
||||
private func mapChatMessages(from events: [MXEvent]) -> [TemplateRoomChatMessage] {
|
||||
return events
|
||||
.filter({ event in
|
||||
events
|
||||
.filter { event in
|
||||
event.type == kMXEventTypeStringRoomMessage
|
||||
&& event.content[kMXMessageTypeKey] as? String == kMXMessageTypeText
|
||||
|
||||
// TODO: New to our SwiftUI Template? Why not implement another message type like image?
|
||||
|
||||
})
|
||||
.compactMap({ event -> TemplateRoomChatMessage? in
|
||||
}
|
||||
.compactMap { event -> TemplateRoomChatMessage? in
|
||||
guard let eventId = event.eventId,
|
||||
let body = event.content[kMXMessageBodyKey] as? String,
|
||||
let sender = senderForMessage(event: event)
|
||||
@@ -112,7 +111,7 @@ class TemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
sender: sender,
|
||||
timestamp: Date(timeIntervalSince1970: TimeInterval(event.originServerTs / 1000))
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private func senderForMessage(event: MXEvent) -> TemplateRoomChatMember? {
|
||||
|
||||
Reference in New Issue
Block a user