Skip to content

Commit

Permalink
Fix: allow to render a TimelinePoll even if the poll is loading
Browse files Browse the repository at this point in the history
  • Loading branch information
nimau committed Apr 26, 2023
1 parent 5cfac70 commit 27fd56d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ private extension PollHistoryService {

do {
newContext.pollAggregator = try PollAggregator(session: room.mxSession, room: room, pollEvent: pollStartEvent, delegate: self)
newContext.pollAggregator?.reloadPollData()
} catch {
pollAggregationContexts.removeValue(forKey: eventId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ final class TimelinePollCoordinator: Coordinator, Presentable, PollAggregatorDel
}
}
.store(in: &cancellables)

pollAggregator.reloadPollData()
}

// MARK: - Public
Expand Down Expand Up @@ -109,13 +111,20 @@ final class TimelinePollCoordinator: Coordinator, Presentable, PollAggregatorDel

func pollAggregatorDidUpdateData(_ aggregator: PollAggregator) {
viewModel.updateWithPollDetails(buildTimelinePollFrom(aggregator.poll))
viewModel.updateWithPollState(.loaded)
}

func pollAggregatorDidStartLoading(_ aggregator: PollAggregator) { }
func pollAggregatorDidStartLoading(_ aggregator: PollAggregator) {
viewModel.updateWithPollState(.loading)
}

func pollAggregatorDidEndLoading(_ aggregator: PollAggregator) { }
func pollAggregatorDidEndLoading(_ aggregator: PollAggregator) {
viewModel.updateWithPollState(.loaded)
}

func pollAggregator(_ aggregator: PollAggregator, didFailWithError: Error) { }
func pollAggregator(_ aggregator: PollAggregator, didFailWithError: Error) {
viewModel.updateWithPollState(.invalidStartEvent)
}

// MARK: - Private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ enum TimelinePollEventType {
case ended
}

enum TimelinePollState {
case loading
case loaded
case invalidStartEvent
}

struct TimelinePollAnswerOption: Identifiable {
var id: String
var text: String
Expand Down Expand Up @@ -99,6 +105,7 @@ struct TimelinePollViewState: BindableState {
}

struct TimelinePollViewStateBindings {
var pollState: TimelinePollState
var alertInfo: AlertInfo<TimelinePollAlertType>?
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ enum MockTimelinePollScreenState: MockScreenState, CaseIterable {
case openUndisclosed
case closedUndisclosed
case closedPollEnded
case loading
case invalidStartEvent
case withAlert

var screenType: Any.Type {
TimelinePollDetails.self
Expand All @@ -47,6 +50,20 @@ enum MockTimelinePollScreenState: MockScreenState, CaseIterable {

let viewModel = TimelinePollViewModel(timelinePollDetails: poll)

switch self {
case .loading:
viewModel.updateWithPollState(.loading)
case .invalidStartEvent:
viewModel.updateWithPollState(.invalidStartEvent)
default:
viewModel.updateWithPollState(.loaded)
}

if self == .withAlert {
viewModel.showAnsweringFailure()
}


return ([viewModel], AnyView(TimelinePollView(viewModel: viewModel.context)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TimelinePollViewModel: TimelinePollViewModelType, TimelinePollViewModelPro
// MARK: - Setup

init(timelinePollDetails: TimelinePollDetails) {
super.init(initialViewState: TimelinePollViewState(poll: timelinePollDetails, bindings: TimelinePollViewStateBindings()))
super.init(initialViewState: TimelinePollViewState(poll: timelinePollDetails, bindings: TimelinePollViewStateBindings(pollState: .loading)))
}

// MARK: - Public
Expand All @@ -58,6 +58,10 @@ class TimelinePollViewModel: TimelinePollViewModelType, TimelinePollViewModelPro
state.poll = pollDetails
}

func updateWithPollState(_ pollState: TimelinePollState) {
state.bindings.pollState = pollState
}

func showAnsweringFailure() {
state.bindings.alertInfo = AlertInfo(id: .failedSubmittingAnswer,
title: VectorL10n.pollTimelineVoteNotRegisteredTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protocol TimelinePollViewModelProtocol {
var completion: ((TimelinePollViewModelResult) -> Void)? { get set }

func updateWithPollDetails(_ pollDetails: TimelinePollDetails)
func updateWithPollState(_ pollState: TimelinePollState)
func showAnsweringFailure()
func showClosingFailure()
}
20 changes: 17 additions & 3 deletions RiotSwiftUI/Modules/Room/TimelinePoll/View/TimelinePollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ struct TimelinePollView: View {
@ObservedObject var viewModel: TimelinePollViewModel.Context

var body: some View {
Group {
switch viewModel.pollState {
case .loading:
TimelinePollMessageView(message: "loading...")
case .loaded:
pollContent
case .invalidStartEvent:
TimelinePollMessageView(message: VectorL10n.pollTimelineReplyEndedPoll)
}
}
.alert(item: $viewModel.alertInfo) { info in
info.alert
}
}

@ViewBuilder
private var pollContent: some View {
let poll = viewModel.viewState.poll

VStack(alignment: .leading, spacing: 16.0) {
Expand Down Expand Up @@ -61,9 +78,6 @@ struct TimelinePollView: View {
}
.padding([.horizontal, .top], 2.0)
.padding([.bottom])
.alert(item: $viewModel.alertInfo) { info in
info.alert
}
}

private var totalVotesString: String {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/7497.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Poll: The timeline sometimes displayed closed polls in the wrong order.

0 comments on commit 27fd56d

Please sign in to comment.