Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Guretzki committed Nov 8, 2024
1 parent 36177fd commit 53b0525
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 144 deletions.
6 changes: 5 additions & 1 deletion Adyen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@
8122B9BD2B9A0FF3002FC4D6 /* ErrorMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8122B9BC2B9A0FF3002FC4D6 /* ErrorMock.swift */; };
81272F182CC7E1DD00D8773A /* PayByBankUSComponent+ConfirmationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81272F172CC7E1CF00D8773A /* PayByBankUSComponent+ConfirmationViewController.swift */; };
81272F1A2CC8FD4600D8773A /* PayByBankUSComponent+Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81272F192CC8FD4600D8773A /* PayByBankUSComponent+Style.swift */; };
8128977C2CCA660A00275487 /* StoredPayByBankUSPaymentMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 812897752CC8FEC100275487 /* StoredPayByBankUSPaymentMethod.swift */; };
8128977B2CC93CB100275487 /* SupportedPaymentMethodsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8128977A2CC93CB000275487 /* SupportedPaymentMethodsView.swift */; };
8128977C2CCA660A00275487 /* StoredPayByBankUSPaymentMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 812897752CC8FEC100275487 /* StoredPayByBankUSPaymentMethod.swift */; };
8128977E2CCA6FE900275487 /* SupportedPaymentMethodLogosViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8128977D2CCA6FE400275487 /* SupportedPaymentMethodLogosViewTests.swift */; };
812B24872CDE56D90074A5D9 /* PayByBankUSComponent+ConfirmationViewController+Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 812B24862CDE56D90074A5D9 /* PayByBankUSComponent+ConfirmationViewController+Model.swift */; };
8136619E2BE0F6F7009AA0CD /* Adyen3DS2 in Frameworks */ = {isa = PBXBuildFile; productRef = 8136619D2BE0F6F7009AA0CD /* Adyen3DS2 */; };
813BF1122B2365400096940E /* XCTestCase+FirstResponder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813BF1102B2364E00096940E /* XCTestCase+FirstResponder.swift */; };
813BF1132B2365400096940E /* XCTestCase+FirstResponder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813BF1102B2364E00096940E /* XCTestCase+FirstResponder.swift */; };
Expand Down Expand Up @@ -1621,6 +1622,7 @@
812897752CC8FEC100275487 /* StoredPayByBankUSPaymentMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoredPayByBankUSPaymentMethod.swift; sourceTree = "<group>"; };
8128977A2CC93CB000275487 /* SupportedPaymentMethodsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportedPaymentMethodsView.swift; sourceTree = "<group>"; };
8128977D2CCA6FE400275487 /* SupportedPaymentMethodLogosViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportedPaymentMethodLogosViewTests.swift; sourceTree = "<group>"; };
812B24862CDE56D90074A5D9 /* PayByBankUSComponent+ConfirmationViewController+Model.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PayByBankUSComponent+ConfirmationViewController+Model.swift"; sourceTree = "<group>"; };
813BF1102B2364E00096940E /* XCTestCase+FirstResponder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "XCTestCase+FirstResponder.swift"; sourceTree = "<group>"; };
813EF9DD2A5DA0BC00C65D15 /* FormPickerItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormPickerItem.swift; sourceTree = "<group>"; };
813EF9E12A5DA2D400C65D15 /* FormPickerItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormPickerItemView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3159,6 +3161,7 @@
children = (
81D2186C2CC7D57100B7FC4C /* PayByBankUSComponent.swift */,
81272F172CC7E1CF00D8773A /* PayByBankUSComponent+ConfirmationViewController.swift */,
812B24862CDE56D90074A5D9 /* PayByBankUSComponent+ConfirmationViewController+Model.swift */,
81272F192CC8FD4600D8773A /* PayByBankUSComponent+Style.swift */,
81D2186E2CC7D7F900B7FC4C /* PayByBankUSComponent+Configuration.swift */,
);
Expand Down Expand Up @@ -7488,6 +7491,7 @@
F9175E9B259394F200D653BE /* IssuerListComponent.swift in Sources */,
A0414C21278C2AE400DF3FE9 /* ACHDirectDebitDetails.swift in Sources */,
00EACBAE2872EDEC0082B360 /* OnlineBankingDetails.swift in Sources */,
812B24872CDE56D90074A5D9 /* PayByBankUSComponent+ConfirmationViewController+Model.swift in Sources */,
C933801E276A06E1005B66CD /* BACSDirectDebitComponentTracker.swift in Sources */,
5A9889C8264E71210007F4C0 /* BoletoDetails.swift in Sources */,
C9F468E227465FCF00500FF5 /* BACSDirectDebitData.swift in Sources */,
Expand Down
2 changes: 0 additions & 2 deletions Adyen/Core/Models/DisplayInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public struct DisplayInformation: Equatable {
/// An optional custom `accessibilityLabel` to use.
@_spi(AdyenInternal)
public let accessibilityLabel: String?

// TODO: Allow adding a custom view - to show the bank icons

/// Initializes a `DisplayInformation`
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) 2024 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//

