Skip to content

Commit ab8f07b

Browse files
authored
Merge pull request #8573 from woocommerce/issue/8491-loading-indicators
Variations: Show loading indicators
2 parents e297611 + 3616738 commit ab8f07b

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,40 @@ private extension ProductVariationsViewController {
639639
controller.addCancelActionWithTitle(Localization.cancel) { _ in
640640
onCompletion(false)
641641
}
642-
present(controller, animated: true)
642+
643+
// There should be an `inProgressViewController` being presented. Fallback to `self` in case there isn't one.
644+
let baseViewController = presentedViewController ?? self
645+
baseViewController.present(controller, animated: true)
646+
}
647+
648+
/// Presents a blocking view controller while variations are being fetched.
649+
///
650+
private func presentFetchingIndicator() {
651+
let inProgressViewController = InProgressViewController(viewProperties: .init(title: Localization.fetchingVariations, message: ""))
652+
present(inProgressViewController, animated: true)
653+
}
654+
655+
/// Presents a blocking view controller while variations are being created.
656+
///
657+
private func presentCreatingIndicator() {
658+
let newViewProperties = InProgressViewProperties(title: Localization.creatingVariations, message: "")
659+
660+
// There should be already a presented `InProgressViewController`. But we present one in case there isn;t.
661+
guard let inProgressViewController = self.presentedViewController as? InProgressViewController else {
662+
let inProgressViewController = InProgressViewController(viewProperties: newViewProperties)
663+
return present(inProgressViewController, animated: true)
664+
}
665+
666+
// Update the already presented view controller with the "Creating..." copy.
667+
inProgressViewController.updateViewProperties(newViewProperties)
668+
}
669+
670+
/// Dismiss any `InProgressViewController` being presented.
671+
///
672+
private func dismissBlockingIndicator() {
673+
if let inProgressViewController = self.presentedViewController as? InProgressViewController {
674+
inProgressViewController.dismiss(animated: true)
675+
}
643676
}
644677
}
645678

@@ -720,19 +753,19 @@ extension ProductVariationsViewController: SyncingCoordinatorDelegate {
720753
viewModel.generateAllVariations(for: product) { [weak self] currentState in
721754
switch currentState {
722755
case .fetching:
723-
break // TODO: Show fetching loading Indicator
756+
self?.presentFetchingIndicator()
724757
case .confirmation(let variationCount, let onCompletion):
725758
self?.presentGenerationConfirmation(numberOfVariations: variationCount, onCompletion: onCompletion)
726759
case .creating:
727-
break // TODO: Show creating loading Indicator
760+
self?.presentCreatingIndicator()
728761
case .canceled:
729-
break // TODO: Remove loading indicator
762+
self?.dismissBlockingIndicator()
730763
case .finished(let variationsCreated):
731-
// TODO: Remove loading indicator
764+
self?.dismissBlockingIndicator()
732765
// TODO: Inform about created variations
733766
break
734767
case .error(let error):
735-
// TODO: Remove loading indicator
768+
self?.dismissBlockingIndicator()
736769
self?.presentGenerationError(error)
737770
}
738771
}
@@ -852,6 +885,10 @@ private extension ProductVariationsViewController {
852885
}
853886
static let ok = NSLocalizedString("OK", comment: "Button text to confirm that we want to generate all variations")
854887
static let cancel = NSLocalizedString("Cancel", comment: "Button text to confirm that we don't want to generate all variations")
888+
static let fetchingVariations = NSLocalizedString("Fetching Variations...",
889+
comment: "Blocking indicator text when fetching existing variations prior generating them.")
890+
static let creatingVariations = NSLocalizedString("Creating Variations...",
891+
comment: "Blocking indicator text when creating multiple variations remotely.")
855892

856893
}
857894

WooCommerce/Classes/ViewRelated/Progress/InProgressViewController.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class InProgressViewController: UIViewController {
1616
@IBOutlet private weak var activityIndicatorView: UIActivityIndicatorView!
1717
@IBOutlet private weak var messageLabel: UILabel!
1818

19-
private let viewProperties: InProgressViewProperties
19+
private var viewProperties: InProgressViewProperties
2020
private let hidesNavigationBar: Bool
2121

2222
/// - Parameters:
@@ -43,6 +43,7 @@ final class InProgressViewController: UIViewController {
4343
configureTitle()
4444
configureActivityIndicator()
4545
configureMessage()
46+
configureViewsValues()
4647
}
4748

4849
override func viewWillAppear(_ animated: Bool) {
@@ -62,6 +63,13 @@ final class InProgressViewController: UIViewController {
6263

6364
super.viewWillDisappear(animated)
6465
}
66+
67+
/// Assign new values for the view to render.
68+
///
69+
func updateViewProperties(_ viewProperties: InProgressViewProperties) {
70+
self.viewProperties = viewProperties
71+
configureViewsValues()
72+
}
6573
}
6674

6775
private extension InProgressViewController {
@@ -82,8 +90,6 @@ private extension InProgressViewController {
8290
titleLabel.textColor = .white
8391
titleLabel.textAlignment = .center
8492
titleLabel.numberOfLines = 0
85-
86-
titleLabel.text = viewProperties.title
8793
}
8894

8995
func configureActivityIndicator() {
@@ -96,7 +102,10 @@ private extension InProgressViewController {
96102
messageLabel.textColor = .gray(.shade10)
97103
messageLabel.textAlignment = .center
98104
messageLabel.numberOfLines = 0
105+
}
99106

107+
func configureViewsValues() {
108+
titleLabel.text = viewProperties.title
100109
messageLabel.text = viewProperties.message
101110
}
102111
}

0 commit comments

Comments
 (0)