Skip to content

Commit

Permalink
Update currentAmount in Settings of ExampleApp. Fix text color for EC…
Browse files Browse the repository at this point in the history
…ontext screen
  • Loading branch information
Andrei Solovev committed Mar 22, 2024
1 parent 98243bc commit 4fff96b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
10 changes: 5 additions & 5 deletions ExampleApp/Models/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ struct PaymentPreset {
var allowedPaymentMethods: [SourceType]

static let allPreset = PaymentPreset(
paymentAmount: 5_000_00,
paymentAmount: 3_500_00,
paymentCurrency: .thb,
allowedPaymentMethods: SourceType.allCases
)

static let thailandPreset = PaymentPreset(
paymentAmount: 5_000_00,
paymentAmount: 3_500_00,
paymentCurrency: .thb,
allowedPaymentMethods: SourceType.availableByDefaultInThailand
)

static let japanPreset = PaymentPreset(
paymentAmount: 5_000,
paymentAmount: 3_500,
paymentCurrency: .jpy,
allowedPaymentMethods: SourceType.availableByDefaultInJapan
)

static let singaporePreset = PaymentPreset(
paymentAmount: 5_000_00,
paymentAmount: 3_500_00,
paymentCurrency: .sgd,
allowedPaymentMethods: SourceType.availableByDefaultSingapore
)

static let malaysiaPreset = PaymentPreset(
paymentAmount: 5_000_00,
paymentAmount: 3_500_00,
paymentCurrency: .myr,
allowedPaymentMethods: SourceType.availableByDefaultMalaysia
)
Expand Down
2 changes: 1 addition & 1 deletion ExampleApp/Resources/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Pt6-Zr-Dfr">
<rect key="frame" x="20" y="36.5" width="374" height="46.5"/>
<string key="text">Here's an Omise iOS SDK example screen. Please see the below options for how to present the forms built into the SDK.
<string key="text">Here's Omise iOS SDK example screen. Please see the below options for how to present the forms built into the SDK.
You can see the code in the ExampleApp.</string>
<fontDescription key="fontDescription" style="UICTFontTextStyleCaption1"/>
<nil key="textColor"/>
Expand Down
54 changes: 45 additions & 9 deletions ExampleApp/Views/PaymentSettingTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PaymentSettingTableViewController: UITableViewController {
return numberFormatter
}()

var currencyInitialized = false
var currentAmount: Int64 = 0
var currentCurrency: Currency = .thb {
willSet {
Expand All @@ -22,15 +23,20 @@ class PaymentSettingTableViewController: UITableViewController {
cell(for: currentCurrency)?.accessoryType = .none
}
didSet {
let numberOfDigits = Int(log10(Double(currentCurrency.factor)))
amountFormatter.minimumFractionDigits = numberOfDigits
amountFormatter.maximumFractionDigits = numberOfDigits

guard isViewLoaded else {
return
if currencyInitialized == false {
currencyInitialized = true
} else {
switch (oldValue.factor, currentCurrency.factor) {
case (identicalBasedCurrencyFactor, centBasedCurrencyFactor):
currentAmount *= 100
case (centBasedCurrencyFactor, identicalBasedCurrencyFactor):
currentAmount /= 100
default:
break
}
}
cell(for: currentCurrency)?.accessoryType = .checkmark
amountField.text = amountFormatter.string(from: NSNumber(value: currentCurrency.convert(fromSubunit: currentAmount)))
updateAmountField()
}
}

Expand Down Expand Up @@ -143,6 +149,8 @@ class PaymentSettingTableViewController: UITableViewController {

useCapabilityAPIValuesCell.accessoryType = usesCapabilityDataForPaymentMethods ? .checkmark : .none
useSpecifiedValuesCell.accessoryType = usesCapabilityDataForPaymentMethods ? .none : .checkmark

updateAmountField()
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Expand Down Expand Up @@ -221,13 +229,23 @@ extension PaymentSettingTableViewController: UITextFieldDelegate {
}
}

func textFieldDidEndEditing(_ textField: UITextField) {
guard let amount = textField.text.flatMap(Double.init) else {
func updateCurrentAmount(_ textField: UITextField) {
guard isViewLoaded, let amount = textField.text.flatMap(Double.init) else {
return
}

currentAmount = currentCurrency.convert(toSubunit: amount)
textField.text = amountFormatter.string(from: NSNumber(value: amount))
}

func textFieldDidEndEditing(_ textField: UITextField) {
updateCurrentAmount(textField)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
updateCurrentAmount(textField)
return true
}
}

extension PaymentSettingTableViewController {
Expand Down Expand Up @@ -452,3 +470,21 @@ extension PaymentSettingTableViewController {
}
}
}

extension PaymentSettingTableViewController {
func updateAmountField() {
guard isViewLoaded else { return }

let numberOfDigits = Int(log10(Double(currentCurrency.factor)))
amountFormatter.minimumFractionDigits = numberOfDigits
amountFormatter.maximumFractionDigits = numberOfDigits

guard isViewLoaded else {
return
}
cell(for: currentCurrency)?.accessoryType = .checkmark

amountField.text = amountFormatter.string(from: NSNumber(value: currentCurrency.convert(fromSubunit: currentAmount)))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class EContextPaymentFormController: UIViewController, PaymentFormUIController {
phoneNumberLabel.text = localized("EContext.field.phone")
submitButton.setTitle(localized("EContext.nextButton.title"), for: .normal)

fullNameLabel.textColor = UIColor.omisePrimary
emailLabel.textColor = UIColor.omisePrimary
phoneNumberLabel.textColor = UIColor.omisePrimary
}

private func setupTextFieldHandlers() {
Expand All @@ -108,6 +111,7 @@ class EContextPaymentFormController: UIViewController, PaymentFormUIController {
}

private func setupTextField(_ field: OmiseTextField) {
field.textColor = UIColor.omisePrimary
field.addTarget(self, action: #selector(gotoNextField), for: .editingDidEndOnExit)
field.addTarget(self, action: #selector(validateFieldData), for: .editingChanged)
field.addTarget(self, action: #selector(updateInputAccessoryViewFor), for: .editingDidBegin)
Expand Down

0 comments on commit 4fff96b

Please sign in to comment.