Move last message timestamp formatting to the room summary provider and its background processing queue

This commit is contained in:
Stefan Ceriu
2023-01-26 13:38:51 +02:00
committed by Stefan Ceriu
parent a8aa61de29
commit b43165049c
6 changed files with 18 additions and 20 deletions

View File

@@ -229,10 +229,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
room.name = details.name
room.hasUnreads = details.unreadNotificationCount > 0
room.lastMessage = details.lastMessage
if let lastMessageTimestamp = details.lastMessageTimestamp {
room.timestamp = lastMessageTimestamp.formattedMinimal()
}
room.timestamp = details.lastMessageFormattedTimestamp
roomsForIdentifiers[details.id] = room

View File

@@ -26,7 +26,8 @@ class RoomProxy: RoomProxyProtocol {
private let backgroundTaskService: BackgroundTaskServiceProtocol
private let backgroundTaskName = "SendRoomEvent"
private let serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomproxy.serial", qos: .utility)
private let userInitiatedDispatchQueue = DispatchQueue(label: "io.element.elementx.roomproxy.userinitiated", qos: .userInitiated)
private let lowPriorityDispatchQueue = DispatchQueue(label: "io.element.elementx.roomproxy.lowpriority", qos: .utility)
private var sendMessageBackgroundTask: BackgroundTaskProtocol?
@@ -114,7 +115,7 @@ class RoomProxy: RoomProxyProtocol {
func loadAvatarURLForUserId(_ userId: String) async -> Result<URL?, RoomProxyError> {
do {
guard let urlString = try await Task.dispatch(on: serialDispatchQueue, {
guard let urlString = try await Task.dispatch(on: lowPriorityDispatchQueue, {
try self.room.memberAvatarUrl(userId: userId)
}) else {
return .success(nil)
@@ -138,7 +139,7 @@ class RoomProxy: RoomProxyProtocol {
func loadDisplayNameForUserId(_ userId: String) async -> Result<String?, RoomProxyError> {
do {
let displayName = try await Task.dispatch(on: serialDispatchQueue) {
let displayName = try await Task.dispatch(on: lowPriorityDispatchQueue) {
try self.room.memberDisplayName(userId: userId)
}
update(displayName: displayName, forUserId: userId)
@@ -175,7 +176,7 @@ class RoomProxy: RoomProxyProtocol {
sendMessageBackgroundTask?.stop()
}
return await Task.dispatch(on: serialDispatchQueue) {
return await Task.dispatch(on: userInitiatedDispatchQueue) {
do {
try self.room.sendReadReceipt(eventId: eventID)
return .success(())
@@ -193,7 +194,7 @@ class RoomProxy: RoomProxyProtocol {
let transactionId = genTransactionId()
return await Task.dispatch(on: serialDispatchQueue) {
return await Task.dispatch(on: userInitiatedDispatchQueue) {
do {
if let eventID {
try self.room.sendReply(msg: message, inReplyToEventId: eventID, txnId: transactionId)
@@ -214,7 +215,7 @@ class RoomProxy: RoomProxyProtocol {
sendMessageBackgroundTask?.stop()
}
return await Task.dispatch(on: serialDispatchQueue) {
return await Task.dispatch(on: userInitiatedDispatchQueue) {
do {
try self.room.sendReaction(eventId: eventID, key: reaction)
return .success(())
@@ -232,7 +233,7 @@ class RoomProxy: RoomProxyProtocol {
let transactionId = genTransactionId()
return await Task.dispatch(on: serialDispatchQueue) {
return await Task.dispatch(on: userInitiatedDispatchQueue) {
do {
try self.room.edit(newMsg: newMessage, originalEventId: eventID, txnId: transactionId)
return .success(())
@@ -250,7 +251,7 @@ class RoomProxy: RoomProxyProtocol {
let transactionID = genTransactionId()
return await Task.dispatch(on: serialDispatchQueue) {
return await Task.dispatch(on: userInitiatedDispatchQueue) {
do {
try self.room.redact(eventId: eventID, reason: nil, txnId: transactionID)
return .success(())

View File

@@ -53,21 +53,21 @@ class MockRoomSummaryProvider: RoomSummaryProviderProtocol {
isDirect: true,
avatarURL: nil,
lastMessage: AttributedString("Prosciutto beef ribs pancetta filet mignon kevin hamburger, chuck ham venison picanha. Beef ribs chislic turkey biltong tenderloin."),
lastMessageTimestamp: .distantPast,
lastMessageFormattedTimestamp: "Now",
unreadNotificationCount: 4)),
.filled(details: RoomSummaryDetails(id: "2",
name: "Second room",
isDirect: true,
avatarURL: URL.picturesDirectory,
lastMessage: nil,
lastMessageTimestamp: nil,
lastMessageFormattedTimestamp: nil,
unreadNotificationCount: 1)),
.filled(details: RoomSummaryDetails(id: "3",
name: "Third room",
isDirect: true,
avatarURL: nil,
lastMessage: try? AttributedString(markdown: "**@mock:client.com**: T-bone beef ribs bacon"),
lastMessageTimestamp: .distantPast,
lastMessageFormattedTimestamp: "Later",
unreadNotificationCount: 0)),
.empty
]

View File

@@ -22,7 +22,7 @@ struct RoomSummaryDetails {
let isDirect: Bool
let avatarURL: URL?
let lastMessage: AttributedString?
let lastMessageTimestamp: Date?
let lastMessageFormattedTimestamp: String?
let unreadNotificationCount: UInt
}

View File

@@ -97,14 +97,14 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
}
var attributedLastMessage: AttributedString?
var lastMessageTimestamp: Date?
var lastMessageFormattedTimestamp: String?
// Dispatch onto another queue otherwise the rust method latestRoomMessage crashes.
// This will be fixed when we get async uniffi support.
DispatchQueue.global(qos: .default).sync {
if let latestRoomMessage = room.latestRoomMessage() {
let lastMessage = EventTimelineItemProxy(item: latestRoomMessage)
lastMessageTimestamp = lastMessage.timestamp
lastMessageFormattedTimestamp = lastMessage.timestamp.formattedMinimal()
attributedLastMessage = eventStringBuilder.buildAttributedString(for: lastMessage)
}
}
@@ -116,7 +116,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
isDirect: room.isDm() ?? false,
avatarURL: avatarURL,
lastMessage: attributedLastMessage,
lastMessageTimestamp: lastMessageTimestamp,
lastMessageFormattedTimestamp: lastMessageFormattedTimestamp,
unreadNotificationCount: UInt(room.unreadNotifications().notificationCount()))
return invalidated ? .invalidated(details: details) : .filled(details: details)

View File

@@ -183,7 +183,7 @@ class LoggingTests: XCTestCase {
isDirect: true,
avatarURL: nil,
lastMessage: AttributedString(lastMessage),
lastMessageTimestamp: .now,
lastMessageFormattedTimestamp: "Now",
unreadNotificationCount: 0)
// When logging that value