|
| 1 | +// periphery:ignore:all |
| 2 | +import Foundation |
| 3 | +import Yosemite |
| 4 | +import protocol Storage.StorageManagerType |
| 5 | + |
| 6 | +/// View model for `BookingListView` |
| 7 | +final class BookingListViewModel: ObservableObject { |
| 8 | + |
| 9 | + @Published private(set) var bookings: [Booking] = [] |
| 10 | + |
| 11 | + private let siteID: Int64 |
| 12 | + private let stores: StoresManager |
| 13 | + private let storage: StorageManagerType |
| 14 | + |
| 15 | + /// Keeps track of the current state of the syncing |
| 16 | + @Published private(set) var syncState: SyncState = .empty |
| 17 | + |
| 18 | + /// Tracks if the infinite scroll indicator should be displayed. |
| 19 | + @Published private(set) var shouldShowBottomActivityIndicator = false |
| 20 | + |
| 21 | + /// Supports infinite scroll. |
| 22 | + private let paginationTracker: PaginationTracker |
| 23 | + private let pageFirstIndex: Int = PaginationTracker.Defaults.pageFirstIndex |
| 24 | + |
| 25 | + /// Booking ResultsController. |
| 26 | + private lazy var resultsController: ResultsController<StorageBooking> = { |
| 27 | + let predicate = NSPredicate(format: "siteID == %lld", siteID) |
| 28 | + let sortDescriptorByDate = NSSortDescriptor(key: "dateCreated", ascending: false) |
| 29 | + let resultsController = ResultsController<StorageBooking>(storageManager: storage, |
| 30 | + matching: predicate, |
| 31 | + sortedBy: [sortDescriptorByDate]) |
| 32 | + return resultsController |
| 33 | + }() |
| 34 | + |
| 35 | + init(siteID: Int64, |
| 36 | + stores: StoresManager = ServiceLocator.stores, |
| 37 | + storage: StorageManagerType = ServiceLocator.storageManager) { |
| 38 | + self.siteID = siteID |
| 39 | + self.stores = stores |
| 40 | + self.storage = storage |
| 41 | + self.paginationTracker = PaginationTracker(pageFirstIndex: pageFirstIndex) |
| 42 | + |
| 43 | + configureResultsController() |
| 44 | + configurePaginationTracker() |
| 45 | + } |
| 46 | + |
| 47 | + /// Called when loading the first page of bookings. |
| 48 | + func loadBookings() { |
| 49 | + paginationTracker.syncFirstPage() |
| 50 | + } |
| 51 | + |
| 52 | + /// Called when the next page should be loaded. |
| 53 | + func onLoadNextPageAction() { |
| 54 | + paginationTracker.ensureNextPageIsSynced() |
| 55 | + } |
| 56 | + |
| 57 | + /// Called when the user pulls down the list to refresh. |
| 58 | + /// - Parameter completion: called when the refresh completes. |
| 59 | + func onRefreshAction(completion: @escaping () -> Void) { |
| 60 | + paginationTracker.resync(reason: nil) { |
| 61 | + completion() |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: Configuration |
| 67 | + |
| 68 | +private extension BookingListViewModel { |
| 69 | + func configurePaginationTracker() { |
| 70 | + paginationTracker.delegate = self |
| 71 | + } |
| 72 | + |
| 73 | + /// Performs initial fetch from storage and updates results. |
| 74 | + func configureResultsController() { |
| 75 | + resultsController.onDidChangeContent = { [weak self] in |
| 76 | + self?.updateResults() |
| 77 | + } |
| 78 | + resultsController.onDidResetContent = { [weak self] in |
| 79 | + self?.updateResults() |
| 80 | + } |
| 81 | + do { |
| 82 | + try resultsController.performFetch() |
| 83 | + updateResults() |
| 84 | + } catch { |
| 85 | + ServiceLocator.crashLogging.logError(error) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Updates row view models and sync state. |
| 90 | + func updateResults() { |
| 91 | + bookings = resultsController.fetchedObjects |
| 92 | + transitionToResultsUpdatedState() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +extension BookingListViewModel: PaginationTrackerDelegate { |
| 97 | + func sync(pageNumber: Int, pageSize: Int, reason: String?, onCompletion: SyncCompletion?) { |
| 98 | + transitionToSyncingState() |
| 99 | + let action = BookingAction.synchronizeBookings(siteID: siteID, pageNumber: pageNumber, pageSize: pageSize) { [weak self] result in |
| 100 | + switch result { |
| 101 | + case .success(let hasNextPage): |
| 102 | + onCompletion?(.success(hasNextPage)) |
| 103 | + |
| 104 | + case .failure(let error): |
| 105 | + DDLogError("⛔️ Error synchronizing bookings: \(error)") |
| 106 | + onCompletion?(.failure(error)) |
| 107 | + } |
| 108 | + |
| 109 | + self?.updateResults() |
| 110 | + } |
| 111 | + stores.dispatch(action) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// MARK: State Machine |
| 116 | + |
| 117 | +extension BookingListViewModel { |
| 118 | + /// Represents possible states for syncing bookings. |
| 119 | + enum SyncState: Equatable { |
| 120 | + case syncingFirstPage |
| 121 | + case results |
| 122 | + case empty |
| 123 | + } |
| 124 | + |
| 125 | + /// Update states for sync from remote. |
| 126 | + func transitionToSyncingState() { |
| 127 | + shouldShowBottomActivityIndicator = true |
| 128 | + if bookings.isEmpty { |
| 129 | + syncState = .syncingFirstPage |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Update states after sync is complete. |
| 134 | + func transitionToResultsUpdatedState() { |
| 135 | + shouldShowBottomActivityIndicator = false |
| 136 | + syncState = bookings.isNotEmpty ? .results : .empty |
| 137 | + } |
| 138 | +} |
0 commit comments