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

  1. //
  2. // InternationalTopupViewController.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 2019/10/30.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import RxCocoa
  11. class InternationalTopupViewController: UIViewController {
  12. // MARK: Properties
  13. var presenter: InternationalTopupPresenter!
  14. private let disposeBag = DisposeBag()
  15. private let cellPhoneNumber = PublishSubject<String>()
  16. private let walletBalance = PublishSubject<String>()
  17. // MARK: Computed Properties
  18. // MARK: IBOutlets
  19. @IBOutlet weak var contactsButton: UIButton!
  20. @IBOutlet weak var showRateButton: UIButton!
  21. @IBOutlet weak var nextButton: UIButton!
  22. @IBOutlet weak var cellphoneTextField: ValidationTextField!
  23. @IBOutlet weak var walletBalanceLabel: UILabel!
  24. @IBOutlet weak var selectCallingCardTextField: ValidationTextField!
  25. @IBOutlet weak var selectRechargeAmountTextField: ValidationTextField!
  26. @IBOutlet weak var step2ContainerView: UIStackView!
  27. // MARK: VC's Life cycle
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. setup()
  31. }
  32. override func viewWillAppear(_ animated: Bool) {
  33. super.viewWillAppear(animated)
  34. setupNormalNavigation()
  35. cellPhoneNumber.onNext(GMEDB.shared.user.string(.mobileNumber) ?? "")
  36. walletBalance.onNext(GMEDB.shared.user.string(.availableBalance)?.currencyToDecimal() ?? "0")
  37. }
  38. override func viewWillDisappear(_ animated: Bool) {
  39. super.viewWillDisappear(animated)
  40. }
  41. // MARK: IBActions
  42. }
  43. // MARK: Other Functions
  44. extension InternationalTopupViewController {
  45. private func setup() {
  46. setUI()
  47. setBinding()
  48. setUIBinding()
  49. }
  50. private func setUI() {
  51. showRateButton.titleLabel?.minimumScaleFactor = 0.5
  52. showRateButton.titleLabel?.adjustsFontSizeToFitWidth = true
  53. showRateButton.layer.cornerRadius = 5
  54. nextButton.layer.cornerRadius = 5
  55. step2ContainerView.isHidden = true
  56. }
  57. private func setUIBinding() {
  58. cellPhoneNumber.asDriver(onErrorJustReturn: "").drive(cellphoneTextField.rx.text).disposed(by: disposeBag)
  59. walletBalance.asDriver(onErrorJustReturn: "0")
  60. .map {$0.decimalToCurrency()}
  61. .drive(walletBalanceLabel.rx.text).disposed(by: disposeBag)
  62. }
  63. private func setBinding() {
  64. let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear(_:)))
  65. .mapToVoid().asDriverOnErrorJustComplete()
  66. let selectedCallingCard = selectCallingCardTextField.selectedItem
  67. .map {$0 as? MainCardInformation}.asDriverOnErrorJustComplete()
  68. let selectedAmount = selectRechargeAmountTextField.selectedItem
  69. .map {$0 as? PriceModel}.asDriverOnErrorJustComplete()
  70. let input = InternationalTopupPresenter.Input(
  71. viewWillAppear: viewWillAppear,
  72. walletBalance: walletBalance.asDriverOnErrorJustComplete(),
  73. selectCellPhoneNumber: cellPhoneNumber.asDriverOnErrorJustComplete(),
  74. selectedCallingCard: selectedCallingCard,
  75. selectedAmount: selectedAmount
  76. )
  77. let output = presenter.transform(input: input)
  78. output.isError
  79. .drive(
  80. onNext: { self.alert(message: $0.localizedDescription) }
  81. ).disposed(by: disposeBag)
  82. output.isProgress
  83. .drive(
  84. onNext: { $0 ? self.showProgressHud() : self.hideProgressHud() }
  85. ).disposed(by: disposeBag)
  86. output.isHiddenStep2ContainerView.drive(step2ContainerView.rx.isAnimateHidden).disposed(by: disposeBag)
  87. output.model
  88. .map {($0.cardInfomations, $0.buttonPrices)}
  89. .drive(
  90. onNext: {[weak self] (cards, amounts) in
  91. guard let `self` = self else { return }
  92. let cardConfigure = TablePresenterConfiguration(presenterTitle: "Select Internation Calling Card")
  93. self.selectCallingCardTextField.useAsDropDown(with: cardConfigure, items: cards)
  94. let amountConfigure = TablePresenterConfiguration(presenterTitle: "Select Recharge Amount")
  95. self.selectRechargeAmountTextField.useAsDropDown(with: amountConfigure, items: amounts)
  96. }
  97. ).disposed(by: disposeBag)
  98. output.isEnableNext.map {[weak self] isEnable -> Bool in
  99. self?.nextButton.backgroundColor = isEnable ? .themeRed : .lightGray
  100. return isEnable
  101. }.drive(nextButton.rx.isEnabled).disposed(by: disposeBag)
  102. }
  103. }