Skip to content

Commit

Permalink
Adopt new reaction toggling API introduced in matrix-org/matrix-rust-…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Oct 16, 2024
1 parent 236d142 commit 5c1061c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 40 deletions.
6 changes: 5 additions & 1 deletion ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,11 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
MXLog.debug("Selected \(emoji) for \(itemID)")
navigationStackCoordinator.setSheetCoordinator(nil)
Task {
await self.timelineController?.toggleReaction(emoji, to: itemID)
guard let eventOrTransactionID = itemID.eventOrTransactionID else {
fatalError()
}

await self.timelineController?.toggleReaction(emoji, to: eventOrTransactionID)
}
case .dismiss:
navigationStackCoordinator.setSheetCoordinator(nil)
Expand Down
14 changes: 7 additions & 7 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14867,8 +14867,8 @@ class TimelineProxyMock: TimelineProxyProtocol {
var toggleReactionToCalled: Bool {
return toggleReactionToCallsCount > 0
}
var toggleReactionToReceivedArguments: (reaction: String, itemID: TimelineItemIdentifier)?
var toggleReactionToReceivedInvocations: [(reaction: String, itemID: TimelineItemIdentifier)] = []
var toggleReactionToReceivedArguments: (reaction: String, eventID: EventOrTransactionId)?
var toggleReactionToReceivedInvocations: [(reaction: String, eventID: EventOrTransactionId)] = []

var toggleReactionToUnderlyingReturnValue: Result<Void, TimelineProxyError>!
var toggleReactionToReturnValue: Result<Void, TimelineProxyError>! {
Expand All @@ -14894,16 +14894,16 @@ class TimelineProxyMock: TimelineProxyProtocol {
}
}
}
var toggleReactionToClosure: ((String, TimelineItemIdentifier) async -> Result<Void, TimelineProxyError>)?
var toggleReactionToClosure: ((String, EventOrTransactionId) async -> Result<Void, TimelineProxyError>)?

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async -> Result<Void, TimelineProxyError> {
func toggleReaction(_ reaction: String, to eventID: EventOrTransactionId) async -> Result<Void, TimelineProxyError> {
toggleReactionToCallsCount += 1
toggleReactionToReceivedArguments = (reaction: reaction, itemID: itemID)
toggleReactionToReceivedArguments = (reaction: reaction, eventID: eventID)
DispatchQueue.main.async {
self.toggleReactionToReceivedInvocations.append((reaction: reaction, itemID: itemID))
self.toggleReactionToReceivedInvocations.append((reaction: reaction, eventID: eventID))
}
if let toggleReactionToClosure = toggleReactionToClosure {
return await toggleReactionToClosure(reaction, itemID)
return await toggleReactionToClosure(reaction, eventID)
} else {
return toggleReactionToReturnValue
}
Expand Down
36 changes: 18 additions & 18 deletions ElementX/Sources/Mocks/Generated/SDKGeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19484,48 +19484,48 @@ open class TimelineSDKMock: MatrixRustSDK.Timeline {

//MARK: - toggleReaction

open var toggleReactionUniqueIdKeyThrowableError: Error?
var toggleReactionUniqueIdKeyUnderlyingCallsCount = 0
open var toggleReactionUniqueIdKeyCallsCount: Int {
open var toggleReactionItemIdKeyThrowableError: Error?
var toggleReactionItemIdKeyUnderlyingCallsCount = 0
open var toggleReactionItemIdKeyCallsCount: Int {
get {
if Thread.isMainThread {
return toggleReactionUniqueIdKeyUnderlyingCallsCount
return toggleReactionItemIdKeyUnderlyingCallsCount
} else {
var returnValue: Int? = nil
DispatchQueue.main.sync {
returnValue = toggleReactionUniqueIdKeyUnderlyingCallsCount
returnValue = toggleReactionItemIdKeyUnderlyingCallsCount
}

return returnValue!
}
}
set {
if Thread.isMainThread {
toggleReactionUniqueIdKeyUnderlyingCallsCount = newValue
toggleReactionItemIdKeyUnderlyingCallsCount = newValue
} else {
DispatchQueue.main.sync {
toggleReactionUniqueIdKeyUnderlyingCallsCount = newValue
toggleReactionItemIdKeyUnderlyingCallsCount = newValue
}
}
}
}
open var toggleReactionUniqueIdKeyCalled: Bool {
return toggleReactionUniqueIdKeyCallsCount > 0
open var toggleReactionItemIdKeyCalled: Bool {
return toggleReactionItemIdKeyCallsCount > 0
}
open var toggleReactionUniqueIdKeyReceivedArguments: (uniqueId: String, key: String)?
open var toggleReactionUniqueIdKeyReceivedInvocations: [(uniqueId: String, key: String)] = []
open var toggleReactionUniqueIdKeyClosure: ((String, String) async throws -> Void)?
open var toggleReactionItemIdKeyReceivedArguments: (itemId: EventOrTransactionId, key: String)?
open var toggleReactionItemIdKeyReceivedInvocations: [(itemId: EventOrTransactionId, key: String)] = []
open var toggleReactionItemIdKeyClosure: ((EventOrTransactionId, String) async throws -> Void)?

open override func toggleReaction(uniqueId: String, key: String) async throws {
if let error = toggleReactionUniqueIdKeyThrowableError {
open override func toggleReaction(itemId: EventOrTransactionId, key: String) async throws {
if let error = toggleReactionItemIdKeyThrowableError {
throw error
}
toggleReactionUniqueIdKeyCallsCount += 1
toggleReactionUniqueIdKeyReceivedArguments = (uniqueId: uniqueId, key: key)
toggleReactionItemIdKeyCallsCount += 1
toggleReactionItemIdKeyReceivedArguments = (itemId: itemId, key: key)
DispatchQueue.main.async {
self.toggleReactionUniqueIdKeyReceivedInvocations.append((uniqueId: uniqueId, key: key))
self.toggleReactionItemIdKeyReceivedInvocations.append((itemId: itemId, key: key))
}
try await toggleReactionUniqueIdKeyClosure?(uniqueId, key)
try await toggleReactionItemIdKeyClosure?(itemId, key)
}

//MARK: - unpinEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ class TimelineInteractionHandler {
case .react:
displayEmojiPicker(for: itemID)
case .toggleReaction(let key):
Task { await timelineController.toggleReaction(key, to: itemID) }
Task {
guard let eventID = itemID.eventOrTransactionID else {
fatalError()
}

await timelineController.toggleReaction(key, to: eventID)
}
case .endPoll(let pollStartID):
endPoll(pollStartID: pollStartID)
case .pin:
Expand Down
8 changes: 6 additions & 2 deletions ElementX/Sources/Screens/Timeline/TimelineViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
Task { await handleItemTapped(with: id) }
case .itemSendInfoTapped(let itemID):
handleItemSendInfoTapped(itemID: itemID)
case .toggleReaction(let emoji, let itemId):
Task { await timelineController.toggleReaction(emoji, to: itemId) }
case .toggleReaction(let emoji, let itemID):
guard let eventID = itemID.eventOrTransactionID else {
fatalError()
}

Task { await timelineController.toggleReaction(emoji, to: eventID) }
case .sendReadReceiptIfNeeded(let lastVisibleItemID):
Task { await sendReadReceiptIfNeeded(for: lastVisibleItemID) }
case .paginateBackwards:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
inReplyTo itemID: TimelineItemIdentifier?,
intentionalMentions: IntentionalMentions) async { }

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async { }
func toggleReaction(_ reaction: String, to eventID: EventOrTransactionId) async { }

func edit(_ timelineItemID: TimelineItemIdentifier,
message: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
}
}

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async {
MXLog.info("Toggle reaction in \(roomID)")
func toggleReaction(_ reaction: String, to eventOrTransactionID: EventOrTransactionId) async {
MXLog.info("Toggle reaction \(reaction) to \(eventOrTransactionID)")

switch await activeTimeline.toggleReaction(reaction, to: itemID) {
switch await activeTimeline.toggleReaction(reaction, to: eventOrTransactionID) {
case .success:
MXLog.info("Finished toggling reaction")
case .failure(let error):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protocol RoomTimelineControllerProtocol {
html: String?,
intentionalMentions: IntentionalMentions) async

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async
func toggleReaction(_ reaction: String, to eventOrTransactionID: EventOrTransactionId) async

func redact(_ itemID: TimelineItemIdentifier) async

Expand Down
10 changes: 5 additions & 5 deletions ElementX/Sources/Services/Timeline/TimelineProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,15 @@ final class TimelineProxy: TimelineProxyProtocol {
}
}

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async -> Result<Void, TimelineProxyError> {
MXLog.info("Toggling reaction for event: \(itemID)")
func toggleReaction(_ reaction: String, to eventOrTransactionID: EventOrTransactionId) async -> Result<Void, TimelineProxyError> {
MXLog.info("Toggling reaction \(reaction) for event: \(eventOrTransactionID)")

do {
try await timeline.toggleReaction(uniqueId: itemID.uniqueID, key: reaction)
MXLog.info("Finished toggling reaction for event: \(itemID)")
try await timeline.toggleReaction(itemId: eventOrTransactionID, key: reaction)
MXLog.info("Finished toggling reaction for event: \(eventOrTransactionID)")
return .success(())
} catch {
MXLog.error("Failed toggling reaction for event: \(itemID)")
MXLog.error("Failed toggling reaction for event: \(eventOrTransactionID)")
return .failure(.sdkError(error))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protocol TimelineProxyProtocol {
inReplyTo eventID: String?,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError>

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async -> Result<Void, TimelineProxyError>
func toggleReaction(_ reaction: String, to eventID: EventOrTransactionId) async -> Result<Void, TimelineProxyError>

// Polls
func createPoll(question: String, answers: [String], pollKind: Poll.Kind) async -> Result<Void, TimelineProxyError>
Expand Down

0 comments on commit 5c1061c

Please sign in to comment.