Add live synced days

This commit is contained in:
Alfonso Grillo
2023-01-25 15:12:19 +01:00
parent 1f953c8a93
commit 94ca5e3ccc
5 changed files with 41 additions and 11 deletions
@@ -17,7 +17,8 @@
// MARK: View model
enum PollHistoryConstants {
static let chunkSizeInDays: UInt = 30
static let chunkSizeInDays: UInt = 10
static let oneDayInSeconds: TimeInterval = 8.6 * 10e3
}
enum PollHistoryViewModelResult: Equatable {
@@ -44,7 +45,8 @@ struct PollHistoryViewState: BindableState {
var isLoading = false
var canLoadMoreContent = true
var polls: [TimelinePollDetails]?
var numberOfFetchedBatches: UInt = 0
var syncStartDate: Date = .init()
var syncedUpTo: Date = .distantFuture
}
enum PollHistoryViewAction {
@@ -83,7 +83,7 @@ private extension PollHistoryViewModel {
switch completion {
case .finished:
state.numberOfFetchedBatches += 1
break
case .failure(_):
#warning("Handle errors")
}
@@ -106,6 +106,11 @@ private extension PollHistoryViewModel {
#warning("Handle errors")
}
.store(in: &subcriptions)
pollService
.fetchedUpTo
.weakAssign(to: \.state.syncedUpTo, on: self)
.store(in: &subcriptions)
}
func update(poll: TimelinePollDetails) {
@@ -136,17 +141,20 @@ private extension PollHistoryViewModel {
extension PollHistoryViewModel.Context {
var emptyPollsText: String {
let days = PollHistoryConstants.chunkSizeInDays * viewState.numberOfFetchedBatches
switch (viewState.bindings.mode, viewState.canLoadMoreContent) {
case (.active, true):
return VectorL10n.pollHistoryNoActivePollPeriodText("\(days)")
return VectorL10n.pollHistoryNoActivePollPeriodText("\(syncedPastDays)")
case (.active, false):
return VectorL10n.pollHistoryNoActivePollText
case (.past, true):
return VectorL10n.pollHistoryNoPastPollPeriodText("\(days)")
return VectorL10n.pollHistoryNoPastPollPeriodText("\(syncedPastDays)")
case (.past, false):
return VectorL10n.pollHistoryNoPastPollText
}
}
var syncedPastDays: UInt {
let timeDelta = max(viewState.syncStartDate.timeIntervalSince(viewState.syncedUpTo), 0)
return UInt((timeDelta / PollHistoryConstants.oneDayInSeconds).rounded())
}
}
@@ -29,7 +29,7 @@ final class PollHistoryService: PollHistoryServiceProtocol {
private var pollAggregators: [String: PollAggregator] = [:]
private var targetTimestamp: Date?
private var oldestEventDate: Date = .distantFuture
private var oldestEventDateSubject: CurrentValueSubject<Date, Never> = .init(Date.distantFuture)
private var currentBatchSubject: PassthroughSubject<TimelinePollDetails, Error>?
var updates: AnyPublisher<TimelinePollDetails, Never> {
@@ -54,6 +54,10 @@ final class PollHistoryService: PollHistoryServiceProtocol {
var hasNextBatch: Bool {
timeline.canPaginate(.backwards)
}
var fetchedUpTo: AnyPublisher<Date, Never> {
oldestEventDateSubject.eraseToAnyPublisher()
}
}
private extension PollHistoryService {
@@ -135,13 +139,20 @@ private extension PollHistoryService {
}
return oldestEventDate <= targetTimestamp
}
var oldestEventDate: Date {
get {
oldestEventDateSubject.value
}
set {
oldestEventDateSubject.send(newValue)
}
}
}
private extension Date {
private static let oneDayInSeconds: TimeInterval = 8.6 * 10e3
func subtractingDays(_ days: UInt) -> Date {
addingTimeInterval(-TimeInterval(days) * Self.oneDayInSeconds)
addingTimeInterval(-TimeInterval(days) * PollHistoryConstants.oneDayInSeconds)
}
}
@@ -37,6 +37,11 @@ final class MockPollHistoryService: PollHistoryServiceProtocol {
}
var hasNextBatch: Bool = true
var fetchedUpToPublisher: AnyPublisher<Date, Never> = Just(.init()).eraseToAnyPublisher()
var fetchedUpTo: AnyPublisher<Date, Never> {
fetchedUpToPublisher
}
}
private extension MockPollHistoryService {
@@ -31,4 +31,8 @@ protocol PollHistoryServiceProtocol {
/// 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 }
/// Publishes the date up to the service is synced (in the past).
/// This date doesn't need to be related with any poll event.
var fetchedUpTo: AnyPublisher<Date, Never> { get }
}