mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Add max selections support for polls (#5726)
This commit is contained in:
committed by
GitHub
parent
173a927bf8
commit
30e4e6339d
@@ -77,7 +77,18 @@ enum PollFormMode: Hashable {
|
||||
|
||||
struct PollFormScreenViewStateBindings: Equatable {
|
||||
var question = ""
|
||||
var options: [Option] = [.init(), .init()]
|
||||
var options: [Option] = [.init(), .init()] {
|
||||
didSet {
|
||||
maxSelections = clampedMaxSelections
|
||||
}
|
||||
}
|
||||
|
||||
var maxSelections = 1 {
|
||||
didSet {
|
||||
maxSelections = clampedMaxSelections
|
||||
}
|
||||
}
|
||||
|
||||
var isUndisclosed = false
|
||||
|
||||
struct Option: Identifiable, Equatable {
|
||||
@@ -86,13 +97,20 @@ struct PollFormScreenViewStateBindings: Equatable {
|
||||
}
|
||||
|
||||
var hasValidContent: Bool {
|
||||
!question.isEmpty && options.count >= 2 && options.allSatisfy { !$0.text.isEmpty }
|
||||
!question.isEmpty && options.count >= 2 && options.allSatisfy { !$0.text.isEmpty } && maxSelections >= 1 && maxSelections <= options.count
|
||||
}
|
||||
|
||||
var alertInfo: AlertInfo<UUID>?
|
||||
|
||||
static func == (lhs: PollFormScreenViewStateBindings, rhs: PollFormScreenViewStateBindings) -> Bool {
|
||||
lhs.question == rhs.question && lhs.options.map(\.text) == rhs.options.map(\.text) && lhs.isUndisclosed == rhs.isUndisclosed
|
||||
lhs.question == rhs.question &&
|
||||
lhs.options.map(\.text) == rhs.options.map(\.text) &&
|
||||
lhs.maxSelections == rhs.maxSelections &&
|
||||
lhs.isUndisclosed == rhs.isUndisclosed
|
||||
}
|
||||
|
||||
private var clampedMaxSelections: Int {
|
||||
min(max(maxSelections, 1), max(options.count, 1))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +118,7 @@ extension PollFormScreenViewStateBindings {
|
||||
init(poll: Poll) {
|
||||
self.init(question: poll.question,
|
||||
options: poll.options.map { .init(text: $0.text) },
|
||||
maxSelections: min(max(poll.maxSelections, 1), max(poll.options.count, 1)),
|
||||
isUndisclosed: poll.kind == .undisclosed)
|
||||
}
|
||||
}
|
||||
@@ -110,4 +129,6 @@ enum PollFormScreenViewAction {
|
||||
case delete
|
||||
case deleteOption(index: Int)
|
||||
case addOption
|
||||
case decrementMaxSelections
|
||||
case incrementMaxSelections
|
||||
}
|
||||
|
||||
@@ -40,14 +40,15 @@ class PollFormScreenViewModel: PollFormScreenViewModelType, PollFormScreenViewMo
|
||||
case .submit:
|
||||
let question = state.bindings.question
|
||||
let options = state.bindings.options.map(\.text)
|
||||
let maxSelections = state.bindings.maxSelections
|
||||
let pollKind = state.bindings.isUndisclosed ? Poll.Kind.undisclosed : .disclosed
|
||||
|
||||
Task {
|
||||
switch state.mode {
|
||||
case .new:
|
||||
await createPoll(question: question, options: options, pollKind: pollKind)
|
||||
await createPoll(question: question, options: options, maxSelections: maxSelections, pollKind: pollKind)
|
||||
case .edit(let eventID, _):
|
||||
await editPoll(pollStartID: eventID, question: question, options: options, pollKind: pollKind)
|
||||
await editPoll(pollStartID: eventID, question: question, options: options, maxSelections: maxSelections, pollKind: pollKind)
|
||||
}
|
||||
}
|
||||
case .delete:
|
||||
@@ -80,13 +81,17 @@ class PollFormScreenViewModel: PollFormScreenViewModelType, PollFormScreenViewMo
|
||||
return
|
||||
}
|
||||
state.bindings.options.append(.init())
|
||||
case .decrementMaxSelections:
|
||||
state.bindings.maxSelections -= 1
|
||||
case .incrementMaxSelections:
|
||||
state.bindings.maxSelections += 1
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func createPoll(question: String, options: [String], pollKind: Poll.Kind) async {
|
||||
guard case .success = await timelineController.createPoll(question: question, answers: options, pollKind: pollKind) else {
|
||||
private func createPoll(question: String, options: [String], maxSelections: Int, pollKind: Poll.Kind) async {
|
||||
guard case .success = await timelineController.createPoll(question: question, answers: options, maxSelections: maxSelections, pollKind: pollKind) else {
|
||||
userIndicatorController.submitIndicator(UserIndicator(title: L10n.errorUnknown))
|
||||
return
|
||||
}
|
||||
@@ -102,8 +107,8 @@ class PollFormScreenViewModel: PollFormScreenViewModelType, PollFormScreenViewMo
|
||||
analytics.trackPollCreated(isUndisclosed: pollKind == .undisclosed, numberOfAnswers: options.count)
|
||||
}
|
||||
|
||||
private func editPoll(pollStartID: String, question: String, options: [String], pollKind: Poll.Kind) async {
|
||||
switch await timelineController.editPoll(original: pollStartID, question: question, answers: options, pollKind: pollKind) {
|
||||
private func editPoll(pollStartID: String, question: String, options: [String], maxSelections: Int, pollKind: Poll.Kind) async {
|
||||
switch await timelineController.editPoll(original: pollStartID, question: question, answers: options, maxSelections: maxSelections, pollKind: pollKind) {
|
||||
case .success:
|
||||
actionsSubject.send(.close)
|
||||
case .failure:
|
||||
|
||||
@@ -22,7 +22,7 @@ struct PollFormScreen: View {
|
||||
Form {
|
||||
questionSection
|
||||
optionsSection
|
||||
showResultsSection
|
||||
settingsSection
|
||||
deletePollSection
|
||||
}
|
||||
.trackAnalyticsIfNeeded(context: context)
|
||||
@@ -106,8 +106,16 @@ struct PollFormScreen: View {
|
||||
focus = nextOptionIndex.map { .option(index: $0) }
|
||||
}
|
||||
|
||||
private var showResultsSection: some View {
|
||||
private var settingsSection: some View {
|
||||
Section {
|
||||
PollFormMaxSelectionsRow(maxSelections: context.maxSelections,
|
||||
maxNumberOfSelections: context.options.count) {
|
||||
context.send(viewAction: .decrementMaxSelections)
|
||||
} incrementAction: {
|
||||
context.send(viewAction: .incrementMaxSelections)
|
||||
}
|
||||
.accessibilityIdentifier(A11yIdentifiers.pollFormScreen.maxSelections)
|
||||
|
||||
ListRow(label: .plain(title: L10n.screenCreatePollAnonymousDesc),
|
||||
kind: .toggle($context.isUndisclosed))
|
||||
.accessibilityIdentifier(A11yIdentifiers.pollFormScreen.pollKind)
|
||||
@@ -160,6 +168,55 @@ private extension View {
|
||||
}
|
||||
}
|
||||
|
||||
private struct PollFormMaxSelectionsRow: View {
|
||||
let maxSelections: Int
|
||||
let maxNumberOfSelections: Int
|
||||
let decrementAction: () -> Void
|
||||
let incrementAction: () -> Void
|
||||
|
||||
var body: some View {
|
||||
ListRow(kind: .custom {
|
||||
HStack(spacing: 16) {
|
||||
Text(UntranslatedL10n.screenCreatePollVotesAllowedPerPerson)
|
||||
.font(.compound.bodyLG)
|
||||
.foregroundStyle(.compound.textPrimary)
|
||||
.lineLimit(2)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
HStack(spacing: 8) {
|
||||
Button(action: decrementAction) {
|
||||
Image(systemSymbol: .minus)
|
||||
.frame(width: 20, height: 20)
|
||||
}
|
||||
.disabled(maxSelections <= 1)
|
||||
.buttonStyle(.compound(.tertiary, size: .toolbarIcon))
|
||||
.accessibilityLabel(UntranslatedL10n.a11yScreenCreatePollVotesAllowedDecrease)
|
||||
.accessibilityIdentifier(A11yIdentifiers.pollFormScreen.maxSelectionsDecrement)
|
||||
|
||||
Text("\(maxSelections)")
|
||||
.font(.compound.bodyLGSemibold)
|
||||
.foregroundStyle(.compound.textPrimary)
|
||||
.monospacedDigit()
|
||||
.frame(minWidth: 28)
|
||||
.accessibilityLabel(Text(maxSelections.formatted()))
|
||||
|
||||
Button(action: incrementAction) {
|
||||
Image(systemSymbol: .plus)
|
||||
.frame(width: 20, height: 20)
|
||||
}
|
||||
.disabled(maxSelections >= maxNumberOfSelections)
|
||||
.buttonStyle(.compound(.tertiary, size: .toolbarIcon))
|
||||
.accessibilityLabel(UntranslatedL10n.a11yScreenCreatePollVotesAllowedIncrease)
|
||||
.accessibilityIdentifier(A11yIdentifiers.pollFormScreen.maxSelectionsIncrement)
|
||||
}
|
||||
.layoutPriority(1)
|
||||
}
|
||||
.padding(.horizontal, ListRowPadding.horizontal)
|
||||
.padding(.vertical, ListRowPadding.vertical)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private struct PollFormOptionRow: View {
|
||||
@Environment(\.editMode) var editMode
|
||||
@Binding var text: String
|
||||
|
||||
Reference in New Issue
Block a user