mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-22 01:22:46 +02:00
Handle number of batches / last batch
This commit is contained in:
@@ -44,6 +44,7 @@ struct PollHistoryViewState: BindableState {
|
||||
var isLoading = false
|
||||
var canLoadMoreContent = true
|
||||
var polls: [TimelinePollDetails]?
|
||||
var numberOfFetchedBatches: UInt = 0
|
||||
}
|
||||
|
||||
enum PollHistoryViewAction {
|
||||
|
||||
@@ -29,6 +29,7 @@ final class PollHistoryViewModel: PollHistoryViewModelType, PollHistoryViewModel
|
||||
init(mode: PollHistoryMode, pollService: PollHistoryServiceProtocol) {
|
||||
self.pollService = pollService
|
||||
super.init(initialViewState: PollHistoryViewState(mode: mode))
|
||||
state.canLoadMoreContent = pollService.hasNextBatch
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
@@ -53,9 +54,8 @@ private extension PollHistoryViewModel {
|
||||
pollService
|
||||
.nextBatch()
|
||||
.collect()
|
||||
.sink { [weak self] _ in
|
||||
#warning("Handle errors")
|
||||
self?.state.isLoading = false
|
||||
.sink { [weak self] completion in
|
||||
self?.handleBatchEnded(completion: completion)
|
||||
} receiveValue: { [weak self] polls in
|
||||
self?.polls = polls
|
||||
self?.updateViewState()
|
||||
@@ -68,9 +68,8 @@ private extension PollHistoryViewModel {
|
||||
|
||||
pollService
|
||||
.nextBatch()
|
||||
.sink { [weak self] _ in
|
||||
#warning("Handle errors")
|
||||
self?.state.isLoading = false
|
||||
.sink { [weak self] completion in
|
||||
self?.handleBatchEnded(completion: completion)
|
||||
} receiveValue: { [weak self] poll in
|
||||
self?.add(poll: poll)
|
||||
self?.updateViewState()
|
||||
@@ -78,6 +77,18 @@ private extension PollHistoryViewModel {
|
||||
.store(in: &subcriptions)
|
||||
}
|
||||
|
||||
func handleBatchEnded(completion: Subscribers.Completion<Error>) {
|
||||
state.isLoading = false
|
||||
state.canLoadMoreContent = pollService.hasNextBatch
|
||||
|
||||
switch completion {
|
||||
case .finished:
|
||||
state.numberOfFetchedBatches += 1
|
||||
case .failure(_):
|
||||
#warning("Handle errors")
|
||||
}
|
||||
}
|
||||
|
||||
func setupUpdateSubscriptions() {
|
||||
subcriptions.removeAll()
|
||||
|
||||
@@ -125,7 +136,7 @@ private extension PollHistoryViewModel {
|
||||
|
||||
extension PollHistoryViewModel.Context {
|
||||
var emptyPollsText: String {
|
||||
let days = PollHistoryConstants.chunkSizeInDays
|
||||
let days = PollHistoryConstants.chunkSizeInDays * viewState.numberOfFetchedBatches
|
||||
|
||||
switch (viewState.bindings.mode, viewState.canLoadMoreContent) {
|
||||
case (.active, true):
|
||||
|
||||
@@ -50,6 +50,10 @@ final class PollHistoryService: PollHistoryServiceProtocol {
|
||||
func nextBatch() -> AnyPublisher<TimelinePollDetails, Error> {
|
||||
currentBatchSubject?.eraseToAnyPublisher() ?? startPagination()
|
||||
}
|
||||
|
||||
var hasNextBatch: Bool {
|
||||
timeline.canPaginate(.backwards)
|
||||
}
|
||||
}
|
||||
|
||||
private extension PollHistoryService {
|
||||
|
||||
@@ -17,6 +17,15 @@
|
||||
import Combine
|
||||
|
||||
final class MockPollHistoryService: PollHistoryServiceProtocol {
|
||||
lazy var nextBatchPublisher: AnyPublisher<TimelinePollDetails, Error> = (activePollsData + pastPollsData)
|
||||
.publisher
|
||||
.setFailureType(to: Error.self)
|
||||
.eraseToAnyPublisher()
|
||||
|
||||
func nextBatch() -> AnyPublisher<TimelinePollDetails, Error> {
|
||||
nextBatchPublisher
|
||||
}
|
||||
|
||||
var updatesPublisher: AnyPublisher<TimelinePollDetails, Never> = Empty().eraseToAnyPublisher()
|
||||
var updates: AnyPublisher<TimelinePollDetails, Never> {
|
||||
updatesPublisher
|
||||
@@ -27,14 +36,7 @@ final class MockPollHistoryService: PollHistoryServiceProtocol {
|
||||
pollErrorPublisher
|
||||
}
|
||||
|
||||
lazy var nextBatchPublisher: AnyPublisher<TimelinePollDetails, Error> = (activePollsData + pastPollsData)
|
||||
.publisher
|
||||
.setFailureType(to: Error.self)
|
||||
.eraseToAnyPublisher()
|
||||
|
||||
func nextBatch() -> AnyPublisher<TimelinePollDetails, Error> {
|
||||
nextBatchPublisher
|
||||
}
|
||||
var hasNextBatch: Bool = true
|
||||
}
|
||||
|
||||
private extension MockPollHistoryService {
|
||||
|
||||
@@ -27,4 +27,8 @@ protocol PollHistoryServiceProtocol {
|
||||
/// Publishes errors regarding poll aggregations.
|
||||
/// Note: `nextBatch()` will continue to publish new polls even if some poll isn't being aggregated correctly.
|
||||
var pollErrors: AnyPublisher<Error, Never> { get }
|
||||
|
||||
/// Returns true every time the service can fetch another batch.
|
||||
/// There is no guarantee the `nextBatch()` returned publisher will publish something anyway.
|
||||
var hasNextBatch: Bool { get }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user