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.

271 lines
10 KiB

6 years ago
  1. //
  2. // ProfileBankViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 1/30/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. class ProfileBankViewController: UIViewController {
  11. var userinfoviewmodel: UserInfoViewModel?
  12. var alertController: UIAlertController!
  13. @IBOutlet weak var labelBankName: UILabel!
  14. @IBOutlet weak var labelAccountNumber: UILabel!
  15. @IBOutlet weak var labelVerificationIDType: UILabel!
  16. @IBOutlet weak var labelVerificationIDNumber: UILabel!
  17. @IBOutlet weak var labelExpiryDate: UILabel!
  18. @IBOutlet weak var labelSourceIncome: UILabel!
  19. let pickerView = UIPickerView()
  20. var pickerData: [String] = [String]()
  21. var editTextField: UITextField!
  22. private var selectedFundSouceIndex: Int!
  23. private var selectedFundSource: String!
  24. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  25. public static var profileConnectionTimeOutCheck = 0
  26. @IBAction func goBack(_ sender: Any) {
  27. navigationController?.popViewController(animated: true)
  28. }
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. setUpNavBar(id: 201, title: "Profile")
  32. guard userinfoviewmodel != nil else {
  33. self.popUpMessageInfo(value: 16, title: "No Data Available", message: "Data not received")
  34. return
  35. }
  36. setUpAnotherLoginListener(genericviewmodel: userinfoviewmodel!)
  37. userinfoviewmodel!.userDataAvailable.value = nil
  38. userinfoviewmodel!.userSourceFundAvailable.value = nil
  39. userinfoviewmodel?.internetConnection.value = nil
  40. userinfoviewmodel?.userConnectionTimeOut.value = nil
  41. /**
  42. connection timeout
  43. */
  44. userinfoviewmodel?.userConnectionTimeOut.bind { [unowned self] in
  45. guard $0 != nil else {
  46. return
  47. }
  48. self.enableUserInteractions()
  49. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  50. if ProfileBankViewController.profileConnectionTimeOutCheck == 0{
  51. ProfileBankViewController.profileConnectionTimeOutCheck = ProfileBankViewController.profileConnectionTimeOutCheck+1
  52. self.popUpMessage(value: 33)
  53. }
  54. }
  55. /**
  56. Internet check
  57. */
  58. userinfoviewmodel?.internetConnection.bind { [unowned self] in
  59. guard $0 != nil else {
  60. return
  61. }
  62. self.enableUserInteractions()
  63. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  64. self.popUpMessage(value: 15)
  65. }
  66. /**
  67. Update the view for Source fund
  68. */
  69. userinfoviewmodel!.userSourceFundAvailable.bind { [weak self] in
  70. guard $0 != nil else {
  71. if let controller = self?.alertController {
  72. controller.dismiss(animated: true, completion: nil)
  73. }
  74. return
  75. }
  76. guard $0! else {
  77. if ProfileBankViewController.profileConnectionTimeOutCheck == 0{
  78. ProfileBankViewController.profileConnectionTimeOutCheck = ProfileBankViewController.profileConnectionTimeOutCheck+1
  79. self?.popUpMessageError(value: 10, message: (self?.userinfoviewmodel?.getErrorMessage())!)
  80. }
  81. return
  82. }
  83. self?.setUpTextField()
  84. }
  85. /**
  86. Update the view for user detail
  87. */
  88. userinfoviewmodel!.userDataAvailable.bind { [unowned self] in
  89. guard $0 != nil else {
  90. return
  91. }
  92. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  93. self.enableUserInteractions()
  94. guard $0! else {
  95. if ProfileBankViewController.profileConnectionTimeOutCheck == 0{
  96. ProfileBankViewController.profileConnectionTimeOutCheck = ProfileBankViewController.profileConnectionTimeOutCheck+1
  97. self.popUpMessageError(value: 10, message: self.userinfoviewmodel!.getErrorMessage())
  98. }
  99. return
  100. }
  101. // self.labelSourceIncome.attributedText = self.getAttributedLabel(inputLabel: self.updatedString)
  102. self.labelSourceIncome.attributedText = self.getAttributedLabel(inputLabel: self.selectedFundSource)
  103. }
  104. labelBankName.text = userinfoviewmodel!.getInfo().primaryBankName
  105. labelAccountNumber.text = userinfoviewmodel!.getInfo().primaryAccountNumber
  106. labelVerificationIDType.text = userinfoviewmodel!.getInfo().verificationIdType
  107. labelVerificationIDNumber.text = userinfoviewmodel!.getInfo().verificationIdNumber
  108. labelExpiryDate.text = unixTimeStampToDate(unixTimeStamp: userinfoviewmodel!.getInfo().expiryDate)
  109. selectedFundSource = userinfoviewmodel!.getInfo().sourceOfFund
  110. labelSourceIncome.attributedText = getAttributedLabel(inputLabel: selectedFundSource)
  111. labelSourceIncome.isUserInteractionEnabled = true
  112. labelSourceIncome.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
  113. }
  114. /**
  115. To set data in textfield which is selected from picker view
  116. */
  117. func setUpTextField() {
  118. if self.userinfoviewmodel!.getSourceFundList().count > 0 {
  119. self.pickerData = self.userinfoviewmodel!.getSourceFundList()
  120. selectedFundSouceIndex = pickerData.index(of: selectedFundSource)
  121. self.editTextField.text = self.pickerData[selectedFundSouceIndex]
  122. self.pickerView.dataSource = self
  123. self.pickerView.delegate = self
  124. pickerView.selectRow(selectedFundSouceIndex, inComponent: 0, animated: true)
  125. self.editTextField.becomeFirstResponder()
  126. }
  127. }
  128. /**
  129. To dispaly image and Text in single label
  130. - parameter String : sourcr of fund
  131. - returns: NSMutableAttributedString
  132. */
  133. func getAttributedLabel(inputLabel: String) -> NSMutableAttributedString {
  134. let fullString: NSMutableAttributedString = NSMutableAttributedString(string: "")
  135. // create our NSTextAttachment
  136. let imageAttachment = NSTextAttachment()
  137. imageAttachment.image = UIImage(named: "ic_profile_change_edit")
  138. // wrap the attachment in its own attributed string so we can append it
  139. let imageString = NSAttributedString(attachment: imageAttachment)
  140. // add the NSTextAttachment wrapper to our full string, then add some more text.
  141. fullString.append(imageString)
  142. fullString.append(NSAttributedString(string: " "))
  143. fullString.append(NSAttributedString(string: inputLabel))
  144. return fullString
  145. }
  146. /**
  147. To edit user Source of Fund.
  148. */
  149. @objc func editDetail(tapGestureRecognizer: UITapGestureRecognizer) {
  150. let title: String = "Source of Fund"
  151. alertController = UIAlertController(title: "Edit " + title, message: nil, preferredStyle: .alert)
  152. alertController.addTextField { (textField : UITextField!) -> Void in
  153. textField.placeholder = "New value"
  154. if Reachability.isConnectedToNetwork() == true{
  155. textField.inputView = self.pickerView
  156. self.editTextField = textField
  157. self.editTextField.delegate = self
  158. if self.userinfoviewmodel!.getSourceFundList().count > 0 {
  159. self.setUpTextField()
  160. } else {
  161. self.userinfoviewmodel!.fetchCDDList(cddName: cddCode.SourceFund.rawValue, param: nil)
  162. }
  163. }
  164. }
  165. let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: {
  166. alert -> Void in
  167. let valueTextField = self.alertController.textFields![0] as UITextField
  168. if valueTextField.text! != "" {
  169. self.selectedFundSource = valueTextField.text!
  170. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  171. self.disableUserInteractions()
  172. self.userinfoviewmodel!.updateUserInfo(param: ["sourceOfFund": self.userinfoviewmodel!.getSourceFundID(index: self.selectedFundSouceIndex)])
  173. } else {
  174. self.popUpMessageError(value: 10, message: "No data provided")
  175. }
  176. })
  177. let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
  178. (action : UIAlertAction!) -> Void in
  179. })
  180. cancelAction.setValue(UIColor.black, forKey: "titleTextColor")
  181. confirmAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor")
  182. alertController.addAction(cancelAction)
  183. alertController.addAction(confirmAction)
  184. if Reachability.isConnectedToNetwork() == true{
  185. self.present(alertController, animated: true, completion: nil)
  186. }else{
  187. popUpMessage(value: 15)
  188. }
  189. }
  190. }
  191. extension ProfileBankViewController: UIPickerViewDataSource, UIPickerViewDelegate {
  192. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  193. return 1
  194. }
  195. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  196. return pickerData.count
  197. }
  198. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  199. return pickerData[row]
  200. }
  201. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  202. editTextField!.text = pickerData[row]
  203. selectedFundSouceIndex = row
  204. }
  205. }
  206. extension ProfileBankViewController: UITextFieldDelegate {
  207. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  208. return false
  209. }
  210. }