Vote on a poll and end a poll (#1597)

* Fix project file

* Add vote and end poll actions

* Handle redaction

* Fix local echo behavior

* Cleanup

* Fix UI tests

* Add localisation

* Cleanup

* Fix end poll action style
This commit is contained in:
Alfonso Grillo
2023-08-31 12:04:25 +02:00
committed by GitHub
parent d43b578dce
commit f0fad25d09
18 changed files with 151 additions and 32 deletions
@@ -60,7 +60,7 @@ enum RoomScreenViewAction {
case toggleReaction(key: String, itemID: TimelineItemIdentifier)
case sendReadReceiptIfNeeded(TimelineItemIdentifier)
case paginateBackwards
case selectedPollOption(poll: Poll, optionID: String)
case selectedPollOption(pollStartID: String, optionID: String)
case timelineItemMenu(itemID: TimelineItemIdentifier)
case timelineItemMenuAction(itemID: TimelineItemIdentifier, action: TimelineItemMenuAction)
@@ -139,8 +139,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
if state.swiftUITimelineEnabled {
renderPendingTimelineItems()
}
case .selectedPollOption:
break
case let .selectedPollOption(pollStartID, optionID):
sendPollResponse(pollStartID: pollStartID, optionID: optionID)
}
}
@@ -489,7 +489,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
actionsSubject.send(.composer(action: .removeFocus))
state.bindings.actionMenuInfo = .init(item: eventTimelineItem)
}
// swiftlint:disable:next cyclomatic_complexity
private func timelineItemMenuActionsForItemId(_ itemID: TimelineItemIdentifier) -> TimelineItemMenuActions? {
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID),
let item = timelineItem as? EventBasedTimelineItemProtocol else {
@@ -510,11 +511,10 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
return .init(actions: [], debugActions: debugActions)
}
var actions: [TimelineItemMenuAction] = [
.reply
]
var actions: [TimelineItemMenuAction] = []
if item.isMessage {
actions.append(.reply)
actions.append(.forward(itemID: itemID))
}
@@ -527,6 +527,10 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
}
actions.append(.copyPermalink)
if canRedactItem(item), let poll = item.pollIfAvailable, !poll.hasEnded, let eventID = itemID.eventID {
actions.append(.endPoll(pollStartID: eventID))
}
if canRedactItem(item) {
actions.append(.redact)
@@ -610,6 +614,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
actionsSubject.send(.displayReportContent(itemID: itemID, senderID: eventTimelineItem.sender.id))
case .react:
showEmojiPicker(for: itemID)
case .endPoll(let pollStartID):
endPoll(pollStartID: pollStartID)
}
if action.switchToDefaultComposer {
@@ -791,6 +797,32 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
state.bindings.reactionSummaryInfo = .init(reactions: eventTimelineItem.properties.reactions, selectedKey: selectedKey)
}
// MARK: - Polls
private func sendPollResponse(pollStartID: String, optionID: String) {
Task {
let sendPollResponseResult = await roomProxy.sendPollResponse(pollStartID: pollStartID, answers: [optionID])
switch sendPollResponseResult {
case .success:
break
case .failure:
displayError(.toast(L10n.errorUnknown))
}
}
}
private func endPoll(pollStartID: String) {
Task {
let endPollResult = await roomProxy.endPoll(pollStartID: pollStartID)
switch endPollResult {
case .success:
break
case .failure:
displayError(.toast(L10n.errorUnknown))
}
}
}
}
private extension RoomProxyProtocol {
@@ -29,14 +29,15 @@ struct PollRoomTimelineView: View {
ForEach(poll.options, id: \.id) { option in
Button {
context.send(viewAction: .selectedPollOption(poll: poll, optionID: option.id))
guard let eventID else { return }
context.send(viewAction: .selectedPollOption(pollStartID: eventID, optionID: option.id))
} label: {
PollOptionView(pollOption: option,
showVotes: showVotes,
isFinalResult: poll.hasEnded)
.foregroundColor(progressBarColor(for: option))
}
.disabled(poll.hasEnded)
.disabled(poll.hasEnded || eventID == nil)
}
summaryView
@@ -51,6 +52,10 @@ struct PollRoomTimelineView: View {
timelineItem.poll
}
private var eventID: String? {
timelineItem.id.eventID
}
private var questionView: some View {
HStack(alignment: .top, spacing: 12) {
Image(Asset.Images.timelinePoll.name)
@@ -102,10 +107,6 @@ private extension Poll {
return L10n.commonPollUndisclosedText
}
}
var hasEnded: Bool {
endDate != nil
}
}
struct PollRoomTimelineView_Previews: PreviewProvider {
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Compound
import SwiftUI
struct TimelineItemMenuActions {
@@ -51,6 +52,7 @@ enum TimelineItemMenuAction: Identifiable, Hashable {
case retryDecryption(sessionID: String)
case report
case react
case endPoll(pollStartID: String)
var id: Self { self }
@@ -107,6 +109,7 @@ enum TimelineItemMenuAction: Identifiable, Hashable {
case .retryDecryption: return Label(L10n.actionRetryDecryption, systemImage: "arrow.down.message")
case .report: return Label(L10n.actionReportContent, systemImage: "exclamationmark.bubble")
case .react: return Label(L10n.actionReact, systemImage: "hand.thumbsup")
case .endPoll: return Label { Text(L10n.actionEndPoll) } icon: { Image.compound.check.resizable() }
}
}
}