@@ -160,9 +160,47 @@ public final class GRDBObservableDataSource: POSObservableDataSourceProtocol {
160160
161161 private func setupVariationObservation( parentProduct: POSVariableParentProduct ) {
162162 let currentPage = currentVariationPage
163+ let parentProductID = parentProduct. productID
164+
165+ struct ObservationResult {
166+ let variations : [ POSProductVariation ]
167+ let parentProduct : POSVariableParentProduct
168+ }
169+
163170 let observation = ValueObservation
164- . tracking { [ weak self] database -> [ POSProductVariation ] in
165- guard let self else { return [ ] }
171+ . tracking { [ weak self] database -> ObservationResult in
172+ guard let self else { return ObservationResult ( variations: [ ] , parentProduct: parentProduct) }
173+
174+ // Fetch parent product with updated attributes
175+ struct ParentProductWithAttributes : Decodable , FetchableRecord {
176+ let product : PersistedProduct
177+ let attributes : [ PersistedProductAttribute ]
178+ }
179+
180+ let parentWithAttributes = try PersistedProduct
181+ . filter ( PersistedProduct . Columns. siteID == self . siteID)
182+ . filter ( PersistedProduct . Columns. id == parentProductID)
183+ . including ( all: PersistedProduct . attributes)
184+ . asRequest ( of: ParentProductWithAttributes . self)
185+ . fetchOne ( database)
186+
187+ // Create updated parent product with fresh attributes
188+ let updatedParentProduct : POSVariableParentProduct
189+ if let parentWithAttributes {
190+ let attributes = ( parentWithAttributes. attributes) . map {
191+ $0. toProductAttribute ( siteID: parentWithAttributes. product. siteID)
192+ }
193+ let product = parentWithAttributes. product
194+ updatedParentProduct = POSVariableParentProduct (
195+ id: parentProduct. id,
196+ name: product. name,
197+ productImageSource: nil , // Image not needed for variation name generation
198+ productID: product. id,
199+ allAttributes: attributes
200+ )
201+ } else {
202+ updatedParentProduct = parentProduct
203+ }
166204
167205 struct VariationWithRelations : Decodable , FetchableRecord {
168206 let persistedProductVariation : PersistedProductVariation
@@ -171,36 +209,42 @@ public final class GRDBObservableDataSource: POSObservableDataSourceProtocol {
171209 }
172210
173211 let variationsWithRelations = try PersistedProductVariation
174- . posVariationsRequest ( siteID: self . siteID, parentProductID: parentProduct . productID )
212+ . posVariationsRequest ( siteID: self . siteID, parentProductID: parentProductID )
175213 . limit ( self . pageSize * currentPage)
176214 . including ( all: PersistedProductVariation . attributes)
177215 . including ( optional: PersistedProductVariation . image)
178216 . asRequest ( of: VariationWithRelations . self)
179217 . fetchAll ( database)
180218
181- return variationsWithRelations. map { record in
219+ let variations = variationsWithRelations. map { record in
182220 record. persistedProductVariation. toPOSProductVariation (
183221 attributes: ( record. attributes ?? [ ] ) . map { $0. toProductVariationAttribute ( ) } ,
184222 image: record. image? . toProductImage ( )
185223 )
186224 }
225+
226+ return ObservationResult ( variations: variations, parentProduct: updatedParentProduct)
187227 }
188228
189229 variationObservationCancellable = observation
190230 . publisher ( in: grdbManager. databaseConnection)
191231 . receive ( on: DispatchQueue . main)
192232 . sink (
193- receiveCompletion: { [ weak self] completion in
233+ receiveCompletion: { [ weak self] ( completion: Subscribers . Completion < Error > ) in
194234 if case . failure( let error) = completion {
195235 self ? . variationError = error
196236 self ? . isLoadingVariations = false
197237 }
198238 } ,
199- receiveValue: { [ weak self] observedVariations in
239+ receiveValue: { [ weak self] ( result : ObservationResult ) in
200240 guard let self else { return }
241+
242+ // Update current parent product with fresh attributes
243+ self . currentParentProduct = result. parentProduct
244+
201245 let posItems = itemMapper. mapVariationsToPOSItems (
202- variations: observedVariations ,
203- parentProduct: parentProduct
246+ variations: result . variations ,
247+ parentProduct: result . parentProduct
204248 )
205249 variationItems = posItems
206250 variationError = nil
0 commit comments