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.
438 lines
17 KiB
438 lines
17 KiB
//
|
|
// SendMoneyPaymentModeViewController.swift
|
|
// GMERemittance
|
|
//
|
|
// Created by gme_2 on 28/08/2018.
|
|
//Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SendMoneyPaymentModeViewController: UIViewController {
|
|
|
|
struct Constants {
|
|
static let validationBankKey = "Bank"
|
|
static let validationBranchKey = "Branch"
|
|
static let validationAccountKey = "Account Number"
|
|
static let validationErrorMessageBank = "Please select a bank"
|
|
static let validationErrorMessageBranch = "Please select a branch"
|
|
static let validationErrorMessageAccountNumber = "Please provide an account no"
|
|
}
|
|
|
|
// MARK: IBOutlets
|
|
|
|
@IBOutlet weak var collectionView: UICollectionView!
|
|
@IBOutlet weak var titleLabel: UILabel!
|
|
|
|
@IBOutlet weak var bankStackView: UIStackView!
|
|
@IBOutlet weak var branchStackView: UIStackView!
|
|
@IBOutlet weak var countinueButton: UIButton!
|
|
@IBOutlet weak var accountNumberStackView: UIStackView!
|
|
@IBOutlet weak var accountTextField: UITextField!
|
|
|
|
@IBOutlet weak var bankStackViewConstraint: NSLayoutConstraint!
|
|
@IBOutlet weak var branchStackViewConstraint: NSLayoutConstraint!
|
|
@IBOutlet weak var accountNumberStackViewConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var branchTextField: UITextField!
|
|
@IBOutlet weak var bankTextField: UITextField!
|
|
// MARK: Properties
|
|
|
|
var presenter: SendMoneyPaymentModeModuleInterface?
|
|
var recipient: Recipient?
|
|
var hudDelegate: HUDStatusDelegate?
|
|
var actionDelegate: SendMoneyPaymentModeActionDelegate?
|
|
|
|
var payoutMode: [SendMoneyPayoutMode]? {
|
|
didSet {
|
|
self.collectionView.reloadData()
|
|
self.selectedPaymentIndex = IndexPath.init(row: 0, section: 0)
|
|
}
|
|
}
|
|
|
|
var selectedPaymentIndex: IndexPath = IndexPath.init(row: 0, section: 0) {
|
|
didSet {
|
|
self.selectedPayoutMode = self.payoutMode?.elementAt(index: self.selectedPaymentIndex.row)
|
|
}
|
|
}
|
|
var selectedPayoutMode: SendMoneyPayoutMode? {
|
|
didSet {
|
|
self.accountTextField.text = ""
|
|
self.bankTextField.text = ""
|
|
self.branchTextField.text = ""
|
|
|
|
// self.bankStackView.isHidden = true
|
|
self.bankStackViewConstraint.constant = 0
|
|
// self.branchStackView.isHidden = true
|
|
self.branchStackViewConstraint.constant = 0
|
|
// self.accountNumberStackView.isHidden = true
|
|
self.accountNumberStackViewConstraint.constant = 0
|
|
|
|
self.bankStackView.alpha = 0
|
|
self.branchStackView.alpha = 0
|
|
self.accountNumberStackView.alpha = 0
|
|
|
|
if (self.selectedPayoutMode?.bankRequired ?? "false").lowercased() == "true" {
|
|
// self.bankStackView.isHidden = false
|
|
UIView.animate(withDuration: 0.33) {
|
|
self.bankStackViewConstraint.constant = 44
|
|
self.bankStackView.alpha = 1
|
|
}
|
|
}else {
|
|
|
|
}
|
|
collectionView.reloadData()
|
|
}
|
|
}
|
|
|
|
var selectedBank: SendMoneyBank? {
|
|
didSet {
|
|
self.bankTextField.text = selectedBank?.name
|
|
|
|
let shouldShowBranch = (self.selectedBank?.branchRequired ?? "false").lowercased() == "true"
|
|
// self.branchStackView.isHidden = !shouldShowBranch
|
|
if shouldShowBranch {
|
|
UIView.animate(withDuration: 0.33) {
|
|
self.branchStackViewConstraint.constant = 44
|
|
self.branchStackView.alpha = 1
|
|
}
|
|
}else {
|
|
UIView.animate(withDuration: 0.33) {
|
|
self.branchStackViewConstraint.constant = 0
|
|
self.branchStackView.alpha = 0
|
|
}
|
|
}
|
|
|
|
|
|
let shouldShowAccountNumber = (self.selectedBank?.accountRequired ?? "false").lowercased() == "true"
|
|
if shouldShowAccountNumber {
|
|
UIView.animate(withDuration: 0.33) {
|
|
self.accountNumberStackViewConstraint.constant = 44
|
|
self.accountNumberStackView.alpha = 1
|
|
}
|
|
}else {
|
|
UIView.animate(withDuration: 0.33) {
|
|
self.accountNumberStackViewConstraint.constant = 0
|
|
self.accountNumberStackView.alpha = 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var selectedBranch: SendMoneyBankBranch? {
|
|
didSet {
|
|
self.branchTextField.text = selectedBranch?.name
|
|
}
|
|
}
|
|
// MARK: VC's Life cycle
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
self.setup()
|
|
if let reciepient = self.recipient {
|
|
self.presenter?.viewIsReady(for: reciepient)
|
|
}
|
|
}
|
|
|
|
// MARK: IBActions
|
|
|
|
@IBAction func _continue(_ sender: UIButton) {
|
|
// these validation should be in interactor, but since it is only empty validation and the time constraint is very tight, I am putting these in here for now.
|
|
// if bank validate bank, if branch validate branch if account validate account
|
|
|
|
var shouldGoNextPage = true
|
|
var callingApi = false
|
|
var errors = ""
|
|
if self.selectedPayoutMode == nil {
|
|
self.show(error: "please select Payment mode")
|
|
return
|
|
}
|
|
|
|
if shouldValidateBank() {
|
|
let bank = bankTextField.text!
|
|
if bank.isEmpty {
|
|
shouldGoNextPage = false
|
|
errors = errors + " " + Constants.validationErrorMessageBank
|
|
}else {
|
|
if shouldValidateBranch() {
|
|
let branch = self.branchTextField.text!
|
|
if branch.isEmpty {
|
|
shouldGoNextPage = false
|
|
errors = errors + " " + Constants.validationErrorMessageBranch
|
|
}
|
|
}
|
|
|
|
let shouldShowAccountNumber = (self.selectedBank?.accountRequired ?? "false").lowercased() == "true"
|
|
|
|
if shouldShowAccountNumber {
|
|
let accountNumber = self.accountTextField.text!
|
|
if accountNumber.isEmpty {
|
|
shouldGoNextPage = false
|
|
errors = errors + " " + Constants.validationErrorMessageAccountNumber
|
|
}
|
|
}
|
|
|
|
if shouldValidateAccount() {
|
|
let accountNumber = self.accountTextField.text!
|
|
if accountNumber.isEmpty {
|
|
shouldGoNextPage = false
|
|
errors = errors + " " + Constants.validationErrorMessageAccountNumber
|
|
}else {
|
|
let validationRequest = SendMoneyAccountValidationRequestModel()
|
|
validationRequest.idType = "1"
|
|
validationRequest.idNumber = ""
|
|
validationRequest.customerFirstName = ""
|
|
|
|
validationRequest.customerLastName = ""
|
|
validationRequest.reciverFirstName = self.recipient?.firstName
|
|
validationRequest.reciverLastName = self.recipient?.lastName
|
|
validationRequest.country = self.recipient?.countryCode
|
|
validationRequest.accountType = "1" // saving account, current account type ko
|
|
validationRequest.issuerCode = self.selectedBank?.code
|
|
validationRequest.accountNo = self.accountTextField.text!
|
|
validationRequest.bankCode = self.selectedBank?.code
|
|
validationRequest.payoutPartner = self.selectedPayoutMode?.payoutPartner
|
|
validationRequest.amount = "10000.0"
|
|
validationRequest.processId = ""
|
|
callingApi = true
|
|
self.presenter?.validate(model: validationRequest)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if !callingApi {
|
|
if shouldGoNextPage {
|
|
proceedToNextPage()
|
|
}else {
|
|
self.show(error: errors)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Other Functions
|
|
|
|
private func setup() {
|
|
// all setup should be done here
|
|
setupDelegates()
|
|
configureViews()
|
|
configureTitle()
|
|
}
|
|
|
|
|
|
private func configureViews() {
|
|
self.selectedPaymentIndex = IndexPath.init(row: 0, section: 0)
|
|
self.view.layer.cornerRadius = 8
|
|
self.countinueButton.rounded()
|
|
DispatchQueue.main.async {
|
|
self.bankStackView.alpha = 0
|
|
self.branchStackView.alpha = 0
|
|
self.accountNumberStackView.alpha = 0
|
|
self.bankStackViewConstraint.constant = 0
|
|
self.branchStackViewConstraint.constant = 0
|
|
self.accountNumberStackViewConstraint.constant = 0
|
|
// self.bankStackView.isHidden = true
|
|
// self.branchStackView.isHidden = true
|
|
// self.accountNumberStackView.isHidden = true
|
|
}
|
|
}
|
|
|
|
private func configureTitle() {
|
|
if let country = self.recipient?.country {
|
|
self.titleLabel.text = "How would you like to pick your money in \(country)?"
|
|
}
|
|
}
|
|
|
|
|
|
private func goToExchangeView() {
|
|
self.actionDelegate?.continueToExchangeAction()
|
|
}
|
|
|
|
private func shouldValidateAccount() -> Bool {
|
|
// return true
|
|
return (self.selectedBank?.accountValidationRequired ?? "false").lowercased() == "true"
|
|
}
|
|
|
|
private func shouldValidateBank() -> Bool {
|
|
return (self.selectedPayoutMode?.bankRequired ?? "false").lowercased() == "true"
|
|
}
|
|
|
|
private func shouldValidateBranch() -> Bool {
|
|
return (self.selectedBank?.branchRequired ?? "false").lowercased() == "true"
|
|
}
|
|
|
|
private func setupDelegates() {
|
|
self.bankTextField.delegate = self
|
|
self.branchTextField.delegate = self
|
|
self.collectionView.delegate = self
|
|
self.collectionView.dataSource = self
|
|
}
|
|
|
|
private func showBankPickerView() {
|
|
let viewcontroller = self.getPickerViewController()
|
|
let banks = self.selectedPayoutMode?.bankList ?? []
|
|
viewcontroller.data = banks
|
|
viewcontroller.type = PickerTitle.bank
|
|
viewcontroller.doneAction = self.bankSelected
|
|
self.present(viewcontroller, animated: true, completion: nil)
|
|
}
|
|
|
|
private func showBranchPickerView() {
|
|
let viewcontroller = self.getBankBranchPickerViewController()
|
|
// let branches = self.selectedBank?.branches ?? []
|
|
// viewcontroller.data = branches
|
|
viewcontroller.type = PickerTitle.branch
|
|
viewcontroller.countryCode = self.recipient?.countryCode ?? ""
|
|
viewcontroller.bankId = self.selectedBank?.id ?? ""
|
|
viewcontroller.doneAction = self.branchSelected
|
|
viewcontroller.defaultSelectedData = [self.selectedBranch]
|
|
self.present(viewcontroller, animated: true, completion: nil)
|
|
}
|
|
|
|
func branchSelected(branches: [SendMoneyBankBranch?]) {
|
|
if let data = branches.first {
|
|
self.selectedBranch = data
|
|
}
|
|
}
|
|
|
|
func bankSelected(banks: [SendMoneyBank?]) {
|
|
if let data = banks.first {
|
|
self.selectedBank = data
|
|
}
|
|
}
|
|
|
|
private func proceedToNextPage() {
|
|
self.selectedPayoutMode?.accountNumber = self.accountTextField.text
|
|
self.actionDelegate?.selected(payoutMethod: self.selectedPayoutMode)
|
|
|
|
if shouldValidateBank() { self.actionDelegate?.selected(bank: self.selectedBank) }
|
|
|
|
if shouldValidateBranch() { self.actionDelegate?.selected(branch: self.selectedBranch) }
|
|
|
|
if shouldValidateAccount() { self.actionDelegate?.added(acountNumber: accountTextField.text!) }
|
|
|
|
self.goToExchangeView()
|
|
}
|
|
|
|
private func getPickerViewController()-> BankPickerViewController {
|
|
return UIStoryboard.init(name: "BankPicker", bundle: nil).instantiateViewController(withIdentifier: "BankPickerViewController") as! BankPickerViewController
|
|
}
|
|
|
|
private func getBankBranchPickerViewController()-> BankBranchPickerViewController {
|
|
return UIStoryboard.init(name: "BankBranchPicker", bundle: nil).instantiateViewController(withIdentifier: "BankBranchPickerViewController") as! BankBranchPickerViewController
|
|
}
|
|
}
|
|
|
|
extension SendMoneyPaymentModeViewController: UICollectionViewDelegate {
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
if selectedPaymentIndex != indexPath {
|
|
self.selectedPaymentIndex = indexPath
|
|
self.selectedPayoutMode = self.payoutMode?.elementAt(index: indexPath.row)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SendMoneyPaymentModeViewController: UICollectionViewDataSource {
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return self.payoutMode?.count ?? 0
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let paymentMode = self.payoutMode?.elementAt(index: indexPath.row)
|
|
guard let index = PaymentMode.init(rawValue: paymentMode?.id ?? "") else {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SendMoenyPaymentModeCollectionViewCell", for: indexPath) as! SendMoenyPaymentModeCollectionViewCell
|
|
cell.image = #imageLiteral(resourceName: "ic_cash")
|
|
cell.cellSelected = indexPath == self.selectedPaymentIndex
|
|
cell.setup()
|
|
return cell
|
|
|
|
}
|
|
switch index {
|
|
case .bankDeposite:
|
|
return configureBankDepositeCell(collectionView: collectionView, indexPath: indexPath)
|
|
case .cashDelivery:
|
|
return configureCashDeliveryCell(collectionView: collectionView, indexPath: indexPath)
|
|
case .homeDelivery:
|
|
return configureHomeDeliveryCell(collectionView: collectionView, indexPath: indexPath)
|
|
case .mobileWallet:
|
|
return configureWalletDeliveryCell(collectionView: collectionView, indexPath: indexPath)
|
|
}
|
|
|
|
}
|
|
|
|
func configureBankDepositeCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SendMoenyPaymentModeCollectionViewCell", for: indexPath) as! SendMoenyPaymentModeCollectionViewCell
|
|
cell.cellSelected = self.selectedPaymentIndex == indexPath
|
|
cell.paymentServiceMethod = self.payoutMode?.elementAt(index: indexPath.row)
|
|
cell.image = #imageLiteral(resourceName: "ic_bank")
|
|
cell.setup()
|
|
return cell
|
|
}
|
|
|
|
func configureWalletDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SendMoenyPaymentModeCollectionViewCell", for: indexPath) as! SendMoenyPaymentModeCollectionViewCell
|
|
cell.cellSelected = self.selectedPaymentIndex == indexPath
|
|
cell.paymentServiceMethod = self.payoutMode?.elementAt(index: indexPath.row)
|
|
cell.image = #imageLiteral(resourceName: "wallet-transfer")
|
|
cell.setup()
|
|
return cell
|
|
}
|
|
|
|
func configureCashDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SendMoenyPaymentModeCollectionViewCell", for: indexPath) as! SendMoenyPaymentModeCollectionViewCell
|
|
cell.cellSelected = self.selectedPaymentIndex == indexPath
|
|
cell.paymentServiceMethod = self.payoutMode?.elementAt(index: indexPath.row)
|
|
cell.image = #imageLiteral(resourceName: "ic_cash")
|
|
cell.setup()
|
|
return cell
|
|
}
|
|
|
|
func configureHomeDeliveryCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SendMoenyPaymentModeCollectionViewCell", for: indexPath) as! SendMoenyPaymentModeCollectionViewCell
|
|
cell.cellSelected = self.selectedPaymentIndex == indexPath
|
|
cell.paymentServiceMethod = self.payoutMode?.elementAt(index: indexPath.row)
|
|
cell.image = #imageLiteral(resourceName: "ic_homeDelivery")
|
|
cell.setup()
|
|
return cell
|
|
}
|
|
}
|
|
|
|
// MARK: SendMoneyPaymentModeViewInterface
|
|
extension SendMoneyPaymentModeViewController: SendMoneyPaymentModeViewInterface {
|
|
|
|
func success() {
|
|
self.proceedToNextPage()
|
|
}
|
|
|
|
func show(model: [SendMoneyPayoutMode]) {
|
|
self.payoutMode = model
|
|
}
|
|
|
|
func show(error: String) {
|
|
self.alert(message: error)
|
|
}
|
|
|
|
func showLoading() {
|
|
self.hudDelegate?.showLoading()
|
|
}
|
|
|
|
func hideLoading() {
|
|
self.hudDelegate?.hideLoading()
|
|
}
|
|
}
|
|
|
|
extension SendMoneyPaymentModeViewController: UITextFieldDelegate {
|
|
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
|
|
switch textField {
|
|
case bankTextField:
|
|
self.showBankPickerView()
|
|
return false
|
|
case branchTextField:
|
|
self.showBranchPickerView()
|
|
return false
|
|
default:
|
|
return true
|
|
}
|
|
}
|
|
}
|