From 7c9831b8d241faf1ef1f46bed3e7b6cc3d1baf01 Mon Sep 17 00:00:00 2001 From: Yuki Date: Tue, 22 Oct 2024 12:24:15 -0700 Subject: [PATCH] Disable embedded and playground while updating and loading, respectively --- .../EmbeddedPlaygroundViewController.swift | 16 ++++++++++++++-- .../PlaygroundController.swift | 4 +++- .../Embedded/EmbeddedPaymentElement.swift | 5 ++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Example/PaymentSheet Example/PaymentSheet Example/EmbeddedPlaygroundViewController.swift b/Example/PaymentSheet Example/PaymentSheet Example/EmbeddedPlaygroundViewController.swift index 743862171cc..e7d8fb05a26 100644 --- a/Example/PaymentSheet Example/PaymentSheet Example/EmbeddedPlaygroundViewController.swift +++ b/Example/PaymentSheet Example/PaymentSheet Example/EmbeddedPlaygroundViewController.swift @@ -10,6 +10,18 @@ import Foundation import UIKit class EmbeddedPlaygroundViewController: UIViewController { + var isLoading: Bool = false { + didSet { + if isLoading { + view.bringSubviewToFront(loadingIndicator) + loadingIndicator.startAnimating() + view.isUserInteractionEnabled = false + } else { + loadingIndicator.stopAnimating() + view.isUserInteractionEnabled = true + } + } + } private let appearance: PaymentSheet.Appearance private let configuration: EmbeddedPaymentElement.Configuration @@ -119,7 +131,7 @@ class EmbeddedPlaygroundViewController: UIViewController { scrollView.contentLayoutGuide.leadingAnchor.constraint(equalTo: stackView.leadingAnchor), scrollView.contentLayoutGuide.trailingAnchor.constraint(equalTo: stackView.trailingAnchor), scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: view.widthAnchor), - checkoutButton.heightAnchor.constraint(equalToConstant: 45) + checkoutButton.heightAnchor.constraint(equalToConstant: 45), ]) paymentOptionView.configure(with: embeddedPaymentElement.paymentOption, showMandate: !configuration.embeddedViewDisplaysMandateText) } @@ -226,7 +238,7 @@ private class EmbeddedPaymentOptionView: UIView { verticalStackView.topAnchor.constraint(equalTo: self.topAnchor, constant: 15), verticalStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -15), imageView.widthAnchor.constraint(equalToConstant: 25), - imageView.heightAnchor.constraint(equalToConstant: 25) + imageView.heightAnchor.constraint(equalToConstant: 25), ]) } diff --git a/Example/PaymentSheet Example/PaymentSheet Example/PlaygroundController.swift b/Example/PaymentSheet Example/PaymentSheet Example/PlaygroundController.swift index b212d25b3df..88b3993e9bb 100644 --- a/Example/PaymentSheet Example/PaymentSheet Example/PlaygroundController.swift +++ b/Example/PaymentSheet Example/PaymentSheet Example/PlaygroundController.swift @@ -594,6 +594,7 @@ extension PlaygroundController { addressViewController = nil paymentSheet = nil lastPaymentResult = nil + embeddedPlaygroundViewController?.isLoading = true isLoading = true let settingsToLoad = self.settings @@ -729,7 +730,8 @@ extension PlaygroundController { } case .embedded: guard !shouldUpdateEmbeddedInsteadOfRecreating else { - // Update embedded rather than re-creating it + // Update embedded rather than re-creating it + self.embeddedPlaygroundViewController?.isLoading = false self.updateEmbedded() self.currentlyRenderedSettings = self.settings return diff --git a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift index 26c303f9dae..d8ef4376b11 100644 --- a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift +++ b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift @@ -103,6 +103,7 @@ public final class EmbeddedPaymentElement { public func update( intentConfiguration: IntentConfiguration ) async -> UpdateResult { + embeddedPaymentMethodsView.isUserInteractionEnabled = false // Cancel the old task and let it finish so that merchants receive update results in order currentUpdateTask?.cancel() _ = await currentUpdateTask?.value @@ -158,7 +159,9 @@ public final class EmbeddedPaymentElement { return .succeeded } self.currentUpdateTask = currentUpdateTask - return await currentUpdateTask.value + let updateResult = await currentUpdateTask.value + embeddedPaymentMethodsView.isUserInteractionEnabled = true + return updateResult } /// Completes the payment or setup.