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.

507 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
  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? = "np"
  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.title = "Today's Rate"
  107. }
  108. // IBActions
  109. @IBAction func calculateExchangeRate(_ sender: UIButton) {
  110. let senderAmount = self.senderTextField.text!
  111. let reciepientAmount = self.reciepientTextField.text!
  112. print("sender textfield \(senderAmount)")
  113. print("reciepientTextfield \(reciepientAmount)")
  114. print(calcBy)
  115. let recipientCurrency = self.selectedExchageRateModel?.currency
  116. let reciepientCountryId = self.selectedExchageRateModel?.countryId
  117. let paymentMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: selectedPaymentIndex.row)
  118. let reciepientCountryName = self.selectedExchageRateModel?.country
  119. self.calculate(senderAmt: senderAmount, recieverAmt: reciepientAmount, recieverCurrency: recipientCurrency, recieverCountryName: reciepientCountryName, recieverCountryId: reciepientCountryId, paymentMethod: paymentMethod?.id, calcBy: self.calcBy)
  120. // call calculate( ... ) function
  121. // todo send to api for calculation
  122. }
  123. 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 ) {
  124. let param: [String: String] =
  125. [
  126. ApiConstants.senderAmount : senderAmt ?? "",
  127. ApiConstants.senderCurrency : senderCurrency ?? "",
  128. ApiConstants.recieverAmount : recieverAmt ?? "",
  129. ApiConstants.recieverCurrency : recieverCurrency ?? "",
  130. ApiConstants.recieverCountryId : recieverCountryId ?? "",
  131. ApiConstants.paymentMethod: paymentMethod ?? "",
  132. ApiConstants.calcBy : calcBy ?? "",
  133. ApiConstants.senderCountryId : senderCountryId ?? "",
  134. ApiConstants.recieverCountryName: recieverCountryName ?? ""
  135. ]
  136. print(param)
  137. // todo
  138. if shouldShowLoading { self.showProgressHud() }
  139. self.getExchangeRateInformation(params: param, success: { (exchageRateDetail) in
  140. self.senderTextField.text = exchageRateDetail?.senderAmount
  141. self.reciepientTextField.text = exchageRateDetail?.recipientAmount
  142. let transferFee = exchageRateDetail?.transferFee ?? ""
  143. let currency = exchageRateDetail?.senderCurrency ?? ""
  144. self.transferFeeInfoLabel.text = "\(transferFee) \(currency) (\(Constants.transferFeeDetailText))"
  145. let exchangeRate = exchageRateDetail?.exchangeRate ?? ""
  146. self.exchangeRateInfoLabel.text = "\(exchangeRate) (\(Constants.currentExchangeRateText))"
  147. self.hideProgressHud()
  148. }) { (error) in
  149. self.hideProgressHud()
  150. self.alert(message: error.localizedDescription)
  151. }
  152. // call api with these params
  153. }
  154. @objc func showCountryList(_ sender: UITapGestureRecognizer) {
  155. print("show Country List")
  156. let viewcontroller = UIStoryboard.init(name: "TableViewPicker", bundle: nil).instantiateViewController(withIdentifier: "TablePickerViewController") as! TablePickerViewController
  157. viewcontroller.data = self.exchangeRateModels ?? []
  158. viewcontroller.type = TablePickerViewTitle.country
  159. viewcontroller.doneAction = self.countrySelected
  160. // viewcontroller.defaultSelectedData = [self.selectedExchageRateModel]
  161. self.present(viewcontroller, animated: true, completion: nil)
  162. }
  163. // other function
  164. func showPaymentModeView() {
  165. UIView.animate(withDuration: 0.33) {
  166. self.paymentModeStackViewConstraint.constant = Constants.paymentModeHeightConstant
  167. self.paymentModeStackView.alpha = 1
  168. self.view.layoutIfNeeded()
  169. }
  170. }
  171. func countrySelected(model: [ExchangeRateModel]) {
  172. self.selectedExchageRateModel = model.first
  173. // show country with flag.
  174. }
  175. @objc private func textChanged(sender: UITextField) {
  176. switch sender {
  177. case senderTextField:
  178. self.reciepientTextField.text = ""
  179. self.calcBy = "c"
  180. case reciepientTextField:
  181. self.senderTextField.text = ""
  182. self.calcBy = "p"
  183. default:
  184. break
  185. }
  186. }
  187. private func setCountryFlag(countryCode: String) {
  188. let flag = CountryInfo().getFlag(for: countryCode)
  189. self.countryFlagImage.image = flag
  190. }
  191. private func setCurrencyLabel(currency: String) {
  192. self.countryCodeLabel.text = currency.uppercased()
  193. }
  194. private func populateDefaultAmounts() {
  195. guard let nativeCountry = self.nativeCountryCode else {return}
  196. // todo: native country cha bhane tesko CountryInfo class ma defaullt value cha ki chaina herne
  197. // CountryInfo class ma defaullt value cha bhane tyo value rakhera second api hit garna paryo ani aako data dekhaune
  198. // natra bhane tesko 1000000 kwr ko native ma kati huncha teti populate garaune
  199. guard let exchangeModel = self.exchangeRateModels?.filter({
  200. $0.countryCode?.lowercased() == nativeCountry.lowercased() // countryCode
  201. }).first else {return}
  202. // self.selectedExchageRateModel = exchangeModel
  203. // if there is no default amount, then we have to calculate from korean won. so guard ma rakhna thik nahola, yo case hereko chaina
  204. if let recievingAmount = CountryInfo().getDefaultSendingAmount(for: exchangeModel.countryCode ?? "") {
  205. self.setDefaultValuesForCountriesHavingDefaultValue(recievingAmount: recievingAmount, exchangeModel: exchangeModel)
  206. }
  207. // else {
  208. // setDefaultValuesForCountriesNotHavingDefaultValue()
  209. // }
  210. }
  211. private func setDefaultValuesForCountriesNotHavingDefaultValue() {
  212. self.selectedExchageRateModel = self.exchangeRateModels?.first
  213. let senderAmount = CountryInfo().getDefaultSendingMoneyInKoreanWon()
  214. self.senderTextField.text = senderAmount
  215. let senderCurrency = "KWR"
  216. let recieverCurrency = self.selectedExchageRateModel?.currency
  217. let recieverCountryId = self.selectedExchageRateModel?.countryId
  218. let paymentMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: selectedPaymentIndex.row)
  219. let reciepientCountryName = self.selectedExchageRateModel?.country ?? ""
  220. let calcBy = "c"
  221. let senderCountryName = "KOREA"
  222. self.calculate(senderAmt: senderAmount, senderCurrency: senderCurrency, recieverAmt: nil, recieverCurrency: recieverCurrency, recieverCountryName: reciepientCountryName, recieverCountryId: recieverCountryId, paymentMethod: paymentMethod?.id, calcBy: calcBy, shouldShowLoading: false)
  223. }
  224. private func setDefaultValuesForCountriesHavingDefaultValue(recievingAmount: String, exchangeModel: ExchangeRateModel) {
  225. guard let recievingCurrency = CountryInfo().getDefaultSendingCurrency(for: exchangeModel.countryCode ?? ""),
  226. let recievingCountryId = exchangeModel.countryId,
  227. let paymentMethod = exchangeModel.availableServices?.elementAt(index: selectedPaymentIndex.row)
  228. else {
  229. // do something if you have to do
  230. return
  231. }
  232. self.calcBy = "p"
  233. self.reciepientTextField.text = recievingAmount
  234. let reciepientCountryName = exchangeModel.country
  235. self.calculate(senderAmt: nil, senderCurrency: "KRW", recieverAmt: recievingAmount, recieverCurrency: recievingCurrency, recieverCountryName: reciepientCountryName, recieverCountryId: recievingCountryId, paymentMethod: paymentMethod.id, calcBy: calcBy, shouldShowLoading: false)
  236. }
  237. private func fetchExchangeRateInformation() {
  238. self.showProgressHud()
  239. self.fetchCountryCurrencyInfo(success: { (models) in
  240. self.exchangeRateModels = models
  241. }) { (error) in
  242. self.hideProgressHud()
  243. self.alert(message: error.localizedDescription)
  244. }
  245. }
  246. private func setupTargets() {
  247. let tapGuesture = UITapGestureRecognizer(target: self, action: #selector(self.showCountryList(_:)))
  248. self.countryListTapGuesture = tapGuesture
  249. self.countryListStackView.addGestureRecognizer(self.countryListTapGuesture!)
  250. self.reciepientTextField.addTarget(self, action: #selector(self.textChanged(sender:)), for: UIControlEvents.editingChanged)
  251. self.senderTextField.addTarget(self, action: #selector(self.textChanged(sender:)), for: UIControlEvents.editingChanged)
  252. }
  253. private func setupDelegates() {
  254. self.collectionView.delegate = self
  255. self.collectionView.dataSource = self
  256. }
  257. private func setup() {
  258. let dropDownImage = #imageLiteral(resourceName: "dropdown_white").withRenderingMode(UIImageRenderingMode.alwaysTemplate)
  259. let image = dropDownImage
  260. self.dropDownImageView.image = image
  261. self.dropDownImageView.tintColor = UIColor.white
  262. self.paymentModeStackViewConstraint.constant = 0
  263. self.paymentModeStackView.alpha = 0
  264. // corner Radius
  265. [backgroundViewCountryLabel1, backgroundViewCountryLabel2].forEach({
  266. $0?.layer.cornerRadius = 5
  267. })
  268. [exchangeBackground1, exchangeBackground2].forEach({
  269. $0?.layer.borderWidth = 1
  270. $0?.layer.borderColor = UIColor.init(hex: "#E0E0E0").cgColor
  271. $0?.layer.cornerRadius = 5
  272. })
  273. }
  274. private func setupNavigation() {
  275. var backBtnImage = #imageLiteral(resourceName: "backIconBlack")
  276. backBtnImage = backBtnImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
  277. self.navigationController!.navigationBar.backIndicatorImage = backBtnImage
  278. self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backBtnImage
  279. self.navigationController?.navigationBar.barTintColor = UIColor.white
  280. }
  281. }
  282. extension ExchangeRateViewControllerV2: UICollectionViewDelegate {
  283. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  284. let cell = collectionView.cellForItem(at: indexPath) as! ExchangeRateCollectionViewCell
  285. self.selectedPaymentIndex = indexPath
  286. self.collectionView.reloadData()
  287. }
  288. }
  289. extension ExchangeRateViewControllerV2: UICollectionViewDataSource {
  290. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  291. return self.selectedExchageRateModel?.availableServices?.count ?? 0
  292. }
  293. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  294. let service = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  295. guard let index = PaymentMode.init(rawValue: service?.id ?? "") else {
  296. return UICollectionViewCell()
  297. }
  298. switch index {
  299. case .bankDeposite:
  300. return configureBankDepositeCell(collectionView: collectionView, indexPath: indexPath)
  301. case .cashDelivery:
  302. return configureCashDeliveryCell(collectionView: collectionView, indexPath: indexPath)
  303. case .homeDelivery:
  304. return configureHomeDeliveryCell(collectionView: collectionView, indexPath: indexPath)
  305. case .mobileWallet:
  306. return configureWalletDeliveryCell(collectionView: collectionView, indexPath: indexPath)
  307. }
  308. }
  309. func configureBankDepositeCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  310. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  311. cell.cellSelected = self.selectedPaymentIndex == indexPath
  312. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  313. cell.image = #imageLiteral(resourceName: "ic_bank")
  314. cell.setup()
  315. return cell
  316. }
  317. func configureWalletDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  318. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  319. cell.cellSelected = self.selectedPaymentIndex == indexPath
  320. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  321. cell.image = #imageLiteral(resourceName: "wallet-transfer")
  322. cell.setup()
  323. return cell
  324. }
  325. func configureCashDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  326. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  327. cell.cellSelected = self.selectedPaymentIndex == indexPath
  328. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  329. cell.image = #imageLiteral(resourceName: "ic_cash")
  330. cell.setup()
  331. return cell
  332. }
  333. func configureHomeDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
  334. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExchangeRateCollectionViewCell", for: indexPath) as! ExchangeRateCollectionViewCell
  335. cell.cellSelected = self.selectedPaymentIndex == indexPath
  336. cell.paymentServiceMethod = self.selectedExchageRateModel?.availableServices?.elementAt(index: indexPath.row)
  337. cell.image = #imageLiteral(resourceName: "ic_homeDelivery")
  338. cell.setup()
  339. return cell
  340. }
  341. }
  342. extension ExchangeRateViewControllerV2: FetchCountryCurrencyInformation, getExchangeRateInformation {}
  343. extension ExchangeRateViewControllerV2 {
  344. func createTestModel() {
  345. let service1 = PaymentServiceType()
  346. service1.id = "1"
  347. service1.type = "cash"
  348. service1.subtitle = "1 business day"
  349. let service2 = PaymentServiceType()
  350. service2.id = "2"
  351. service2.type = "bank"
  352. service2.subtitle = "2 hour"
  353. let service3 = PaymentServiceType()
  354. service3.id = "3"
  355. service3.type = "home"
  356. service3.subtitle = "5 business day"
  357. let model = ExchangeRateModel()
  358. model.country = "Nepal"
  359. model.countryCode = "Np"
  360. model.countryId = "1"
  361. model.currency = "NRS"
  362. model.availableServices = [service2, service1]
  363. let model1 = ExchangeRateModel()
  364. model1.country = "Korea"
  365. model1.countryCode = "krw"
  366. model1.countryId = "2"
  367. model1.currency = "KRW"
  368. model1.availableServices = [service2, service1, service3]
  369. let model2 = ExchangeRateModel()
  370. model2.country = "India"
  371. model2.countryCode = "in"
  372. model2.currency = "INR"
  373. model2.countryId = "3"
  374. model2.availableServices = [service2]
  375. let model3 = ExchangeRateModel()
  376. model3.country = "China"
  377. model3.countryCode = "cn"
  378. model3.currency = "CN"
  379. model3.countryId = "4"
  380. model3.availableServices = [service2, service1, service3]
  381. let model4 = ExchangeRateModel()
  382. model4.country = "Cambodia"
  383. model4.countryCode = "Kh"
  384. model4.currency = "USD"
  385. model4.countryId = "5"
  386. model4.availableServices = [service2, service1, service3]
  387. let model5 = ExchangeRateModel()
  388. model5.country = "Vietnam"
  389. model5.countryCode = "Vn"
  390. model5.currency = "VND"
  391. model5.countryId = "6"
  392. model5.availableServices = [service2, service1, service3]
  393. }
  394. }