Skip to content

Commit 3be9236

Browse files
committed
Integrate GenerateAllVariations Presenter
1 parent fb36d16 commit 3be9236

File tree

1 file changed

+8
-121
lines changed

1 file changed

+8
-121
lines changed

WooCommerce/Classes/ViewRelated/Products/Variations/ProductVariationsViewController.swift

Lines changed: 8 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,7 @@ private extension ProductVariationsViewController {
565565

566566
@objc func addButtonTapped() {
567567
analytics.track(event: WooAnalyticsEvent.Variations.addMoreVariationsButtonTapped(productID: product.productID))
568-
569-
if featureFlagService.isFeatureFlagEnabled(.generateAllVariations) {
570-
presentGenerateVariationOptions()
571-
} else {
572-
createVariation()
573-
}
568+
presentGenerateVariationOptions()
574569
}
575570

576571
/// More Options Action Sheet
@@ -616,83 +611,6 @@ private extension ProductVariationsViewController {
616611
}
617612
}
618613

619-
/// Informs the merchant about errors that happen during the variation generation
620-
///
621-
private func presentGenerationError(_ error: ProductVariationsViewModel.GenerationError) {
622-
let notice = Notice(title: error.errorTitle, message: error.errorDescription)
623-
noticePresenter.enqueue(notice: notice)
624-
}
625-
626-
/// Asks the merchant for confirmation before generating all variations.
627-
///
628-
private func presentGenerationConfirmation(numberOfVariations: Int, onCompletion: @escaping (_ confirmed: Bool) -> Void) {
629-
let controller = UIAlertController(title: Localization.confirmationTitle,
630-
message: Localization.confirmationDescription(variationCount: numberOfVariations),
631-
preferredStyle: .alert)
632-
controller.addDefaultActionWithTitle(Localization.ok) { _ in
633-
onCompletion(true)
634-
}
635-
controller.addCancelActionWithTitle(Localization.cancel) { _ in
636-
onCompletion(false)
637-
}
638-
639-
// There should be an `inProgressViewController` being presented. Fallback to `self` in case there isn't one.
640-
let baseViewController = presentedViewController ?? self
641-
baseViewController.present(controller, animated: true)
642-
}
643-
644-
/// Presents a blocking view controller while variations are being fetched.
645-
///
646-
private func presentFetchingIndicator() {
647-
let inProgressViewController = InProgressViewController(viewProperties: .init(title: Localization.fetchingVariations, message: ""))
648-
present(inProgressViewController, animated: true)
649-
}
650-
651-
/// Presents a blocking view controller while variations are being created.
652-
///
653-
private func presentCreatingIndicator() {
654-
let newViewProperties = InProgressViewProperties(title: Localization.creatingVariations, message: "")
655-
656-
// There should be already a presented `InProgressViewController`. But we present one in case there isn;t.
657-
guard let inProgressViewController = self.presentedViewController as? InProgressViewController else {
658-
let inProgressViewController = InProgressViewController(viewProperties: newViewProperties)
659-
return present(inProgressViewController, animated: true)
660-
}
661-
662-
// Update the already presented view controller with the "Creating..." copy.
663-
inProgressViewController.updateViewProperties(newViewProperties)
664-
}
665-
666-
/// Dismiss any `InProgressViewController` being presented.
667-
/// By default retires the dismissal one time to overcome UIKit presentation delays.
668-
///
669-
private func dismissBlockingIndicator(retry: Bool = true) {
670-
if let inProgressViewController = self.presentedViewController as? InProgressViewController {
671-
inProgressViewController.dismiss(animated: true)
672-
} else {
673-
// When this method is invoked right after `InProgressViewController` is presented, chances are that it won't exists in the view controller
674-
// hierarchy yet.
675-
// Here we are retrying it with a small delay to give UIKit APIs time to finish its presentation.
676-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
677-
self.dismissBlockingIndicator(retry: false)
678-
}
679-
}
680-
}
681-
682-
/// Informs the merchant that no variations were created.
683-
///
684-
private func presentNoGenerationNotice() {
685-
let notice = Notice(title: Localization.noVariationsCreatedTitle, message: Localization.noVariationsCreatedDescription)
686-
noticePresenter.enqueue(notice: notice)
687-
}
688-
689-
/// Informs the merchant that some variations were created.
690-
///
691-
private func presentVariationsCreatedNotice() {
692-
let notice = Notice(title: Localization.variationsCreatedTitle)
693-
noticePresenter.enqueue(notice: notice)
694-
}
695-
696614
/// Updates the current product with the up-to-date list of variations IDs.
697615
/// This is needed in order to reflect variations count changes back to this and to other screens.
698616
///
@@ -775,28 +693,18 @@ extension ProductVariationsViewController: SyncingCoordinatorDelegate {
775693
/// Generates all possible variations for the product attributes.
776694
///
777695
private func generateAllVariations() {
778-
viewModel.generateAllVariations(for: product) { [weak self] currentState in
696+
let presenter = GenerateAllVariationsPresenter(baseViewController: self)
697+
viewModel.generateAllVariations(for: product) { [weak self, presenter] currentState in
698+
// Perform Presentation Actions
699+
presenter.handleStateChanges(state: currentState)
700+
701+
// Perform other side effects
779702
switch currentState {
780-
case .fetching:
781-
self?.presentFetchingIndicator()
782-
case .confirmation(let variationCount, let onCompletion):
783-
self?.presentGenerationConfirmation(numberOfVariations: variationCount, onCompletion: onCompletion)
784-
case .creating:
785-
self?.presentCreatingIndicator()
786-
case .canceled:
787-
self?.dismissBlockingIndicator()
788703
case .finished(let variationsCreated):
789-
self?.dismissBlockingIndicator()
790704
if variationsCreated {
791705
self?.updateProductVariationCount()
792-
self?.presentVariationsCreatedNotice()
793-
} else {
794-
self?.presentNoGenerationNotice()
795706
}
796-
break
797-
case .error(let error):
798-
self?.dismissBlockingIndicator()
799-
self?.presentGenerationError(error)
707+
default: break
800708
}
801709
}
802710
}
@@ -905,27 +813,6 @@ private extension ProductVariationsViewController {
905813
static let generateVariationError = NSLocalizedString("The variation couldn't be generated.",
906814
comment: "Error title when failing to generate a variation.")
907815
static let variationCreated = NSLocalizedString("Variation created", comment: "Text for the notice after creating the first variation.")
908-
909-
static let confirmationTitle = NSLocalizedString("Generate all variations?",
910-
comment: "Alert title to allow the user confirm if they want to generate all variations")
911-
static func confirmationDescription(variationCount: Int) -> String {
912-
let format = NSLocalizedString("This will create a variation for each and every possible combination of variation attributes (%d variations).",
913-
comment: "Alert description to allow the user confirm if they want to generate all variations")
914-
return String.localizedStringWithFormat(format, variationCount)
915-
}
916-
static let ok = NSLocalizedString("OK", comment: "Button text to confirm that we want to generate all variations")
917-
static let cancel = NSLocalizedString("Cancel", comment: "Button text to confirm that we don't want to generate all variations")
918-
static let fetchingVariations = NSLocalizedString("Fetching Variations...",
919-
comment: "Blocking indicator text when fetching existing variations prior generating them.")
920-
static let creatingVariations = NSLocalizedString("Creating Variations...",
921-
comment: "Blocking indicator text when creating multiple variations remotely.")
922-
static let noVariationsCreatedTitle = NSLocalizedString("No variations to generate",
923-
comment: "Title for the notice when there were no variations to generate")
924-
static let noVariationsCreatedDescription = NSLocalizedString("All variations are already generated.",
925-
comment: "Message for the notice when there were no variations to generate")
926-
static let variationsCreatedTitle = NSLocalizedString("Variations created successfully",
927-
comment: "Title for the notice when after variations were created")
928-
929816
}
930817

931818
/// Localizated strings for the action sheet options

0 commit comments

Comments
 (0)