From fb947f2dba719aef45dff35547c2d4d01575480d Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 14 Sep 2022 09:40:00 +0100 Subject: [PATCH] #178: Add redaction menu (disabled). --- .../Screens/RoomScreen/RoomScreenModels.swift | 1 + .../RoomScreen/RoomScreenViewModel.swift | 19 ++++++++++++++++++- .../View/TimelineItemContextMenu.swift | 16 ++++++++++------ .../Sources/Services/Room/MockRoomProxy.swift | 4 ++++ .../Sources/Services/Room/RoomProxy.swift | 16 ++++++++++++++++ .../Services/Room/RoomProxyProtocol.swift | 3 +++ .../Timeline/MockRoomTimelineController.swift | 2 ++ .../Timeline/RoomTimelineController.swift | 7 +++++++ .../RoomTimelineControllerProtocol.swift | 2 ++ .../Timeline/RoomTimelineProvider.swift | 9 +++++++++ .../RoomTimelineProviderProtocol.swift | 3 +++ changelog.d/178.wip | 1 + project.yml | 6 +++--- 13 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 changelog.d/178.wip diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenModels.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenModels.swift index 907701bed..7ef42e023 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenModels.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenModels.swift @@ -23,6 +23,7 @@ enum TimelineItemContextMenuAction: Hashable { case copy case quote case copyPermalink + case redact } enum RoomScreenViewAction { diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift index 347eb70c7..ebeb2f4d4 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift @@ -119,7 +119,16 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol return [] } - return [.copy, .quote, .copyPermalink] + let actions: [TimelineItemContextMenuAction] = [ + .copy, .quote, .copyPermalink + ] + + #warning("Outgoing actions to be handled with the new Timeline API.") +// if timelineItem.isOutgoing { +// actions.append(.redact) +// } + + return actions } private func processContentMenuAction(_ action: TimelineItemContextMenuAction, itemId: String) { @@ -140,6 +149,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol } catch { displayError(.alert(ElementL10n.roomTimelinePermalinkCreationFailure)) } + case .redact: + redactItem(itemId) } } @@ -151,4 +162,10 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol message: message) } } + + private func redactItem(_ itemID: String) { + Task { + await timelineController.redactItem(itemID) + } + } } diff --git a/ElementX/Sources/Screens/RoomScreen/View/TimelineItemContextMenu.swift b/ElementX/Sources/Screens/RoomScreen/View/TimelineItemContextMenu.swift index f4f35287c..b7a32f75c 100644 --- a/ElementX/Sources/Screens/RoomScreen/View/TimelineItemContextMenu.swift +++ b/ElementX/Sources/Screens/RoomScreen/View/TimelineItemContextMenu.swift @@ -25,16 +25,20 @@ public struct TimelineItemContextMenu: View { ForEach(contextMenuActions, id: \.self) { item in switch item { case .copy: - Button(ElementL10n.actionCopy) { - callback(item) + Button { callback(item) } label: { + Label(ElementL10n.actionCopy, systemImage: "doc.on.doc") } case .quote: - Button(ElementL10n.actionQuote) { - callback(item) + Button { callback(item) } label: { + Label(ElementL10n.actionQuote, systemImage: "quote.bubble") } case .copyPermalink: - Button(ElementL10n.permalink) { - callback(item) + Button { callback(item) } label: { + Label(ElementL10n.permalink, systemImage: "link") + } + case .redact: + Button(role: .destructive) { callback(item) } label: { + Label(ElementL10n.messageActionItemRedact, systemImage: "trash") } } } diff --git a/ElementX/Sources/Services/Room/MockRoomProxy.swift b/ElementX/Sources/Services/Room/MockRoomProxy.swift index 2afa83977..31e2b53f5 100644 --- a/ElementX/Sources/Services/Room/MockRoomProxy.swift +++ b/ElementX/Sources/Services/Room/MockRoomProxy.swift @@ -57,4 +57,8 @@ struct MockRoomProxy: RoomProxyProtocol { func sendMessage(_ message: String) async -> Result { .failure(.failedSendingMessage) } + + func redactItem(_ itemId: String) async -> Result { + .failure(.failedRedactingEvent) + } } diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index e21924cc6..9a6805ba6 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -194,6 +194,22 @@ class RoomProxy: RoomProxyProtocol { .value } + func redactItem(_ itemId: String) async -> Result { + return .failure(.failedRedactingEvent) + #warning("Redactions to be enabled on next SDK release.") +// let transactionID = genTransactionId() +// +// return await Task { +// do { +// try room.redact(eventId: itemId, reason: nil, txnId: transactionID) +// return .success(()) +// } catch { +// return .failure(.failedRedactingEvent) +// } +// } +// .value + } + // MARK: - Private fileprivate func appendMessage(_ message: AnyMessage) { diff --git a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift index 1e0bf6b58..80ff3159b 100644 --- a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift +++ b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift @@ -24,6 +24,7 @@ enum RoomProxyError: Error { case failedRetrievingMemberAvatarURL case failedRetrievingMemberDisplayName case failedSendingMessage + case failedRedactingEvent } enum RoomProxyCallback { @@ -56,5 +57,7 @@ protocol RoomProxyProtocol { func sendMessage(_ message: String) async -> Result + func redactItem(_ itemId: String) async -> Result + var callbacks: PassthroughSubject { get } } diff --git a/ElementX/Sources/Services/Timeline/MockRoomTimelineController.swift b/ElementX/Sources/Services/Timeline/MockRoomTimelineController.swift index 31c972030..5ceaddd1b 100644 --- a/ElementX/Sources/Services/Timeline/MockRoomTimelineController.swift +++ b/ElementX/Sources/Services/Timeline/MockRoomTimelineController.swift @@ -65,4 +65,6 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol { func processItemDisappearance(_ itemId: String) async { } func sendMessage(_ message: String) async { } + + func redactItem(_ itemId: String) async { } } diff --git a/ElementX/Sources/Services/Timeline/RoomTimelineController.swift b/ElementX/Sources/Services/Timeline/RoomTimelineController.swift index 12e2ef8f9..9da685d47 100644 --- a/ElementX/Sources/Services/Timeline/RoomTimelineController.swift +++ b/ElementX/Sources/Services/Timeline/RoomTimelineController.swift @@ -104,6 +104,13 @@ class RoomTimelineController: RoomTimelineControllerProtocol { } } + func redactItem(_ itemId: String) async { + switch await timelineProvider.redactItem(itemId) { + default: + break + } + } + // MARK: - Private @objc private func contentSizeCategoryDidChange() { diff --git a/ElementX/Sources/Services/Timeline/RoomTimelineControllerProtocol.swift b/ElementX/Sources/Services/Timeline/RoomTimelineControllerProtocol.swift index 3b93965ea..82d03088c 100644 --- a/ElementX/Sources/Services/Timeline/RoomTimelineControllerProtocol.swift +++ b/ElementX/Sources/Services/Timeline/RoomTimelineControllerProtocol.swift @@ -40,4 +40,6 @@ protocol RoomTimelineControllerProtocol { func paginateBackwards(_ count: UInt) async -> Result func sendMessage(_ message: String) async + + func redactItem(_ itemId: String) async } diff --git a/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift b/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift index 78609c46f..6c62ec626 100644 --- a/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift +++ b/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift @@ -59,4 +59,13 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol { return .failure(.failedSendingMessage) } } + + func redactItem(_ itemID: String) async -> Result { + switch await roomProxy.redactItem(itemID) { + case .success: + return .success(()) + case .failure: + return .failure(.failedRedactingItem) + } + } } diff --git a/ElementX/Sources/Services/Timeline/RoomTimelineProviderProtocol.swift b/ElementX/Sources/Services/Timeline/RoomTimelineProviderProtocol.swift index 15bbba1c4..3ac49263a 100644 --- a/ElementX/Sources/Services/Timeline/RoomTimelineProviderProtocol.swift +++ b/ElementX/Sources/Services/Timeline/RoomTimelineProviderProtocol.swift @@ -23,6 +23,7 @@ enum RoomTimelineProviderCallback { enum RoomTimelineProviderError: Error { case failedSendingMessage + case failedRedactingItem case generic } @@ -35,4 +36,6 @@ protocol RoomTimelineProviderProtocol { func paginateBackwards(_ count: UInt) async -> Result func sendMessage(_ message: String) async -> Result + + func redactItem(_ itemID: String) async -> Result } diff --git a/changelog.d/178.wip b/changelog.d/178.wip new file mode 100644 index 000000000..02ebc66b9 --- /dev/null +++ b/changelog.d/178.wip @@ -0,0 +1 @@ +Add a redactions context menu item (disabled for now whilst waiting for SDK releases). diff --git a/project.yml b/project.yml index 7f07c1a20..dfccf1c8b 100644 --- a/project.yml +++ b/project.yml @@ -32,9 +32,9 @@ include: packages: MatrixRustSDK: - url: https://github.com/matrix-org/matrix-rust-components-swift - exactVersion: 1.0.13-alpha - # path: ../matrix-rust-components-swift + # url: https://github.com/matrix-org/matrix-rust-components-swift.git + # exactVersion: 1.0.11-alpha + path: ../matrix-rust-components-swift DesignKit: path: ./ AnalyticsEvents: