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.

435 lines
17 KiB

6 years ago
  1. //
  2. // ProfileEditViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 1/9/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class ProfileGeneralViewController: UIViewController {
  10. var userinfoviewmodel: UserInfoViewModel?
  11. var alertController: UIAlertController!
  12. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  13. @IBOutlet weak var labelFirstName: UILabel!
  14. @IBOutlet weak var labelMiddleName: UILabel!
  15. @IBOutlet weak var labelLastName: UILabel!
  16. @IBOutlet weak var labelNickName: UILabel!
  17. @IBOutlet weak var labelGender: UILabel!
  18. @IBOutlet weak var labelDOB: UILabel!
  19. @IBOutlet weak var labelMobile: UILabel!
  20. @IBOutlet weak var labelEmail: UILabel!
  21. @IBOutlet weak var labelCountry: UILabel!
  22. @IBOutlet weak var labelCountryNative: UILabel!
  23. @IBOutlet weak var labelProvince: UILabel!
  24. @IBOutlet weak var labelOccupation: UILabel!
  25. private var indexTag: Int = 0
  26. private var selectedOccupationIndex: Int = 0
  27. private var selectedProvinceIndex: Int = 0
  28. private var updatedString: String = ""
  29. private var selectedOccupation: String!
  30. private var selectedProvince: String!
  31. let pickerView = UIPickerView()
  32. var pickerData: [String] = [String]()
  33. var editTextField: UITextField!
  34. public static var profileGeneralConnectionTimeOutCheck = 0
  35. @IBAction func goBack(_ sender: Any) {
  36. navigationController?.popViewController(animated: true)
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. setUpNavBar(id: 201, title: "Profile")
  41. guard userinfoviewmodel != nil else {
  42. self.popUpMessageInfo(value: 16, title: "No Data Available", message: "Data not received")
  43. return
  44. }
  45. setUpAnotherLoginListener(genericviewmodel: userinfoviewmodel!)
  46. userinfoviewmodel!.userDataAvailable.value = nil
  47. userinfoviewmodel!.userOccupationProvinceAvailable.value = nil
  48. userinfoviewmodel?.internetConnection.value = nil
  49. userinfoviewmodel?.userConnectionTimeOut.value = nil
  50. /**
  51. connection timeout
  52. */
  53. userinfoviewmodel?.userConnectionTimeOut.bind { [unowned self] in
  54. guard $0 != nil else {
  55. return
  56. }
  57. self.enableUserInteractions()
  58. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  59. if ProfileGeneralViewController.profileGeneralConnectionTimeOutCheck == 0{
  60. ProfileGeneralViewController.profileGeneralConnectionTimeOutCheck = ProfileGeneralViewController.profileGeneralConnectionTimeOutCheck+1
  61. self.popUpMessage(value: 34)
  62. }
  63. }
  64. /**
  65. Internet check
  66. */
  67. userinfoviewmodel?.internetConnection.bind { [unowned self] in
  68. guard $0 != nil else {
  69. return
  70. }
  71. self.enableUserInteractions()
  72. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  73. self.popUpMessage(value: 15)
  74. }
  75. /**
  76. Update the view for Occupation and Province
  77. */
  78. userinfoviewmodel!.userOccupationProvinceAvailable.bind { [unowned self] in
  79. guard $0 != nil else {
  80. return
  81. }
  82. if $0 == 0 {
  83. self.popUpMessageError(value: 10, message: (self.userinfoviewmodel?.getErrorMessage())!)
  84. } else {
  85. if $0 == 3 {
  86. self.setUpTextField(category: 3)
  87. } else if $0 == 5 {
  88. self.setUpTextField(category: 5)
  89. } else if $0 == -1 {
  90. if self.alertController != nil {
  91. self.alertController.dismiss(animated: true, completion: nil)
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. Update the view for User data
  98. */
  99. userinfoviewmodel!.userDataAvailable.bind { [unowned self] in
  100. guard $0 != nil else {
  101. return
  102. }
  103. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  104. self.enableUserInteractions()
  105. guard $0! else {
  106. //self.popUpMessageError(value: 10, message: (self.userinfoviewmodel?.getErrorMessage())!)
  107. return
  108. }
  109. switch self.indexTag {
  110. case 1:
  111. self.labelNickName.attributedText = self.getAttributedLabel(inputLabel: self.updatedString)
  112. UserDefaults.standard.set(self.updatedString, forKey: "com.gmeremit.nickName") //Updating UserDefaults so that when the user goes back to HomeScreen, the changes are reflected
  113. case 3:
  114. self.labelProvince.attributedText = self.getAttributedLabel(inputLabel: self.pickerData[self.selectedProvinceIndex])
  115. case 5:
  116. self.labelOccupation.attributedText = self.getAttributedLabel(inputLabel: self.pickerData[self.selectedOccupationIndex])
  117. default:
  118. return
  119. }
  120. }
  121. if let firstName = userinfoviewmodel!.getInfo().firstName {
  122. labelFirstName.text = firstName
  123. }
  124. if let lastName = userinfoviewmodel!.getInfo().lastName {
  125. labelLastName.text = lastName
  126. }
  127. if let middleName = userinfoviewmodel!.getInfo().middleName {
  128. labelMiddleName.text = middleName
  129. } else {
  130. labelMiddleName.text = "-"
  131. }
  132. if let gender = userinfoviewmodel!.getInfo().gender{
  133. labelGender.text = gender
  134. }
  135. if let dob = userinfoviewmodel!.getInfo().dateOfBirth {
  136. labelDOB.text = unixTimeStampToDate(unixTimeStamp:dob )
  137. }
  138. if let mobileNumber = userinfoviewmodel!.getInfo().mobileNumber {
  139. labelMobile.text = mobileNumber
  140. }
  141. if let email = userinfoviewmodel!.getInfo().email{
  142. labelEmail.text = email
  143. }
  144. if let nativeCountry = userinfoviewmodel!.getInfo().nativeCountry{
  145. labelCountryNative.text = nativeCountry
  146. }
  147. if let country = userinfoviewmodel!.getInfo().country{
  148. labelCountry.text = country
  149. }
  150. if let nickName = userinfoviewmodel!.getInfo().nickName {
  151. labelNickName.attributedText = getAttributedLabel(inputLabel: nickName)
  152. } else {
  153. labelNickName.attributedText = getAttributedLabel(inputLabel: "")
  154. }
  155. if let occupation = userinfoviewmodel!.getInfo().occupation{
  156. selectedOccupation = occupation
  157. labelOccupation.attributedText = getAttributedLabel(inputLabel: selectedOccupation)
  158. }
  159. if let province = userinfoviewmodel!.getInfo().province{
  160. selectedProvince = province
  161. labelProvince.attributedText = getAttributedLabel(inputLabel: selectedProvince)
  162. }
  163. labelNickName.isUserInteractionEnabled = true
  164. labelProvince.isUserInteractionEnabled = true
  165. labelOccupation.isUserInteractionEnabled = true
  166. labelNickName.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
  167. labelProvince.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
  168. labelOccupation.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
  169. }
  170. /**
  171. Picker view data list
  172. - parameter Integer:
  173. - parameter: 5 for Occupation list
  174. - parameter: 3 for Province list
  175. */
  176. func setUpTextField(category: Int) {
  177. if category == 5 {
  178. if let userinfoModelCount = self.userinfoviewmodel?.getOccupationList().count{
  179. if userinfoModelCount > 0{
  180. self.pickerData = (self.userinfoviewmodel?.getOccupationList())!
  181. if let selectedOccupationIndex = self.pickerData.index(of: selectedOccupation) {
  182. self.editTextField.text = self.pickerData[selectedOccupationIndex]
  183. self.pickerView.dataSource = self
  184. self.pickerView.delegate = self
  185. pickerView.selectRow(selectedOccupationIndex, inComponent: 0, animated: true)
  186. }
  187. }
  188. }
  189. } else if category == 3 {
  190. if let provinceCount = self.userinfoviewmodel?.getProvinceList().count {
  191. if provinceCount > 0 {
  192. self.pickerData = self.userinfoviewmodel!.getProvinceList()
  193. if let selectedProvinceIndex = self.pickerData.index(of: selectedProvince) {
  194. self.editTextField.text = self.pickerData[selectedProvinceIndex]
  195. self.pickerView.dataSource = self
  196. self.pickerView.delegate = self
  197. pickerView.selectRow(selectedProvinceIndex, inComponent: 0, animated: true)
  198. }
  199. }
  200. }
  201. }
  202. self.editTextField.becomeFirstResponder()
  203. }
  204. /**
  205. To dispaly image and Text in single label
  206. - parameter String : Occupation or Province
  207. - returns: NSMutableAttributedString
  208. */
  209. func getAttributedLabel(inputLabel: String) -> NSMutableAttributedString {
  210. let fullString: NSMutableAttributedString = NSMutableAttributedString(string: "")
  211. // create our NSTextAttachment
  212. let imageAttachment = NSTextAttachment()
  213. imageAttachment.image = UIImage(named: "ic_profile_change_edit")
  214. // wrap the attachment in its own attributed string so we can append it
  215. let imageString = NSAttributedString(attachment: imageAttachment)
  216. // add the NSTextAttachment wrapper to our full string, then add some more text.
  217. fullString.append(imageString)
  218. fullString.append(NSAttributedString(string: " "))
  219. fullString.append(NSAttributedString(string: inputLabel))
  220. return fullString
  221. }
  222. override func didReceiveMemoryWarning() {
  223. super.didReceiveMemoryWarning()
  224. }
  225. /**
  226. To edit user nick name, 0ccupation and province
  227. */
  228. @objc func editDetail(tapGestureRecognizer: UITapGestureRecognizer) {
  229. let tappedLabel:UILabel = (tapGestureRecognizer.view as! UILabel) // Type cast it with the class for which you have added gesture
  230. var title: String = ""
  231. switch tappedLabel.tag {
  232. case 1:
  233. indexTag = 1
  234. title = "Nick Name"
  235. case 3:
  236. indexTag = 3
  237. title = "Province"
  238. if self.userinfoviewmodel!.getProvinceList().count > 0 {
  239. self.pickerData = self.userinfoviewmodel!.getProvinceList()
  240. } else {
  241. pickerData = self.userinfoviewmodel!.getEmptyData()
  242. self.userinfoviewmodel!.fetchCDDList(cddName: cddCode.Province.rawValue, param: ["countryId": "118"])
  243. }
  244. case 5:
  245. indexTag = 5
  246. title = "Occupation"
  247. if Reachability.isConnectedToNetwork() == true{
  248. if self.userinfoviewmodel!.getOccupationList().count > 0 {
  249. self.pickerData = self.userinfoviewmodel!.getOccupationList()
  250. } else {
  251. pickerData = self.userinfoviewmodel!.getEmptyData()
  252. self.userinfoviewmodel!.fetchCDDList(cddName: cddCode.Occupation.rawValue, param: nil)
  253. }
  254. }
  255. default:
  256. title = ""
  257. }
  258. alertController = UIAlertController(title: "Edit " + title, message: nil, preferredStyle: .alert)
  259. alertController.addTextField { (textField : UITextField!) -> Void in
  260. textField.placeholder = "New value"
  261. if self.indexTag == 5 {
  262. if Reachability.isConnectedToNetwork() == true{
  263. textField.inputView = self.pickerView
  264. self.editTextField = textField
  265. self.editTextField.delegate = self
  266. if self.userinfoviewmodel!.getOccupationList().count > 0 {
  267. self.setUpTextField(category: self.indexTag)
  268. }
  269. }
  270. } else if self.indexTag == 3 {
  271. textField.inputView = self.pickerView
  272. self.editTextField = textField
  273. self.editTextField.delegate = self
  274. if self.userinfoviewmodel!.getProvinceList().count > 0 {
  275. self.setUpTextField(category: self.indexTag)
  276. }
  277. } else {
  278. textField.tag = 51 //unique identifier set
  279. textField.delegate = self
  280. }
  281. }
  282. let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: {
  283. alert -> Void in
  284. let valueTextField = self.alertController.textFields![0] as UITextField
  285. let trimmedString = valueTextField.text?.removeSpacesTrailingPreceding()
  286. if trimmedString != "" {
  287. self.updatedString = trimmedString!
  288. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  289. self.disableUserInteractions()
  290. switch tappedLabel.tag {
  291. case 1:
  292. self.userinfoviewmodel!.updateUserInfo(param: ["nickName": trimmedString!])
  293. case 3:
  294. self.userinfoviewmodel!.updateUserInfo(param: ["provinceId": self.userinfoviewmodel!.getProvinceID(index: self.selectedProvinceIndex)])
  295. case 5:
  296. self.userinfoviewmodel!.updateUserInfo(param: ["occupation": self.userinfoviewmodel!.getOccupationID(index: self.selectedOccupationIndex)])
  297. default:
  298. return
  299. }
  300. } else {
  301. self.popUpMessageError(value: 10, message: "No data provided")
  302. //self.present(self.prepareAlert(title: "No updates done", message: "No data supplied"), animated: true, completion: nil )
  303. }
  304. })
  305. let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
  306. (action : UIAlertAction!) -> Void in
  307. })
  308. cancelAction.setValue(UIColor.black, forKey: "titleTextColor")
  309. confirmAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor")
  310. alertController.addAction(cancelAction)
  311. alertController.addAction(confirmAction)
  312. if Reachability.isConnectedToNetwork() == true{
  313. self.present(alertController, animated: true, completion: nil)
  314. }else{
  315. popUpMessage(value: 15)
  316. }
  317. }
  318. }
  319. extension ProfileGeneralViewController: UIPickerViewDataSource, UIPickerViewDelegate {
  320. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  321. return 1
  322. }
  323. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  324. return pickerData.count
  325. }
  326. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  327. return pickerData[row]
  328. }
  329. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  330. editTextField!.text = pickerData[row]
  331. if indexTag == 5 {
  332. selectedOccupation = pickerData[row]
  333. selectedOccupationIndex = row
  334. } else if indexTag == 3 {
  335. selectedProvince = pickerData[row]
  336. selectedProvinceIndex = row
  337. }
  338. }
  339. }
  340. extension ProfileGeneralViewController: UITextFieldDelegate {
  341. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  342. if self.indexTag == 3 || self.indexTag == 5 {
  343. return false
  344. }
  345. return true
  346. }
  347. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  348. if textField.tag == 51 {
  349. textField.tag = 0
  350. return false
  351. }
  352. return true
  353. }
  354. }