@@ -110,7 +110,6 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
110110
111111 private var actionHandlersByProduct : [ Key : ProductImageActionHandler ] = [ : ]
112112 private var imagesSaverByProduct : [ Key : ProductImagesSaver ] = [ : ]
113- private var initialStatusesByProduct : [ Key : [ ProductImageStatus ] ] = [ : ]
114113
115114 @Published private var activeUploadsPublisher : [ ProductImageUploaderKey ] = [ ]
116115
@@ -125,12 +124,11 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
125124
126125 func actionHandler( key: ProductImageUploaderKey , originalStatuses: [ ProductImageStatus ] ) -> ProductImageActionHandler {
127126 let actionHandler : ProductImageActionHandler
128- if let handler = actionHandlersByProduct [ key] , handler . productImageStatuses . hasPendingUpload {
127+ if let handler = actionHandlersByProduct [ key] {
129128 actionHandler = handler
130129 } else {
131130 actionHandler = ProductImageActionHandler ( siteID: key. siteID, productID: key. productOrVariationID, imageStatuses: originalStatuses, stores: stores)
132131 actionHandlersByProduct [ key] = actionHandler
133- initialStatusesByProduct [ key] = originalStatuses
134132 observeStatusUpdates ( key: key, actionHandler: actionHandler)
135133 }
136134
@@ -171,14 +169,24 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
171169 guard let handler = actionHandlersByProduct [ key] else {
172170 return false
173171 }
174- if let productImagesSaver = imagesSaverByProduct [ key] , productImagesSaver. imageStatusesToSave. isNotEmpty {
172+ let productImagesSaver = imagesSaverByProduct [ key]
173+
174+ if let productImagesSaver, productImagesSaver. imageStatusesToSave. isNotEmpty {
175175 // If there are images scheduled to be saved, there are no unsaved changes if the image statuses to save match the latest image statuses.
176176 return handler. productImageStatuses != productImagesSaver. imageStatusesToSave
177177 } else {
178- // Otherwise, there are unsaved changes if there is any pending image upload or any difference in the remote image IDs between the
178+ if handler. productImageStatuses. hasPendingUpload {
179+ return true
180+ }
181+
182+ /// If there's a product saved in background, compare the images to determine unsaved changes.
183+ if let savedProduct = productImagesSaver? . savedProduct {
184+ return handler. productImageStatuses. images. map { $0. imageID } != savedProduct. images. map { $0. imageID }
185+ }
186+
187+ // Otherwise, there are unsaved changes if there is any difference in the remote image IDs between the
179188 // original and latest product.
180- return handler. productImageStatuses. hasPendingUpload ||
181- handler. productImageStatuses. images. map { $0. imageID } != originalImages. map { $0. imageID }
189+ return handler. productImageStatuses. images. map { $0. imageID } != originalImages. map { $0. imageID }
182190 }
183191 }
184192
@@ -187,12 +195,10 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
187195 // The product has to exist remotely in order to save its images remotely.
188196 // In product creation, this save function should be called after a new product is saved remotely for the first time.
189197 guard key. isLocalID == false else {
190- removeProductFromActiveUploads ( key: key)
191198 return onProductSave ( . failure( ProductImageUploaderError . noRemoteProductIDFound) )
192199 }
193200
194201 guard let handler = actionHandlersByProduct [ key] else {
195- removeProductFromActiveUploads ( key: key)
196202 return onProductSave ( . failure( ProductImageUploaderError . noActionHandlerFound) )
197203 }
198204
@@ -208,7 +214,6 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
208214
209215 imagesSaver. saveProductImagesWhenNoneIsPendingUploadAnymore ( imageActionHandler: handler) { [ weak self] result in
210216 guard let self = self else { return }
211- removeProductFromActiveUploads ( key: key)
212217 onProductSave ( result)
213218 if case let . failure( error) = result {
214219 self . errorsSubject. send ( . init( siteID: key. siteID,
@@ -229,7 +234,6 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
229234
230235 actionHandlersByProduct = [ : ]
231236 imagesSaverByProduct = [ : ]
232- initialStatusesByProduct = [ : ]
233237 }
234238}
235239
@@ -254,10 +258,9 @@ private extension ProductImageUploader {
254258
255259 if !activeUploadsPublisher. contains ( key) , productImageStatuses. hasPendingUpload {
256260 activeUploadsPublisher. append ( key)
257- } else if let initialStatuses = initialStatusesByProduct [ key] ,
258- initialStatuses == productImageStatuses,
259- activeUploadsPublisher. contains ( key) {
260- /// When upload is reset, remove the key from active uploads
261+ } else if activeUploadsPublisher. contains ( key) , !productImageStatuses. hasPendingUpload {
262+ /// When all pending uploads are completed or removed,
263+ /// remove the key from active uploads
261264 removeProductFromActiveUploads ( key: key)
262265 }
263266
@@ -274,7 +277,6 @@ private extension ProductImageUploader {
274277
275278 func removeProductFromActiveUploads( key: Key ) {
276279 activeUploadsPublisher. removeAll ( where: { $0 == key } )
277- initialStatusesByProduct. removeValue ( forKey: key)
278280 }
279281}
280282
0 commit comments