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.

462 lines
17 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. //
  2. // SendMoneyParentViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 28/08/2018.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. // controls the progress hud of parent view from child view
  11. protocol HUDStatusDelegate: class {
  12. func showLoading()
  13. func hideLoading()
  14. }
  15. // Actions from SendMoneyExchangeRateViewController are delegated to Parent View
  16. protocol SendMoneyExchangeRateActionDelegate: class {
  17. func continueToVerificationAction(model: CDDIRequestData)
  18. func calculated(model: SendMoneyExchangeRateModel?)
  19. func continueToCDDI()
  20. func continueToTermsAndConditions()
  21. }
  22. // A model encapsulating all field required to make the api request for send money
  23. class SendMoneyRequestModel {
  24. var paymemtMode: SendMoneyPayoutMode? = SendMoneyPayoutMode()
  25. var bank: SendMoneyBank? = SendMoneyBank()
  26. var branch: SendMoneyBankBranch? = SendMoneyBankBranch()
  27. var accountNumber: String?
  28. var payingAmount: String?
  29. var exchangeRateDetail: SendMoneyExchangeRateModel?
  30. var transactionPassword: String?
  31. var autoDebitAccount: Account?
  32. var isUseBiometric: Bool?
  33. }
  34. class SendMoneyParentViewController: UIViewController {
  35. private enum StateButtons: Int {
  36. case exchange = 1
  37. case cddi = 2
  38. case verification = 3
  39. case terms = 4
  40. }
  41. private struct Constants {
  42. static let stateRedColor = UIColor.init(hex: "#EC1C24")
  43. static let stateGreenColor = UIColor.themeBlue
  44. static let stateLabelGreyColor = UIColor.init(hex: "4a4a4a")
  45. }
  46. private struct StringConstants {
  47. let payoutModeText = "payout_mode_text".localized()
  48. let amountDetailText = "amount_detail_text".localized()
  49. let verificationDetailText = "verification_detail_text".localized()
  50. }
  51. // MARK: IBOutlets
  52. // Container view holds the child view controllers
  53. // There are three child view controllers.
  54. // 2. SendMoneyExchangeRateViewController
  55. // 3. SendMoneyVerificationViewController
  56. @IBOutlet weak var containerView: UIView!
  57. @IBOutlet weak var exchangeRateButton: UIButton!
  58. @IBOutlet weak var verificationButton: UIButton!
  59. @IBOutlet weak var exchangeRateLabel: UILabel!
  60. @IBOutlet weak var verificationLabel: UILabel!
  61. @IBOutlet weak var cddiButton: UIButton!
  62. @IBOutlet weak var termsButton: UIButton!
  63. @IBOutlet weak var cddiLabel: UILabel!
  64. @IBOutlet weak var termsLabel: UILabel!
  65. // MARK: Properties
  66. var sendMoneyExchangeRateViewController: SendMoneyExchangeRateViewController?
  67. var sendMoneyVerificationViewController: SendMoneyVerificationViewController?
  68. var cddiViewController: CDDIViewControllerViewController?
  69. var termsViewController: TermsAndConditionViewController?
  70. lazy private var requestModel = SendMoneyRequestModel()
  71. lazy private var cddiModel = CDDIRequestData()
  72. var presenter: SendMoneyParentModuleInterface?
  73. // Buttons representing the pages marked as 1, 2, 3 in UI.
  74. var stateButtons: [UIButton] = []
  75. private var state: StateButtons = .exchange {
  76. didSet {
  77. self.updateState(state: state)
  78. }
  79. }
  80. var receipient: Recipient? {
  81. didSet {
  82. self.requestModel.bank?.id = receipient?.agent?.id
  83. self.requestModel.bank?.name = receipient?.agent?.name
  84. self.requestModel.bank?.payCurrency = receipient?.agent?.currency
  85. self.requestModel.accountNumber = receipient?.agent?.accountNumber
  86. self.requestModel.branch?.id = receipient?.agent?.branch?.id
  87. self.requestModel.branch?.name = receipient?.agent?.branch?.name
  88. self.requestModel.paymemtMode?.id = receipient?.paymentMethod?.id
  89. self.requestModel.paymemtMode?.mode = receipient?.paymentMethod?.name
  90. self.requestModel.paymemtMode?.payCurrency = receipient?.paymentMethod?.currency
  91. self.requestModel.paymemtMode?.payoutPartner = receipient?.payoutPartner
  92. self.requestModel.paymemtMode?.accountNumber = receipient?.agent?.accountNumber
  93. }
  94. }
  95. // MARK: VC's Life cycle
  96. override func viewDidLoad() {
  97. super.viewDidLoad()
  98. self.setup()
  99. self.presenter?.viewIsReady()
  100. self.setupViewControllers()
  101. self.addExchangeViewController()
  102. }
  103. override func viewWillAppear(_ animated: Bool) {
  104. super.viewWillAppear(animated)
  105. self.setupNormalNavigation()
  106. setTitle(title: "send_money_text".localized())
  107. }
  108. override func viewWillDisappear(_ animated: Bool) {
  109. super.viewWillDisappear(animated)
  110. self.navigationItem.title = ""
  111. view.endEditing(true)
  112. }
  113. // MARK: IBActions
  114. @IBAction func exchangeRate(_ sender: UIButton) {
  115. self.addExchangeViewController()
  116. self.state = StateButtons.exchange
  117. }
  118. @IBAction func verification(_ sender: UIButton) {
  119. self.addVerificationViewController()
  120. self.state = StateButtons.verification
  121. }
  122. @IBAction func cddiTapped(_ sender: UIButton) {
  123. self.addCddiViewController()
  124. self.state = StateButtons.cddi
  125. }
  126. @IBAction func termsAndConditions(_ sender: UIButton) {
  127. self.addTermsAndConditions()
  128. self.state = StateButtons.terms
  129. }
  130. // MARK: Other Functions
  131. private func setup() {
  132. // all setup should be done here
  133. self.setTitle(title: "send_money_text".localized())
  134. self.state = StateButtons.exchange
  135. self.configureViews()
  136. self.requestModel = SendMoneyRequestModel()
  137. configureLanguage()
  138. }
  139. private func configureLanguage() {
  140. self.exchangeRateLabel.text = StringConstants().amountDetailText
  141. self.verificationLabel.text = StringConstants().verificationDetailText
  142. }
  143. private func setTitle(title: String) {
  144. self.navigationItem.title = title
  145. }
  146. private func setupViewControllers() {
  147. sendMoneyExchangeRateViewController = SendMoneyExchangeRateWireframe()
  148. .getMainView() as? SendMoneyExchangeRateViewController
  149. sendMoneyExchangeRateViewController?.reciepient = self.receipient
  150. sendMoneyExchangeRateViewController?.actionDelegate = self
  151. sendMoneyExchangeRateViewController?.hudDelegate = self
  152. sendMoneyVerificationViewController = SendMoneyVerificationWireframe()
  153. .getMainView() as? SendMoneyVerificationViewController
  154. sendMoneyVerificationViewController?.reciepient = self.receipient
  155. sendMoneyVerificationViewController?.requestModel = self.requestModel
  156. sendMoneyVerificationViewController?.hudDelegate = self
  157. cddiViewController = CDDIViewControllerWireframe().getMainView() as? CDDIViewControllerViewController
  158. cddiViewController?.reciepient = self.receipient
  159. cddiViewController?.requestModel = self.requestModel
  160. cddiViewController?.hudDelegate = self
  161. termsViewController = TermsAndConditionWireframe().getMainView() as? TermsAndConditionViewController
  162. termsViewController?.reciepient = self.receipient
  163. termsViewController?.requestModel = self.requestModel
  164. termsViewController?.hudDelegate = self
  165. }
  166. private func configureViews() {
  167. self.exchangeRateButton.tag = 2
  168. self.verificationButton.tag = 3
  169. self.cddiButton.tag = 4
  170. self.termsButton.tag = 5
  171. self.stateButtons = [exchangeRateButton, verificationButton, cddiButton, termsButton]
  172. [exchangeRateButton, verificationButton, cddiButton, termsButton].forEach({
  173. $0?.rounded()
  174. })
  175. }
  176. private func updateState(state: StateButtons) {
  177. switch state {
  178. case .exchange:
  179. // buttons
  180. self.exchangeRateButton.backgroundColor = .themeBlue
  181. self.exchangeRateButton.setTitleColor(.white, for: .normal)
  182. self.verificationButton.backgroundColor = .white
  183. self.verificationButton.setTitleColor(.black, for: .normal)
  184. self.verificationButton.layer.borderColor = UIColor.lightGray.cgColor
  185. self.verificationButton.layer.borderWidth = 1
  186. self.cddiButton.backgroundColor = .white
  187. self.cddiButton.setTitleColor(.black, for: .normal)
  188. self.cddiButton.layer.borderColor = UIColor.lightGray.cgColor
  189. self.cddiButton.layer.borderWidth = 1
  190. self.termsButton.backgroundColor = .white
  191. self.termsButton.setTitleColor(.black, for: .normal)
  192. self.termsButton.layer.borderColor = UIColor.lightGray.cgColor
  193. self.termsButton.layer.borderWidth = 1
  194. self.exchangeRateButton.isUserInteractionEnabled = true
  195. self.verificationButton.isUserInteractionEnabled = false
  196. self.cddiButton.isUserInteractionEnabled = false
  197. self.termsButton.isUserInteractionEnabled = false
  198. // labels
  199. self.exchangeRateLabel.textColor = .themeText
  200. self.verificationLabel.textColor = .themeText
  201. self.cddiLabel.textColor = .themeText
  202. self.termsLabel.textColor = .themeText
  203. case .cddi:
  204. self.exchangeRateButton.backgroundColor = .themeBlue
  205. self.exchangeRateButton.setTitleColor(.white, for: .normal)
  206. self.exchangeRateButton.isUserInteractionEnabled = true
  207. self.cddiButton.backgroundColor = .themeBlue
  208. self.cddiButton.setTitleColor(.white, for: .normal)
  209. self.cddiButton.layer.borderWidth = 0
  210. self.verificationButton.backgroundColor = .white
  211. self.verificationButton.setTitleColor(.black, for: .normal)
  212. self.verificationButton.layer.borderColor = UIColor.lightGray.cgColor
  213. self.verificationButton.layer.borderWidth = 1
  214. self.termsButton.backgroundColor = .white
  215. self.termsButton.setTitleColor(.black, for: .normal)
  216. self.termsButton.layer.borderColor = UIColor.lightGray.cgColor
  217. self.termsButton.layer.borderWidth = 1
  218. self.exchangeRateButton.isUserInteractionEnabled = true
  219. self.verificationButton.isUserInteractionEnabled = false
  220. self.cddiButton.isUserInteractionEnabled = true
  221. self.termsButton.isUserInteractionEnabled = false
  222. // labels
  223. self.exchangeRateLabel.textColor = .themeText
  224. self.verificationLabel.textColor = .themeText
  225. self.cddiLabel.textColor = .themeText
  226. self.termsLabel.textColor = .themeText
  227. case .verification:
  228. // button
  229. self.exchangeRateButton.backgroundColor = .themeBlue
  230. self.exchangeRateButton.setTitleColor(.white, for: .normal)
  231. self.exchangeRateButton.isUserInteractionEnabled = true
  232. self.cddiButton.backgroundColor = .themeBlue
  233. self.cddiButton.setTitleColor(.white, for: .normal)
  234. self.cddiButton.isUserInteractionEnabled = true
  235. self.verificationButton.backgroundColor = .themeBlue
  236. self.verificationButton.setTitleColor(.white, for: .normal)
  237. self.verificationButton.layer.borderWidth = 0
  238. self.verificationButton.isUserInteractionEnabled = true
  239. self.termsButton.backgroundColor = .white
  240. self.termsButton.setTitleColor(.black, for: .normal)
  241. self.termsButton.layer.borderColor = UIColor.lightGray.cgColor
  242. self.termsButton.layer.borderWidth = 1
  243. self.termsButton.isUserInteractionEnabled = false
  244. // labels
  245. self.exchangeRateLabel.textColor = .themeText
  246. self.verificationLabel.textColor = .themeText
  247. self.cddiLabel.textColor = .themeText
  248. self.termsLabel.textColor = .themeText
  249. case .terms:
  250. self.exchangeRateButton.backgroundColor = .themeBlue
  251. self.exchangeRateButton.isUserInteractionEnabled = true
  252. self.exchangeRateButton.setTitleColor(.white, for: .normal)
  253. self.cddiButton.backgroundColor = .themeBlue
  254. self.cddiButton.isUserInteractionEnabled = true
  255. self.cddiButton.setTitleColor(.white, for: .normal)
  256. self.verificationButton.backgroundColor = .themeBlue
  257. self.verificationButton.setTitleColor(.white, for: .normal)
  258. self.cddiButton.isUserInteractionEnabled = true
  259. self.termsButton.backgroundColor = .themeBlue
  260. self.termsButton.setTitleColor(.white, for: .normal)
  261. self.termsButton.layer.borderWidth = 0
  262. self.termsButton.isUserInteractionEnabled = true
  263. // labels
  264. self.exchangeRateLabel.textColor = .themeText
  265. self.verificationLabel.textColor = .themeText
  266. self.cddiLabel.textColor = .themeText
  267. self.termsLabel.textColor = .themeText
  268. }
  269. }
  270. private func addExchangeViewController() {
  271. guard let exchangeViewController = sendMoneyExchangeRateViewController else { return }
  272. exchangeViewController.requestModel = self.requestModel
  273. exchangeViewController.actionDelegate = self
  274. self.addChild(exchangeViewController)
  275. UIView.transition(
  276. with: self.containerView,
  277. duration: 0.33,
  278. options: .transitionCrossDissolve,
  279. animations: {
  280. self.containerView.addSubview(exchangeViewController.view)
  281. },
  282. completion: nil
  283. )
  284. exchangeViewController.view.frame = containerView.bounds
  285. exchangeViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  286. exchangeViewController.didMove(toParent: self)
  287. }
  288. private func addCddiViewController() {
  289. guard let ccdiView = cddiViewController else {return}
  290. ccdiView.requestModel = self.requestModel
  291. ccdiView.actionDelegate = self
  292. self.addChild(ccdiView)
  293. UIView.transition(
  294. with: self.containerView,
  295. duration: 0.33,
  296. options: .transitionCrossDissolve,
  297. animations: {
  298. self.containerView.addSubview(ccdiView.view)
  299. },
  300. completion: nil
  301. )
  302. ccdiView.view.frame = containerView.bounds
  303. ccdiView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  304. ccdiView.didMove(toParent: self)
  305. }
  306. // Make SendMoneyExchangeRateViewController as front View Controller
  307. private func addVerificationViewController() {
  308. guard let verificationViewController = sendMoneyVerificationViewController else {return}
  309. verificationViewController.reciepient = self.receipient
  310. verificationViewController.requestModel = self.requestModel
  311. verificationViewController.actionDelegate = self
  312. self.addChild(verificationViewController)
  313. UIView.transition(
  314. with: self.containerView,
  315. duration: 0.33,
  316. options: .transitionCrossDissolve,
  317. animations: {
  318. self.containerView.addSubview(verificationViewController.view)
  319. },
  320. completion: nil
  321. )
  322. verificationViewController.view.frame = containerView.bounds
  323. verificationViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  324. verificationViewController.didMove(toParent: self)
  325. }
  326. private func addTermsAndConditions() {
  327. guard let termsView = termsViewController else {return}
  328. termsView.reciepient = self.receipient
  329. termsView.requestModel = self.requestModel
  330. termsView.cddiModel = self.cddiModel
  331. self.addChild(termsView)
  332. UIView.transition(
  333. with: self.containerView,
  334. duration: 0.33,
  335. options: .transitionCrossDissolve,
  336. animations: {
  337. self.containerView.addSubview(termsView.view)
  338. },
  339. completion: nil
  340. )
  341. termsView.view.frame = containerView.bounds
  342. termsView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  343. termsView.didMove(toParent: self)
  344. }
  345. }
  346. // MARK: SendMoneyParentViewInterface
  347. extension SendMoneyParentViewController: SendMoneyParentViewInterface {
  348. func show(model: Recipient) {
  349. self.receipient = model
  350. }
  351. }
  352. extension SendMoneyParentViewController: HUDStatusDelegate {
  353. func showLoading() {
  354. self.tabBarController?.showProgressHud()
  355. }
  356. func hideLoading() {
  357. self.tabBarController?.hideProgressHud()
  358. }
  359. }
  360. extension SendMoneyParentViewController: SendMoneyExchangeRateActionDelegate {
  361. func continueToCDDI() {
  362. self.addCddiViewController()
  363. self.state = StateButtons.cddi
  364. }
  365. func continueToTermsAndConditions() {
  366. self.addTermsAndConditions()
  367. self.state = StateButtons.terms
  368. }
  369. func continueToVerificationAction(model: CDDIRequestData) {
  370. self.cddiModel = model
  371. print("model:\(model)")
  372. self.addVerificationViewController()
  373. self.state = StateButtons.verification
  374. }
  375. func calculated(model: SendMoneyExchangeRateModel?) {
  376. self.requestModel.exchangeRateDetail = model
  377. }
  378. }