// // ProfileBankViewController.swift // GMERemittance // // Created by Fm-user on 1/30/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import UIKit class ProfileBankViewController: UIViewController { var userinfoviewmodel: UserInfoViewModel? var alertController: UIAlertController! @IBOutlet weak var labelBankName: UILabel! @IBOutlet weak var labelAccountNumber: UILabel! @IBOutlet weak var labelVerificationIDType: UILabel! @IBOutlet weak var labelVerificationIDNumber: UILabel! @IBOutlet weak var labelExpiryDate: UILabel! @IBOutlet weak var labelSourceIncome: UILabel! let pickerView = UIPickerView() var pickerData: [String] = [String]() var editTextField: UITextField! private var selectedFundSouceIndex: Int! private var selectedFundSource: String! private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() public static var profileConnectionTimeOutCheck = 0 @IBAction func goBack(_ sender: Any) { navigationController?.popViewController(animated: true) } override func viewDidLoad() { super.viewDidLoad() setUpNavBar(id: 201, title: "Profile") guard userinfoviewmodel != nil else { self.popUpMessageInfo(value: 16, title: "No Data Available", message: "Data not received") return } setUpAnotherLoginListener(genericviewmodel: userinfoviewmodel!) userinfoviewmodel!.userDataAvailable.value = nil userinfoviewmodel!.userSourceFundAvailable.value = nil userinfoviewmodel?.internetConnection.value = nil userinfoviewmodel?.userConnectionTimeOut.value = nil /** connection timeout */ userinfoviewmodel?.userConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) if ProfileBankViewController.profileConnectionTimeOutCheck == 0{ ProfileBankViewController.profileConnectionTimeOutCheck = ProfileBankViewController.profileConnectionTimeOutCheck+1 self.popUpMessage(value: 33) } } /** Internet check */ userinfoviewmodel?.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) } /** Update the view for Source fund */ userinfoviewmodel!.userSourceFundAvailable.bind { [weak self] in guard $0 != nil else { if let controller = self?.alertController { controller.dismiss(animated: true, completion: nil) } return } guard $0! else { if ProfileBankViewController.profileConnectionTimeOutCheck == 0{ ProfileBankViewController.profileConnectionTimeOutCheck = ProfileBankViewController.profileConnectionTimeOutCheck+1 self?.popUpMessageError(value: 10, message: (self?.userinfoviewmodel?.getErrorMessage())!) } return } self?.setUpTextField() } /** Update the view for user detail */ userinfoviewmodel!.userDataAvailable.bind { [unowned self] in guard $0 != nil else { return } self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() guard $0! else { if ProfileBankViewController.profileConnectionTimeOutCheck == 0{ ProfileBankViewController.profileConnectionTimeOutCheck = ProfileBankViewController.profileConnectionTimeOutCheck+1 self.popUpMessageError(value: 10, message: self.userinfoviewmodel!.getErrorMessage()) } return } // self.labelSourceIncome.attributedText = self.getAttributedLabel(inputLabel: self.updatedString) self.labelSourceIncome.attributedText = self.getAttributedLabel(inputLabel: self.selectedFundSource) } labelBankName.text = userinfoviewmodel!.getInfo().primaryBankName labelAccountNumber.text = userinfoviewmodel!.getInfo().primaryAccountNumber labelVerificationIDType.text = userinfoviewmodel!.getInfo().verificationIdType labelVerificationIDNumber.text = userinfoviewmodel!.getInfo().verificationIdNumber labelExpiryDate.text = unixTimeStampToDate(unixTimeStamp: userinfoviewmodel!.getInfo().expiryDate) selectedFundSource = userinfoviewmodel!.getInfo().sourceOfFund labelSourceIncome.attributedText = getAttributedLabel(inputLabel: selectedFundSource) labelSourceIncome.isUserInteractionEnabled = true labelSourceIncome.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:)))) } /** To set data in textfield which is selected from picker view */ func setUpTextField() { if self.userinfoviewmodel!.getSourceFundList().count > 0 { self.pickerData = self.userinfoviewmodel!.getSourceFundList() selectedFundSouceIndex = pickerData.index(of: selectedFundSource) self.editTextField.text = self.pickerData[selectedFundSouceIndex] self.pickerView.dataSource = self self.pickerView.delegate = self pickerView.selectRow(selectedFundSouceIndex, inComponent: 0, animated: true) self.editTextField.becomeFirstResponder() } } /** To dispaly image and Text in single label - parameter String : sourcr of fund - returns: NSMutableAttributedString */ func getAttributedLabel(inputLabel: String) -> NSMutableAttributedString { let fullString: NSMutableAttributedString = NSMutableAttributedString(string: "") // create our NSTextAttachment let imageAttachment = NSTextAttachment() imageAttachment.image = UIImage(named: "ic_profile_change_edit") // wrap the attachment in its own attributed string so we can append it let imageString = NSAttributedString(attachment: imageAttachment) // add the NSTextAttachment wrapper to our full string, then add some more text. fullString.append(imageString) fullString.append(NSAttributedString(string: " ")) fullString.append(NSAttributedString(string: inputLabel)) return fullString } /** To edit user Source of Fund. */ @objc func editDetail(tapGestureRecognizer: UITapGestureRecognizer) { let title: String = "Source of Fund" alertController = UIAlertController(title: "Edit " + title, message: nil, preferredStyle: .alert) alertController.addTextField { (textField : UITextField!) -> Void in textField.placeholder = "New value" if Reachability.isConnectedToNetwork() == true{ textField.inputView = self.pickerView self.editTextField = textField self.editTextField.delegate = self if self.userinfoviewmodel!.getSourceFundList().count > 0 { self.setUpTextField() } else { self.userinfoviewmodel!.fetchCDDList(cddName: cddCode.SourceFund.rawValue, param: nil) } } } let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: { alert -> Void in let valueTextField = self.alertController.textFields![0] as UITextField if valueTextField.text! != "" { self.selectedFundSource = valueTextField.text! self.showActivityIndicator(activityIndicator: self.activityIndicator) self.disableUserInteractions() self.userinfoviewmodel!.updateUserInfo(param: ["sourceOfFund": self.userinfoviewmodel!.getSourceFundID(index: self.selectedFundSouceIndex)]) } else { self.popUpMessageError(value: 10, message: "No data provided") } }) let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action : UIAlertAction!) -> Void in }) cancelAction.setValue(UIColor.black, forKey: "titleTextColor") confirmAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor") alertController.addAction(cancelAction) alertController.addAction(confirmAction) if Reachability.isConnectedToNetwork() == true{ self.present(alertController, animated: true, completion: nil) }else{ popUpMessage(value: 15) } } } extension ProfileBankViewController: UIPickerViewDataSource, UIPickerViewDelegate { func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerData.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerData[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { editTextField!.text = pickerData[row] selectedFundSouceIndex = row } } extension ProfileBankViewController: UITextFieldDelegate { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { return false } }