Skip to content
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

Convert BaseViewControllers to Swift #1116

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Demo/Application/Base/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import BraintreeCore

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import UIKit
import BraintreeCore

// TODO: remove @objcMembers when final VC is Swift
@objcMembers class BaseViewController: UIViewController {

var progressBlock: ((String?) -> Void) = { _ in }
var completionBlock: ((BTPaymentMethodNonce?) -> Void) = { _ in }
var transactionBlock: (() -> Void) = { }

// TODO: remove @objc when final VC is Swift
@objc(initWithAuthorization:)
init(authorization: String) {
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
let tapToDismissKeyboard = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tapToDismissKeyboard)
}

@objc func dismissKeyboard() {
view.endEditing(true)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import UIKit
import BraintreeCore

class PaymentButtonBaseViewController: BaseViewController {

let apiClient: BTAPIClient

private var paymentButton: UIView = UIView()

override init(authorization: String) {
apiClient = BTAPIClient(authorization: authorization)!
super.init(authorization: authorization)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

title = "Payment Button"
view.backgroundColor = .systemBackground

paymentButton = createPaymentButton()
view.addSubview(paymentButton)

NSLayoutConstraint.activate([
paymentButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
paymentButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
paymentButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
paymentButton.heightAnchor.constraint(equalToConstant: 100)
])
}

/// A factory method that subclasses must implement to return a payment button view.
func createPaymentButton() -> UIView {
UIView()
}

func createButton(title: String, action: Selector) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.setTitleColor(.blue, for: .normal)
button.setTitleColor(.lightGray, for: .highlighted)
button.setTitleColor(.lightGray, for: .disabled)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: action, for: .touchUpInside)
return button
}
}
11 changes: 5 additions & 6 deletions Demo/Application/Base/BraintreeDemoContainmentViewController.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import "BraintreeDemoContainmentViewController.h"
#import "BraintreeDemoBaseViewController.h"
#import "Demo-Swift.h"
@import InAppSettingsKit;
@import BraintreeCore;
Expand All @@ -9,7 +8,7 @@ @interface BraintreeDemoContainmentViewController () <IASKSettingsDelegate>
@property (nonatomic, strong) UIBarButtonItem *statusItem;
@property (nonatomic, strong) BTPaymentMethodNonce *latestTokenizedPayment;
@property (nonatomic, strong) NSString *latestTokenizedPaymentString;
@property (nonatomic, strong) BraintreeDemoBaseViewController *currentDemoViewController;
@property (nonatomic, strong) BaseViewController *currentDemoViewController;

@end

Expand Down Expand Up @@ -190,7 +189,7 @@ - (void)reloadIntegration {
}
}

