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.

341 lines
12 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
  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 {
  12. func showLoading()
  13. func hideLoading()
  14. }
  15. // Actions from SendMoneyPaymentModeViewController are delegated to parent view
  16. protocol SendMoneyPaymentModeActionDelegate {
  17. func selected(bank: SendMoneyBank?)
  18. func selected(branch: SendMoneyBankBranch?)
  19. func selected(payoutMethod: SendMoneyPayoutMode?)
  20. func added(acountNumber: String)
  21. func continueToExchangeAction()
  22. }
  23. // Actions from SendMoneyExchangeRateViewController are delegated to Parent View
  24. protocol SendMoneyExchangeRateActionDelegate {
  25. func continueToVerificationAction()
  26. func calculated(model: SendMoneyExchangeRateModel?)
  27. }
  28. // A model encapsulating all field required to make the api request for send money
  29. class SendMoneyRequestModel {
  30. var paymemtMode: SendMoneyPayoutMode?
  31. var bank: SendMoneyBank?
  32. var branch: SendMoneyBankBranch?
  33. var accountNumber: String?
  34. var payingAmount: String?
  35. var exchangeRateDetail: SendMoneyExchangeRateModel?
  36. var transactionPassword: String?
  37. var autoDebitAccount: Account?
  38. }
  39. class SendMoneyParentViewController: UIViewController {
  40. private enum StateButtons: Int {
  41. case paymentMode = 1
  42. case exchange = 2
  43. case verification = 3
  44. }
  45. private struct Constants {
  46. static let stateRedColor = UIColor.init(hex: "#EC1C24")
  47. static let stateGreenColor = AppConstants.themeBlueColor
  48. static let stateLabelGreyColor = UIColor.init(hex: "4a4a4a")
  49. }
  50. private struct StringConstants {
  51. let payoutModeText = "payout_mode_text".localized()
  52. let amountDetailText = "amount_detail_text".localized()
  53. let verificationDetailText = "verification_detail_text".localized()
  54. }
  55. // MARK: IBOutlets
  56. // Container view holds the child view controllers
  57. // There are three child view controllers.
  58. // 1. SendMoneyPaymentModeViewController
  59. // 2. SendMoneyExchangeRateViewController
  60. // 3. SendMoneyVerificationViewController
  61. @IBOutlet weak var containerView: UIView!
  62. @IBOutlet weak var paymentModeButton: UIButton!
  63. @IBOutlet weak var exchangeRateButton: UIButton!
  64. @IBOutlet weak var verificationButton: UIButton!
  65. @IBOutlet weak var paymentModeLabel: UILabel!
  66. @IBOutlet weak var exchangeRateLabel: UILabel!
  67. @IBOutlet weak var verificationLabel: UILabel!
  68. // MARK: Properties
  69. var SendMoneyPaymentModeViewController: UIViewController?
  70. var SendMoneyExchangeRateViewController: UIViewController?
  71. var SendMoneyVerificationViewController: UIViewController?
  72. var requestModel: SendMoneyRequestModel?
  73. var presenter: SendMoneyParentModuleInterface?
  74. // Buttons representing the pages marked as 1, 2, 3 in UI.
  75. var stateButtons: [UIButton] = []
  76. private var state: StateButtons = .paymentMode {
  77. didSet {
  78. self.updateState(state: state)
  79. }
  80. }
  81. var receipient: Recipient?
  82. var account: Account? {
  83. didSet {
  84. self.requestModel?.autoDebitAccount = account
  85. }
  86. }
  87. // MARK: VC's Life cycle
  88. override func viewDidLoad() {
  89. super.viewDidLoad()
  90. self.setup()
  91. self.presenter?.viewIsReady()
  92. self.setupViewControllers()
  93. self.addPaymentModeViewController()
  94. }
  95. override func viewWillAppear(_ animated: Bool) {
  96. super.viewWillAppear(animated)
  97. setTitle(title: "send_money_text".localized())
  98. }
  99. override func viewWillDisappear(_ animated: Bool) {
  100. super.viewWillDisappear(animated)
  101. self.navigationItem.title = ""
  102. }
  103. // MARK: IBActions
  104. @IBAction func paymentMode(_ sender: UIButton) {
  105. self.addPaymentModeViewController()
  106. self.state = StateButtons.paymentMode
  107. }
  108. @IBAction func exchangeRate(_ sender: UIButton) {
  109. self.addExchangeViewController()
  110. self.state = StateButtons.exchange
  111. }
  112. @IBAction func verification(_ sender: UIButton) {
  113. self.addVerificationViewController()
  114. self.state = StateButtons.verification
  115. }
  116. // MARK: Other Functions
  117. private func setup() {
  118. // all setup should be done here
  119. self.setTitle(title: "send_money_text".localized())
  120. self.state = StateButtons.paymentMode
  121. self.configureViews()
  122. self.requestModel = SendMoneyRequestModel()
  123. configureLanguage()
  124. }
  125. private func configureLanguage() {
  126. self.paymentModeLabel.text = StringConstants().payoutModeText
  127. self.exchangeRateLabel.text = StringConstants().amountDetailText
  128. self.verificationLabel.text = StringConstants().verificationDetailText
  129. }
  130. private func setTitle(title: String) {
  131. self.navigationItem.title = title
  132. }
  133. private func setupViewControllers() {
  134. let paymentModelWireframe = SendMoneyPaymentModeWireframe()
  135. let pvc = paymentModelWireframe.getMainView() as! SendMoneyPaymentModeViewController
  136. pvc.recipient = self.receipient
  137. pvc.hudDelegate = self
  138. pvc.actionDelegate = self
  139. self.SendMoneyPaymentModeViewController = pvc
  140. let exchangeViewWireframe = SendMoneyExchangeRateWireframe()
  141. let evc = exchangeViewWireframe.getMainView() as! SendMoneyExchangeRateViewController
  142. evc.reciepient = self.receipient
  143. evc.actionDelegate = self
  144. evc.hudDelegate = self
  145. self.SendMoneyExchangeRateViewController = evc
  146. let verificationWireframe = SendMoneyVerificationWireframe()
  147. let vvc = verificationWireframe.getMainView() as! SendMoneyVerificationViewController
  148. vvc.reciepient = self.receipient
  149. vvc.requestModel = self.requestModel
  150. vvc.hudDelegate = self
  151. self.SendMoneyVerificationViewController = vvc
  152. }
  153. private func configureViews() {
  154. self.paymentModeButton.tag = 1
  155. self.exchangeRateButton.tag = 2
  156. self.verificationButton.tag = 3
  157. self.stateButtons = [paymentModeButton,exchangeRateButton, verificationButton ]
  158. [paymentModeButton, exchangeRateButton, verificationButton].forEach({
  159. $0?.rounded()
  160. })
  161. }
  162. private func updateState(state: StateButtons) {
  163. switch state {
  164. case .paymentMode:
  165. // buttons
  166. self.paymentModeButton.backgroundColor = Constants.stateGreenColor
  167. self.exchangeRateButton.backgroundColor = Constants.stateRedColor
  168. self.verificationButton.backgroundColor = Constants.stateRedColor
  169. self.exchangeRateButton.isUserInteractionEnabled = false
  170. self.verificationButton.isUserInteractionEnabled = false
  171. // labels
  172. self.paymentModeLabel.textColor = Constants.stateGreenColor
  173. self.exchangeRateLabel.textColor = Constants.stateLabelGreyColor
  174. self.verificationLabel.textColor = Constants.stateLabelGreyColor
  175. self.requestModel?.bank = nil
  176. self.requestModel?.branch = nil
  177. case .exchange:
  178. // buttons
  179. self.paymentModeButton.backgroundColor = Constants.stateGreenColor
  180. self.exchangeRateButton.backgroundColor = Constants.stateGreenColor
  181. self.verificationButton.backgroundColor = Constants.stateRedColor
  182. self.exchangeRateButton.isUserInteractionEnabled = true
  183. self.verificationButton.isUserInteractionEnabled = false
  184. // labels
  185. self.paymentModeLabel.textColor = Constants.stateLabelGreyColor
  186. self.exchangeRateLabel.textColor = Constants.stateGreenColor
  187. self.verificationLabel.textColor = Constants.stateLabelGreyColor
  188. case .verification:
  189. // button
  190. self.paymentModeButton.backgroundColor = Constants.stateGreenColor
  191. self.exchangeRateButton.backgroundColor = Constants.stateGreenColor
  192. self.verificationButton.backgroundColor = Constants.stateGreenColor
  193. self.exchangeRateButton.isUserInteractionEnabled = true
  194. // labels
  195. self.paymentModeLabel.textColor = Constants.stateLabelGreyColor
  196. self.exchangeRateLabel.textColor = Constants.stateLabelGreyColor
  197. self.verificationLabel.textColor = Constants.stateLabelGreyColor
  198. }
  199. }
  200. // Make SendMoneyPaymentModeViewController as front View Controller
  201. private func addPaymentModeViewController() {
  202. guard let paymentModeViewController = self.SendMoneyPaymentModeViewController else {return}
  203. self.addChildViewController(paymentModeViewController)
  204. UIView.transition(with: self.containerView, duration: 0.33, options: .transitionCrossDissolve, animations: {
  205. self.containerView.addSubview(paymentModeViewController.view)
  206. }, completion: nil)
  207. paymentModeViewController.view.frame = containerView.bounds
  208. paymentModeViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  209. paymentModeViewController.didMove(toParentViewController: self)
  210. }
  211. // Make SendMoneyPaymentModeViewController as front View Controller
  212. private func addExchangeViewController() {
  213. guard let exchangeViewController = self.SendMoneyExchangeRateViewController as? SendMoneyExchangeRateViewController else {return}
  214. exchangeViewController.requestModel = self.requestModel
  215. exchangeViewController.actionDelegate = self
  216. self.addChildViewController(exchangeViewController)
  217. UIView.transition(with: self.containerView, duration: 0.33, options: .transitionCrossDissolve, animations: {
  218. self.containerView.addSubview(exchangeViewController.view)
  219. }, completion: nil)
  220. exchangeViewController.view.frame = containerView.bounds
  221. exchangeViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  222. exchangeViewController.didMove(toParentViewController: self)
  223. }
  224. // Make SendMoneyExchangeRateViewController as front View Controller
  225. private func addVerificationViewController() {
  226. guard let verificationViewController = self.SendMoneyVerificationViewController as? SendMoneyVerificationViewController else {return}
  227. verificationViewController.reciepient = self.receipient
  228. verificationViewController.requestModel = self.requestModel
  229. self.addChildViewController(verificationViewController)
  230. UIView.transition(with: self.containerView, duration: 0.33, options: .transitionCrossDissolve, animations: {
  231. self.containerView.addSubview(verificationViewController.view)
  232. }, completion: nil)
  233. verificationViewController.view.frame = containerView.bounds
  234. verificationViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  235. verificationViewController.didMove(toParentViewController: self)
  236. }
  237. }
  238. // MARK: SendMoneyParentViewInterface
  239. extension SendMoneyParentViewController: SendMoneyParentViewInterface {
  240. func show(model: Recipient) {
  241. self.receipient = model
  242. }
  243. func show(model: Account) {
  244. self.account = model
  245. }
  246. }
  247. extension SendMoneyParentViewController: HUDStatusDelegate {
  248. func showLoading() {
  249. self.showProgressHud()
  250. }
  251. func hideLoading() {
  252. self.hideProgressHud()
  253. }
  254. }
  255. extension SendMoneyParentViewController: SendMoneyPaymentModeActionDelegate {
  256. func selected(bank: SendMoneyBank?) {
  257. self.requestModel?.bank = bank
  258. }
  259. func selected(branch: SendMoneyBankBranch?) {
  260. self.requestModel?.branch = branch
  261. }
  262. func selected(payoutMethod: SendMoneyPayoutMode?) {
  263. self.requestModel?.paymemtMode = payoutMethod
  264. }
  265. func continueToExchangeAction() {
  266. self.addExchangeViewController()
  267. self.state = StateButtons.exchange
  268. }
  269. func added(acountNumber: String) {
  270. self.requestModel?.accountNumber = acountNumber
  271. }
  272. }
  273. extension SendMoneyParentViewController: SendMoneyExchangeRateActionDelegate {
  274. func continueToVerificationAction() {
  275. self.addVerificationViewController()
  276. self.state = StateButtons.verification
  277. }
  278. func calculated(model: SendMoneyExchangeRateModel?) {
  279. self.requestModel?.exchangeRateDetail = model
  280. }
  281. }