Skip to content

Commit 400817f

Browse files
committed
Add method to trigger notice from ProductImageUploader
1 parent 7b9d692 commit 400817f

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

WooCommerce/Classes/ServiceLocator/ProductImageUploader.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Combine
2+
import Foundation
23
import struct Yosemite.ProductImage
34
import enum Yosemite.ProductAction
45
import protocol Yosemite.StoresManager
@@ -79,6 +80,11 @@ protocol ProductImageUploaderProtocol {
7980
/// - key: identifiable information about the product.
8081
func startEmittingErrors(key: ProductImageUploaderKey)
8182

83+
/// Triggers a notice about background image upload for a product if needed.
84+
/// - Parameter key: identifiable information about the product.
85+
///
86+
func sendBackgroundUploadNoticeIfNeeded(key: ProductImageUploaderKey, using noticePresenter: NoticePresenter)
87+
8288
/// Determines whether there are unsaved changes on a product's images.
8389
/// If the product had any save request before, it checks whether the image statuses to save match the latest image statuses.
8490
/// Otherwise, it checks whether there is any pending upload or the image statuses match the given original image statuses.
@@ -167,6 +173,18 @@ final class ProductImageUploader: ProductImageUploaderProtocol {
167173
statusUpdatesExcludedProductKeys.remove(key)
168174
}
169175

176+
func sendBackgroundUploadNoticeIfNeeded(key: ProductImageUploaderKey, using noticePresenter: NoticePresenter) {
177+
if activeUploadsPublisher.contains(key) {
178+
let title = NSLocalizedString(
179+
"productImageUploader.backgroundUploadNotice.title",
180+
value: "Image uploading will continue in the background",
181+
comment: ""
182+
)
183+
let notice = Notice(title: title)
184+
noticePresenter.enqueue(notice: notice)
185+
}
186+
}
187+
170188
func hasUnsavedChangesOnImages(key: ProductImageUploaderKey, originalImages: [ProductImage]) -> Bool {
171189
guard let handler = actionHandlersByProduct[key] else {
172190
return false

WooCommerce/WooCommerceTests/Mocks/MockProductImageUploader.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ extension MockProductImageUploader: ProductImageUploaderProtocol {
6363
hasUnsavedChangesOnImages
6464
}
6565

66+
func sendBackgroundUploadNoticeIfNeeded(key: ProductImageUploaderKey, using noticePresenter: NoticePresenter) {
67+
// no-op
68+
}
69+
6670
func reset() {
6771
resetWasCalled = true
6872
}

WooCommerce/WooCommerceTests/ViewRelated/Products/Media/ProductImageUploaderTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ final class ProductImageUploaderTests: XCTestCase {
617617
}
618618

619619
func test_product_is_removed_from_activeUploads_when_upload_is_cancelled() {
620+
// Given
620621
let stores = MockStoresManager(sessionManager: .testingInstance)
621622
let imageUploader = ProductImageUploader(stores: stores)
622623
let key = ProductImageUploaderKey(siteID: siteID,
@@ -648,6 +649,45 @@ final class ProductImageUploaderTests: XCTestCase {
648649
activeUploads == []
649650
}
650651
}
652+
653+
func test_background_upload_notice_is_sent_when_there_are_active_uploads() {
654+
// Given
655+
let stores = MockStoresManager(sessionManager: .testingInstance)
656+
let imageUploader = ProductImageUploader(stores: stores)
657+
let key = ProductImageUploaderKey(siteID: siteID,
658+
productOrVariationID: .product(id: productID),
659+
isLocalID: false)
660+
let actionHandler = imageUploader.actionHandler(key: key, originalStatuses: [])
661+
662+
let noticePresenter = MockNoticePresenter()
663+
var isNoticeTriggered = false
664+
noticePresenter.onNoticeQueued = { _ in
665+
isNoticeTriggered = true
666+
}
667+
668+
var activeUploads: [ProductImageUploaderKey] = []
669+
activeUploadsSubscription = imageUploader.activeUploads
670+
.sink { keys in
671+
activeUploads = keys
672+
}
673+
674+
// When
675+
imageUploader.sendBackgroundUploadNoticeIfNeeded(key: key, using: noticePresenter)
676+
677+
// Then
678+
XCTAssertFalse(isNoticeTriggered)
679+
680+
// When
681+
let asset = PHAsset()
682+
actionHandler.uploadMediaAssetToSiteMediaLibrary(asset: .phAsset(asset: asset))
683+
waitUntil {
684+
activeUploads == [key]
685+
}
686+
687+
// Then
688+
imageUploader.sendBackgroundUploadNoticeIfNeeded(key: key, using: noticePresenter)
689+
XCTAssertTrue(isNoticeTriggered)
690+
}
651691
}
652692

653693
extension ProductImageUploadErrorInfo: @retroactive Equatable {

0 commit comments

Comments
 (0)