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.

226 lines
6.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // InboundAccountsViewController.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 2019/11/12.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import RxCocoa
  11. import XLPagerTabStrip
  12. class InboundAccountsViewController: UIViewController {
  13. private enum Section: Int {
  14. case primary = 0
  15. case other
  16. }
  17. // MARK: Properties
  18. var presenter: InboundAccountsPresenter!
  19. private let disposeBag = DisposeBag()
  20. private var primaryAccount: InboundAccount?
  21. private var otherAccounts: [InboundAccount]?
  22. private let openPenneyTest = PublishSubject<PenneyTestRequest>()
  23. private let delete = PublishSubject<InboundAccount>()
  24. private let refreshControl = UIRefreshControl()
  25. // MARK: Computed Properties
  26. private var accounts: [InboundAccount] = [] {
  27. didSet {
  28. primaryAccount = accounts.first {$0.isPrimary ?? false}
  29. otherAccounts = accounts.filter {!($0.isPrimary ?? false)}
  30. tableView.reloadData()
  31. }
  32. }
  33. // MARK: IBOutlets
  34. @IBOutlet private weak var tableView: UITableView!
  35. @IBOutlet private weak var deleteImageView: UIImageView!
  36. @IBOutlet private weak var waitingVerifiedLabel: UILabel!
  37. @IBOutlet private weak var addButton: UIButton!
  38. @IBOutlet private weak var waitingImageView: UIImageView!
  39. // MARK: VC's Life cycle
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. setup()
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. super.viewWillAppear(animated)
  46. setupNormalNavigation()
  47. title = "inbound_title_text".localized()
  48. }
  49. override func viewWillDisappear(_ animated: Bool) {
  50. super.viewWillDisappear(animated)
  51. }
  52. // MARK: IBActions
  53. }
  54. // MARK: Other Functions
  55. extension InboundAccountsViewController {
  56. private func setup() {
  57. setUI()
  58. setBinding()
  59. setUIBinding()
  60. }
  61. private func setUI() {
  62. tableView.dataSource = self
  63. tableView.delegate = self
  64. waitingImageView.tintColor = .themeRed
  65. waitingImageView.image = #imageLiteral(resourceName: "ic_warning").withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
  66. let colorOption = [NSAttributedString.Key.foregroundColor : UIColor.themeWhite]
  67. let title = NSAttributedString(string: "pull to refresh", attributes: colorOption)
  68. tableView.refreshControl = refreshControl
  69. refreshControl.attributedTitle = title
  70. refreshControl.backgroundColor = .themeBlue
  71. refreshControl.tintColor = .themeWhite
  72. waitingVerifiedLabel.text = "waiting_verified_text".localized()
  73. }
  74. private func setUIBinding() {
  75. }
  76. private func setBinding() {
  77. let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear(_:)))
  78. .mapToVoid()
  79. .asDriverOnErrorJustComplete()
  80. let input = InboundAccountsPresenter.Input(
  81. viewWillAppear: viewWillAppear,
  82. add: addButton.rx.tap.asDriverOnErrorJustComplete(),
  83. pennyTest: openPenneyTest.asDriverOnErrorJustComplete(),
  84. refresh: refreshControl.rx.controlEvent(.valueChanged).asDriverOnErrorJustComplete(),
  85. delete: delete.asDriverOnErrorJustComplete()
  86. )
  87. let output = presenter.transform(input: input)
  88. output.isError
  89. .drive(
  90. onNext: {
  91. self.tableView.refreshControl?.endRefreshing()
  92. self.alert(type: .error, message: $0.localizedDescription)
  93. }
  94. ).disposed(by: disposeBag)
  95. output.isProgress
  96. .drive(
  97. onNext: { $0 ? self.showProgressHud() : self.hideProgressHud() }
  98. ).disposed(by: disposeBag)
  99. output.accounts.drive(onNext: {[weak self] in
  100. self?.tableView.refreshControl?.endRefreshing()
  101. self?.accounts = $0
  102. }).disposed(by: disposeBag)
  103. output.deleteMessage.drive(onNext: {[weak self] in
  104. self?.alert(message: $0) {
  105. self?.refreshControl.sendActions(for: .valueChanged)
  106. }
  107. }).disposed(by: disposeBag)
  108. }
  109. }
  110. // MARK: - XLPagerTabStrip's IndicatorInfoProvider
  111. extension InboundAccountsViewController: IndicatorInfoProvider {
  112. func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
  113. return IndicatorInfo.init(title: "Inbound")
  114. }
  115. }
  116. // MARK: UITableViewDataSource
  117. extension InboundAccountsViewController: UITableViewDataSource {
  118. func numberOfSections(in tableView: UITableView) -> Int {
  119. 2
  120. }
  121. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  122. guard let sectionType = Section(rawValue: section) else {return 0}
  123. switch sectionType {
  124. case .primary:
  125. return primaryAccount != nil ? 1 : 0
  126. case .other:
  127. return otherAccounts?.count ?? 0
  128. }
  129. }
  130. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  131. guard let cell = tableView
  132. .dequeueReusableCell(withIdentifier: "InboundAccountCell") as? InboundAccountCell else {
  133. return UITableViewCell()
  134. }
  135. let sectionType = Section(rawValue: indexPath.section) ?? .other
  136. switch sectionType {
  137. case .primary:
  138. cell.setModel(with: primaryAccount)
  139. case .other:
  140. cell.setModel(with: otherAccounts?[indexPath.row])
  141. }
  142. cell.delegate = self
  143. return cell
  144. }
  145. }
  146. // MARK: UITableViewDelegate
  147. extension InboundAccountsViewController: UITableViewDelegate {
  148. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  149. guard let sectionType = Section(rawValue: section) else {return ""}
  150. switch sectionType {
  151. case .primary: return "primary_account_text".localized()
  152. case .other: return "inbound_accounts_text".localized()
  153. }
  154. }
  155. }
  156. extension InboundAccountsViewController: InboundAccountCellDelegate {
  157. func penneyTest(of model: InboundAccount?) {
  158. let bankInformation = BankInformation(
  159. bankCode: model?.bankCode ?? "",
  160. bankName: model?.bankName ?? ""
  161. )
  162. let pennyTestRequest = PenneyTestRequest(
  163. bank: bankInformation,
  164. account: model?.accountNo ?? ""
  165. )
  166. openPenneyTest.onNext(pennyTestRequest)
  167. }
  168. func delete(of model: InboundAccount?) {
  169. self.alertWithOkCancel(
  170. type: .normal,
  171. message: "delete_account_message_text".localized(),
  172. title: "delete_account_text".localized(),
  173. okTitle: "delete_text".localized(),
  174. cancelTitle: "cancel_text".localized(),
  175. okAction: {[weak self] in
  176. guard let account = model else { return }
  177. self?.delete.onNext(account)
  178. },
  179. cancelAction: nil
  180. )
  181. }
  182. }