From 413b433806ed0e20415267064fde25ddb615fdde Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Thu, 26 Jan 2023 13:38:51 +0200 Subject: [PATCH] Move last message timestamp formatting to the room summary provider and its background processing queue --- .../HomeScreen/HomeScreenViewModel.swift | 5 +---- ElementX/Sources/Services/Room/RoomProxy.swift | 17 +++++++++-------- .../RoomSummary/MockRoomSummaryProvider.swift | 6 +++--- .../Room/RoomSummary/RoomSummaryDetails.swift | 2 +- .../Room/RoomSummary/RoomSummaryProvider.swift | 6 +++--- UnitTests/Sources/LoggingTests.swift | 2 +- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift index 54a178f479..619ae9b141 100644 --- a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift +++ b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift @@ -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 diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index db29cc3836..e95b7fc97b 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -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 { 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 { 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(()) diff --git a/ElementX/Sources/Services/Room/RoomSummary/MockRoomSummaryProvider.swift b/ElementX/Sources/Services/Room/RoomSummary/MockRoomSummaryProvider.swift index bb9fc6d5fe..60fba889fe 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/MockRoomSummaryProvider.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/MockRoomSummaryProvider.swift @@ -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 ] diff --git a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryDetails.swift b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryDetails.swift index 6d39c87b2e..53a4831a54 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryDetails.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryDetails.swift @@ -22,7 +22,7 @@ struct RoomSummaryDetails { let isDirect: Bool let avatarURL: URL? let lastMessage: AttributedString? - let lastMessageTimestamp: Date? + let lastMessageFormattedTimestamp: String? let unreadNotificationCount: UInt } diff --git a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift index 0765348a2e..381bd5ec88 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift @@ -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) diff --git a/UnitTests/Sources/LoggingTests.swift b/UnitTests/Sources/LoggingTests.swift index c4acc43f4d..f76deb1b89 100644 --- a/UnitTests/Sources/LoggingTests.swift +++ b/UnitTests/Sources/LoggingTests.swift @@ -183,7 +183,7 @@ class LoggingTests: XCTestCase { isDirect: true, avatarURL: nil, lastMessage: AttributedString(lastMessage), - lastMessageTimestamp: .now, + lastMessageFormattedTimestamp: "Now", unreadNotificationCount: 0) // When logging that value