You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
4.3 KiB

//
// InternationalTopupViewController.swift
// GME Remit
//
// Created by InKwon James Kim on 2019/10/30.
//Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
class InternationalTopupViewController: UIViewController {
// MARK: Properties
var presenter: InternationalTopupPresenter!
private let disposeBag = DisposeBag()
private let cellPhoneNumber = PublishSubject<String>()
private let walletBalance = PublishSubject<String>()
// MARK: Computed Properties
// MARK: IBOutlets
@IBOutlet weak var contactsButton: UIButton!
@IBOutlet weak var showRateButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var cellphoneTextField: ValidationTextField!
@IBOutlet weak var walletBalanceLabel: UILabel!
@IBOutlet weak var selectCallingCardTextField: ValidationTextField!
@IBOutlet weak var selectRechargeAmountTextField: ValidationTextField!
@IBOutlet weak var step2ContainerView: UIStackView!
// MARK: VC's Life cycle
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupNormalNavigation()
cellPhoneNumber.onNext(GMEDB.shared.user.string(.mobileNumber) ?? "")
walletBalance.onNext(GMEDB.shared.user.string(.availableBalance)?.currencyToDecimal() ?? "0")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
// MARK: IBActions
}
// MARK: Other Functions
extension InternationalTopupViewController {
private func setup() {
setUI()
setBinding()
setUIBinding()
}
private func setUI() {
showRateButton.titleLabel?.minimumScaleFactor = 0.5
showRateButton.titleLabel?.adjustsFontSizeToFitWidth = true
showRateButton.layer.cornerRadius = 5
nextButton.layer.cornerRadius = 5
step2ContainerView.isHidden = true
}
private func setUIBinding() {
cellPhoneNumber.asDriver(onErrorJustReturn: "").drive(cellphoneTextField.rx.text).disposed(by: disposeBag)
walletBalance.asDriver(onErrorJustReturn: "0")
.map {$0.decimalToCurrency()}
.drive(walletBalanceLabel.rx.text).disposed(by: disposeBag)
}
private func setBinding() {
let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear(_:)))
.mapToVoid().asDriverOnErrorJustComplete()
let selectedCallingCard = selectCallingCardTextField.selectedItem
.map {$0 as? MainCardInformation}.asDriverOnErrorJustComplete()
let selectedAmount = selectRechargeAmountTextField.selectedItem
.map {$0 as? PriceModel}.asDriverOnErrorJustComplete()
let input = InternationalTopupPresenter.Input(
viewWillAppear: viewWillAppear,
walletBalance: walletBalance.asDriverOnErrorJustComplete(),
selectCellPhoneNumber: cellPhoneNumber.asDriverOnErrorJustComplete(),
selectedCallingCard: selectedCallingCard,
selectedAmount: selectedAmount
)
let output = presenter.transform(input: input)
output.isError
.drive(
onNext: { self.alert(message: $0.localizedDescription) }
).disposed(by: disposeBag)
output.isProgress
.drive(
onNext: { $0 ? self.showProgressHud() : self.hideProgressHud() }
).disposed(by: disposeBag)
output.isHiddenStep2ContainerView.drive(step2ContainerView.rx.isAnimateHidden).disposed(by: disposeBag)
output.model
.map {($0.cardInfomations, $0.buttonPrices)}
.drive(
onNext: {[weak self] (cards, amounts) in
guard let `self` = self else { return }
let cardConfigure = TablePresenterConfiguration(presenterTitle: "Select Internation Calling Card")
self.selectCallingCardTextField.useAsDropDown(with: cardConfigure, items: cards)
let amountConfigure = TablePresenterConfiguration(presenterTitle: "Select Recharge Amount")
self.selectRechargeAmountTextField.useAsDropDown(with: amountConfigure, items: amounts)
}
).disposed(by: disposeBag)
output.isEnableNext.map {[weak self] isEnable -> Bool in
self?.nextButton.backgroundColor = isEnable ? .themeRed : .lightGray
return isEnable
}.drive(nextButton.rx.isEnabled).disposed(by: disposeBag)
}
}