import Foundation
@_spi(AdyenInternal) import Adyen

extension PayByBankUSComponent.ConfirmationViewController {

internal class Model {

internal let headerImageUrl: URL
internal let supportedBankLogoURLs: [URL]
internal let supportedBanksMoreText: String
internal let title: String
internal let subtitle: String
internal let message: String
internal let submitTitle: String

internal let style: PayByBankUSComponent.Style
internal let headerImageViewSize = CGSize(width: 80, height: 52)

internal let continueHandler: () -> Void

private let imageLoader: ImageLoading = ImageLoaderProvider.imageLoader()
private var imageLoadingTask: AdyenCancellable? {
willSet { imageLoadingTask?.cancel() }
}

internal init(
title: String,
headerImageUrl: URL,
supportedBankLogoNames: [String],
style: PayByBankUSComponent.Style,
localizationParameters: LocalizationParameters?,
logoUrlProvider: LogoURLProvider,
continueHandler: @escaping () -> Void
) {
self.headerImageUrl = headerImageUrl
self.supportedBankLogoURLs = supportedBankLogoNames.map { logoUrlProvider.logoURL(withName: $0) }
self.supportedBanksMoreText = localizedString(.payByBankAISDDMore, localizationParameters)
self.title = title
self.subtitle = localizedString(.payByBankAISDDDisclaimerHeader, localizationParameters)
self.message = localizedString(.payByBankAISDDDisclaimerBody, localizationParameters)
self.submitTitle = localizedString(.payByBankAISDDSubmit, localizationParameters)
self.style = style
self.continueHandler = continueHandler
}

internal func loadHeaderImage(for imageView: UIImageView) {
imageLoadingTask = imageView.load(url: headerImageUrl, using: imageLoader)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,16 @@ extension PayByBankUSComponent {

private let model: Model

internal lazy var headerImageView: UIImageView = {
UIImageView()
}()
// MARK: Views

internal lazy var headerImageView = UIImageView()
internal let supportedBankLogosView: SupportedPaymentMethodLogosView
internal lazy var titleLabel = Self.defaultLabel
internal lazy var subtitleLabel = Self.defaultLabel
internal lazy var messageLabel = Self.defaultLabel
internal lazy var submitButton: SubmitButton

internal lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .center
return titleLabel
}()

internal lazy var subtitleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .center
return titleLabel
}()

internal lazy var messageLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .center
return titleLabel
}()

internal lazy var submitButton: SubmitButton = {
let buttonStyle = ButtonStyle(
title: TextStyle(font: .preferredFont(forTextStyle: .headline), color: .white),
cornerRounding: .fixed(8),
background: UIColor.Adyen.defaultBlue
)

return SubmitButton(style: buttonStyle)
}()
// MARK: UIViewController

public init(model: Model) {
self.model = model
Expand All @@ -64,74 +38,22 @@ extension PayByBankUSComponent {
imageUrls: model.supportedBankLogoURLs,
trailingText: model.supportedBanksMoreText
)

self.submitButton = SubmitButton(style: model.style.submitButton)

super.init(nibName: nil, bundle: nil)
}

override public func viewDidLoad() {
super.viewDidLoad()

headerImageView.adyen.apply(model.style.headerImage)

titleLabel.text = model.title
titleLabel.adyen.apply(model.style.title)

subtitleLabel.text = model.subtitle
subtitleLabel.adyen.apply(model.style.subtitle)

messageLabel.text = model.message
messageLabel.adyen.apply(model.style.message)

submitButton.title = model.submitTitle
submitButton.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)

let contentStack = UIStackView(arrangedSubviews: [
headerImageView,
titleLabel,
supportedBankLogosView,
subtitleLabel,
messageLabel
])
contentStack.spacing = 0
contentStack.axis = .vertical
contentStack.alignment = .center
contentStack.setCustomSpacing(Constants.subtitleLabelSpacing, after: subtitleLabel)
contentStack.setCustomSpacing(Constants.supportedBankLogosSpacing, after: supportedBankLogosView)

let contentButtonStack = UIStackView(arrangedSubviews: [
contentStack,
submitButton
])
contentButtonStack.spacing = 32
contentButtonStack.axis = .vertical
contentButtonStack.alignment = .fill

view.addSubview(contentButtonStack)
contentButtonStack.adyen.anchor(
inside: view.layoutMarginsGuide,
with: .init(
top: Constants.topPadding,
left: 0,
bottom: Constants.bottomPadding,
right: 0
)
)

model.loadHeaderImage(for: headerImageView)

configureConstraints()
setupViews()
}

@available(*, unavailable)
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc private func submitTapped() {
submitButton.showsActivityIndicator = true
model.continueHandler()
}

