mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-27 11:46:58 +02:00
Support load more in PollHistoryService
This commit is contained in:
@@ -49,4 +49,5 @@ struct PollHistoryViewState: BindableState {
|
||||
enum PollHistoryViewAction {
|
||||
case viewAppeared
|
||||
case segmentDidChange
|
||||
case loadMoreContent
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ final class PollHistoryViewModel: PollHistoryViewModelType, PollHistoryViewModel
|
||||
fetchFirstBatch()
|
||||
case .segmentDidChange:
|
||||
updateViewState()
|
||||
case .loadMoreContent:
|
||||
fetchMoreContent()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,6 +63,21 @@ private extension PollHistoryViewModel {
|
||||
.store(in: &subcriptions)
|
||||
}
|
||||
|
||||
func fetchMoreContent() {
|
||||
state.isLoading = true
|
||||
|
||||
pollService
|
||||
.nextBatch()
|
||||
.sink { [weak self] _ in
|
||||
#warning("Handle errors")
|
||||
self?.state.isLoading = false
|
||||
} receiveValue: { [weak self] poll in
|
||||
self?.add(poll: poll)
|
||||
self?.updateViewState()
|
||||
}
|
||||
.store(in: &subcriptions)
|
||||
}
|
||||
|
||||
func setupUpdateSubscriptions() {
|
||||
subcriptions.removeAll()
|
||||
|
||||
@@ -88,6 +105,10 @@ private extension PollHistoryViewModel {
|
||||
polls?[pollIndex] = poll
|
||||
}
|
||||
|
||||
func add(poll: TimelinePollDetails) {
|
||||
polls?.append(poll)
|
||||
}
|
||||
|
||||
func updateViewState() {
|
||||
let renderedPolls: [TimelinePollDetails]?
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ final class PollHistoryService: PollHistoryServiceProtocol {
|
||||
private let pollErrorsSubject: PassthroughSubject<Error, Never> = .init()
|
||||
|
||||
private var pollAggregators: [String: PollAggregator] = [:]
|
||||
private var targetTimestamp: Date
|
||||
private var targetTimestamp: Date?
|
||||
private var oldestEventDate: Date = .distantFuture
|
||||
private var currentBatchSubject: PassthroughSubject<TimelinePollDetails, Error>?
|
||||
|
||||
@@ -44,7 +44,6 @@ final class PollHistoryService: PollHistoryServiceProtocol {
|
||||
self.room = room
|
||||
self.chunkSizeInDays = chunkSizeInDays
|
||||
timeline = MXRoomEventTimeline(room: room, andInitialEventId: nil)
|
||||
targetTimestamp = Date().addingTimeInterval(-TimeInterval(chunkSizeInDays) * Constants.oneDayInSeconds)
|
||||
setup(timeline: timeline)
|
||||
}
|
||||
|
||||
@@ -56,7 +55,6 @@ final class PollHistoryService: PollHistoryServiceProtocol {
|
||||
private extension PollHistoryService {
|
||||
enum Constants {
|
||||
static let pageSize: UInt = 500
|
||||
static let oneDayInSeconds: TimeInterval = 8.6 * 10e3
|
||||
}
|
||||
|
||||
func setup(timeline: MXEventTimeline) {
|
||||
@@ -74,6 +72,9 @@ private extension PollHistoryService {
|
||||
}
|
||||
|
||||
func startPagination() -> AnyPublisher<TimelinePollDetails, Error> {
|
||||
let startingTimestamp = targetTimestamp ?? .init()
|
||||
targetTimestamp = startingTimestamp.subtractingDays(chunkSizeInDays)
|
||||
|
||||
let batchSubject = PassthroughSubject<TimelinePollDetails, Error>()
|
||||
currentBatchSubject = batchSubject
|
||||
|
||||
@@ -125,7 +126,18 @@ private extension PollHistoryService {
|
||||
}
|
||||
|
||||
var timestampTargetReached: Bool {
|
||||
oldestEventDate <= targetTimestamp
|
||||
guard let targetTimestamp = targetTimestamp else {
|
||||
return true
|
||||
}
|
||||
return oldestEventDate <= targetTimestamp
|
||||
}
|
||||
}
|
||||
|
||||
private extension Date {
|
||||
private static let oneDayInSeconds: TimeInterval = 8.6 * 10e3
|
||||
|
||||
func subtractingDays(_ days: UInt) -> Date {
|
||||
addingTimeInterval(-TimeInterval(days) * Self.oneDayInSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ struct PollHistory: View {
|
||||
}
|
||||
|
||||
Button {
|
||||
#warning("handle action in next ticket")
|
||||
viewModel.send(viewAction: .loadMoreContent)
|
||||
} label: {
|
||||
Text(VectorL10n.pollHistoryLoadMore)
|
||||
.font(theme.fonts.body)
|
||||
|
||||
Reference in New Issue
Block a user