Skip to content

Commit

Permalink
Refactoring PaymentChooserViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Solovev committed Dec 5, 2023
1 parent 8051fbf commit b1283f8
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions OmiseSDK/PaymentChooserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -577,20 +577,9 @@ class PaymentChooserViewController: AdaptableStaticTableViewController<PaymentCh
}

private func updateShowingValues() {
var paymentMethodsToShow: [PaymentChooserOption] = allowedPaymentMethods.reduce(into: []) { (result, sourceType) in
let paymentOptions = PaymentChooserOption.paymentOptions(for: sourceType)
for paymentOption in paymentOptions where !result.contains(paymentOption) {
result.append(paymentOption)
}
}

if showsCreditCardPayment {
paymentMethodsToShow.insert(.creditCard, at: 0)
}

if paymentMethodsToShow.contains(.truemoney) && paymentMethodsToShow.contains(.truemoneyJumpApp) {
paymentMethodsToShow.removeAll { $0 == .truemoney }
}
var paymentMethodsToShow = paymentOptions(from: allowedPaymentMethods)
paymentMethodsToShow = appendCreditCardPayment(paymentOptions: paymentMethodsToShow)
paymentMethodsToShow = filterTrueMoney(paymentOptions: paymentMethodsToShow)
showingValues = paymentMethodsToShow

os_log("Payment Chooser: Showing options - %{private}@",
Expand All @@ -603,3 +592,31 @@ class PaymentChooserViewController: AdaptableStaticTableViewController<PaymentCh
flowSession?.requestToCancel()
}
}

private extension PaymentChooserViewController {
func paymentOptions(from sourceTypes: [OMSSourceTypeValue]) -> [PaymentChooserOption] {
let paymentOptions: [PaymentChooserOption] = sourceTypes.reduce(into: []) { (result, sourceType) in
let paymentOptions = PaymentChooserOption.paymentOptions(for: sourceType)
for paymentOption in paymentOptions where !result.contains(paymentOption) {
result.append(paymentOption)
}
}
return paymentOptions
}

func appendCreditCardPayment(paymentOptions: [PaymentChooserOption]) -> [PaymentChooserOption] {
var filter = paymentOptions
if showsCreditCardPayment {
filter.insert(.creditCard, at: 0)
}
return filter
}

func filterTrueMoney(paymentOptions: [PaymentChooserOption]) -> [PaymentChooserOption] {
var filter = paymentOptions
if filter.contains(.truemoney) && filter.contains(.truemoneyJumpApp) {
filter.removeAll { $0 == .truemoney }
}
return filter
}
}

0 comments on commit b1283f8

Please sign in to comment.