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 1433100535
commit 413b433806
6 changed files with 18 additions and 20 deletions
@@ -229,10 +229,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
room.name = details.name room.name = details.name
room.hasUnreads = details.unreadNotificationCount > 0 room.hasUnreads = details.unreadNotificationCount > 0
room.lastMessage = details.lastMessage room.lastMessage = details.lastMessage
room.timestamp = details.lastMessageFormattedTimestamp
if let lastMessageTimestamp = details.lastMessageTimestamp {
room.timestamp = lastMessageTimestamp.formattedMinimal()
}
roomsForIdentifiers[details.id] = room roomsForIdentifiers[details.id] = room
@@ -26,7 +26,8 @@ class RoomProxy: RoomProxyProtocol {
private let backgroundTaskService: BackgroundTaskServiceProtocol private let backgroundTaskService: BackgroundTaskServiceProtocol
private let backgroundTaskName = "SendRoomEvent" 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? private var sendMessageBackgroundTask: BackgroundTaskProtocol?
@@ -114,7 +115,7 @@ class RoomProxy: RoomProxyProtocol {
func loadAvatarURLForUserId(_ userId: String) async -> Result<URL?, RoomProxyError> { func loadAvatarURLForUserId(_ userId: String) async -> Result<URL?, RoomProxyError> {
do { 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) try self.room.memberAvatarUrl(userId: userId)
}) else { }) else {
return .success(nil) return .success(nil)
@@ -138,7 +139,7 @@ class RoomProxy: RoomProxyProtocol {
func loadDisplayNameForUserId(_ userId: String) async -> Result<String?, RoomProxyError> { func loadDisplayNameForUserId(_ userId: String) async -> Result<String?, RoomProxyError> {
do { do {
let displayName = try await Task.dispatch(on: serialDispatchQueue) { let displayName = try await Task.dispatch(on: lowPriorityDispatchQueue) {
try self.room.memberDisplayName(userId: userId) try self.room.memberDisplayName(userId: userId)
} }
update(displayName: displayName, forUserId: userId) update(displayName: displayName, forUserId: userId)
@@ -175,7 +176,7 @@ class RoomProxy: RoomProxyProtocol {
sendMessageBackgroundTask?.stop() sendMessageBackgroundTask?.stop()
} }
return await Task.dispatch(on: serialDispatchQueue) { return await Task.dispatch(on: userInitiatedDispatchQueue) {
do { do {
try self.room.sendReadReceipt(eventId: eventID) try self.room.sendReadReceipt(eventId: eventID)
return .success(()) return .success(())
@@ -193,7 +194,7 @@ class RoomProxy: RoomProxyProtocol {
let transactionId = genTransactionId() let transactionId = genTransactionId()
return await Task.dispatch(on: serialDispatchQueue) { return await Task.dispatch(on: userInitiatedDispatchQueue) {
do { do {
if let eventID { if let eventID {
try self.room.sendReply(msg: message, inReplyToEventId: eventID, txnId: transactionId) try self.room.sendReply(msg: message, inReplyToEventId: eventID, txnId: transactionId)
@@ -214,7 +215,7 @@ class RoomProxy: RoomProxyProtocol {
sendMessageBackgroundTask?.stop() sendMessageBackgroundTask?.stop()
} }
return await Task.dispatch(on: serialDispatchQueue) { return await Task.dispatch(on: userInitiatedDispatchQueue) {
do { do {
try self.room.sendReaction(eventId: eventID, key: reaction) try self.room.sendReaction(eventId: eventID, key: reaction)
return .success(()) return .success(())
@@ -232,7 +233,7 @@ class RoomProxy: RoomProxyProtocol {
let transactionId = genTransactionId() let transactionId = genTransactionId()
return await Task.dispatch(on: serialDispatchQueue) { return await Task.dispatch(on: userInitiatedDispatchQueue) {
do { do {
try self.room.edit(newMsg: newMessage, originalEventId: eventID, txnId: transactionId) try self.room.edit(newMsg: newMessage, originalEventId: eventID, txnId: transactionId)
return .success(()) return .success(())
@@ -250,7 +251,7 @@ class RoomProxy: RoomProxyProtocol {
let transactionID = genTransactionId() let transactionID = genTransactionId()
return await Task.dispatch(on: serialDispatchQueue) { return await Task.dispatch(on: userInitiatedDispatchQueue) {
do { do {
try self.room.redact(eventId: eventID, reason: nil, txnId: transactionID) try self.room.redact(eventId: eventID, reason: nil, txnId: transactionID)
return .success(()) return .success(())
@@ -53,21 +53,21 @@ class MockRoomSummaryProvider: RoomSummaryProviderProtocol {
isDirect: true, isDirect: true,
avatarURL: nil, avatarURL: nil,
lastMessage: AttributedString("Prosciutto beef ribs pancetta filet mignon kevin hamburger, chuck ham venison picanha. Beef ribs chislic turkey biltong tenderloin."), 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)), unreadNotificationCount: 4)),
.filled(details: RoomSummaryDetails(id: "2", .filled(details: RoomSummaryDetails(id: "2",
name: "Second room", name: "Second room",
isDirect: true, isDirect: true,
avatarURL: URL.picturesDirectory, avatarURL: URL.picturesDirectory,
lastMessage: nil, lastMessage: nil,
lastMessageTimestamp: nil, lastMessageFormattedTimestamp: nil,
unreadNotificationCount: 1)), unreadNotificationCount: 1)),
.filled(details: RoomSummaryDetails(id: "3", .filled(details: RoomSummaryDetails(id: "3",
name: "Third room", name: "Third room",
isDirect: true, isDirect: true,
avatarURL: nil, avatarURL: nil,
lastMessage: try? AttributedString(markdown: "**@mock:client.com**: T-bone beef ribs bacon"), lastMessage: try? AttributedString(markdown: "**@mock:client.com**: T-bone beef ribs bacon"),
lastMessageTimestamp: .distantPast, lastMessageFormattedTimestamp: "Later",
unreadNotificationCount: 0)), unreadNotificationCount: 0)),
.empty .empty
] ]
@@ -22,7 +22,7 @@ struct RoomSummaryDetails {
let isDirect: Bool let isDirect: Bool
let avatarURL: URL? let avatarURL: URL?
let lastMessage: AttributedString? let lastMessage: AttributedString?
let lastMessageTimestamp: Date? let lastMessageFormattedTimestamp: String?
let unreadNotificationCount: UInt let unreadNotificationCount: UInt
} }
@@ -97,14 +97,14 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
} }
var attributedLastMessage: AttributedString? var attributedLastMessage: AttributedString?
var lastMessageTimestamp: Date? var lastMessageFormattedTimestamp: String?
// Dispatch onto another queue otherwise the rust method latestRoomMessage crashes. // Dispatch onto another queue otherwise the rust method latestRoomMessage crashes.
// This will be fixed when we get async uniffi support. // This will be fixed when we get async uniffi support.
DispatchQueue.global(qos: .default).sync { DispatchQueue.global(qos: .default).sync {
if let latestRoomMessage = room.latestRoomMessage() { if let latestRoomMessage = room.latestRoomMessage() {
let lastMessage = EventTimelineItemProxy(item: latestRoomMessage) let lastMessage = EventTimelineItemProxy(item: latestRoomMessage)
lastMessageTimestamp = lastMessage.timestamp lastMessageFormattedTimestamp = lastMessage.timestamp.formattedMinimal()
attributedLastMessage = eventStringBuilder.buildAttributedString(for: lastMessage) attributedLastMessage = eventStringBuilder.buildAttributedString(for: lastMessage)
} }
} }
@@ -116,7 +116,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
isDirect: room.isDm() ?? false, isDirect: room.isDm() ?? false,
avatarURL: avatarURL, avatarURL: avatarURL,
lastMessage: attributedLastMessage, lastMessage: attributedLastMessage,
lastMessageTimestamp: lastMessageTimestamp, lastMessageFormattedTimestamp: lastMessageFormattedTimestamp,
unreadNotificationCount: UInt(room.unreadNotifications().notificationCount())) unreadNotificationCount: UInt(room.unreadNotifications().notificationCount()))
return invalidated ? .invalidated(details: details) : .filled(details: details) return invalidated ? .invalidated(details: details) : .filled(details: details)
+1 -1
View File
@@ -183,7 +183,7 @@ class LoggingTests: XCTestCase {
isDirect: true, isDirect: true,
avatarURL: nil, avatarURL: nil,
lastMessage: AttributedString(lastMessage), lastMessage: AttributedString(lastMessage),
lastMessageTimestamp: .now, lastMessageFormattedTimestamp: "Now",
unreadNotificationCount: 0) unreadNotificationCount: 0)
// When logging that value // When logging that value