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.
 
 
 
 

471 lines
19 KiB

//
// SendMoneyPaymentModeViewController.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
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"
}
struct StringConstants {
let headerTitle = "pick_money_text".localized()
let bankPlaceholder = "select_bank_text".localized()
let branchPlaceholder = "select_branch_text".localized()
let accountNumber = "account_number_text".localized()
let continueText = "continue_text".localized()
}
// 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.bankStackViewConstraint.constant = 0
self.branchStackViewConstraint.constant = 0
self.accountNumberStackViewConstraint.constant = 0
self.bankStackView.alpha = 0
self.branchStackView.alpha = 0
self.accountNumberStackView.alpha = 0
if (self.selectedPayoutMode?.bankRequired ?? "false").lowercased() == "true" {
UIView.animate(withDuration: 0.33) {
self.bankStackViewConstraint.constant = 44
self.bankStackView.alpha = 1
}
}else {
self.selectedBranch = nil
self.selectedBank = nil
}
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)
}
configureTitle()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "send_money_title_text".localized()
}
func configureLanguage() {
self.titleLabel.text = "pick_money_text".localized()
self.bankTextField.placeholder = StringConstants().bankPlaceholder
self.branchTextField.placeholder = StringConstants().branchPlaceholder
self.accountTextField.placeholder = StringConstants().accountNumber
self.countinueButton.setTitle(StringConstants().continueText, for: UIControlState.normal)
}
// 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: "payment_mode_empty_error".localized())
return
}
if shouldValidateBank() {
let bank = bankTextField.text!
if bank.isEmpty {
shouldGoNextPage = false
errors = errors + " " + "bank_empty_error".localized()
}else {
if shouldValidateBranch() {
let branch = self.branchTextField.text!
if branch.isEmpty {
shouldGoNextPage = false
errors = errors + " " + "branch_empty_error".localized()
}
}
let shouldShowAccountNumber = (self.selectedBank?.accountRequired ?? "false").lowercased() == "true"
if shouldShowAccountNumber {
let accountNumber = self.accountTextField.text!
if accountNumber.isEmpty {
shouldGoNextPage = false
errors = errors + " " + "account_number_empty_error".localized()
}
}
if shouldValidateAccount() {
let accountNumber = self.accountTextField.text!
if accountNumber.isEmpty {
shouldGoNextPage = false
errors = errors + " " + "account_number_empty_error".localized()
}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()
configureLanguage()
}
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
}
}
private func configureTitle() {
if let country = self.recipient?.country {
if Localize.currentLanguage() == "ne" {
self.titleLabel.text = "\(country) " + "pick_money_text".localized() + "?"
} else {
self.titleLabel.text = "pick_money_text".localized() + " \(country)?"
}
}
}
private func goToExchangeView() {
self.actionDelegate?.continueToExchangeAction()
}
private func shouldValidateAccount() -> Bool {
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.accountTextField.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()
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
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == accountTextField {
if string.isEmpty {
return true
}
let alphaNumericRegEx = "[a-zA-Z0-9]"
let predicate = NSPredicate(format:"SELF MATCHES %@", alphaNumericRegEx)
return predicate.evaluate(with: string)
}
return true
}
}