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.

237 lines
7.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // ResendViewController.swift
  3. // GME Remit
  4. //
  5. // Created by gme_2 on 18/03/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. // func getParams(model: SendMoneyRequestModel, reciepient: Recipient) -> [String: String] { // last api call
  11. class ResendRequestModel {
  12. var exchangeRateDetail: SendMoneyExchangeRateModel?
  13. var autoDebitAccount: Account?
  14. var paymemtMode: SendMoneyPayoutMode?
  15. var bankAgent: SendMoneyBank?
  16. var branch: SendMoneyBankBranch?
  17. var accountNumber: String?
  18. var payingAmount: String?
  19. var transactionPassword: String?
  20. var transaction: ResendTransactionModel?
  21. var reciepient: Recipient?
  22. }
  23. class ResendViewController: UIViewController {
  24. struct Constant {
  25. static let heightOfDateRangeLabel: CGFloat = 35
  26. }
  27. struct StringConstants {
  28. let searchPlaceholderText = "resend_search_hint_text".localized()
  29. }
  30. // MARK: IBOutlets
  31. @IBOutlet weak var tableView: UITableView!
  32. @IBOutlet weak var dateRangeLabelHeightConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var emptyLabel: UILabel!
  34. @IBOutlet weak var fromDateLabel: UILabel!
  35. @IBOutlet weak var toDateLabel: UILabel!
  36. @IBOutlet weak var searchTextField: UITextField!
  37. // MARK: Properties
  38. var requestModel: ResendRequestModel?
  39. var from: String?
  40. var to: String?
  41. var model: ResendListModel? {
  42. didSet {
  43. self.transactions = model?.transactions ?? []
  44. self.accounts = model?.account ?? []
  45. }
  46. }
  47. var accounts: [Account] = []
  48. var transactions: [ResendTransactionModel] = [] {
  49. didSet {
  50. self.filteredTransaction = transactions
  51. }
  52. }
  53. var filteredTransaction: [ResendTransactionModel] = [] {
  54. didSet {
  55. self.emptyLabel.isHidden = !transactions.isEmpty
  56. self.tableView.reloadData()
  57. guard let fromDate = from, let toDate = to else {
  58. self.dateRangeLabelHeightConstraint.constant = 0
  59. return
  60. }
  61. self.fromDateLabel.text = self.getHUmanReadableDate(date: fromDate)
  62. self.toDateLabel.text = self.getHUmanReadableDate(date: toDate)
  63. UIView.animate(withDuration: 0.33, animations: {
  64. self.dateRangeLabelHeightConstraint.constant = Constant.heightOfDateRangeLabel
  65. })
  66. }
  67. }
  68. var selectedIndex: Int?
  69. var presenter: ResendModuleInterface?
  70. // MARK: VC's Life cycle
  71. override func viewDidLoad() {
  72. super.viewDidLoad()
  73. self.setupNormalNavigation()
  74. self.setup()
  75. self.presenter?.viewIsReady()
  76. }
  77. override func viewWillAppear(_ animated: Bool) {
  78. super.viewWillAppear(animated)
  79. self.navigationItem.title = "resend_money_text".localized()
  80. }
  81. override func viewWillDisappear(_ animated: Bool) {
  82. super.viewWillAppear(animated)
  83. self.navigationItem.title = ""
  84. }
  85. // MARK: IBActions
  86. @IBAction func openSearchFilter(_ sender: UIButton) {
  87. // open search filter
  88. let viewController = UIStoryboard.init(name: "TransactionHistoryDatePicker", bundle: nil).instantiateViewController(withIdentifier: "DatePickerViewController") as! DatePickerViewController
  89. viewController.completion = self.selectedDate
  90. self.present(viewController, animated: true, completion: nil)
  91. }
  92. // MARK: Other Functions
  93. private func setup() {
  94. // all setup should be done here
  95. self.requestModel = ResendRequestModel()
  96. setupSearchTextField()
  97. setupDelegates()
  98. self.dateRangeLabelHeightConstraint.constant = 0
  99. }
  100. private func selectedDate(from: String?, to: String?) {
  101. self.from = from
  102. self.to = to
  103. self.fetchList(fromDate: from, toDate: to)
  104. // TODO
  105. }
  106. private func fetchList(fromDate: String? = nil, toDate: String? = nil) {
  107. guard let fromDate = fromDate, let toDate = toDate else { return }
  108. self.presenter?.fetchList(fromDate: fromDate, toDate: toDate)
  109. }
  110. private func setupSearchTextField() {
  111. searchTextField.placeholder = StringConstants().searchPlaceholderText
  112. let imageview = UIImageView(image: #imageLiteral(resourceName: "ic_search"))
  113. self.searchTextField.leftView = imageview
  114. self.searchTextField.leftViewMode = .always
  115. self.searchTextField.addTarget(self, action: #selector(search(sender:)), for: UIControlEvents.editingChanged)
  116. }
  117. @objc func search(sender: UITextField) {
  118. let searchText = sender.text!.lowercased()
  119. self.presenter?.filter(searchText: searchText)
  120. }
  121. private func setupDelegates() {
  122. self.tableView.delegate = self
  123. self.tableView.dataSource = self
  124. }
  125. private func showMethodSelection(for index: Int) {
  126. self.selectedIndex = index
  127. let wireframe = PaymentSelectionWireframe()
  128. wireframe.openPaymentSelection(accounts: self.accounts ?? [], onSelection: self.selectedAcunt, source: self)
  129. }
  130. func selectedAcunt(acunt: Account) {
  131. guard let index = self.selectedIndex, let transaction = self.filteredTransaction.elementAt(index: index), let requestModel = self.requestModel, let transactionId = self.filteredTransaction.elementAt(index: index)?.transactionId else {return}
  132. requestModel.autoDebitAccount = acunt
  133. requestModel.transaction = transaction
  134. if let navigation = self.navigationController {
  135. let wireframe = ResendExchangeWireframe()
  136. wireframe.openExchangeWireFrame(model: requestModel, transactionId: transactionId , source: navigation)
  137. }
  138. }
  139. private func getHUmanReadableDate(date: String?) -> String? {
  140. if let date = DateFormatter.toDate(dateString: date ?? "", format: AppConstants.dateFormat) {
  141. let dateString = DateFormatter.toString(date: date, format: AppConstants.humanReadableDateFormat)
  142. return dateString
  143. }
  144. return nil
  145. }
  146. }
  147. // MARK: TransactionHistoryViewInterface
  148. extension ResendViewController: ResendViewInterface {
  149. func show(model: ResendListModel) {
  150. self.model = model
  151. }
  152. func showLoading() {
  153. self.showProgressHud()
  154. }
  155. func hideLoading() {
  156. self.hideProgressHud()
  157. }
  158. func show(error: String) {
  159. self.alert(message: error)
  160. }
  161. func show(models: [ResendTransactionModel]) {
  162. self.filteredTransaction = models
  163. }
  164. }
  165. // MARK: TableViewDelegate
  166. extension ResendViewController: UITableViewDelegate {
  167. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  168. self.selectedIndex = indexPath.row
  169. self.showMethodSelection(for: indexPath.row)
  170. }
  171. }
  172. // MARK: TableViewDatasource
  173. extension ResendViewController: UITableViewDataSource {
  174. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  175. return filteredTransaction.count
  176. }
  177. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  178. let cell = tableView.dequeueReusableCell(withIdentifier: "ResendTableViewCell") as! ResendTableViewCell
  179. cell.model = self.filteredTransaction.elementAt(index: indexPath.row)
  180. cell.setup()
  181. return cell
  182. }
  183. }