override public var preferredContentSize: CGSize {
get {
view.adyen.minimalSize
Expand All @@ -145,71 +67,88 @@ extension PayByBankUSComponent {
""") }
}

private func configureConstraints() {

let constraints = [
headerImageView.widthAnchor.constraint(equalToConstant: model.headerImageViewSize.width),
headerImageView.heightAnchor.constraint(equalToConstant: model.headerImageViewSize.height)
]

headerImageView.setContentHuggingPriority(.required, for: .horizontal)

NSLayoutConstraint.activate(constraints)
}

override public func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
headerImageView.layer.borderColor = model.style.headerImage.borderColor?.cgColor ?? UIColor.Adyen.componentSeparator.cgColor
}
}
}

// MARK: - Model

extension PayByBankUSComponent.ConfirmationViewController {

static var defaultLabel: UILabel {
let titleLabel = UILabel()
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .center
return titleLabel
}

internal class Model {

internal let headerImageUrl: URL
internal let supportedBankLogoURLs: [URL]
internal let supportedBanksMoreText: String
internal let title: String
internal let subtitle: String
internal let message: String
internal let submitTitle: String

internal let style: PayByBankUSComponent.Style
internal let headerImageViewSize = CGSize(width: 80, height: 52)
@objc private func submitTapped() {
submitButton.showsActivityIndicator = true
model.continueHandler()
}

private func setupViews() {
headerImageView.adyen.apply(model.style.headerImage)

titleLabel.text = model.title
titleLabel.adyen.apply(model.style.title)

subtitleLabel.text = model.subtitle
subtitleLabel.adyen.apply(model.style.subtitle)

messageLabel.text = model.message
messageLabel.adyen.apply(model.style.message)

submitButton.title = model.submitTitle
submitButton.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)

let contentStack = UIStackView(arrangedSubviews: [
headerImageView,
titleLabel,
supportedBankLogosView,
subtitleLabel,
messageLabel
])
contentStack.spacing = 0
contentStack.axis = .vertical
contentStack.alignment = .center
contentStack.setCustomSpacing(Constants.subtitleLabelSpacing, after: subtitleLabel)
contentStack.setCustomSpacing(Constants.supportedBankLogosSpacing, after: supportedBankLogosView)

let contentButtonStack = UIStackView(arrangedSubviews: [
contentStack,
submitButton
])
contentButtonStack.spacing = 32
contentButtonStack.axis = .vertical
contentButtonStack.alignment = .fill

view.addSubview(contentButtonStack)
contentButtonStack.adyen.anchor(
inside: view.layoutMarginsGuide,
with: .init(
top: Constants.topPadding,
left: 0,
bottom: Constants.bottomPadding,
right: 0
)
)

internal let continueHandler: () -> Void
model.loadHeaderImage(for: headerImageView)

private let imageLoader: ImageLoading = ImageLoaderProvider.imageLoader()
private var imageLoadingTask: AdyenCancellable? {
willSet { imageLoadingTask?.cancel() }
}
configureConstraints()
}

private func configureConstraints() {

internal init(
title: String,
headerImageUrl: URL,
supportedBankLogoNames: [String],
style: PayByBankUSComponent.Style,
localizationParameters: LocalizationParameters?,
logoUrlProvider: LogoURLProvider,
continueHandler: @escaping () -> Void
) {
self.headerImageUrl = headerImageUrl
self.supportedBankLogoURLs = supportedBankLogoNames.map { logoUrlProvider.logoURL(withName: $0) }
self.supportedBanksMoreText = localizedString(.payByBankAISDDMore, localizationParameters)
self.title = title
self.subtitle = localizedString(.payByBankAISDDDisclaimerHeader, localizationParameters)
self.message = localizedString(.payByBankAISDDDisclaimerBody, localizationParameters)
self.submitTitle = localizedString(.payByBankAISDDSubmit, localizationParameters)
self.style = style
self.continueHandler = continueHandler
}
let constraints = [
headerImageView.widthAnchor.constraint(equalToConstant: model.headerImageViewSize.width),
headerImageView.heightAnchor.constraint(equalToConstant: model.headerImageViewSize.height)
]

headerImageView.setContentHuggingPriority(.required, for: .horizontal)

internal func loadHeaderImage(for imageView: UIImageView) {
imageLoadingTask = imageView.load(url: headerImageUrl, using: imageLoader)
}
NSLayoutConstraint.activate(constraints)
}
}
6 changes: 6 additions & 0 deletions AdyenComponents/PayByBank/US/PayByBankUSComponent+Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ extension PayByBankUSComponent {
contentMode: .scaleAspectFit
)

public var submitButton: ButtonStyle = .init(
title: TextStyle(font: .preferredFont(forTextStyle: .headline), color: .white),
cornerRounding: .fixed(8),
background: UIColor.Adyen.defaultBlue
)

public init() {}
}
}

0 comments on commit 53b0525

Please sign in to comment.