// // SendMoneyParentViewController.swift // GMERemittance // // Created by gme_2 on 28/08/2018. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import Localize_Swift // controls the progress hud of parent view from child view protocol HUDStatusDelegate { func showLoading() func hideLoading() } // Actions from SendMoneyPaymentModeViewController are delegated to parent view protocol SendMoneyPaymentModeActionDelegate { func selected(bank: SendMoneyBank?) func selected(branch: SendMoneyBankBranch?) func selected(payoutMethod: SendMoneyPayoutMode?) func added(acountNumber: String) func continueToExchangeAction() } // Actions from SendMoneyExchangeRateViewController are delegated to Parent View protocol SendMoneyExchangeRateActionDelegate { func continueToVerificationAction() func calculated(model: SendMoneyExchangeRateModel?) } // A model encapsulating all field required to make the api request for send money class SendMoneyRequestModel { var paymemtMode: SendMoneyPayoutMode? var bank: SendMoneyBank? var branch: SendMoneyBankBranch? var accountNumber: String? var payingAmount: String? var exchangeRateDetail: SendMoneyExchangeRateModel? var transactionPassword: String? var autoDebitAccount: Account? } class SendMoneyParentViewController: UIViewController { private enum StateButtons: Int { case paymentMode = 1 case exchange = 2 case verification = 3 } private struct Constants { static let stateRedColor = UIColor.init(hex: "#EC1C24") static let stateGreenColor = AppConstants.themeBlueColor static let stateLabelGreyColor = UIColor.init(hex: "4a4a4a") } private struct StringConstants { let payoutModeText = "payout_mode_text".localized() let amountDetailText = "amount_detail_text".localized() let verificationDetailText = "verification_detail_text".localized() } // MARK: IBOutlets // Container view holds the child view controllers // There are three child view controllers. // 1. SendMoneyPaymentModeViewController // 2. SendMoneyExchangeRateViewController // 3. SendMoneyVerificationViewController @IBOutlet weak var containerView: UIView! @IBOutlet weak var paymentModeButton: UIButton! @IBOutlet weak var exchangeRateButton: UIButton! @IBOutlet weak var verificationButton: UIButton! @IBOutlet weak var paymentModeLabel: UILabel! @IBOutlet weak var exchangeRateLabel: UILabel! @IBOutlet weak var verificationLabel: UILabel! // MARK: Properties var SendMoneyPaymentModeViewController: UIViewController? var SendMoneyExchangeRateViewController: UIViewController? var SendMoneyVerificationViewController: UIViewController? var requestModel: SendMoneyRequestModel? var presenter: SendMoneyParentModuleInterface? // Buttons representing the pages marked as 1, 2, 3 in UI. var stateButtons: [UIButton] = [] private var state: StateButtons = .paymentMode { didSet { self.updateState(state: state) } } var receipient: Recipient? var account: Account? { didSet { self.requestModel?.autoDebitAccount = account } } // MARK: VC's Life cycle override func viewDidLoad() { super.viewDidLoad() self.setup() self.presenter?.viewIsReady() self.setupViewControllers() self.addPaymentModeViewController() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) setTitle(title: "send_money_text".localized()) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationItem.title = "" } // MARK: IBActions @IBAction func paymentMode(_ sender: UIButton) { self.addPaymentModeViewController() self.state = StateButtons.paymentMode } @IBAction func exchangeRate(_ sender: UIButton) { self.addExchangeViewController() self.state = StateButtons.exchange } @IBAction func verification(_ sender: UIButton) { self.addVerificationViewController() self.state = StateButtons.verification } // MARK: Other Functions private func setup() { // all setup should be done here self.setTitle(title: "send_money_text".localized()) self.state = StateButtons.paymentMode self.configureViews() self.requestModel = SendMoneyRequestModel() configureLanguage() } private func configureLanguage() { self.paymentModeLabel.text = StringConstants().payoutModeText self.exchangeRateLabel.text = StringConstants().amountDetailText self.verificationLabel.text = StringConstants().verificationDetailText } private func setTitle(title: String) { self.navigationItem.title = title } private func setupViewControllers() { let paymentModelWireframe = SendMoneyPaymentModeWireframe() let pvc = paymentModelWireframe.getMainView() as! SendMoneyPaymentModeViewController pvc.recipient = self.receipient pvc.hudDelegate = self pvc.actionDelegate = self self.SendMoneyPaymentModeViewController = pvc let exchangeViewWireframe = SendMoneyExchangeRateWireframe() let evc = exchangeViewWireframe.getMainView() as! SendMoneyExchangeRateViewController evc.reciepient = self.receipient evc.actionDelegate = self evc.hudDelegate = self self.SendMoneyExchangeRateViewController = evc let verificationWireframe = SendMoneyVerificationWireframe() let vvc = verificationWireframe.getMainView() as! SendMoneyVerificationViewController vvc.reciepient = self.receipient vvc.requestModel = self.requestModel vvc.hudDelegate = self self.SendMoneyVerificationViewController = vvc } private func configureViews() { self.paymentModeButton.tag = 1 self.exchangeRateButton.tag = 2 self.verificationButton.tag = 3 self.stateButtons = [paymentModeButton,exchangeRateButton, verificationButton ] [paymentModeButton, exchangeRateButton, verificationButton].forEach({ $0?.rounded() }) } private func updateState(state: StateButtons) { switch state { case .paymentMode: // buttons self.paymentModeButton.backgroundColor = Constants.stateGreenColor self.exchangeRateButton.backgroundColor = Constants.stateRedColor self.verificationButton.backgroundColor = Constants.stateRedColor self.exchangeRateButton.isUserInteractionEnabled = false self.verificationButton.isUserInteractionEnabled = false // labels self.paymentModeLabel.textColor = Constants.stateGreenColor self.exchangeRateLabel.textColor = Constants.stateLabelGreyColor self.verificationLabel.textColor = Constants.stateLabelGreyColor self.requestModel?.bank = nil self.requestModel?.branch = nil case .exchange: // buttons self.paymentModeButton.backgroundColor = Constants.stateGreenColor self.exchangeRateButton.backgroundColor = Constants.stateGreenColor self.verificationButton.backgroundColor = Constants.stateRedColor self.exchangeRateButton.isUserInteractionEnabled = true self.verificationButton.isUserInteractionEnabled = false // labels self.paymentModeLabel.textColor = Constants.stateLabelGreyColor self.exchangeRateLabel.textColor = Constants.stateGreenColor self.verificationLabel.textColor = Constants.stateLabelGreyColor case .verification: // button self.paymentModeButton.backgroundColor = Constants.stateGreenColor self.exchangeRateButton.backgroundColor = Constants.stateGreenColor self.verificationButton.backgroundColor = Constants.stateGreenColor self.exchangeRateButton.isUserInteractionEnabled = true // labels self.paymentModeLabel.textColor = Constants.stateLabelGreyColor self.exchangeRateLabel.textColor = Constants.stateLabelGreyColor self.verificationLabel.textColor = Constants.stateLabelGreyColor } } // Make SendMoneyPaymentModeViewController as front View Controller private func addPaymentModeViewController() { guard let paymentModeViewController = self.SendMoneyPaymentModeViewController else {return} self.addChildViewController(paymentModeViewController) UIView.transition(with: self.containerView, duration: 0.33, options: .transitionCrossDissolve, animations: { self.containerView.addSubview(paymentModeViewController.view) }, completion: nil) paymentModeViewController.view.frame = containerView.bounds paymentModeViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] paymentModeViewController.didMove(toParentViewController: self) } // Make SendMoneyPaymentModeViewController as front View Controller private func addExchangeViewController() { guard let exchangeViewController = self.SendMoneyExchangeRateViewController as? SendMoneyExchangeRateViewController else {return} exchangeViewController.requestModel = self.requestModel exchangeViewController.actionDelegate = self self.addChildViewController(exchangeViewController) UIView.transition(with: self.containerView, duration: 0.33, options: .transitionCrossDissolve, animations: { self.containerView.addSubview(exchangeViewController.view) }, completion: nil) exchangeViewController.view.frame = containerView.bounds exchangeViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] exchangeViewController.didMove(toParentViewController: self) } // Make SendMoneyExchangeRateViewController as front View Controller private func addVerificationViewController() { guard let verificationViewController = self.SendMoneyVerificationViewController as? SendMoneyVerificationViewController else {return} verificationViewController.reciepient = self.receipient verificationViewController.requestModel = self.requestModel self.addChildViewController(verificationViewController) UIView.transition(with: self.containerView, duration: 0.33, options: .transitionCrossDissolve, animations: { self.containerView.addSubview(verificationViewController.view) }, completion: nil) verificationViewController.view.frame = containerView.bounds verificationViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] verificationViewController.didMove(toParentViewController: self) } } // MARK: SendMoneyParentViewInterface extension SendMoneyParentViewController: SendMoneyParentViewInterface { func show(model: Recipient) { self.receipient = model } func show(model: Account) { self.account = model } } extension SendMoneyParentViewController: HUDStatusDelegate { func showLoading() { self.showProgressHud() } func hideLoading() { self.hideProgressHud() } } extension SendMoneyParentViewController: SendMoneyPaymentModeActionDelegate { func selected(bank: SendMoneyBank?) { self.requestModel?.bank = bank } func selected(branch: SendMoneyBankBranch?) { self.requestModel?.branch = branch } func selected(payoutMethod: SendMoneyPayoutMode?) { self.requestModel?.paymemtMode = payoutMethod } func continueToExchangeAction() { self.addExchangeViewController() self.state = StateButtons.exchange } func added(acountNumber: String) { self.requestModel?.accountNumber = acountNumber } } extension SendMoneyParentViewController: SendMoneyExchangeRateActionDelegate { func continueToVerificationAction() { self.addVerificationViewController() self.state = StateButtons.verification } func calculated(model: SendMoneyExchangeRateModel?) { self.requestModel?.exchangeRateDetail = model } }