@@ -11,6 +11,9 @@ final class BookingListViewModel: ObservableObject {
1111 private let type : BookingListTab
1212 private let stores : StoresManager
1313 private let storage : StorageManagerType
14+ private let currentDate : Date
15+
16+ private static let refreshCacheReason = " refresh-cache "
1417
1518 /// Keeps track of the current state of the syncing
1619 @Published private( set) var syncState : SyncState = . empty
@@ -24,22 +27,31 @@ final class BookingListViewModel: ObservableObject {
2427
2528 /// Booking ResultsController.
2629 private lazy var resultsController : ResultsController < StorageBooking > = {
27- let predicate = NSPredicate ( format: " siteID == %lld " , siteID)
28- let sortDescriptorByDate = NSSortDescriptor ( key: " dateCreated " , ascending: false )
30+ var predicates = [ NSPredicate ( format: " siteID == %lld " , siteID) ]
31+ if let before = type. startDateBefore ( currentDate: currentDate) {
32+ predicates. append ( NSPredicate ( format: " startDate < %@ " , before as NSDate ) )
33+ }
34+ if let after = type. startDateAfter ( currentDate: currentDate) {
35+ predicates. append ( NSPredicate ( format: " startDate > %@ " , after as NSDate ) )
36+ }
37+ let combinedPredicate = NSCompoundPredicate ( type: . and, subpredicates: predicates)
38+ let sortDescriptorByDate = NSSortDescriptor ( key: " startDate " , ascending: false )
2939 let resultsController = ResultsController < StorageBooking > ( storageManager: storage,
30- matching: predicate ,
40+ matching: combinedPredicate ,
3141 sortedBy: [ sortDescriptorByDate] )
3242 return resultsController
3343 } ( )
3444
3545 init ( siteID: Int64 ,
3646 type: BookingListTab ,
3747 stores: StoresManager = ServiceLocator . stores,
38- storage: StorageManagerType = ServiceLocator . storageManager) {
48+ storage: StorageManagerType = ServiceLocator . storageManager,
49+ currentDate: Date = Date ( ) ) {
3950 self . siteID = siteID
4051 self . type = type
4152 self . stores = stores
4253 self . storage = storage
54+ self . currentDate = currentDate
4355 self . paginationTracker = PaginationTracker ( pageFirstIndex: pageFirstIndex)
4456
4557 configureResultsController ( )
@@ -60,7 +72,7 @@ final class BookingListViewModel: ObservableObject {
6072 @MainActor
6173 func onRefreshAction( ) async {
6274 await withCheckedContinuation { continuation in
63- paginationTracker. resync ( reason: nil ) {
75+ paginationTracker. resync ( reason: Self . refreshCacheReason ) {
6476 continuation. resume ( returning: ( ) )
6577 }
6678 }
@@ -92,20 +104,23 @@ private extension BookingListViewModel {
92104
93105 /// Updates row view models and sync state.
94106 func updateResults( ) {
95- /// TODO: update logic for fetching bookings
96- if type == . all {
97- bookings = resultsController. fetchedObjects
98- } else {
99- bookings = [ ]
100- }
107+ bookings = resultsController. fetchedObjects
101108 transitionToResultsUpdatedState ( )
102109 }
103110}
104111
105112extension BookingListViewModel : PaginationTrackerDelegate {
106113 func sync( pageNumber: Int , pageSize: Int , reason: String ? , onCompletion: SyncCompletion ? ) {
107114 transitionToSyncingState ( )
108- let action = BookingAction . synchronizeBookings ( siteID: siteID, pageNumber: pageNumber, pageSize: pageSize) { [ weak self] result in
115+ let shouldClearCache = reason == Self . refreshCacheReason
116+ let action = BookingAction . synchronizeBookings (
117+ siteID: siteID,
118+ pageNumber: pageNumber,
119+ pageSize: pageSize,
120+ startDateBefore: type. startDateBefore ( currentDate: currentDate) ? . ISO8601Format ( ) ,
121+ startDateAfter: type. startDateAfter ( currentDate: currentDate) ? . ISO8601Format ( ) ,
122+ shouldClearCache: shouldClearCache
123+ ) { [ weak self] result in
109124 switch result {
110125 case . success( let hasNextPage) :
111126 onCompletion ? ( . success( hasNextPage) )
0 commit comments