mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-21 09:02:44 +02:00
Configured and applied SwiftFormat
This commit is contained in:
committed by
Stefan Ceriu
parent
ff2e6ddfa7
commit
43c28d23b7
+20
-21
@@ -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? {
|
||||
|
||||
+9
-10
@@ -1,4 +1,4 @@
|
||||
//
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -14,39 +14,38 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
class MockTemplateRoomChatService: TemplateRoomChatServiceProtocol {
|
||||
|
||||
let roomName: String? = "New Vector"
|
||||
|
||||
static let amadine = TemplateRoomChatMember(id: "@amadine:matrix.org", avatarUrl: "!aaabaa:matrix.org", displayName: "Amadine")
|
||||
static let mathew = TemplateRoomChatMember(id: "@mathew:matrix.org", avatarUrl: "!bbabb:matrix.org", displayName: "Mathew")
|
||||
static let mockMessages = [
|
||||
TemplateRoomChatMessage(id: "!0:matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: "Shall I put it live?")) , sender: amadine, timestamp: Date(timeIntervalSinceNow: 60 * -3)),
|
||||
TemplateRoomChatMessage(id: "!0:matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: "Shall I put it live?")), sender: amadine, timestamp: Date(timeIntervalSinceNow: 60 * -3)),
|
||||
TemplateRoomChatMessage(id: "!1:matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: "Yea go for it! ...and then let's head to the pub")), sender: mathew, timestamp: Date(timeIntervalSinceNow: 60)),
|
||||
TemplateRoomChatMessage(id: "!2:matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: "Deal.")), sender: amadine, timestamp: Date(timeIntervalSinceNow: 60 * -2)),
|
||||
TemplateRoomChatMessage(id: "!3:matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: "Ok, Done. 🍻")), sender: amadine, timestamp: Date(timeIntervalSinceNow: 60 * -1)),
|
||||
TemplateRoomChatMessage(id: "!3:matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: "Ok, Done. 🍻")), sender: amadine, timestamp: Date(timeIntervalSinceNow: 60 * -1))
|
||||
]
|
||||
var roomInitializationStatus: CurrentValueSubject<TemplateRoomChatRoomInitializationStatus, Never>
|
||||
var chatMessagesSubject: CurrentValueSubject<[TemplateRoomChatMessage], Never>
|
||||
|
||||
init(messages: [TemplateRoomChatMessage] = mockMessages) {
|
||||
self.roomInitializationStatus = CurrentValueSubject(.notInitialized)
|
||||
self.chatMessagesSubject = CurrentValueSubject(messages)
|
||||
roomInitializationStatus = CurrentValueSubject(.notInitialized)
|
||||
chatMessagesSubject = CurrentValueSubject(messages)
|
||||
}
|
||||
|
||||
func send(textMessage: String) {
|
||||
let newMessage = TemplateRoomChatMessage(id: "!\(chatMessagesSubject.value.count):matrix.org", content: .text(TemplateRoomChatMessageTextContent(body: textMessage)), sender: Self.amadine, timestamp: Date())
|
||||
self.chatMessagesSubject.value += [newMessage]
|
||||
chatMessagesSubject.value += [newMessage]
|
||||
}
|
||||
|
||||
func simulateUpdate(initializationStatus: TemplateRoomChatRoomInitializationStatus) {
|
||||
self.roomInitializationStatus.value = initializationStatus
|
||||
roomInitializationStatus.value = initializationStatus
|
||||
}
|
||||
|
||||
func simulateUpdate(messages: [TemplateRoomChatMessage]) {
|
||||
self.chatMessagesSubject.value = messages
|
||||
chatMessagesSubject.value = messages
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
//
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -14,8 +14,8 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
protocol TemplateRoomChatServiceProtocol {
|
||||
var roomInitializationStatus: CurrentValueSubject<TemplateRoomChatRoomInitializationStatus, Never> { get }
|
||||
|
||||
Reference in New Issue
Block a user