|
| 1 | +// periphery:ignore:all |
| 2 | +import Foundation |
| 3 | +import GRDB |
| 4 | +import Combine |
| 5 | +import Observation |
| 6 | +import Storage |
| 7 | +import WooFoundation |
| 8 | + |
| 9 | +/// Observable data source for GRDB-based POS items using ValueObservation |
| 10 | +/// Provides automatic SwiftUI updates when database changes occur |
| 11 | +@Observable |
| 12 | +public final class GRDBObservableDataSource: POSObservableDataSourceProtocol { |
| 13 | + // MARK: - Observable Properties |
| 14 | + |
| 15 | + public private(set) var productItems: [POSItem] = [] |
| 16 | + public private(set) var variationItems: [POSItem] = [] |
| 17 | + public private(set) var isLoadingProducts: Bool = false |
| 18 | + public private(set) var isLoadingVariations: Bool = false |
| 19 | + public private(set) var error: Error? = nil |
| 20 | + |
| 21 | + public var hasMoreProducts: Bool { |
| 22 | + productItems.count >= (pageSize * currentProductPage) && totalProductCount > productItems.count |
| 23 | + } |
| 24 | + |
| 25 | + public var hasMoreVariations: Bool { |
| 26 | + variationItems.count >= (pageSize * currentVariationPage) && totalVariationCount > variationItems.count |
| 27 | + } |
| 28 | + |
| 29 | + // MARK: - Private Properties |
| 30 | + |
| 31 | + private let siteID: Int64 |
| 32 | + private let grdbManager: GRDBManagerProtocol |
| 33 | + private let itemMapper: PointOfSaleItemMapperProtocol |
| 34 | + private let pageSize: Int |
| 35 | + |
| 36 | + private var currentProductPage: Int = 1 |
| 37 | + private var currentVariationPage: Int = 1 |
| 38 | + private var currentParentProduct: POSVariableParentProduct? |
| 39 | + private var totalProductCount: Int = 0 |
| 40 | + private var totalVariationCount: Int = 0 |
| 41 | + |
| 42 | + // ValueObservation subscriptions |
| 43 | + private var productObservationCancellable: AnyCancellable? |
| 44 | + private var variationObservationCancellable: AnyCancellable? |
| 45 | + private var statisticsObservationCancellable: AnyCancellable? |
| 46 | + private var variationStatisticsObservationCancellable: AnyCancellable? |
| 47 | + |
| 48 | + // MARK: - Initialization |
| 49 | + |
| 50 | + public init(siteID: Int64, |
| 51 | + grdbManager: GRDBManagerProtocol, |
| 52 | + currencySettings: CurrencySettings, |
| 53 | + itemMapper: PointOfSaleItemMapperProtocol? = nil, |
| 54 | + pageSize: Int = 20) { |
| 55 | + self.siteID = siteID |
| 56 | + self.grdbManager = grdbManager |
| 57 | + self.itemMapper = itemMapper ?? PointOfSaleItemMapper(currencySettings: currencySettings) |
| 58 | + self.pageSize = pageSize |
| 59 | + |
| 60 | + setupStatisticsObservation() |
| 61 | + } |
| 62 | + |
| 63 | + deinit { |
| 64 | + productObservationCancellable?.cancel() |
| 65 | + variationObservationCancellable?.cancel() |
| 66 | + statisticsObservationCancellable?.cancel() |
| 67 | + variationStatisticsObservationCancellable?.cancel() |
| 68 | + } |
| 69 | + |
| 70 | + // MARK: - POSObservableDataSourceProtocol |
| 71 | + |
| 72 | + public func loadProducts() { |
| 73 | + currentProductPage = 1 |
| 74 | + isLoadingProducts = true |
| 75 | + setupProductObservation() |
| 76 | + } |
| 77 | + |
| 78 | + public func loadMoreProducts() { |
| 79 | + guard hasMoreProducts && !isLoadingProducts else { return } |
| 80 | + |
| 81 | + isLoadingProducts = true |
| 82 | + currentProductPage += 1 |
| 83 | + setupProductObservation() |
| 84 | + } |
| 85 | + |
| 86 | + public func loadVariations(for parentProduct: POSVariableParentProduct) { |
| 87 | + guard currentParentProduct?.productID != parentProduct.productID else { |
| 88 | + return // Same parent - idempotent |
| 89 | + } |
| 90 | + |
| 91 | + currentParentProduct = parentProduct |
| 92 | + currentVariationPage = 1 |
| 93 | + isLoadingVariations = true |
| 94 | + variationItems = [] |
| 95 | + |
| 96 | + setupVariationObservation(parentProduct: parentProduct) |
| 97 | + setupVariationStatisticsObservation(parentProduct: parentProduct) |
| 98 | + } |
| 99 | + |
| 100 | + public func loadMoreVariations() { |
| 101 | + guard let parentProduct = currentParentProduct, |
| 102 | + hasMoreVariations && !isLoadingVariations else { return } |
| 103 | + |
| 104 | + isLoadingVariations = true |
| 105 | + currentVariationPage += 1 |
| 106 | + setupVariationObservation(parentProduct: parentProduct) |
| 107 | + } |
| 108 | + |
| 109 | + public func refresh() { |
| 110 | + // No-op: database observation automatically updates when data changes during incremental sync |
| 111 | + } |
| 112 | + |
| 113 | + // MARK: - ValueObservation Setup |
| 114 | + |
| 115 | + private func setupProductObservation() { |
| 116 | + let currentPage = currentProductPage |
| 117 | + let observation = ValueObservation |
| 118 | + .tracking { [weak self] database -> [POSProduct] in |
| 119 | + guard let self else { return [] } |
| 120 | + |
| 121 | + struct ProductWithRelations: Decodable, FetchableRecord { |
| 122 | + let product: PersistedProduct |
| 123 | + let images: [PersistedImage]? |
| 124 | + let attributes: [PersistedProductAttribute]? |
| 125 | + } |
| 126 | + |
| 127 | + let productsWithRelations = try PersistedProduct |
| 128 | + .posProductsRequest(siteID: siteID) |
| 129 | + .limit(pageSize * currentPage) |
| 130 | + .including(all: PersistedProduct.images) |
| 131 | + .including(all: PersistedProduct.attributes) |
| 132 | + .asRequest(of: ProductWithRelations.self) |
| 133 | + .fetchAll(database) |
| 134 | + |
| 135 | + return productsWithRelations.map { record in |
| 136 | + record.product.toPOSProduct( |
| 137 | + images: (record.images ?? []).map { $0.toProductImage() }, |
| 138 | + attributes: (record.attributes ?? []).map { $0.toProductAttribute(siteID: record.product.siteID) } |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + productObservationCancellable = observation |
| 144 | + .publisher(in: grdbManager.databaseConnection) |
| 145 | + .receive(on: DispatchQueue.main) |
| 146 | + .sink( |
| 147 | + receiveCompletion: { [weak self] completion in |
| 148 | + if case .failure(let error) = completion { |
| 149 | + self?.error = error |
| 150 | + self?.isLoadingProducts = false |
| 151 | + } |
| 152 | + }, |
| 153 | + receiveValue: { [weak self] observedProducts in |
| 154 | + guard let self else { return } |
| 155 | + let posItems = itemMapper.mapProductsToPOSItems(products: observedProducts) |
| 156 | + productItems = posItems |
| 157 | + error = nil |
| 158 | + isLoadingProducts = false |
| 159 | + } |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + private func setupVariationObservation(parentProduct: POSVariableParentProduct) { |
| 164 | + let currentPage = currentVariationPage |
| 165 | + let observation = ValueObservation |
| 166 | + .tracking { [weak self] database -> [POSProductVariation] in |
| 167 | + guard let self else { return [] } |
| 168 | + |
| 169 | + struct VariationWithRelations: Decodable, FetchableRecord { |
| 170 | + let persistedProductVariation: PersistedProductVariation |
| 171 | + let attributes: [PersistedProductVariationAttribute]? |
| 172 | + let image: PersistedImage? |
| 173 | + } |
| 174 | + |
| 175 | + let variationsWithRelations = try PersistedProductVariation |
| 176 | + .posVariationsRequest(siteID: self.siteID, parentProductID: parentProduct.productID) |
| 177 | + .limit(self.pageSize * currentPage) |
| 178 | + .including(all: PersistedProductVariation.attributes) |
| 179 | + .including(optional: PersistedProductVariation.image) |
| 180 | + .asRequest(of: VariationWithRelations.self) |
| 181 | + .fetchAll(database) |
| 182 | + |
| 183 | + return variationsWithRelations.map { record in |
| 184 | + record.persistedProductVariation.toPOSProductVariation( |
| 185 | + attributes: (record.attributes ?? []).map { $0.toProductVariationAttribute() }, |
| 186 | + image: record.image?.toProductImage() |
| 187 | + ) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + variationObservationCancellable = observation |
| 192 | + .publisher(in: grdbManager.databaseConnection) |
| 193 | + .receive(on: DispatchQueue.main) |
| 194 | + .sink( |
| 195 | + receiveCompletion: { [weak self] completion in |
| 196 | + if case .failure(let error) = completion { |
| 197 | + self?.error = error |
| 198 | + self?.isLoadingVariations = false |
| 199 | + } |
| 200 | + }, |
| 201 | + receiveValue: { [weak self] observedVariations in |
| 202 | + guard let self else { return } |
| 203 | + let posItems = itemMapper.mapVariationsToPOSItems( |
| 204 | + variations: observedVariations, |
| 205 | + parentProduct: parentProduct |
| 206 | + ) |
| 207 | + variationItems = posItems |
| 208 | + error = nil |
| 209 | + isLoadingVariations = false |
| 210 | + } |
| 211 | + ) |
| 212 | + } |
| 213 | + |
| 214 | + private func setupStatisticsObservation() { |
| 215 | + let observation = ValueObservation |
| 216 | + .tracking { [weak self] database in |
| 217 | + guard let self else { return 0 } |
| 218 | + |
| 219 | + let productCount = try PersistedProduct |
| 220 | + .posProductsRequest(siteID: siteID) |
| 221 | + .fetchCount(database) |
| 222 | + |
| 223 | + return productCount |
| 224 | + } |
| 225 | + |
| 226 | + statisticsObservationCancellable = observation |
| 227 | + .publisher(in: grdbManager.databaseConnection) |
| 228 | + .receive(on: DispatchQueue.main) |
| 229 | + .sink( |
| 230 | + receiveCompletion: { completion in |
| 231 | + if case .failure = completion { |
| 232 | + // Silently ignore - statistics are not critical |
| 233 | + } |
| 234 | + }, |
| 235 | + receiveValue: { [weak self] productCount in |
| 236 | + self?.totalProductCount = productCount |
| 237 | + } |
| 238 | + ) |
| 239 | + } |
| 240 | + |
| 241 | + private func setupVariationStatisticsObservation(parentProduct: POSVariableParentProduct) { |
| 242 | + let observation = ValueObservation |
| 243 | + .tracking { [weak self] database in |
| 244 | + guard let self else { return 0 } |
| 245 | + |
| 246 | + return try PersistedProductVariation |
| 247 | + .posVariationsRequest(siteID: siteID, parentProductID: parentProduct.productID) |
| 248 | + .fetchCount(database) |
| 249 | + } |
| 250 | + |
| 251 | + variationStatisticsObservationCancellable = observation |
| 252 | + .publisher(in: grdbManager.databaseConnection) |
| 253 | + .receive(on: DispatchQueue.main) |
| 254 | + .sink( |
| 255 | + receiveCompletion: { completion in |
| 256 | + if case .failure = completion { |
| 257 | + // Silently ignore - statistics are not critical |
| 258 | + } |
| 259 | + }, |
| 260 | + receiveValue: { [weak self] variationCount in |
| 261 | + self?.totalVariationCount = variationCount |
| 262 | + } |
| 263 | + ) |
| 264 | + } |
| 265 | +} |
0 commit comments