vector-im/element-ios/issues/5114 - Various tweaks following PR review.

This commit is contained in:
Stefan Ceriu
2022-01-14 17:02:04 +02:00
committed by Stefan Ceriu
parent bf6ef2bce2
commit d7e9fe61b4
14 changed files with 102 additions and 102 deletions
@@ -52,7 +52,7 @@ final class TimelinePollCoordinator: Coordinator, Presentable, PollAggregatorDel
try pollAggregator = PollAggregator(session: parameters.session, room: parameters.room, pollStartEventId: parameters.pollStartEvent.eventId)
pollAggregator.delegate = self
viewModel = TimelinePollViewModel(timelinePoll: buildTimelinePollFrom(pollAggregator.poll))
viewModel = TimelinePollViewModel(timelinePollDetails: buildTimelinePollFrom(pollAggregator.poll))
viewModel.callback = { [weak self] result in
guard let self = self else { return }
@@ -95,7 +95,7 @@ final class TimelinePollCoordinator: Coordinator, Presentable, PollAggregatorDel
}
func canEditPoll() -> Bool {
return (pollAggregator.poll.isClosed == false && pollAggregator.poll.totalAnswerCount == 0)
return pollAggregator.poll.isClosed == false && pollAggregator.poll.totalAnswerCount == 0
}
func endPoll() {
@@ -126,7 +126,7 @@ final class TimelinePollCoordinator: Coordinator, Presentable, PollAggregatorDel
// PollProtocol is intentionally not available in the SwiftUI target as we don't want
// to add the SDK as a dependency to it. We need to translate from one to the other on this level.
func buildTimelinePollFrom(_ poll: PollProtocol) -> TimelinePoll {
func buildTimelinePollFrom(_ poll: PollProtocol) -> TimelinePollDetails {
let answerOptions = poll.answerOptions.map { pollAnswerOption in
TimelinePollAnswerOption(id: pollAnswerOption.id,
text: pollAnswerOption.text,
@@ -135,7 +135,7 @@ final class TimelinePollCoordinator: Coordinator, Presentable, PollAggregatorDel
selected: pollAnswerOption.isCurrentUserSelection)
}
return TimelinePoll(question: poll.text,
return TimelinePollDetails(question: poll.text,
answerOptions: answerOptions,
closed: poll.isClosed,
totalAnswerCount: poll.totalAnswerCount,
@@ -30,15 +30,15 @@ class TimelinePollViewModelTests: XCTestCase {
TimelinePollAnswerOption(id: "2", text: "2", count: 1, winner: false, selected: false),
TimelinePollAnswerOption(id: "3", text: "3", count: 1, winner: false, selected: false)]
let timelinePoll = TimelinePoll(question: "Question",
answerOptions: answerOptions,
closed: false,
totalAnswerCount: 3,
type: .disclosed,
maxAllowedSelections: 1,
hasBeenEdited: false)
let timelinePoll = TimelinePollDetails(question: "Question",
answerOptions: answerOptions,
closed: false,
totalAnswerCount: 3,
type: .disclosed,
maxAllowedSelections: 1,
hasBeenEdited: false)
viewModel = TimelinePollViewModel(timelinePoll: timelinePoll)
viewModel = TimelinePollViewModel(timelinePollDetails: timelinePoll)
context = viewModel.context
}
@@ -21,7 +21,7 @@ typealias TimelinePollViewModelCallback = ((TimelinePollViewModelResult) -> Void
enum TimelinePollStateAction {
case viewAction(TimelinePollViewAction, TimelinePollViewModelCallback?)
case updateWithPoll(TimelinePoll)
case updateWithPoll(TimelinePollDetails)
case showAnsweringFailure
case showClosingFailure
}
@@ -55,7 +55,7 @@ class TimelinePollAnswerOption: Identifiable {
}
}
class TimelinePoll {
class TimelinePollDetails {
var question: String
var answerOptions: [TimelinePollAnswerOption]
var closed: Bool
@@ -68,7 +68,8 @@ class TimelinePoll {
closed: Bool,
totalAnswerCount: UInt,
type: TimelinePollType,
maxAllowedSelections: UInt, hasBeenEdited: Bool) {
maxAllowedSelections: UInt,
hasBeenEdited: Bool) {
self.question = question
self.answerOptions = answerOptions
self.closed = closed
@@ -81,10 +82,18 @@ class TimelinePoll {
var hasCurrentUserVoted: Bool {
answerOptions.filter { $0.selected == true}.count > 0
}
var shouldDiscloseResults: Bool {
if closed {
return totalAnswerCount > 0
} else {
return type == .disclosed && totalAnswerCount > 0 && hasCurrentUserVoted
}
}
}
struct TimelinePollViewState: BindableState {
var poll: TimelinePoll
var poll: TimelinePollDetails
var bindings: TimelinePollViewStateBindings
}
@@ -25,23 +25,23 @@ enum MockTimelinePollScreenState: MockScreenState, CaseIterable {
case closedUndisclosed
var screenType: Any.Type {
TimelinePoll.self
TimelinePollDetails.self
}
var screenView: ([Any], AnyView) {
let answerOptions = [TimelinePollAnswerOption(id: "1", text: "First", count: 10, winner: false, selected: false),
TimelinePollAnswerOption(id: "2", text: "Second", count: 5, winner: false, selected: true),
TimelinePollAnswerOption(id: "3", text: "Third", count: 15, winner: true, selected: false)]
TimelinePollAnswerOption(id: "2", text: "Second", count: 5, winner: false, selected: true),
TimelinePollAnswerOption(id: "3", text: "Third", count: 15, winner: true, selected: false)]
let poll = TimelinePoll(question: "Question",
answerOptions: answerOptions,
closed: (self == .closedDisclosed || self == .closedUndisclosed ? true : false),
totalAnswerCount: 20,
type: (self == .closedDisclosed || self == .openDisclosed ? .disclosed : .undisclosed),
maxAllowedSelections: 1,
hasBeenEdited: false)
let poll = TimelinePollDetails(question: "Question",
answerOptions: answerOptions,
closed: (self == .closedDisclosed || self == .closedUndisclosed ? true : false),
totalAnswerCount: 20,
type: (self == .closedDisclosed || self == .openDisclosed ? .disclosed : .undisclosed),
maxAllowedSelections: 1,
hasBeenEdited: false)
let viewModel = TimelinePollViewModel(timelinePoll: poll)
let viewModel = TimelinePollViewModel(timelinePollDetails: poll)
return ([viewModel], AnyView(TimelinePollView(viewModel: viewModel.context)))
}
@@ -34,8 +34,8 @@ class TimelinePollViewModel: TimelinePollViewModelType {
// MARK: - Setup
init(timelinePoll: TimelinePoll) {
super.init(initialViewState: TimelinePollViewState(poll: timelinePoll, bindings: TimelinePollViewStateBindings()))
init(timelinePollDetails: TimelinePollDetails) {
super.init(initialViewState: TimelinePollViewState(poll: timelinePollDetails, bindings: TimelinePollViewStateBindings()))
}
// MARK: - Public
@@ -25,7 +25,7 @@ struct TimelinePollAnswerOptionButton: View {
@Environment(\.theme) private var theme: ThemeSwiftUI
let poll: TimelinePoll
let poll: TimelinePollDetails
let answerOption: TimelinePollAnswerOption
let action: () -> Void
@@ -65,12 +65,12 @@ struct TimelinePollAnswerOptionButton: View {
if poll.type == .disclosed || poll.closed {
HStack {
ProgressView(value: Double(shouldDiscloseResults ? answerOption.count : 0),
ProgressView(value: Double(poll.shouldDiscloseResults ? answerOption.count : 0),
total: Double(poll.totalAnswerCount))
.progressViewStyle(LinearProgressViewStyle())
.scaleEffect(x: 1.0, y: 1.2, anchor: .center)
if (shouldDiscloseResults) {
if (poll.shouldDiscloseResults) {
Text(answerOption.count == 1 ? VectorL10n.pollTimelineOneVote : VectorL10n.pollTimelineVotesCount(Int(answerOption.count)))
.font(theme.fonts.footnote)
.foregroundColor(poll.closed && answerOption.winner ? theme.colors.accent : theme.colors.secondaryContent)
@@ -95,14 +95,6 @@ struct TimelinePollAnswerOptionButton: View {
return answerOption.selected ? theme.colors.accent : theme.colors.quarterlyContent
}
private var shouldDiscloseResults: Bool {
if poll.closed {
return poll.totalAnswerCount > 0
} else {
return poll.type == .disclosed && poll.totalAnswerCount > 0 && poll.hasCurrentUserVoted
}
}
}
@available(iOS 14.0, *)
@@ -149,8 +141,8 @@ struct TimelinePollAnswerOptionButton_Previews: PreviewProvider {
}
}
static func buildPoll(closed: Bool, type: TimelinePollType) -> TimelinePoll {
TimelinePoll(question: "",
static func buildPoll(closed: Bool, type: TimelinePollType) -> TimelinePollDetails {
TimelinePollDetails(question: "",
answerOptions: [],
closed: closed,
totalAnswerCount: 100,