Skip to content

Commit

Permalink
Reply to a poll (#2087)
Browse files Browse the repository at this point in the history
* Refactor TimelineItemReplyDetails for polls

* Fix reply for polls

* Update reply details factory

* Revert Poll Hashable conformance

* PR comments
  • Loading branch information
alfogrillo authored Nov 15, 2023
1 parent 0b9da83 commit 9f1092b
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 150 deletions.
49 changes: 25 additions & 24 deletions ElementX/Sources/Screens/ComposerToolbar/View/MessageComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ struct MessageComposer: View {
let editCancellationAction: () -> Void
let onAppearAction: () -> Void
@FocusState private var focused: Bool

@State private var composerTranslation: CGFloat = 0

var body: some View {
VStack(spacing: 0) {
if showResizeGrabber {
resizeGrabber
}

let borderRadius: CGFloat = 21
mainContent
.padding(.horizontal, 12.0)
Expand All @@ -60,11 +60,11 @@ struct MessageComposer: View {
}
.gesture(showResizeGrabber ? dragGesture : nil)
}

// MARK: - Private

@State private var composerFrame = CGRect.zero

private var mainContent: some View {
VStack(alignment: .leading, spacing: -6) {
header
Expand Down Expand Up @@ -187,34 +187,34 @@ struct MessageComposer_Previews: PreviewProvider, TestablePreview {
static let viewModel = RoomScreenViewModel.mock

static let replyTypes: [TimelineItemReplyDetails] = [
.loaded(sender: .init(id: "Dave"), contentType: .audio(.init(body: "Audio: Ride the lightning", duration: 100, waveform: nil, source: nil, contentType: nil))),
.loaded(sender: .init(id: "James"), contentType: .emote(.init(body: "Emote: James thinks he's the phantom lord"))),
.loaded(sender: .init(id: "Robert"), contentType: .file(.init(body: "File: Crash course in brain surgery.pdf", source: nil, thumbnailSource: nil, contentType: nil))),
.loaded(sender: .init(id: "Cliff"), contentType: .image(.init(body: "Image: Pushead",
source: .init(url: .picturesDirectory, mimeType: nil),
thumbnailSource: .init(url: .picturesDirectory, mimeType: nil)))),
.loaded(sender: .init(id: "Jason"), contentType: .notice(.init(body: "Notice: Too far gone?"))),
.loaded(sender: .init(id: "Kirk"), contentType: .text(.init(body: "Text: Where the wild things are"))),
.loaded(sender: .init(id: "Lars"), contentType: .video(.init(body: "Video: Through the never",
duration: 100,
source: nil,
thumbnailSource: .init(url: .picturesDirectory, mimeType: nil)))),
.loaded(sender: .init(id: "Dave"), eventContent: .message(.audio(.init(body: "Audio: Ride the lightning", duration: 100, waveform: nil, source: nil, contentType: nil)))),
.loaded(sender: .init(id: "James"), eventContent: .message(.emote(.init(body: "Emote: James thinks he's the phantom lord")))),
.loaded(sender: .init(id: "Robert"), eventContent: .message(.file(.init(body: "File: Crash course in brain surgery.pdf", source: nil, thumbnailSource: nil, contentType: nil)))),
.loaded(sender: .init(id: "Cliff"), eventContent: .message(.image(.init(body: "Image: Pushead",
source: .init(url: .picturesDirectory, mimeType: nil),
thumbnailSource: .init(url: .picturesDirectory, mimeType: nil))))),
.loaded(sender: .init(id: "Jason"), eventContent: .message(.notice(.init(body: "Notice: Too far gone?")))),
.loaded(sender: .init(id: "Kirk"), eventContent: .message(.text(.init(body: "Text: Where the wild things are")))),
.loaded(sender: .init(id: "Lars"), eventContent: .message(.video(.init(body: "Video: Through the never",
duration: 100,
source: nil,
thumbnailSource: .init(url: .picturesDirectory, mimeType: nil))))),
.loading(eventID: "")
]

static func messageComposer(_ content: String = "",
sendingDisabled: Bool = false,
mode: RoomScreenComposerMode = .default) -> MessageComposer {
let viewModel = WysiwygComposerViewModel(minHeight: 22,
maxExpandedHeight: 250)
viewModel.setMarkdownContent(content)

let composerView = WysiwygComposerView(placeholder: L10n.richTextEditorComposerPlaceholder,
viewModel: viewModel,
itemProviderHelper: nil,
keyCommandHandler: nil,
pasteHandler: nil)

return MessageComposer(composerView: composerView,
mode: mode,
showResizeGrabber: false,
Expand All @@ -225,20 +225,21 @@ struct MessageComposer_Previews: PreviewProvider, TestablePreview {
editCancellationAction: { },
onAppearAction: { viewModel.setup() })
}

static var previews: some View {
VStack(spacing: 8) {
messageComposer(sendingDisabled: true)

messageComposer("Some message",
mode: .edit(originalItemId: .random))

messageComposer(mode: .reply(itemID: .random,
replyDetails: .loaded(sender: .init(id: "Kirk"),
contentType: .text(.init(body: "Text: Where the wild things are"))), isThread: false))
eventContent: .message(.text(.init(body: "Text: Where the wild things are")))),
isThread: false))
}
.padding(.horizontal)

ScrollView {
VStack(spacing: 8) {
ForEach(replyTypes, id: \.self) { replyDetails in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ class RoomScreenInteractionHandler {
}
case .reply:
let replyInfo = buildReplyInfo(for: eventTimelineItem)
let replyDetails = TimelineItemReplyDetails.loaded(sender: eventTimelineItem.sender, contentType: replyInfo.type)

let replyDetails = TimelineItemReplyDetails.loaded(sender: eventTimelineItem.sender, eventContent: replyInfo.type)
actionsSubject.send(.composer(action: .setMode(mode: .reply(itemID: eventTimelineItem.id, replyDetails: replyDetails, isThread: replyInfo.isThread))))
case .forward(let itemID):
actionsSubject.send(.displayMessageForwarding(itemID: itemID))
Expand Down Expand Up @@ -596,11 +596,14 @@ class RoomScreenInteractionHandler {
}

private func buildReplyInfo(for item: EventBasedTimelineItemProtocol) -> ReplyInfo {
guard let messageItem = item as? EventBasedMessageTimelineItemProtocol else {
return .init(type: .text(.init(body: item.body)), isThread: false)
switch item {
case let messageItem as EventBasedMessageTimelineItemProtocol:
return .init(type: .message(messageItem.contentType), isThread: messageItem.isThreaded)
case let pollItem as PollRoomTimelineItem:
return .init(type: .poll(question: pollItem.poll.question), isThread: false)
default:
return .init(type: .message(.text(.init(body: item.body))), isThread: false)
}

return .init(type: messageItem.contentType, isThread: messageItem.isThreaded)
}

private func openSystemSettings() {
Expand All @@ -611,7 +614,7 @@ class RoomScreenInteractionHandler {
private func displayMediaActionIfPossible(timelineItem: RoomTimelineItemProtocol) async -> RoomTimelineControllerAction {
var source: MediaSourceProxy?
var body: String

switch timelineItem {
case let item as ImageRoomTimelineItem:
source = item.content.source
Expand Down Expand Up @@ -657,6 +660,6 @@ class RoomScreenInteractionHandler {
}

private struct ReplyInfo {
let type: EventBasedMessageTimelineItemContentType
let type: TimelineEventContent
let isThread: Bool
}
Loading

0 comments on commit 9f1092b

Please sign in to comment.