-
Notifications
You must be signed in to change notification settings - Fork 1k
ConfirmButton refactor 4/6 - Move update logic #5930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,19 +68,10 @@ class ConfirmButton: UIView { | |
| } | ||
| } | ||
|
|
||
| private(set) var status: Status = .enabled | ||
| private(set) var callToAction: CallToActionType | ||
|
|
||
| // MARK: Private Properties | ||
| private lazy var buyButton: BuyButton = { | ||
| let buyButton = BuyButton(showProcessingLabel: showProcessingLabel, appearance: appearance) | ||
| buyButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside) | ||
| return buyButton | ||
| }() | ||
| private let buyButton: BuyButton | ||
| private let didTap: () -> Void | ||
| private let didTapWhenDisabled: () -> Void | ||
| private let appearance: PaymentSheet.Appearance | ||
| private let showProcessingLabel: Bool | ||
|
|
||
| // MARK: Init | ||
|
|
||
|
|
@@ -92,13 +83,11 @@ class ConfirmButton: UIView { | |
| didTap: @escaping () -> Void, | ||
| didTapWhenDisabled: @escaping () -> Void = {} | ||
| ) { | ||
| self.status = status | ||
| self.callToAction = callToAction | ||
| self.showProcessingLabel = showProcessingLabel | ||
| self.appearance = appearance | ||
| self.buyButton = BuyButton(status: status, callToAction: callToAction, showProcessingLabel: showProcessingLabel, appearance: appearance) | ||
| self.didTap = didTap | ||
| self.didTapWhenDisabled = didTapWhenDisabled | ||
| super.init(frame: .zero) | ||
| buyButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside) | ||
| addAndPinSubview(buyButton) | ||
|
|
||
| update() | ||
|
|
@@ -117,12 +106,12 @@ class ConfirmButton: UIView { | |
| #if !os(visionOS) | ||
| override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { | ||
| super.traitCollectionDidChange(previousTraitCollection) | ||
| self.buyButton.update(status: status, callToAction: callToAction, animated: false) | ||
| self.buyButton.update(status: buyButton.status, callToAction: buyButton.callToAction, animated: false) | ||
| } | ||
| #endif | ||
|
|
||
| @objc private func didBecomeActive() { | ||
| self.buyButton.update(status: self.status, callToAction: self.callToAction, animated: false) | ||
| self.buyButton.update(status: self.buyButton.status, callToAction: self.buyButton.callToAction, animated: false) | ||
| } | ||
|
|
||
| deinit { | ||
|
|
@@ -138,8 +127,8 @@ class ConfirmButton: UIView { | |
| completion: (() -> Void)? = nil | ||
| ) { | ||
| update( | ||
| status: status ?? self.status, | ||
| callToAction: callToAction ?? self.callToAction, | ||
| status: status ?? self.buyButton.status, | ||
| callToAction: callToAction ?? self.buyButton.callToAction, | ||
| animated: animated, | ||
| completion: completion) | ||
| } | ||
|
|
@@ -150,37 +139,21 @@ class ConfirmButton: UIView { | |
| animated: Bool = false, | ||
| completion: (() -> Void)? = nil | ||
| ) { | ||
| self.status = status | ||
| self.callToAction = callToAction | ||
|
|
||
| // Enable/disable | ||
| isUserInteractionEnabled = (status == .enabled || status == .disabled) | ||
|
|
||
| // Update the buy button; it has its own presentation logic | ||
| self.buyButton.update(status: status, callToAction: callToAction, animated: animated) | ||
|
|
||
| if let completion = completion { | ||
| let delay: TimeInterval = { | ||
| guard animated else { | ||
| return 0 | ||
| } | ||
|
|
||
| return status == .succeeded | ||
| ? PaymentSheetUI.delayBetweenSuccessAndDismissal | ||
| : PaymentSheetUI.defaultAnimationDuration | ||
| }() | ||
|
|
||
| DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: completion) | ||
| } | ||
|
Comment on lines
-153
to
-174
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function and the old BuyButton.update functions are unified into a single BuyButton.update function. For now the ConfirmButton.update functions are just wrappers that pass the data to the BuyButton to complete all logic |
||
| self.buyButton.update( | ||
| status: status, | ||
| callToAction: callToAction, | ||
| animated: animated, | ||
| completion: completion | ||
| ) | ||
| } | ||
|
|
||
| // MARK: - Private Methods | ||
|
|
||
| @objc | ||
| private func handleTap() { | ||
| if case .enabled = status { | ||
| if case .enabled = buyButton.status { | ||
| didTap() | ||
| } else if case .disabled = status { | ||
| } else if case .disabled = buyButton.status { | ||
| // When the disabled button is tapped, trigger validation error display | ||
| didTapWhenDisabled() | ||
| // Resign first responder (as we would if the button was disabled) | ||
|
|
@@ -202,7 +175,8 @@ class ConfirmButton: UIView { | |
| return appearance.primaryButton.successBackgroundColor | ||
| } | ||
|
|
||
| private var status: Status = .enabled | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before, the ConfirmButton and BuyButton each had their own status property that needed to be kept in sync. Now there's only one, which is the BuyButton's |
||
| private(set) var status: Status | ||
| private(set) var callToAction: CallToActionType | ||
| private let appearance: PaymentSheet.Appearance | ||
| private let showProcessingLabel: Bool | ||
|
|
||
|
|
@@ -289,9 +263,13 @@ class ConfirmButton: UIView { | |
| var overriddenForegroundColor: UIColor? | ||
|
|
||
| init( | ||
| status: Status = .enabled, | ||
| callToAction: CallToActionType, | ||
| showProcessingLabel: Bool = true, | ||
| appearance: PaymentSheet.Appearance = .default | ||
| ) { | ||
| self.status = status | ||
| self.callToAction = callToAction | ||
| self.showProcessingLabel = showProcessingLabel | ||
| self.appearance = appearance | ||
| super.init(frame: .zero) | ||
|
|
@@ -368,8 +346,12 @@ class ConfirmButton: UIView { | |
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| func update(status: Status, callToAction: CallToActionType, animated: Bool) { | ||
| func update(status: Status, callToAction: CallToActionType, animated: Bool, completion: (() -> Void)? = nil) { | ||
| self.status = status | ||
| self.callToAction = callToAction | ||
|
|
||
| // Enable/disable | ||
| isUserInteractionEnabled = (status == .enabled || status == .disabled) | ||
|
Comment on lines
+351
to
+354
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is from the old ConfirmButton.update function |
||
|
|
||
| // Update the label with a crossfade UIView.transition; UIView.animate doesn't provide an animation for text changes | ||
| let text: String? = { | ||
|
|
@@ -504,6 +486,20 @@ class ConfirmButton: UIView { | |
| self.animateSuccess() | ||
| } | ||
| } | ||
|
|
||
| if let completion = completion { | ||
| let delay: TimeInterval = { | ||
| guard animated else { | ||
| return 0 | ||
| } | ||
|
|
||
| return status == .succeeded | ||
| ? PaymentSheetUI.delayBetweenSuccessAndDismissal | ||
| : PaymentSheetUI.defaultAnimationDuration | ||
| }() | ||
|
|
||
| DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: completion) | ||
| } | ||
|
Comment on lines
+489
to
+502
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is from the old ConfirmButton.update function |
||
| } | ||
|
|
||
| private func applyCornerRadius() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These properties are moved to the BuyButton