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.

511 lines
21 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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // ExchangeRateViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 20/08/2018.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Hex
  10. //enum PaymentMode: String {
  11. // case cashDelivery = "1"
  12. // case bankDeposite = "2"
  13. // case homeDelivery = "12"
  14. // case mobileWallet = "13"
  15. //}
  16. class ExchangeRateViewControllerV2: UIViewController {
  17. struct Constants {
  18. static let transferFeeDetailText = "Transfer Fee Included"
  19. static let currentExchangeRateText = "Current Exchange Rate"
  20. static let paymentModeHeightConstant: CGFloat = 193
  21. }
  22. struct ApiConstants {
  23. static let recieverCountryId = "pCountry"
  24. static let senderCurrency = "sCurrency"
  25. static let recieverCurrency = "pCurrency"
  26. static let senderAmount = "cAmount"
  27. static let recieverAmount = "pAmount"
  28. static let paymentMethod = "serviceType"
  29. static let calcBy = "calcBy"
  30. static let recieverCountryName = "pCountryName"
  31. static let senderCountryId = "sCountry"
  32. }
  33. @IBOutlet weak var collectionView: UICollectionView!
  34. @IBOutlet weak var exchangeBackground1: UIView!
  35. @IBOutlet weak var exchangeBackground2: UIView!
  36. @IBOutlet weak var dropDownImageView: UIImageView!
  37. @IBOutlet weak var backgroundViewCountryLabel1: UIView!
  38. @IBOutlet weak var backgroundViewCountryLabel2: UIView!
  39. @IBOutlet weak var countryListStackView: UIStackView!
  40. @IBOutlet weak var countryCodeLabel: UILabel!
  41. @IBOutlet weak var countryFlagImage: UIImageView!
  42. @IBOutlet weak var paymentModeStackViewConstraint: NSLayoutConstraint!
  43. @IBOutlet weak var paymentModeStackView: UIStackView!
  44. @IBOutlet weak var senderTextField: UITextField!
  45. @IBOutlet weak var reciepientTextField: UITextField!
  46. @IBOutlet weak var transferFeeInfoLabel: UILabel!
  47. @IBOutlet weak var exchangeRateInfoLabel: UILabel!
  48. var countryListTapGuesture: UITapGestureRecognizer?
  49. var selectedPaymentIndex: IndexPath = IndexPath.init(row: 0, section: 0)
  50. var nativeCountryCode: String?
  51. var calcBy = ""
  52. var exchangeRateModels: [ExchangeRateModel]? {
  53. didSet {
  54. if let _ = CountryInfo().defaultCountryCodes.filter({$0.lowercased() == (self.nativeCountryCode ?? "").lowercased()}).first {
  55. if let defaultExchangeRate = self.exchangeRateModels?.filter({
  56. ($0.countryCode ?? "").lowercased() == (self.nativeCountryCode ?? "").lowercased()
  57. }).first {
  58. // there is native country, defaultExchangeRate is the information for that country
  59. self.setCountryFlag(countryCode: defaultExchangeRate.countryCode ?? "")
  60. self.setCurrencyLabel(currency: defaultExchangeRate.currency ?? "")
  61. // set the default amount for this country. there are some default values in CountryInfo
  62. self.selectedExchageRateModel = defaultExchangeRate
  63. self.collectionView.reloadData()
  64. showPaymentModeView()
  65. self.populateDefaultAmounts()
  66. }
  67. } else {
  68. self.setDefaultValuesForCountriesNotHavingDefaultValue()
  69. showPaymentModeView()
  70. }
  71. }
  72. }
  73. var selectedExchageRateModel: ExchangeRateModel? {
  74. didSet {
  75. self.setCurrencyLabel(currency: self.selectedExchageRateModel?.currency ?? "")
  76. self.setCountryFlag(countryCode: self.selectedExchageRateModel?.countryCode ?? "")
  77. self.reciepientTextField.text = ""
  78. calcBy = "c"
  79. collectionView.reloadData()
  80. }
  81. }
  82. // Life Cycle
  83. override func viewDidLoad() {
  84. super.viewDidLoad()
  85. setup()
  86. setupDelegates()
  87. setupTargets()
  88. setupNavigation()
  89. setupDefaultCountryFlagandCurrency()
  90. // todo: show default native country and falg
  91. // Do any additional setup after loading the view.
  92. // populateDefaultAmounts()
  93. fetchExchangeRateInformation()
  94. }
  95. func setupDefaultCountryFlagandCurrency() {
  96. if let defaultExchangeRate = self.exchangeRateModels?.filter({
  97. ($0.country ?? "").lowercased() == (self.nativeCountryCode ?? "").lowercased()
  98. }).first {
  99. // there is native country, defaultExchangeRate is the information for that country
  100. self.setCountryFlag(countryCode: defaultExchangeRate.countryCode ?? "")
  101. self.setCurrencyLabel(currency: defaultExchangeRate.currency ?? "")
  102. }
  103. }
  104. override func viewWillAppear(_ animated: Bool) {
  105. super.viewWillAppear(animated)
  106. self.navigationItem.title = "Today's Rate"
  107. self.title = "Today's Rate"
  108. }
  109. // IBActions
  110. @IBAction func calculateExchangeRate(_ sender: UIButton) {
  111. let senderAmount = self.senderTextField.text!
  112. let reciepientAmount = self.reciepientTextField.text!
  113. print("sender textfield \(senderAmount)")
  114. print("reciepientTextfield \(reciepientAmount)")
  115. print(calcBy)
  116. let recipientCurrency = self.selectedExchageRateModel?.currency
  117. let reciepientCountryId = self.selectedExchageRateModel?.countryId
  118. let paymentMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: selectedPaymentIndex.row)
  119. let reciepientCountryName = self.selectedExchageRateModel?.country
  120. self.calculate(senderAmt: senderAmount, recieverAmt: reciepientAmount, recieverCurrency: recipientCurrency, recieverCountryName: reciepientCountryName, recieverCountryId: reciepientCountryId, paymentMethod: paymentMethod?.id, calcBy: self.calcBy)
  121. // call calculate( ... ) function
  122. // todo send to api for calculation
  123. }
  124. func calculate(senderAmt: String?, senderCurrency: String? = "KRW", recieverAmt: String?, recieverCurrency: String?, recieverCountryName: String?, recieverCountryId: String?, paymentMethod: String?, calcBy: String?, senderCountryId: String? = "118", shouldShowLoading: Bool = true ) {
  125. let param: [String: String] =
  126. [
  127. ApiConstants.senderAmount : senderAmt ?? "",
  128. ApiConstants.senderCurrency : senderCurrency ?? "",
  129. ApiConstants.recieverAmount : recieverAmt ?? "",
  130. ApiConstants.recieverCurrency : recieverCurrency ?? "",
  131. ApiConstants.recieverCountryId : recieverCountryId ?? "",
  132. ApiConstants.paymentMethod: paymentMethod ?? "",
  133. ApiConstants.calcBy : calcBy ?? "",
  134. ApiConstants.senderCountryId : senderCountryId ?? "",
  135. ApiConstants.recieverCountryName: recieverCountryName ?? ""
  136. ]
  137. print(param)
  138. // todo
  139. if shouldShowLoading { self.showProgressHud() }
  140. self.getExchangeRateInformation(params: param, success: { (exchageRateDetail) in
  141. self.senderTextField.text = exchageRateDetail?.senderAmount
  142. self.reciepientTextField.text = exchageRateDetail?.recipientAmount
  143. let transferFee = exchageRateDetail?.transferFee ?? ""
  144. let currency = exchageRateDetail?.senderCurrency ?? ""
  145. self.transferFeeInfoLabel.text = "\(transferFee) \(currency) (\(Constants.transferFeeDetailText))"
  146. let exchangeRate = exchageRateDetail?.exchangeRate ?? ""
  147. self.exchangeRateInfoLabel.text = "\(exchangeRate) (\(Constants.currentExchangeRateText))"
  148. self.hideProgressHud()
  149. }) { (error) in
  150. self.hideProgressHud()
  151. self.alert(message: error.localizedDescription)
  152. }
  153. // call api with these params
  154. }
  155. @objc func showCountryList(_ sender: UITapGestureRecognizer) {
  156. print("show Country List")
  157. let viewcontroller = UIStoryboard.init(name: "TableViewPicker", bundle: nil).instantiateViewController(withIdentifier: "TablePickerViewController") as! TablePickerViewController
  158. viewcontroller.data = self.exchangeRateModels ?? []
  159. viewcontroller.type = TablePickerViewTitle.country
  160. viewcontroller.doneAction = self.countrySelected
  161. // viewcontroller.defaultSelectedData = [self.selectedExchageRateModel]
  162. self.present(viewcontroller, animated: true, completion: nil)
  163. }
  164. // other function
  165. func showPaymentModeView() {
  166. UIView.animate(withDuration: 0.33) {
  167. self.paymentModeStackViewConstraint.constant = Constants.paymentModeHeightConstant
  168. self.paymentModeStackView.alpha = 1
  169. self.view.layoutIfNeeded()
  170. }
  171. }
  172. func countrySelected(model: [ExchangeRateModel]) {
  173. self.selectedExchageRateModel = model.first
  174. // show country with flag.
  175. }
  176. @objc private func textChanged(sender: UITextField) {
  177. switch sender {
  178. case senderTextField:
  179. self.reciepientTextField.text = ""
  180. self.calcBy = "c"
  181. case reciepientTextField:
  182. self.senderTextField.text = ""
  183. self.calcBy = "p"
  184. default:
  185. break
  186. }
  187. }
  188. private func setCountryFlag(countryCode: String) {
  189. let flag = CountryInfo().getFlag(for: countryCode)
  190. self.countryFlagImage.image = flag
  191. }
  192. private func setCurrencyLabel(currency: String) {
  193. self.countryCodeLabel.text = currency.uppercased()
  194. }
  195. private func populateDefaultAmounts() {
  196. guard let nativeCountry = self.nativeCountryCode else {return}
  197. // todo: native country cha bhane tesko CountryInfo class ma defaullt value cha ki chaina herne
  198. // CountryInfo class ma defaullt value cha bhane tyo value rakhera second api hit garna paryo ani aako data dekhaune
  199. // natra bhane tesko 1000000 kwr ko native ma kati huncha teti populate garaune
  200. guard let exchangeModel = self.exchangeRateModels?.filter({
  201. $0.countryCode?.lowercased() == nativeCountry.lowercased() // countryCode
  202. }).first else {return}
  203. // self.selectedExchageRateModel = exchangeModel
  204. // if there is no default amount, then we have to calculate from korean won. so guard ma rakhna thik nahola, yo case hereko chaina
  205. if let recievingAmount = CountryInfo().getDefaultSendingAmount(for: exchangeModel.countryCode ?? "") {
  206. self.setDefaultValuesForCountriesHavingDefaultValue(recievingAmount: recievingAmount, exchangeModel: exchangeModel)
  207. }
  208. // else {
  209. // setDefaultValuesForCountriesNotHavingDefaultValue()
  210. // }
  211. }
  212. private func setDefaultValuesForCountriesNotHavingDefaultValue() {
  213. self.selectedExchageRateModel = self.exchangeRateModels?.first
  214. let senderAmount = CountryInfo().getDefaultSendingMoneyInKoreanWon()
  215. self.senderTextField.text = senderAmount
  216. let senderCurrency = "KWR"
  217. let recieverCurrency = self.selectedExchageRateModel?.currency
  218. let recieverCountryId = self.selectedExchageRateModel?.countryId
  219. let paymentMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: selectedPaymentIndex.row)
  220. let reciepientCountryName = self.selectedExchageRateModel?.country ?? ""
  221. let calcBy = "c"
  222. let senderCountryName = "KOREA"
  223. self.calculate(senderAmt: senderAmount, senderCurrency: senderCurrency, recieverAmt: nil, recieverCurrency: recieverCurrency, recieverCountryName: reciepientCountryName, recieverCountryId: recieverCountryId, paymentMethod: paymentMethod?.id, calcBy: calcBy, shouldShowLoading: false)
  224. }
  225. private func setDefaultValuesForCountriesHavingDefaultValue(recievingAmount: String, exchangeModel: ExchangeRateModel) {
  226. guard let recievingCurrency = CountryInfo().getDefaultSendingCurrency(for: exchangeModel.countryCode ?? ""),
  227. let recievingCountryId = exchangeModel.countryId,
  228. let paymentMethod = exchangeModel.availableServices?.elementAt(index: selectedPaymentIndex.row)
  229. else {
  230. // do something if you have to do
  231. return
  232. }
  233. self.calcBy = "p"
  234. self.reciepientTextField.text = recievingAmount
  235. let reciepientCountryName = exchangeModel.country
  236. self.calculate(senderAmt: nil, senderCurrency: "KRW", recieverAmt: recievingAmount, recieverCurrency: recievingCurrency, recieverCountryName: reciepientCountryName, recieverCountryId: recievingCountryId, paymentMethod: paymentMethod.id, calcBy: calcBy, shouldShowLoading: false)
  237. }
  238. private func fetchExchangeRateInformation() {
  239. self.showProgressHud()
  240. self.fetchCountryCurrencyInfo(success: { (models) in
  241. self.exchangeRateModels = models
  242. }) { (error) in
  243. self.hideProgressHud()
  244. self.alert(message: error.localizedDescription)
  245. }
  246. }
  247. private func setupTargets() {
  248. let tapGuesture = UITapGestureRecognizer(target: self, action: #selector(self.showCountryList(_:)))
  249. self.countryListTapGuesture = tapGuesture
  250. self.countryListStackView.addGestureRecognizer(self.countryListTapGuesture!)
  251. self.reciepientTextField.addTarget(self, action: #selector(self.textChanged(sender:)), for: UIControlEvents.editingChanged)
  252. self.senderTextField.addTarget(self, action: #selector(self.textChanged(sender:)), for: UIControlEvents.editingChanged)
  253. }
  254. private func setupDelegates() {
  255. self.collectionView.delegate = self
  256. self.collectionView.dataSource = self
  257. }
  258. private func setup() {
  259. let nativeCountry = (UserDefaults.standard.value(forKey: UserKeys.countryCode) as? String) ?? ""
  260. self.nativeCountryCode = nativeCountry
  261. print("nativeCountry: \(nativeCountry)")
  262. let dropDownImage = #imageLiteral(resourceName: "dropdown_white").withRenderingMode(UIImageRenderingMode.alwaysTemplate)
  263. let image = dropDownImage
  264. self.dropDownImageView.image = image
  265. self.dropDownImageView.tintColor = UIColor.white
  266. self.paymentModeStackViewConstraint.constant = 0
  267. self.paymentModeStackView.alpha = 0
  268. // corner Radius
  269. [backgroundViewCountryLabel1, backgroundViewCountryLabel2].forEach({
  270. $0?.layer.cornerRadius = 5
  271. })
  272. [exchangeBackground1, exchangeBackground2].forEach({
  273. $0?.layer.borderWidth = 1
  274. $0?.layer.borderColor = UIColor.init(hex: "#E0E0E0").cgColor
  275. $0?.layer.cornerRadius = 5
  276. })
  277. }
  278. private func setupNavigation() {
  279. var backBtnImage = #imageLiteral(resourceName: "backIconBlack")
  280. backBtnImage = backBtnImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
  281. self.navigationController!.navigationBar.backIndicatorImage = backBtnImage
  282. self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backBtnImage
  283. self.navigationController?.navigationBar.barTintColor = UIColor.white
  284. }
  285. }
  286. extension ExchangeRateViewControllerV2: UICollectionViewDelegate {
  287. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  288. let cell = collectionView.cellForItem(at: indexPath) as! ExchangeRateCollectionViewCell
  289. self.selectedPaymentIndex = indexPath
  290. self.collectionView.reloadData()
  291. }
  292. }
  293. extension ExchangeRateViewControllerV2: UICollectionViewDataSource {
  294. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  295. return self.selectedExchageRateModel?.availableServices?.count ?? 0
  296. }
  297. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  298. let service = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  299. guard let index = PaymentMode.init(rawValue: service?.id ?? "") else {
  300. return UICollectionViewCell()
  301. }
  302. switch index {
  303. case .bankDeposite:
  304. return configureBankDepositeCell(collectionView: collectionView, indexPath: indexPath)
  305. case .cashDelivery:
  306. return configureCashDeliveryCell(collectionView: collectionView, indexPath: indexPath)
  307. case .homeDelivery:
  308. return configureHomeDeliveryCell(collectionView: collectionView, indexPath: indexPath)
  309. case .mobileWallet:
  310. return configureWalletDeliveryCell(collectionView: collectionView, indexPath: indexPath)
  311. }
  312. }
  313. func configureBankDepositeCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  314. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  315. cell.cellSelected = self.selectedPaymentIndex == indexPath
  316. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  317. cell.image = #imageLiteral(resourceName: "ic_bank")
  318. cell.setup()
  319. return cell
  320. }
  321. func configureWalletDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  322. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  323. cell.cellSelected = self.selectedPaymentIndex == indexPath
  324. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  325. cell.image = #imageLiteral(resourceName: "wallet-transfer")
  326. cell.setup()
  327. return cell
  328. }
  329. func configureCashDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  330. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  331. cell.cellSelected = self.selectedPaymentIndex == indexPath
  332. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  333. cell.image = #imageLiteral(resourceName: "ic_cash")
  334. cell.setup()
  335. return cell
  336. }
  337. func configureHomeDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  338. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  339. cell.cellSelected = self.selectedPaymentIndex == indexPath
  340. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  341. cell.image = #imageLiteral(resourceName: "ic_homeDelivery")
  342. cell.setup()
  343. return cell
  344. }
  345. }
  346. extension ExchangeRateViewControllerV2: FetchCountryCurrencyInformation, getExchangeRateInformation {}
  347. extension ExchangeRateViewControllerV2 {
  348. func createTestModel() {
  349. let service1 = PaymentServiceType()
  350. service1.id = "1"
  351. service1.type = "cash"
  352. service1.subtitle = "1 business day"
  353. let service2 = PaymentServiceType()
  354. service2.id = "2"
  355. service2.type = "bank"
  356. service2.subtitle = "2 hour"
  357. let service3 = PaymentServiceType()
  358. service3.id = "3"
  359. service3.type = "home"
  360. service3.subtitle = "5 business day"
  361. let model = ExchangeRateModel()
  362. model.country = "Nepal"
  363. model.countryCode = "Np"
  364. model.countryId = "1"
  365. model.currency = "NRS"
  366. model.availableServices = [service2, service1]
  367. let model1 = ExchangeRateModel()
  368. model1.country = "Korea"
  369. model1.countryCode = "krw"
  370. model1.countryId = "2"
  371. model1.currency = "KRW"
  372. model1.availableServices = [service2, service1, service3]
  373. let model2 = ExchangeRateModel()
  374. model2.country = "India"
  375. model2.countryCode = "in"
  376. model2.currency = "INR"
  377. model2.countryId = "3"
  378. model2.availableServices = [service2]
  379. let model3 = ExchangeRateModel()
  380. model3.country = "China"
  381. model3.countryCode = "cn"
  382. model3.currency = "CN"
  383. model3.countryId = "4"
  384. model3.availableServices = [service2, service1, service3]
  385. let model4 = ExchangeRateModel()
  386. model4.country = "Cambodia"
  387. model4.countryCode = "Kh"
  388. model4.currency = "USD"
  389. model4.countryId = "5"
  390. model4.availableServices = [service2, service1, service3]
  391. let model5 = ExchangeRateModel()
  392. model5.country = "Vietnam"
  393. model5.countryCode = "Vn"
  394. model5.currency = "VND"
  395. model5.countryId = "6"
  396. model5.availableServices = [service2, service1, service3]
  397. }
  398. }