Handle poll updates

This commit is contained in:
Alfonso Grillo
2023-01-20 13:39:52 +01:00
parent a3ad7e4beb
commit 775dc8771f
2 changed files with 15 additions and 7 deletions
@@ -39,7 +39,7 @@ final class PollHistoryViewModel: PollHistoryViewModelType, PollHistoryViewModel
setupSubscriptions()
pollService.next()
case .segmentDidChange:
updatePolls()
updateState()
}
}
}
@@ -51,8 +51,8 @@ private extension PollHistoryViewModel {
pollService
.pollHistory
.sink { [weak self] detail in
self?.polls.append(detail)
self?.updatePolls()
self?.updatePolls(with: detail)
self?.updateState()
}
.store(in: &subcriptions)
@@ -80,7 +80,15 @@ private extension PollHistoryViewModel {
.store(in: &subcriptions)
}
func updatePolls() {
func updatePolls(with poll: TimelinePollDetails) {
if let matchIndex = polls.firstIndex(where: { $0.id == poll.id }) {
polls[matchIndex] = poll
} else {
polls.append(poll)
}
}
func updateState() {
let renderedPolls: [TimelinePollDetails]
switch context.mode {
@@ -148,17 +148,17 @@ private extension PollHistoryService {
// MARK: - PollAggregatorDelegate
extension PollHistoryService: PollAggregatorDelegate {
func pollAggregatorDidStartLoading(_ aggregator: PollAggregator) {
}
func pollAggregatorDidStartLoading(_ aggregator: PollAggregator) {}
func pollAggregatorDidEndLoading(_ aggregator: PollAggregator) {
pollsSubject.send(.init(poll: aggregator.poll, represent: .started))
}
func pollAggregator(_ aggregator: PollAggregator, didFailWithError: Error) {
#warning("Handle error")
}
func pollAggregatorDidUpdateData(_ aggregator: PollAggregator) {
pollsSubject.send(.init(poll: aggregator.poll, represent: .started))
}
}