Skip to content

Commit

Permalink
Move last message timestamp formatting to the room summary provider a…
Browse files Browse the repository at this point in the history
…nd its background processing queue
  • Loading branch information
stefanceriu committed Jan 26, 2023
1 parent 1433100 commit 413b433
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions ElementX/Sources/Services/Room/RoomProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(())
Expand All @@ -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)
Expand All @@ -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(())
Expand All @@ -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(())
Expand All @@ -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(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct RoomSummaryDetails {
let isDirect: Bool
let avatarURL: URL?
let lastMessage: AttributedString?
let lastMessageTimestamp: Date?
let lastMessageFormattedTimestamp: String?
let unreadNotificationCount: UInt
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/Sources/LoggingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class LoggingTests: XCTestCase {
isDirect: true,
avatarURL: nil,
lastMessage: AttributedString(lastMessage),
lastMessageTimestamp: .now,
lastMessageFormattedTimestamp: "Now",
unreadNotificationCount: 0)

// When logging that value
Expand Down

0 comments on commit 413b433

Please sign in to comment.