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.

1187 lines
43 KiB

6 years ago
  1. //
  2. // ExchangeRateMultipleCountryViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 4/4/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class ExchangeRateMultipleCountryViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {
  10. @IBOutlet weak var collectionViewOutlet: UICollectionView!
  11. @IBOutlet weak var countryListTextField: UITextField!
  12. @IBOutlet weak var bankListTextField: UITextField!
  13. @IBOutlet weak var selectPayoutModeTitleOutlet: UILabel!
  14. @IBOutlet weak var bankListTitleOutlet: UILabel!
  15. @IBOutlet weak var currencyListTextField: UITextField!
  16. @IBOutlet weak var fromUnitLabel: UILabel!
  17. @IBOutlet weak var toUnitLabel: UILabel!
  18. @IBOutlet weak var currencyTitleLabel: UILabel!
  19. @IBOutlet weak var fromKRWLabelOutlet: UILabel!
  20. @IBOutlet weak var rateTitleViewOutlet: UILabel!
  21. @IBOutlet weak var arrowIconViewOutlet: UIImageView!
  22. @IBOutlet weak var calculateButtonOutlet: UIButton!
  23. @IBOutlet weak var scrollViewOutlet: UIScrollView!
  24. @IBOutlet weak var bankConstraint: NSLayoutConstraint!
  25. private var agentViewModel = AgentViewModel()
  26. private var paymentMethodViewModel = PaymentMethodViewModel()
  27. private var exchanageRateViewModel = ExchangeRateViewModel()
  28. private var transactionViewModel = TransactionViewModel()
  29. private var agentCountryList = [AgentCountryList]()
  30. private var payoutModeTypeArray = [PayoutMode]()
  31. private var bankAgentArray = [AgentModel]()
  32. private var cashAgentArray = [AgentModel]()
  33. private var homeDeliveryArray = [AgentModel]()
  34. private var currenciesArray = [CurrenciesData]()
  35. private var exchangeRateData: ExchangeRateData?
  36. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  37. private var pickerView: UIPickerView = UIPickerView()
  38. private var items = [String]()
  39. private var images = [String]()
  40. private var pId: String = "nil"
  41. private var countryId: String = "nil"
  42. private var status: String = "nil"
  43. private var selectedIndexPath: Int?
  44. private var payoutIdGlobal: String?
  45. private var imageView: UIImageView?
  46. private var countryNameSelected: String?
  47. private var bankNameSelected: String?
  48. private var currencySelected: String?
  49. private var transferLimit: Double?
  50. private var dialogBoolean = 0
  51. public static var exchangeRateAgentConnectionCheck = 0
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54. setUpNavBar(id: 201, title: "Today's Rate")
  55. setUpAnotherLoginListener(genericviewmodel: agentViewModel)
  56. setUpAnotherLoginListener(genericviewmodel: paymentMethodViewModel)
  57. setUpAnotherLoginListener(genericviewmodel: exchanageRateViewModel)
  58. setUpAnotherLoginListener(genericviewmodel: transactionViewModel)
  59. scrollViewOutlet.isScrollEnabled = false
  60. countryListTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);
  61. bankListTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);
  62. currencyListTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);
  63. agentViewModel.agentConnectionTimeOut.value = nil
  64. /**
  65. connection timeout
  66. */
  67. agentViewModel.agentConnectionTimeOut.bind { [unowned self] in
  68. guard $0 != nil else {
  69. return
  70. }
  71. self.stopLoading()
  72. if ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck == 0{
  73. ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck = ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck+1
  74. self.popUpMessage(value: 26)
  75. }
  76. }
  77. paymentMethodViewModel.paymentMethodConnectionTimeOut.value = nil
  78. /**
  79. connection timeout
  80. */
  81. paymentMethodViewModel.paymentMethodConnectionTimeOut.bind { [unowned self] in
  82. guard $0 != nil else {
  83. return
  84. }
  85. self.stopLoading()
  86. if ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck == 0{
  87. ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck = ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck+1
  88. self.popUpMessage(value: 26)
  89. }
  90. }
  91. transactionViewModel.transactionViewConnectionTimeOut.value = nil
  92. /**
  93. connection timeout
  94. */
  95. transactionViewModel.transactionViewConnectionTimeOut.bind { [unowned self] in
  96. guard $0 != nil else {
  97. return
  98. }
  99. self.stopLoading()
  100. if ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck == 0{
  101. ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck = ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck+1
  102. self.popUpMessage(value: 26)
  103. }
  104. }
  105. exchanageRateViewModel.exchangeRateConnectionTimeOut.value = nil
  106. /**
  107. connection timeout
  108. */
  109. exchanageRateViewModel.exchangeRateConnectionTimeOut.bind { [unowned self] in
  110. guard $0 != nil else {
  111. return
  112. }
  113. self.stopLoading()
  114. if ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck == 0{
  115. ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck = ExchangeRateMultipleCountryViewController.exchangeRateAgentConnectionCheck+1
  116. self.popUpMessage(value: 26)
  117. }
  118. }
  119. /**
  120. Check Internet Connection
  121. */
  122. agentViewModel.internetConnection.value = nil
  123. agentViewModel.internetConnection.bind { [unowned self] in
  124. guard $0 != nil else {
  125. return
  126. }
  127. self.enableUserInteractions()
  128. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  129. self.popUpMessage(value: 15)
  130. }
  131. /**
  132. Request Country List
  133. */
  134. agentViewModel.agentCountryListAvailable.bind { [weak self] in
  135. guard $0 != nil else {
  136. return
  137. }
  138. self?.stopLoading()
  139. guard $0! else {
  140. self?.stopLoading()
  141. self?.popUpMessageError(value: 10, message: (self?.agentViewModel.getErrorMessage())!);
  142. return
  143. }
  144. self?.stopLoading()
  145. self?.setAgentCountryList()
  146. }
  147. self.startLoading()
  148. self.agentViewModel.fetchListOfCountry()
  149. self.setTextFieldAndPickerViewDelegate()
  150. /**
  151. Check Internet Connection
  152. */
  153. self.paymentMethodViewModel.internetConnection.value = nil
  154. self.paymentMethodViewModel.internetConnection.bind { [unowned self] in
  155. guard $0 != nil else {
  156. return
  157. }
  158. self.stopLoading()
  159. self.popUpMessage(value: 15)
  160. }
  161. /**
  162. Request Payment Method Type (Cash,Bank,Home)
  163. */
  164. self.paymentMethodViewModel.payoutMethodType.bind{[weak self] in
  165. guard $0 != nil else {
  166. return
  167. }
  168. self?.stopLoading()
  169. guard $0! else {
  170. self?.popUpMessageError(value: 10, message: (self?.paymentMethodViewModel.getErrorMessage())!)
  171. return
  172. }
  173. self?.setPayoutModeData()
  174. self?.stopLoading()
  175. }
  176. /**
  177. TransactionInfo
  178. */
  179. self.transactionViewModel.internetConnection.value = nil
  180. self.transactionViewModel.internetConnection.bind { [unowned self] in
  181. guard $0 != nil else {
  182. return
  183. }
  184. self.stopLoading()
  185. self.popUpMessage(value: 15)
  186. }
  187. self.transactionViewModel.transactionInfoAvailable.bind{[weak self] in
  188. guard $0 != nil else {
  189. return
  190. }
  191. self?.stopLoading()
  192. guard $0! else {
  193. self?.popUpMessageError(value: 10, message: (self?.transactionViewModel.getErrorMessage())!)
  194. return
  195. }
  196. self?.setMinimumTransferAmountPerTransaction()
  197. self?.transferLimit = self?.transactionViewModel.getTransferLimit()
  198. }
  199. /**
  200. Request List of Bank or Cash Agent
  201. */
  202. self.paymentMethodViewModel.payoutBankAvailable.bind{[weak self] in
  203. guard $0 != nil else {
  204. return
  205. }
  206. self?.stopLoading()
  207. guard $0! else {
  208. //self?.popUpMessageError(value: 10, message: (self?.paymentMethodViewModel.getErrorMessage())!)
  209. return
  210. }
  211. self?.setBankOrCashData()
  212. self?.stopLoading()
  213. }
  214. /**
  215. Check Internet Connection
  216. */
  217. exchanageRateViewModel.internetConnection.value = nil
  218. exchanageRateViewModel.internetConnection.bind { [unowned self] in
  219. guard $0 != nil else {
  220. return
  221. }
  222. self.enableUserInteractions()
  223. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  224. //self.popUpMessage(value: 15)
  225. }
  226. /**
  227. Request List of Currencies
  228. */
  229. self.exchanageRateViewModel.currencyInfoAvailable.bind{[weak self] in
  230. guard $0 != nil else {
  231. return
  232. }
  233. self?.stopLoading()
  234. guard $0! else {
  235. //self?.popUpMessageError(value: 10, message: (self?.exchanageRateViewModel.getErrorMessage())!)
  236. return
  237. }
  238. self?.setCurrenciesList()
  239. self?.stopLoading()
  240. }
  241. /**
  242. Request Exchange Rate Details
  243. */
  244. self.exchanageRateViewModel.exchangeRateAvailable.bind{[weak self] in
  245. guard $0 != nil else {
  246. return
  247. }
  248. self?.stopLoading()
  249. guard $0! else {
  250. self?.hideRateViews(visible: true)
  251. self?.popUpMessageError(value: 10, message: (self?.exchanageRateViewModel.getErrorMessage())!)
  252. return
  253. }
  254. self?.setExchangeRateList()
  255. self?.stopLoading()
  256. }
  257. }
  258. /**
  259. - returns: minimum transfer amount in KRW
  260. */
  261. func setMinimumTransferAmountPerTransaction() -> String{
  262. let minimumTransferAmountPerTransaction = transactionViewModel.getMinimumTransferAmountPerTransaction()
  263. if minimumTransferAmountPerTransaction != "nil"{
  264. let replace = minimumTransferAmountPerTransaction.replacingOccurrences(of: "KRW", with:"")
  265. replace.trimmingCharacters(in: .whitespaces)
  266. return replace
  267. }
  268. return "nil"
  269. }
  270. /**
  271. Redirect to ExchangeRateViewController
  272. */
  273. @IBAction func rateCalculateActionButton(_ sender: Any) {
  274. performSegue(withIdentifier: "exchangeRate", sender: nil)
  275. }
  276. /**
  277. Passing a dictionary object to ExchangeRateViewController
  278. */
  279. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  280. var exchangeRateData = [String:Any]()
  281. if segue.identifier == "exchangeRate" {
  282. let exchangeRateViewController
  283. = segue.destination as! ExchangeRateViewController
  284. let amount = "\(String(describing: self.exchangeRateData!.currentRate!))"
  285. if let countryIndex = returnCountryList().index(of: countryListTextField.text!){
  286. if let countryId = self.agentCountryList[countryIndex].countryId,let flagUrl = self.agentCountryList[countryIndex].flagUrl{
  287. exchangeRateData = ["toAmountAndUnit":amount,
  288. "currencyUnit":currencyListTextField.text!,
  289. "countryId": countryId,
  290. "flagUrl": flagUrl,
  291. "payoutmethodId":self.pId] as [String : Any]
  292. }
  293. }
  294. exchangeRateViewController.exchangeRateData = exchangeRateData
  295. exchangeRateViewController.transferLimit = transferLimit
  296. }
  297. }
  298. /**
  299. Setting delegate to views
  300. */
  301. func setTextFieldAndPickerViewDelegate(){
  302. currencyListTextField.delegate = self
  303. countryListTextField.delegate = self
  304. bankListTextField.delegate = self
  305. pickerView.dataSource = self
  306. pickerView.delegate = self
  307. }
  308. /**
  309. Get 1KRW unit value of other currencies
  310. */
  311. func setExchangeRateList(){
  312. self.exchangeRateData = exchanageRateViewModel.getExchangeRateData()
  313. if self.exchangeRateData != nil{
  314. self.hideRateViews(visible: false)
  315. self.updateExchangeRateUI()
  316. }else{
  317. self.hideRateViews(visible: true)
  318. }
  319. }
  320. /**
  321. Update 1KRW unit values with other currencies
  322. */
  323. func updateExchangeRateUI(){
  324. fromUnitLabel.text = "1 KRW"
  325. toUnitLabel.text =
  326. "\(String(describing: self.exchangeRateData!.currentRate!)) \(currencyListTextField.text!)"
  327. }
  328. /**
  329. Get list of currencies as per the selected bank or cash type
  330. */
  331. func setCurrenciesList(){
  332. self.currenciesArray = exchanageRateViewModel.getCurrencies()
  333. if self.currenciesArray.count > 0 {
  334. self.scrollViewOutlet.isScrollEnabled = true
  335. hideCurrencyViews(visible: false)
  336. }else{
  337. stopLoading()
  338. hideCurrencyViews(visible: true)
  339. }
  340. }
  341. /**
  342. Get bank or cash types as per selected payout mode.
  343. */
  344. func setBankOrCashData(){
  345. switch self.pId {
  346. case "1":
  347. if paymentMethodViewModel.getPayoutCashArray().count > 0 {
  348. self.cashAgentArray = paymentMethodViewModel.getPayoutCashArray()
  349. hideBankOrCashViews(visible: false)
  350. }else{
  351. hideRateViews(visible: true)
  352. hideCurrencyViews(visible: true)
  353. hideBankOrCashViews(visible: true)
  354. }
  355. break
  356. case "2":
  357. if paymentMethodViewModel.getPayoutBankDetailArray().count > 0{
  358. self.bankAgentArray = paymentMethodViewModel.getPayoutBankDetailArray()
  359. hideBankOrCashViews(visible: false)
  360. }else{
  361. hideRateViews(visible: true)
  362. hideCurrencyViews(visible: true)
  363. hideBankOrCashViews(visible: true)
  364. }
  365. break
  366. case "12":
  367. if paymentMethodViewModel.getPayoutBankDetailArray().count > 0{
  368. self.homeDeliveryArray = paymentMethodViewModel.getPayoutBankDetailArray()
  369. hideBankOrCashViews(visible: false)
  370. }else{
  371. hideRateViews(visible: true)
  372. hideCurrencyViews(visible: true)
  373. hideBankOrCashViews(visible: true)
  374. }
  375. break
  376. default:
  377. break
  378. }
  379. }
  380. /**
  381. Get payout mode as per selected country.
  382. */
  383. func setPayoutModeData(){
  384. self.payoutModeTypeArray = (self.paymentMethodViewModel.getPayoutMethodArray())
  385. self.updatePayoutUI(payoutModeArray: self.payoutModeTypeArray)
  386. }
  387. /**
  388. Get list of country
  389. */
  390. func setAgentCountryList(){
  391. for country in agentViewModel.getCountryList() {
  392. if country.countryId != "118"{
  393. self.agentCountryList.append(country)
  394. }
  395. }
  396. }
  397. /**
  398. - parameter payoutModeArray: Update payout mode UI as per payoutMethodId
  399. */
  400. func updatePayoutUI(payoutModeArray: [PayoutMode]){
  401. for payout in payoutModeArray{
  402. switch payout.payoutMethodId{
  403. case "1"?:
  404. self.items.append("Cash Payment")
  405. self.images.append("ic_cash")
  406. case "2"?:
  407. self.items.append("Bank Deposit")
  408. self.images.append("ic_bank")
  409. case "12"?:
  410. self.items.append("Home Delivery")
  411. self.images.append("ic_homeDelivery")
  412. default:
  413. break
  414. }
  415. }
  416. if self.items.count > 0{
  417. hidePayoutModeViews(visible: false)
  418. self.collectionViewOutlet.reloadData()
  419. }else{
  420. hidePayoutModeViews(visible: true)
  421. }
  422. }
  423. /**
  424. Remove previous data to store new data
  425. */
  426. func clearExistingPayoutArray(){
  427. self.payoutModeTypeArray.removeAll()
  428. self.items.removeAll()
  429. self.images.removeAll()
  430. }
  431. /**
  432. Visible bank and cash types only if data available
  433. */
  434. func hideBankOrCashViews(visible: Bool){
  435. if visible == true{
  436. self.bankConstraint.constant = 0
  437. self.bankListTextField.isHidden = visible
  438. self.bankListTitleOutlet.isHidden = visible
  439. }else{
  440. self.bankConstraint.constant = 43
  441. self.bankListTextField.isHidden = visible
  442. self.bankListTitleOutlet.isHidden = visible
  443. }
  444. }
  445. /**
  446. Visible payout modes only if payout mode is available
  447. */
  448. func hidePayoutModeViews(visible: Bool){
  449. self.collectionViewOutlet.isHidden = visible
  450. self.selectPayoutModeTitleOutlet.isHidden = visible
  451. }
  452. /**
  453. Visible currency list only if currency is available
  454. */
  455. func hideCurrencyViews(visible: Bool){
  456. self.currencySelected = nil
  457. self.currencyListTextField.text = ""
  458. self.currencyListTextField.isHidden = visible
  459. self.currencyTitleLabel.isHidden = visible
  460. }
  461. /**
  462. Visible unit conversion view only if data is available
  463. */
  464. func hideRateViews(visible: Bool){
  465. self.fromUnitLabel.isHidden = visible
  466. self.toUnitLabel.isHidden = visible
  467. self.rateTitleViewOutlet.isHidden = visible
  468. self.arrowIconViewOutlet.isHidden = visible
  469. self.calculateButtonOutlet.isHidden = visible
  470. }
  471. /**
  472. -returns: List of country with whom GME is tag with
  473. */
  474. func returnCountryList() -> [String]{
  475. var countryList = [String]()
  476. if agentCountryList.count>0{
  477. for country in agentCountryList{
  478. if let countryName = country.country{
  479. countryList.append(countryName)
  480. }
  481. }
  482. }
  483. return countryList
  484. }
  485. /**
  486. - returns: List of currency
  487. */
  488. func returnCurrencyList() -> [String]{
  489. var currencyList = [String]()
  490. if self.currenciesArray.count>0{
  491. for currency in currenciesArray{
  492. if let currency = currency.name{
  493. currencyList.append(currency)
  494. }
  495. }
  496. }
  497. return currencyList
  498. }
  499. /**
  500. -parameter payoutId: cash,bank or home delivery
  501. -returns: array list of agent as per bank, cash or home depending upon payoutId
  502. */
  503. func returnBankName(payoutId: String) -> [String]{
  504. var bankList = [String]()
  505. switch payoutId {
  506. case "1":
  507. self.payoutIdGlobal = "1"
  508. if cashAgentArray.count>0{
  509. for cash in cashAgentArray{
  510. if let cashName = cash.name{
  511. bankList.append(cashName)
  512. }
  513. }
  514. }
  515. case "2":
  516. self.payoutIdGlobal = "2"
  517. if bankAgentArray.count>0{
  518. for bank in bankAgentArray{
  519. if let bankName = bank.name{
  520. bankList.append(bankName)
  521. }
  522. }
  523. }
  524. case "12":
  525. self.payoutIdGlobal = "12"
  526. if homeDeliveryArray.count>0{
  527. for home in homeDeliveryArray{
  528. if let homeDelivery = home.name{
  529. bankList.append(homeDelivery)
  530. }
  531. }
  532. }
  533. default:
  534. break
  535. }
  536. return bankList
  537. }
  538. /**
  539. Disable user interaction while fetching data from api
  540. */
  541. func startLoading(){
  542. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  543. self.disableUserInteractions()
  544. }
  545. /**
  546. Enable user interaction while fetching data from api
  547. */
  548. func stopLoading(){
  549. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  550. self.enableUserInteractions()
  551. }
  552. /**
  553. -returns: update list as per payout out type i.e. cash,bank or home.
  554. */
  555. func clickLogic(textFieldType: String){
  556. switch textFieldType {
  557. case "countryList":
  558. if countryListTextField.text != ""{
  559. if let index = returnCountryList().index(where: { $0 == countryListTextField.text}) {
  560. self.countryListTextField.inputView = self.pickerView
  561. self.status = "countryList"
  562. self.countryListTextField.becomeFirstResponder()
  563. self.pickerView.selectRow(index, inComponent: 0, animated: true)
  564. }
  565. }else{
  566. self.countryListTextField.inputView = self.pickerView
  567. self.status = "countryList"
  568. self.countryListTextField.becomeFirstResponder()
  569. }
  570. case "bankList":
  571. if bankListTextField.text != ""{
  572. if let id = self.payoutIdGlobal{
  573. if let index = returnBankName(payoutId: id).index(where: { $0 == bankListTextField.text}) {
  574. self.bankListTextField.inputView = self.pickerView
  575. self.status = "bankList"
  576. self.bankListTextField.becomeFirstResponder()
  577. self.pickerView.selectRow(index, inComponent: 0, animated: true)
  578. }
  579. }
  580. }else{
  581. self.bankListTextField.inputView = self.pickerView
  582. self.status = "bankList"
  583. self.bankListTextField.becomeFirstResponder()
  584. }
  585. break
  586. case "currencyList":
  587. if currencyListTextField.text != ""{
  588. if let index = returnCurrencyList().index(where: { $0 == currencyListTextField.text}) {
  589. self.currencyListTextField.inputView = self.pickerView
  590. self.status = "currencyList"
  591. self.currencyListTextField.becomeFirstResponder()
  592. self.pickerView.selectRow(index, inComponent: 0, animated: true)
  593. }
  594. }else{
  595. self.currencyListTextField.inputView = self.pickerView
  596. self.status = "currencyList"
  597. self.currencyListTextField.becomeFirstResponder()
  598. }
  599. default:
  600. break
  601. }
  602. }
  603. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  604. return self.items.count
  605. }
  606. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  607. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
  608. cell.myLabel.text = self.items[indexPath.item]
  609. let imageName = self.images[indexPath.item]
  610. cell.myImage.image = UIImage(named: imageName)
  611. var borderColor: CGColor! = UIColor.clear.cgColor
  612. var borderWidth: CGFloat = 0
  613. if indexPath.row == selectedIndexPath{
  614. borderColor = UIColor.red.cgColor
  615. borderWidth = 1 //or whatever you please
  616. }else{
  617. borderColor = UIColor.clear.cgColor
  618. borderWidth = 0
  619. }
  620. cell.viewForBorder.layer.borderWidth = borderWidth
  621. cell.viewForBorder.layer.borderColor = borderColor
  622. return cell
  623. }
  624. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  625. hideRateViews(visible: true)
  626. hideCurrencyViews(visible: true)
  627. hideBankOrCashViews(visible: true)
  628. self.selectedIndexPath = indexPath.row
  629. self.collectionViewOutlet.reloadData()
  630. self.bankListTextField.text = ""
  631. if let index = returnCountryList().index(of: countryListTextField.text!){
  632. if indexPath.row != 2{
  633. if let countryId = self.agentCountryList[index].countryId, let payoutId = self.payoutModeTypeArray[indexPath.row].payoutMethodId{
  634. startLoading()
  635. self.paymentMethodViewModel.fetchPayoutBankDetail(countryId: countryId, payoutMethodId: payoutId)
  636. let param = ["countryId":countryId,"payoutMethodId":payoutId]
  637. self.transactionViewModel.fetchTransactionInfo(transactionInfo: param)
  638. self.pId = payoutId
  639. }
  640. }else{
  641. if let countryId = self.agentCountryList[index].countryId, let payoutId = self.payoutModeTypeArray[indexPath.row].payoutMethodId{
  642. startLoading()
  643. let param = ["countryId":countryId,"payoutMethodId":payoutId]
  644. self.transactionViewModel.fetchTransactionInfo(transactionInfo: param)
  645. self.pId = payoutId
  646. var value = [String:String]()
  647. value["countryId"] = countryId
  648. value["payoutMethodId"] = payoutId
  649. self.hideBankOrCashViews(visible: true)
  650. exchanageRateViewModel.fetchCurrencies(currencyInfo: value)
  651. }
  652. }
  653. if indexPath.row == 0{
  654. bankListTitleOutlet.text = "Agents"
  655. }else if indexPath.row == 1{
  656. self.bankListTitleOutlet.text = "Bank Name"
  657. }else{
  658. self.bankListTitleOutlet.text = "Agents"
  659. }
  660. }
  661. }
  662. //Doing Picker view button
  663. func pickUp(_ textField : UITextField){
  664. // UIPickerView
  665. self.pickerView = UIPickerView(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
  666. self.pickerView.delegate = self
  667. self.pickerView.dataSource = self
  668. //self.pickerView.backgroundColor = UIColor.white
  669. textField.inputView = self.pickerView
  670. // ToolBar
  671. let toolBar = UIToolbar()
  672. toolBar.barStyle = .default
  673. toolBar.isTranslucent = true
  674. toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
  675. toolBar.sizeToFit()
  676. // Adding Button ToolBar
  677. let customButton = UIButton()
  678. customButton.setTitle("Done", for: .normal)
  679. var cancel = UIBarButtonItem()
  680. if textField == self.countryListTextField{
  681. customButton.addTarget(self, action: #selector(ExchangeRateMultipleCountryViewController.doneClick), for: .touchUpInside)
  682. cancel = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(self.cancelClick))
  683. cancel.tintColor = UIColor.black
  684. }else if textField == self.bankListTextField{
  685. customButton.addTarget(self, action: #selector(ExchangeRateMultipleCountryViewController.doneClickBank), for: .touchUpInside)
  686. cancel = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(self.cancelClickBank))
  687. cancel.tintColor = UIColor.black
  688. }else if textField == self.currencyListTextField{
  689. customButton.addTarget(self, action: #selector(ExchangeRateMultipleCountryViewController.doneClickCurrency), for: .touchUpInside)
  690. cancel = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(self.cancelClickCurrency))
  691. cancel.tintColor = UIColor.black
  692. }
  693. customButton.setTitleColor(UIColor(hex : 0xED1C24), for: .normal)
  694. customButton.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
  695. let doneButton = UIBarButtonItem(customView: customButton)
  696. let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  697. toolBar.setItems([cancel,spaceButton, doneButton], animated: false)
  698. toolBar.isUserInteractionEnabled = true
  699. textField.inputAccessoryView = toolBar
  700. }
  701. /**
  702. Action to close country list pickerview
  703. */
  704. @objc func cancelClick() {
  705. countryListTextField.resignFirstResponder()
  706. }
  707. /**
  708. Action to close bank list pickerview
  709. */
  710. @objc func cancelClickBank(){
  711. bankListTextField.resignFirstResponder()
  712. }
  713. /**
  714. Action to close currency list pickerview
  715. */
  716. @objc func cancelClickCurrency(){
  717. currencyListTextField.resignFirstResponder()
  718. }
  719. /**
  720. User selects a country
  721. */
  722. @objc func doneClick() {
  723. if let cName = self.countryNameSelected {
  724. countryListTextField.text = cName
  725. }else{
  726. if returnCountryList().count > 0{
  727. countryListTextField.text = returnCountryList()[0]
  728. }
  729. }
  730. self.scrollViewOutlet.isScrollEnabled = false
  731. hidePayoutModeViews(visible: true)
  732. hideRateViews(visible: true)
  733. hideCurrencyViews(visible: true)
  734. hideBankOrCashViews(visible: true)
  735. self.selectedIndexPath = 100
  736. self.collectionViewOutlet.reloadData()
  737. self.countryListTextField.resignFirstResponder()
  738. if let cName = self.countryListTextField.text{
  739. if let countryIndex = returnCountryList().index(of: cName){
  740. if let countryId = self.agentCountryList[countryIndex].countryId{
  741. startLoading()
  742. clearExistingPayoutArray()
  743. self.paymentMethodViewModel.fetchCountryPayoutMode(countryId: countryId)
  744. }
  745. }
  746. }
  747. if let countryIndex = returnCountryList().index(of: countryListTextField.text!){
  748. if let flagUrl = self.agentCountryList[countryIndex].flagUrl{
  749. self.downloadImage(url: Foundation.URL(string:flagUrl)!)
  750. }
  751. }
  752. }
  753. /**
  754. User selects a bank,cash or home_deliver type
  755. */
  756. @objc func doneClickBank() {
  757. if let name = self.bankNameSelected{
  758. if name != ""{
  759. self.bankListTextField.text = name
  760. }else {
  761. setInitialValueWhenDoneIsClickWithScrollingPickerView()
  762. }
  763. }else{
  764. setInitialValueWhenDoneIsClickWithScrollingPickerView()
  765. }
  766. self.bankNameSelected = ""
  767. if self.bankListTextField.text != ""{
  768. self.requestApiForCurrency()
  769. }
  770. self.bankListTextField.resignFirstResponder()
  771. }
  772. /**
  773. When user click done without scrolling a pickerview
  774. */
  775. func setInitialValueWhenDoneIsClickWithScrollingPickerView(){
  776. if self.pId == "1"{
  777. if cashAgentArray.count > 0 {
  778. self.bankListTextField.text = cashAgentArray[0].name
  779. }
  780. }else if self.pId == "2"{
  781. if bankAgentArray.count > 0 {
  782. self.bankListTextField.text = bankAgentArray[0].name
  783. }
  784. }else if self.pId == "12"{
  785. if homeDeliveryArray.count > 0 {
  786. self.bankListTextField.text = homeDeliveryArray[0].name
  787. }
  788. }
  789. }
  790. /**
  791. User selects a currency
  792. */
  793. @objc func doneClickCurrency() {
  794. if let currency = self.currencySelected{
  795. self.currencyListTextField.text = currency
  796. }else{
  797. if returnCurrencyList().count>0{
  798. self.currencyListTextField.text = returnCurrencyList()[0]
  799. }
  800. }
  801. if self.currencyListTextField.text != ""{
  802. startLoading()
  803. self.requestApiForCurrentRate()
  804. }
  805. self.currencyListTextField.resignFirstResponder()
  806. }
  807. /**
  808. Request api for current rate as per 1 KRW unit
  809. */
  810. func requestApiForCurrentRate(){
  811. if self.setMinimumTransferAmountPerTransaction() != "nil"{
  812. if let countryIndex = returnCountryList().index(of: countryListTextField.text!){
  813. if let countryId = self.agentCountryList[countryIndex].countryId{
  814. let exchangedata = ["from":"KRW",
  815. "to":currencyListTextField.text ?? "nil",
  816. "calcBy":"C",
  817. "cAmt":self.setMinimumTransferAmountPerTransaction(),
  818. "payoutmethodId":self.pId ,
  819. "countryId":countryId,
  820. "sendMoney":false] as [String : Any]
  821. exchanageRateViewModel.fetchExchangeRate(exchangeData: exchangedata)
  822. }
  823. }
  824. }else{
  825. }
  826. }
  827. /**
  828. Request an api for list of currency
  829. */
  830. func requestApiForCurrency(){
  831. self.startLoading()
  832. var value = [String:String]()
  833. var payoutId: String = "nil"
  834. if let countryIndex = returnCountryList().index(of: countryListTextField.text!),
  835. let paymentIndex = returnBankName(payoutId: self.pId).index(of: bankListTextField.text!) {
  836. if self.pId == "1"{
  837. payoutId = self.cashAgentArray[paymentIndex].payoutId
  838. }else if self.pId == "2"{
  839. payoutId = self.bankAgentArray[paymentIndex].payoutId
  840. }else if self.pId == "12"{
  841. payoutId = self.homeDeliveryArray[paymentIndex].payoutId
  842. }
  843. if let countryId = self.agentCountryList[countryIndex].countryId{
  844. value["countryId"] = countryId
  845. if payoutId != "nil"{
  846. value["paymentLocationId"] = payoutId
  847. if self.pId != "nil"{
  848. value["payoutmethodId"] = self.pId
  849. exchanageRateViewModel.fetchCurrencies(currencyInfo: value)
  850. }
  851. }
  852. }
  853. }
  854. }
  855. func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
  856. URLSession.shared.dataTask(with: url) { data, response, error in
  857. completion(data, response, error)
  858. }.resume()
  859. }
  860. /**
  861. - parameter url: var url to download country flag.
  862. */
  863. func downloadImage(url: URL) {
  864. getDataFromUrl(url: url) { data, response, error in
  865. guard let data = data, error == nil else { return }
  866. DispatchQueue.main.async() {
  867. let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
  868. self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  869. let image = UIImage(data: data)
  870. self.imageView?.center = paddingView.center
  871. self.imageView?.image = image
  872. paddingView.addSubview(self.imageView!)
  873. self.countryListTextField.leftViewMode = UITextFieldViewMode.always
  874. self.countryListTextField.leftView = paddingView
  875. }
  876. }
  877. }
  878. }
  879. extension ExchangeRateMultipleCountryViewController: UIPickerViewDataSource, UIPickerViewDelegate {
  880. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  881. return 1
  882. }
  883. /**
  884. Function to display Flag image with country Name
  885. - parameter textField: selected textField
  886. - parameter url: Image url
  887. - parameter index: index of image and country
  888. */
  889. func showCountryWithFlag(textField: UITextField, url: String, index: Int) {
  890. let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
  891. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  892. imageView.center = paddingView.center
  893. if let flagUrl = URL(string: url) {
  894. imageView.sd_setImage(with: flagUrl, placeholderImage: #imageLiteral(resourceName: "gme-logo"), options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  895. }
  896. paddingView.addSubview(imageView)
  897. textField.leftViewMode = UITextFieldViewMode.always
  898. textField.leftView = paddingView
  899. textField.text = agentCountryList[index].country
  900. }
  901. /**
  902. Set total number of row
  903. */
  904. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  905. if self.status == "countryList"{
  906. return self.agentCountryList.count
  907. }
  908. else if self.status == "bankList"{
  909. if self.pId == "1"{
  910. return self.cashAgentArray.count
  911. }else if self.pId == "2"{
  912. return self.bankAgentArray.count
  913. }else if self.pId == "12"{
  914. return homeDeliveryArray.count
  915. }
  916. }
  917. else if self.status == "currencyList"{
  918. return currenciesArray.count
  919. }
  920. return 0
  921. }
  922. /**
  923. Set type of data to be shown in pickerview
  924. */
  925. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  926. if self.status == "countryList"{
  927. return self.agentCountryList[row].country
  928. }else if self.status == "bankList"{
  929. if self.pId == "1"{
  930. if cashAgentArray.indices.contains(row){
  931. if let name = cashAgentArray[row].name{
  932. return name
  933. }
  934. }
  935. }else if self.pId == "2"{
  936. if bankAgentArray.indices.contains(row){
  937. if let name = self.bankAgentArray[row].name{
  938. return name
  939. }
  940. }
  941. }else if self.pId == "12"{
  942. if homeDeliveryArray.indices.contains(row){
  943. if let name = homeDeliveryArray[row].name{
  944. return name
  945. }
  946. }
  947. }
  948. }
  949. else if self.status == "currencyList"{
  950. return self.currenciesArray[row].name
  951. }
  952. return ""
  953. }
  954. /**
  955. Select an element from pickerview
  956. */
  957. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  958. if self.status == "countryList"{
  959. if let countryName = self.agentCountryList[row].country{
  960. self.countryNameSelected = countryName
  961. }
  962. }else if self.status == "bankList"{
  963. if self.pId == "1"{
  964. if cashAgentArray.indices.contains(row){
  965. if let name = self.cashAgentArray[row].name{
  966. self.bankNameSelected = name
  967. }
  968. }
  969. }else if self.pId == "2" {
  970. if bankAgentArray.indices.contains(row){
  971. if let name = self.bankAgentArray[row].name{
  972. self.bankNameSelected = name
  973. }
  974. }
  975. }
  976. else if self.pId == "12"{
  977. if homeDeliveryArray.indices.contains(row){
  978. if let name = homeDeliveryArray[row].name{
  979. self.bankNameSelected = name
  980. }
  981. }
  982. }
  983. }else if self.status == "currencyList"{
  984. self.currencySelected = self.currenciesArray[row].name
  985. }
  986. if row <= self.agentCountryList.count{
  987. if agentCountryList.indices.contains(row){
  988. if let id = self.agentCountryList[row].countryId {
  989. self.countryId = id
  990. }
  991. }
  992. }
  993. }
  994. /**
  995. To display image on picker view row
  996. */
  997. func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  998. var myImageView: UIImageView?
  999. let myView = UIView(frame: CGRect(x: 0, y: 0, width: pickerView.bounds.width - 30, height: 60))
  1000. if self.status == "countryList"{
  1001. let myLabel = UILabel(frame: CGRect(x: 60, y: 0, width: pickerView.bounds.width - 90, height: 60 ))
  1002. myView.addSubview(myLabel)
  1003. myLabel.text = agentCountryList[row].country
  1004. myImageView = UIImageView(frame: CGRect(x: 0, y: 15, width: 30, height: 30))
  1005. myImageView?.sd_setImage(with:URL(string: self.agentCountryList[row].flagUrl ?? ""), placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  1006. myView.addSubview(myImageView!)
  1007. } else if self.status == "bankList" {
  1008. let myLabel = UILabel(frame: CGRect(x: 10, y: 0, width: pickerView.bounds.width - 90, height: 60 ))
  1009. myView.addSubview(myLabel)
  1010. if self.pId == "1"{
  1011. if cashAgentArray.indices.contains(row){
  1012. if let name = self.cashAgentArray[row].name{
  1013. myLabel.text = name
  1014. }
  1015. }
  1016. }else if self.pId == "2" {
  1017. if bankAgentArray.indices.contains(row){
  1018. if let name = self.bankAgentArray[row].name{
  1019. myLabel.text = name
  1020. }
  1021. }
  1022. }
  1023. else if self.pId == "12"{
  1024. if homeDeliveryArray.indices.contains(row){
  1025. if let name = homeDeliveryArray[row].name{
  1026. myLabel.text = name
  1027. }
  1028. }
  1029. }
  1030. } else if self.status == "currencyList"{
  1031. let myLabel = UILabel(frame: CGRect(x: 10, y: 0, width: pickerView.bounds.width - 90, height: 60 ))
  1032. myView.addSubview(myLabel)
  1033. myLabel.text = currenciesArray[row].name
  1034. }
  1035. return myView
  1036. }
  1037. func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  1038. return 60
  1039. }
  1040. }
  1041. extension ExchangeRateMultipleCountryViewController: UITextFieldDelegate {
  1042. func textFieldDidBeginEditing(_ textField: UITextField) {
  1043. if textField == countryListTextField{
  1044. if agentCountryList.count > 0 {
  1045. self.pickUp(textField)
  1046. clickLogic(textFieldType: "countryList")
  1047. } else {
  1048. textField.resignFirstResponder()
  1049. popUpMessageInfo(value: 16, title: "Alert", message: "No country List")
  1050. }
  1051. }else if textField == bankListTextField{
  1052. self.pickUp(textField)
  1053. clickLogic(textFieldType: "bankList")
  1054. }else if textField == currencyListTextField{
  1055. self.pickUp(textField)
  1056. clickLogic(textFieldType: "currencyList")
  1057. }
  1058. }
  1059. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  1060. return true
  1061. }
  1062. func textFieldDidEndEditing(_ textField: UITextField) {
  1063. }
  1064. }