- (void)setCurrentDemoViewController:(BraintreeDemoBaseViewController *)currentDemoViewController {
- (void)setCurrentDemoViewController:(BaseViewController *)currentDemoViewController {
_currentDemoViewController = currentDemoViewController;

if (!_currentDemoViewController) {
Expand All @@ -208,18 +207,18 @@ - (void)setCurrentDemoViewController:(BraintreeDemoBaseViewController *)currentD
self.title = _currentDemoViewController.title;
}

- (BraintreeDemoBaseViewController *)instantiateCurrentIntegrationViewControllerWithAuthorization:(NSString *)authorization {
- (BaseViewController *)instantiateCurrentIntegrationViewControllerWithAuthorization:(NSString *)authorization {
NSString *integrationName = [[NSUserDefaults standardUserDefaults] stringForKey:@"BraintreeDemoSettingsIntegration"];
NSLog(@"Loading integration: %@", integrationName);

// The prefix "Demo." is required for integration view controllers written in Swift
Class integrationClass = NSClassFromString(integrationName) ?: NSClassFromString([NSString stringWithFormat:@"Demo.%@", integrationName]);
if (![integrationClass isSubclassOfClass:[BraintreeDemoBaseViewController class]]) {
if (![integrationClass isSubclassOfClass:[BaseViewController class]]) {
NSLog(@"%@ is not a valid BraintreeDemoBaseViewController", integrationName);
return nil;
}

return [(BraintreeDemoBaseViewController *)[integrationClass alloc] initWithAuthorization:authorization];
return [(BaseViewController *)[integrationClass alloc] initWithAuthorization:authorization];
}

- (void)containIntegrationViewController:(UIViewController *)viewController {
Expand Down
1 change: 1 addition & 0 deletions Demo/Application/Base/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import BraintreeCore

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

Expand Down
18 changes: 3 additions & 15 deletions Demo/Application/Features/AmexViewController.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation
import UIKit
import BraintreeAmericanExpress
import BraintreeCard

class AmexViewController: BraintreeDemoPaymentButtonBaseViewController {
class AmexViewController: PaymentButtonBaseViewController {

lazy var amexClient = BTAmericanExpressClient(apiClient: apiClient)
lazy var cardClient = BTCardClient(apiClient: apiClient)
Expand All @@ -12,7 +12,7 @@ class AmexViewController: BraintreeDemoPaymentButtonBaseViewController {
title = "Amex"
}

override func createPaymentButton() -> UIView! {
override func createPaymentButton() -> UIView {
let validCardButton = createButton(title: "Valid card", action: #selector(tappedValidCard))
let insufficientPointsCardButton = createButton(title: "Insufficient points card", action: #selector(tappedInsufficientPointsCard))
let ineligibleCardButton = createButton(title: "Ineligible card", action: #selector(tappedIneligibleCard))
Expand Down Expand Up @@ -72,16 +72,4 @@ class AmexViewController: BraintreeDemoPaymentButtonBaseViewController {
}
}
}

// TODO: move this helper into BraintreeDemoPaymentButtonBaseViewController once converted so all buttons share the same characteristics
private func createButton(title: String, action: Selector) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.setTitleColor(.blue, for: .normal)
button.setTitleColor(.lightGray, for: .highlighted)
button.setTitleColor(.lightGray, for: .disabled)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: action, for: .touchUpInside)
return button
}
}
6 changes: 3 additions & 3 deletions Demo/Application/Features/ApplePayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import BraintreeApplePay
import PassKit

class ApplePayViewController: BraintreeDemoPaymentButtonBaseViewController {
class ApplePayViewController: PaymentButtonBaseViewController {

lazy var applePayClient = BTApplePayClient(apiClient: apiClient)

Expand All @@ -12,13 +12,13 @@ class ApplePayViewController: BraintreeDemoPaymentButtonBaseViewController {
title = "Apple Pay"
}

override func createPaymentButton() -> UIView! {
override func createPaymentButton() -> UIView {
if !PKPaymentAuthorizationViewController.canMakePayments() {
progressBlock("canMakePayments returned false, hiding Apple Pay button")
return nil
}

let applePayButton = PKPaymentButton(paymentButtonType: .plain, paymentButtonStyle: .automatic)
applePayButton.translatesAutoresizingMaskIntoConstraints = false
applePayButton.addTarget(self, action: #selector(tappedApplePayButton), for: .touchUpInside)

NSLayoutConstraint.activate([applePayButton.heightAnchor.constraint(equalToConstant: 50)])
Expand Down
20 changes: 5 additions & 15 deletions Demo/Application/Features/CardTokenizationViewController.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import UIKit
import BraintreeCard

class CardTokenizationViewController: BraintreeDemoPaymentButtonBaseViewController {
class CardTokenizationViewController: PaymentButtonBaseViewController {

private let cardFormView = BTCardFormView()
private let autofillButton = UIButton(type: .system)
private var autofillButton = UIButton(type: .system)

override func viewDidLoad() {
super.viewDidLoad()
createSubviews()
layoutConstraints()
}

override func createPaymentButton() -> UIView! {
let submitButton = UIButton(type: .system)
submitButton.setTitle("Submit", for: .normal)
submitButton.setTitleColor(.blue, for: .normal)
submitButton.setTitleColor(.lightGray, for: .highlighted)
submitButton.setTitleColor(.lightGray, for: .disabled)
submitButton.addTarget(self, action: #selector(tappedSubmit), for: .touchUpInside)
submitButton.translatesAutoresizingMaskIntoConstraints = false

override func createPaymentButton() -> UIView {
let submitButton = createButton(title: "Submit", action: #selector(tappedSubmit))
return submitButton
}

Expand Down Expand Up @@ -62,10 +55,7 @@ class CardTokenizationViewController: BraintreeDemoPaymentButtonBaseViewControll
cardFormView.hidePostalCodeField = true
setFieldsEnabled(true)

autofillButton.setTitle("Autofill", for: .normal)
autofillButton.setTitleColor(.blue, for: .normal)
autofillButton.addTarget(self, action: #selector(tappedAutofill), for: .touchUpInside)
autofillButton.translatesAutoresizingMaskIntoConstraints = false
autofillButton = createButton(title: "Autofill", action: #selector(tappedAutofill))

view.addSubview(cardFormView)
view.addSubview(autofillButton)
Expand Down
19 changes: 4 additions & 15 deletions Demo/Application/Features/DataCollectorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit
import BraintreeDataCollector
import BraintreeCore

class DataCollectorViewController: BraintreeDemoPaymentButtonBaseViewController {
class DataCollectorViewController: PaymentButtonBaseViewController {

var dataLabel = UILabel()

Expand All @@ -11,15 +11,8 @@ class DataCollectorViewController: BraintreeDemoPaymentButtonBaseViewController
title = "Braintree Data Collector"
}

override func createPaymentButton() -> UIView! {
let dataCollectorButton = UIButton(type: .system)
dataCollectorButton.setTitle("Collect Device Data", for: .normal)
dataCollectorButton.setTitleColor(.blue, for: .normal)
dataCollectorButton.setTitleColor(.lightGray, for: .highlighted)
dataCollectorButton.setTitleColor(.lightGray, for: .disabled)
dataCollectorButton.addTarget(self, action: #selector(tappedCollect), for: .touchUpInside)
dataCollectorButton.translatesAutoresizingMaskIntoConstraints = false

override func createPaymentButton() -> UIView {
let dataCollectorButton = createButton(title: "Collect Device Data", action: #selector(tappedCollect))
let label = UILabel()
label.numberOfLines = 0
label.adjustsFontSizeToFitWidth = true
Expand All @@ -31,13 +24,9 @@ class DataCollectorViewController: BraintreeDemoPaymentButtonBaseViewController
stackView.axis = .vertical
stackView.spacing = 5
stackView.alignment = .center
stackView.distribution = .fillEqually
stackView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
dataCollectorButton.heightAnchor.constraint(equalToConstant: 19.5),
label.heightAnchor.constraint(equalToConstant: 19.5)
])

return stackView
}

Expand Down
Loading