2022-09-27 07:17:22 +00:00
|
|
|
//
|
2021-11-19 15:40:52 +00:00
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-01-13 15:25:14 +00:00
|
|
|
struct TimelinePollView: View {
|
2021-11-19 15:40:52 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
@Environment(\.theme) private var theme: ThemeSwiftUI
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
2022-01-13 15:25:14 +00:00
|
|
|
@ObservedObject var viewModel: TimelinePollViewModel.Context
|
2021-11-19 15:40:52 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
let poll = viewModel.viewState.poll
|
|
|
|
|
|
|
|
VStack(alignment: .leading, spacing: 16.0) {
|
|
|
|
Text(poll.question)
|
|
|
|
.font(theme.fonts.bodySB)
|
2022-01-12 12:00:09 +00:00
|
|
|
.foregroundColor(theme.colors.primaryContent) +
|
2022-01-13 13:57:26 +00:00
|
|
|
Text(editedText)
|
2022-01-12 12:00:09 +00:00
|
|
|
.font(theme.fonts.footnote)
|
|
|
|
.foregroundColor(theme.colors.secondaryContent)
|
2021-11-19 15:40:52 +00:00
|
|
|
|
|
|
|
VStack(spacing: 24.0) {
|
|
|
|
ForEach(poll.answerOptions) { answerOption in
|
2022-01-13 15:25:14 +00:00
|
|
|
TimelinePollAnswerOptionButton(poll: poll, answerOption: answerOption) {
|
2021-11-19 15:40:52 +00:00
|
|
|
viewModel.send(viewAction: .selectAnswerOptionWithIdentifier(answerOption.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(poll.closed)
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
|
|
|
|
|
|
Text(totalVotesString)
|
|
|
|
.font(theme.fonts.footnote)
|
|
|
|
.foregroundColor(theme.colors.tertiaryContent)
|
|
|
|
}
|
|
|
|
.padding([.horizontal, .top], 2.0)
|
|
|
|
.padding([.bottom])
|
2022-01-13 13:57:26 +00:00
|
|
|
.alert(item: $viewModel.alertInfo) { info in
|
2022-03-03 16:41:37 +00:00
|
|
|
info.alert
|
2022-01-13 13:57:26 +00:00
|
|
|
}
|
2021-11-19 15:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var totalVotesString: String {
|
|
|
|
let poll = viewModel.viewState.poll
|
|
|
|
|
|
|
|
if poll.closed {
|
|
|
|
if poll.totalAnswerCount == 1 {
|
|
|
|
return VectorL10n.pollTimelineTotalFinalResultsOneVote
|
|
|
|
} else {
|
|
|
|
return VectorL10n.pollTimelineTotalFinalResults(Int(poll.totalAnswerCount))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch poll.totalAnswerCount {
|
|
|
|
case 0:
|
|
|
|
return VectorL10n.pollTimelineTotalNoVotes
|
|
|
|
case 1:
|
2022-01-13 13:57:26 +00:00
|
|
|
return (poll.hasCurrentUserVoted || poll.type == .undisclosed ?
|
2022-09-27 07:17:22 +00:00
|
|
|
VectorL10n.pollTimelineTotalOneVote :
|
|
|
|
VectorL10n.pollTimelineTotalOneVoteNotVoted)
|
2021-11-19 15:40:52 +00:00
|
|
|
default:
|
2022-01-13 13:57:26 +00:00
|
|
|
return (poll.hasCurrentUserVoted || poll.type == .undisclosed ?
|
2022-09-27 07:17:22 +00:00
|
|
|
VectorL10n.pollTimelineTotalVotes(Int(poll.totalAnswerCount)) :
|
|
|
|
VectorL10n.pollTimelineTotalVotesNotVoted(Int(poll.totalAnswerCount)))
|
2021-11-19 15:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:00:09 +00:00
|
|
|
private var editedText: String {
|
|
|
|
viewModel.viewState.poll.hasBeenEdited ? " \(VectorL10n.eventFormatterMessageEditedMention)" : ""
|
|
|
|
}
|
2021-11-19 15:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
2022-01-13 15:25:14 +00:00
|
|
|
struct TimelinePollView_Previews: PreviewProvider {
|
|
|
|
static let stateRenderer = MockTimelinePollScreenState.stateRenderer
|
2021-11-19 15:40:52 +00:00
|
|
|
static var previews: some View {
|
|
|
|
stateRenderer.screenGroup()
|
|
|
|
}
|
|
|
|
}
|