|
| 1 | +import Foundation |
| 2 | +import Networking |
| 3 | +import Storage |
| 4 | + |
| 5 | +// MARK: - BookingStore |
| 6 | +// |
| 7 | +public class BookingStore: Store { |
| 8 | + private let remote: BookingsRemoteProtocol |
| 9 | + |
| 10 | + public override convenience init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) { |
| 11 | + let remote = BookingsRemote(network: network) |
| 12 | + self.init(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote) |
| 13 | + } |
| 14 | + |
| 15 | + public init(dispatcher: Dispatcher, |
| 16 | + storageManager: StorageManagerType, |
| 17 | + network: Network, |
| 18 | + remote: BookingsRemoteProtocol) { |
| 19 | + self.remote = remote |
| 20 | + super.init(dispatcher: dispatcher, storageManager: storageManager, network: network) |
| 21 | + } |
| 22 | + |
| 23 | + /// Registers for supported Actions. |
| 24 | + /// |
| 25 | + override public func registerSupportedActions(in dispatcher: Dispatcher) { |
| 26 | + dispatcher.register(processor: self, for: BookingAction.self) |
| 27 | + } |
| 28 | + |
| 29 | + /// Receives and executes Actions. |
| 30 | + /// |
| 31 | + override public func onAction(_ action: Action) { |
| 32 | + guard let action = action as? BookingAction else { |
| 33 | + assertionFailure("BookingStore received an unsupported action") |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + switch action { |
| 38 | + case let .synchronizeBookings(siteID, pageNumber, pageSize, onCompletion): |
| 39 | + synchronizeBookings(siteID: siteID, pageNumber: pageNumber, pageSize: pageSize, onCompletion: onCompletion) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +// MARK: - Services |
| 46 | +// |
| 47 | +private extension BookingStore { |
| 48 | + |
| 49 | + /// Synchronizes the bookings for the specified site. |
| 50 | + /// |
| 51 | + func synchronizeBookings(siteID: Int64, |
| 52 | + pageNumber: Int, |
| 53 | + pageSize: Int, |
| 54 | + onCompletion: @escaping (Result<Bool, Error>) -> Void) { |
| 55 | + Task { @MainActor in |
| 56 | + do { |
| 57 | + let bookings = try await remote.loadAllBookings(for: siteID, |
| 58 | + pageNumber: pageNumber, |
| 59 | + pageSize: pageSize) |
| 60 | + await upsertStoredBookingsInBackground(readOnlyBookings: bookings, siteID: siteID) |
| 61 | + let hasNextPage = bookings.count == pageSize |
| 62 | + onCompletion(.success(hasNextPage)) |
| 63 | + } catch { |
| 64 | + onCompletion(.failure(error)) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +// MARK: - Storage: Booking |
| 72 | +// |
| 73 | +extension BookingStore { |
| 74 | + |
| 75 | + /// Updates (OR Inserts) the specified ReadOnly Booking Entities *in a background thread* async. |
| 76 | + /// Also deletes existing bookings if requested. |
| 77 | + func upsertStoredBookingsInBackground(readOnlyBookings: [Yosemite.Booking], |
| 78 | + siteID: Int64, |
| 79 | + shouldDeleteExistingBookings: Bool = false) async { |
| 80 | + await withCheckedContinuation { [weak self] continuation in |
| 81 | + guard let self else { |
| 82 | + return continuation.resume() |
| 83 | + } |
| 84 | + upsertStoredBookingsInBackground(readOnlyBookings: readOnlyBookings, |
| 85 | + siteID: siteID, |
| 86 | + shouldDeleteExistingBookings: shouldDeleteExistingBookings) { |
| 87 | + continuation.resume() |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// Updates (OR Inserts) the specified ReadOnly Booking Entities *in a background thread*. |
| 93 | + /// Also deletes existing bookings if requested. |
| 94 | + /// `onCompletion` will be called on the main thread! |
| 95 | + /// |
| 96 | + func upsertStoredBookingsInBackground(readOnlyBookings: [Yosemite.Booking], |
| 97 | + siteID: Int64, |
| 98 | + shouldDeleteExistingBookings: Bool = false, |
| 99 | + onCompletion: @escaping () -> Void) { |
| 100 | + storageManager.performAndSave({ [weak self] storage in |
| 101 | + guard let self else { |
| 102 | + return onCompletion() |
| 103 | + } |
| 104 | + if shouldDeleteExistingBookings { |
| 105 | + storage.deleteBookings(siteID: siteID) |
| 106 | + } |
| 107 | + upsertStoredBookings(readOnlyBookings: readOnlyBookings, in: storage) |
| 108 | + }, completion: onCompletion, on: .main) |
| 109 | + } |
| 110 | + |
| 111 | + /// Updates (OR Inserts) the specified ReadOnly Booking Entities into the Storage Layer. |
| 112 | + /// |
| 113 | + /// - Parameters: |
| 114 | + /// - readOnlyBookings: Remote Bookings to be persisted. |
| 115 | + /// - storage: Where we should save all the things! |
| 116 | + /// |
| 117 | + func upsertStoredBookings(readOnlyBookings: [Networking.Booking], in storage: StorageType) { |
| 118 | + // Fetch all existing bookings for the site at once |
| 119 | + let bookingIDs = readOnlyBookings.map { $0.bookingID } |
| 120 | + let siteID = readOnlyBookings.first?.siteID ?? 0 |
| 121 | + let storedBookings = storage.loadBookings(siteID: siteID, bookingIDs: bookingIDs) |
| 122 | + |
| 123 | + for readOnlyBooking in readOnlyBookings { |
| 124 | + // Filter to find existing booking by booking ID |
| 125 | + let storageBooking = storedBookings.first { $0.bookingID == readOnlyBooking.bookingID } ?? |
| 126 | + storage.insertNewObject(ofType: StorageBooking.self) |
| 127 | + |
| 128 | + storageBooking.update(with: readOnlyBooking) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments