|
1 | 1 | import Foundation |
| 2 | +import Yosemite |
| 3 | +import Combine |
| 4 | + |
| 5 | +/// Possible states of an `OrderSynchronizer` type. |
| 6 | +/// |
| 7 | +enum OrderSyncState { |
| 8 | + case syncing |
| 9 | + case synced |
| 10 | + case error(Error) |
| 11 | +} |
| 12 | + |
| 13 | +/// Product input for an `OrderSynchronizer` type. |
| 14 | +/// |
| 15 | +struct OrderSyncProductInput { |
| 16 | + let product: Product |
| 17 | + let quantity: Int |
| 18 | +} |
| 19 | + |
| 20 | +/// Addresses input for an `OrderSynchronizer` type. |
| 21 | +/// |
| 22 | +struct OrderSyncAddressesInput { |
| 23 | + let billing: Address |
| 24 | + let shipping: Address |
| 25 | +} |
| 26 | + |
| 27 | +/// A type that receives "supported" order properties and keeps it synced against another source. |
| 28 | +/// |
| 29 | +protocol OrderSynchronizer { |
| 30 | + |
| 31 | + // MARK: Outputs |
| 32 | + |
| 33 | + /// Defines the current sync state of the synchronizer. |
| 34 | + /// |
| 35 | + var state: Published<OrderSyncState> { get } |
| 36 | + |
| 37 | + /// Defines the latest order to be synced or that is synced. |
| 38 | + /// |
| 39 | + var order: Published<Order> { get } |
| 40 | + |
| 41 | + // MARK: Inputs |
| 42 | + |
| 43 | + /// Changes the underlaying order status. |
| 44 | + /// This property is not synched remotely until `commitAllChanges` method is invoked. |
| 45 | + /// |
| 46 | + var setStatus: PassthroughSubject<OrderStatusEnum, Never> { get } |
| 47 | + |
| 48 | + /// Sets a product with it's quantity. |
| 49 | + /// Set a `zero` quantity to remove a product. |
| 50 | + /// |
| 51 | + var setProduct: PassthroughSubject<OrderSyncProductInput, Never> { get } |
| 52 | + |
| 53 | + /// Sets or removes the order shipping & billing addresses. |
| 54 | + /// |
| 55 | + var setAddresses: PassthroughSubject<OrderSyncAddressesInput?, Never> { get } |
| 56 | + |
| 57 | + /// Sets or removes a shipping line. |
| 58 | + /// |
| 59 | + var setShipping: PassthroughSubject<ShippingLine?, Never> { get } |
| 60 | + |
| 61 | + /// Sets or removes an order fee. |
| 62 | + /// |
| 63 | + var setFee: PassthroughSubject<OrderFeeLine?, Never> { get } |
| 64 | + |
| 65 | + /// Retires the order sync. State needs to be in `.error` to initiate work. |
| 66 | + /// |
| 67 | + func retrySync() |
| 68 | + |
| 69 | + /// Commits all order changes to the remote source. State needs to be in `.synced` to initiate work. |
| 70 | + /// |
| 71 | + func commitAllChanges(onCompletion: (Result<Order, Error>) -> Void) |
| 72 | +} |
0 commit comments