UserDetailsViewController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //
  2. // UserDetailsViewController.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 03/09/21.
  6. //
  7. import UIKit
  8. import Toast_Swift
  9. import Alamofire
  10. class UserDetailsViewController: UIViewController {
  11. @IBOutlet var nameLabel: UILabel!
  12. @IBOutlet var nameTF: UITextField!
  13. @IBOutlet var emailLabel: UILabel!
  14. @IBOutlet var emailTF: UITextField!
  15. @IBOutlet var dobLabel: UILabel!
  16. @IBOutlet var dobTF: UITextField!
  17. @IBOutlet var genderLabel: UILabel!
  18. @IBOutlet var genderTF: UITextField!
  19. @IBOutlet var mobileLabel: UILabel!
  20. @IBOutlet var mobileTF: UITextField!
  21. @IBOutlet var stateLabel: UILabel!
  22. @IBOutlet var stateTF: UITextField!
  23. @IBOutlet var districtLabel: UILabel!
  24. @IBOutlet var districtTF: UITextField!
  25. @IBOutlet var submitButton: CustomButton!
  26. var viewModel = UserDetailsViewModel()
  27. let stateData = DBManager.sharedInstance.database.objects(StateModel.self)
  28. let districtData = DBManager.sharedInstance.database.objects(DistrictModel.self)
  29. let userData = UserDefaults.standard.dictionary(forKey: Constant.userData)
  30. let genderData = [String](arrayLiteral: "Male", "Female", "Third Gender", "Will not specify")
  31. var pageMode = String() //Register / Profile
  32. fileprivate let customPicker = ToolbarPickerView()
  33. var stateArray = [String]()
  34. var stateIdArray = [Int]()
  35. var districtArray = [String]()
  36. var stateId = 1 //default
  37. var districtId = 1 //default
  38. var gender = "Will not specify"
  39. var dateOfBirth = "01/01/2021"
  40. var mobile = "9999999999"
  41. var activeTextField = 0
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. // Do any additional setup after loading the view.
  45. viewModel.delegate = self
  46. ApiManager.generateCookie()
  47. print(pageMode)
  48. }
  49. override func viewWillAppear(_ animated: Bool) {
  50. setupUI()
  51. setupUserData()
  52. }
  53. func setupUI() {
  54. let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
  55. view.addGestureRecognizer(tap)
  56. }
  57. func setupUserData() {
  58. //Set text field title name
  59. nameLabel.text = Helper.translateText(inputText: "Enter Name")
  60. emailLabel.text = Helper.translateText(inputText: "Email")
  61. dobLabel.text = Helper.translateText(inputText: "Enter Date of Birth")
  62. genderLabel.text = Helper.translateText(inputText: "Select Gender")
  63. mobileLabel.text = Helper.translateText(inputText: "Mobile")
  64. stateLabel.text = Helper.translateText(inputText: "Select State")
  65. districtLabel.text = Helper.translateText(inputText: "Select District")
  66. if pageMode == "Profile" {
  67. submitButton.setTitle(Helper.translateText(inputText: "UPDATE"), for: .normal)
  68. } else {
  69. submitButton.setTitle(Helper.translateText(inputText: "SUBMIT"), for: .normal)
  70. }
  71. //Set text field data
  72. if pageMode == "Profile" {
  73. let userModelDB = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  74. if let row = userModelDB.firstIndex(where: {$0.email == self.userData?["email"] as? String}) {
  75. nameTF.text = userModelDB[row].name
  76. emailTF.text = userModelDB[row].email
  77. let dateString = userModelDB[row].dateOfBirth
  78. let requiredFormat = dateString.toDateString(inputDateFormat: "yyyy-MM-dd'T'HH:mm:ssZ", ouputDateFormat: Constant.dateFormat)
  79. dobTF.text = requiredFormat
  80. genderTF.text = userModelDB[row].gender
  81. mobileTF.text = userModelDB[row].mobile
  82. stateTF.text = getStateName(id: userModelDB[row].stateId)
  83. self.stateId = userModelDB[row].stateId
  84. districtTF.text = getDistrictName(id: userModelDB[row].districtId)
  85. self.districtId = userModelDB[row].districtId
  86. }
  87. }
  88. nameTF.text = userData?["name"] as? String
  89. emailTF.text = userData?["email"] as? String
  90. dobTF.datePicker(target: self,
  91. doneAction: #selector(doneAction),
  92. cancelAction: #selector(cancelAction),
  93. datePickerMode: .date)
  94. createPickerView()
  95. nameTF.delegate = self
  96. emailTF.delegate = self
  97. dobTF.delegate = self
  98. genderTF.delegate = self
  99. mobileTF.delegate = self
  100. stateTF.delegate = self
  101. districtTF.delegate = self
  102. districtTF.isUserInteractionEnabled = false
  103. stateArray.removeAll()
  104. stateIdArray.removeAll()
  105. getState()
  106. }
  107. @objc func dismissKeyboard() {
  108. self.view.endEditing(true)
  109. }
  110. func createPickerView() {
  111. self.genderTF.inputView = self.customPicker
  112. self.genderTF.inputAccessoryView = self.customPicker.toolbar
  113. self.stateTF.inputView = self.customPicker
  114. self.stateTF.inputAccessoryView = self.customPicker.toolbar
  115. self.districtTF.inputView = self.customPicker
  116. self.districtTF.inputAccessoryView = self.customPicker.toolbar
  117. self.customPicker.delegate = self
  118. self.customPicker.dataSource = self
  119. self.customPicker.toolbarDelegate = self
  120. }
  121. @objc
  122. func cancelAction() {
  123. self.dobTF.resignFirstResponder()
  124. }
  125. @objc
  126. func doneAction() {
  127. if let datePickerView = self.dobTF.inputView as? UIDatePicker {
  128. let dateFormatter = DateFormatter()
  129. dateFormatter.dateFormat = Constant.dateFormat
  130. let dateString = dateFormatter.string(from: datePickerView.date)
  131. self.dobTF.text = dateString
  132. self.dateOfBirth = dateString
  133. print(datePickerView.date)
  134. print(dateString)
  135. self.dobTF.resignFirstResponder()
  136. }
  137. }
  138. func getStateName(id:Int) -> String {
  139. var stateName = String()
  140. if let row = stateData.firstIndex(where: {$0.id == id}) {
  141. stateName = stateData[row].stateName
  142. }
  143. return stateName
  144. }
  145. func getDistrictName(id:Int) -> String {
  146. var districtName = String()
  147. if let row = districtData.firstIndex(where: {$0.id == id}) {
  148. districtName = districtData[row].districtName
  149. }
  150. return districtName
  151. }
  152. func getState() {
  153. for state in stateData {
  154. let id = state.id
  155. let name = state.stateName
  156. self.stateIdArray.append(id)
  157. self.stateArray.append(name)
  158. }
  159. }
  160. func getDistrict(stateId:Int) {
  161. for district in districtData {
  162. if district.stateId == stateId {
  163. let name = district.districtName
  164. self.districtArray.append(name)
  165. }
  166. }
  167. }
  168. func getStateId(name: String) -> Int {
  169. var id = Int()
  170. for state in stateData {
  171. if name == state.stateName {
  172. id = state.id
  173. }
  174. }
  175. return id
  176. }
  177. func getDistrictId(name: String) -> Int {
  178. var id = Int()
  179. for district in districtData {
  180. if name == district.districtName {
  181. id = district.id
  182. }
  183. }
  184. return id
  185. }
  186. func checkValidation() {
  187. if nameTF.text?.isEmpty == true {
  188. self.view.makeToast(Helper.translateText(inputText: "Enter a valid name, 3 or more characters long"))
  189. }
  190. else if emailTF.text?.isEmpty == true && emailTF.text?.isValidEmail == false {
  191. self.view.makeToast(Helper.translateText(inputText: "Enter valid email"))
  192. }
  193. // else if dobTF.text?.isEmpty == true {
  194. // self.view.makeToast(Helper.translateText(inputText: "Select your date of birth"))
  195. // }
  196. // else if genderTF.text?.isEmpty == true {
  197. // self.view.makeToast(Helper.translateText(inputText: "Select your gender"))
  198. // }
  199. // else if mobileTF.text?.isEmpty == true {
  200. // self.view.makeToast(Helper.translateText(inputText: "Enter Mobile Number"))
  201. // }
  202. // else if mobileTF.text?.count != 10 {
  203. // self.view.makeToast(Helper.translateText(inputText: "Enter Valid Mobile Number"))
  204. // }
  205. // else if stateTF.text?.isEmpty == true {
  206. // self.view.makeToast(Helper.translateText(inputText: "Select your state"))
  207. // }
  208. // else if districtTF.text?.isEmpty == true {
  209. // self.view.makeToast(Helper.translateText(inputText: "Select your district"))
  210. // }
  211. else {
  212. if pageMode == "Profile" {
  213. updateStudent()
  214. } else {
  215. saveStudent()
  216. }
  217. }
  218. }
  219. /*
  220. // MARK: - Navigation
  221. // In a storyboard-based application, you will often want to do a little preparation before navigation
  222. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  223. // Get the new view controller using segue.destination.
  224. // Pass the selected object to the new view controller.
  225. }
  226. */
  227. @IBAction func submitButtonAction(_ sender: Any) {
  228. if Reachability.isConnectedToNetwork() {
  229. checkValidation()
  230. } else {
  231. Alert.showInternetFailureAlert(on: self)
  232. }
  233. }
  234. }
  235. extension UserDetailsViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  236. // MARK: UIPickerView Delegation
  237. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  238. return 1
  239. }
  240. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  241. switch activeTextField
  242. {
  243. case 1:
  244. return genderData.count
  245. case 2:
  246. return stateArray.count
  247. case 3:
  248. return districtArray.count
  249. default:
  250. return districtArray.count
  251. }
  252. }
  253. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  254. switch activeTextField{
  255. case 1:
  256. return genderData[row]
  257. case 2:
  258. return stateArray[row]
  259. case 3:
  260. return districtArray[row]
  261. default:
  262. return districtArray[row]
  263. }
  264. }
  265. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  266. switch activeTextField{
  267. case 1:
  268. genderTF.text = genderData[row]
  269. break
  270. case 2:
  271. stateTF.text = stateArray[row]
  272. break
  273. case 3:
  274. districtTF.text = districtArray[row]
  275. break
  276. default:
  277. districtTF.text = districtArray[row]
  278. break
  279. }
  280. }
  281. }
  282. extension UserDetailsViewController: UITextFieldDelegate {
  283. // when user select a textfield, this method will be called
  284. func textFieldDidBeginEditing(_ textField: UITextField) {
  285. switch textField {
  286. case genderTF:
  287. activeTextField = 1
  288. customPicker.reloadAllComponents()
  289. case stateTF:
  290. activeTextField = 2
  291. customPicker.reloadAllComponents()
  292. case districtTF:
  293. activeTextField = 3
  294. customPicker.reloadAllComponents()
  295. default:
  296. activeTextField = 0
  297. }
  298. }
  299. }
  300. extension UserDetailsViewController: ToolbarPickerViewDelegate {
  301. func didTapDone() {
  302. switch activeTextField {
  303. case 1:
  304. let row = self.customPicker.selectedRow(inComponent: 0)
  305. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  306. self.genderTF.text = self.genderData[row]
  307. self.genderTF.resignFirstResponder()
  308. case 2:
  309. let row = self.customPicker.selectedRow(inComponent: 0)
  310. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  311. self.stateTF.text = self.stateArray[row]
  312. districtTF.isUserInteractionEnabled = true
  313. self.districtTF.text = nil
  314. districtArray.removeAll()
  315. getDistrict(stateId: self.stateIdArray[row])
  316. self.stateId = getStateId(name: self.stateArray[row])
  317. self.stateTF.resignFirstResponder()
  318. case 3:
  319. let row = self.customPicker.selectedRow(inComponent: 0)
  320. self.customPicker.selectRow(row, inComponent: 0, animated: false)
  321. self.districtTF.text = self.districtArray[row]
  322. self.districtId = getDistrictId(name: self.districtArray[row])
  323. self.districtTF.resignFirstResponder()
  324. default:
  325. return
  326. }
  327. }
  328. func didTapCancel() {
  329. switch activeTextField{
  330. case 1:
  331. self.genderTF.text = nil
  332. self.genderTF.resignFirstResponder()
  333. case 2:
  334. self.stateTF.text = nil
  335. self.stateTF.resignFirstResponder()
  336. case 3:
  337. self.districtTF.text = nil
  338. self.districtTF.resignFirstResponder()
  339. default:
  340. return
  341. }
  342. }
  343. }
  344. extension UserDetailsViewController {
  345. func saveStudent() {
  346. ActivityIndicator.start(style: .large, backColor: UIColor.black.withAlphaComponent(0.5), baseColor: UIColor.white)
  347. let topicData = DBManager.sharedInstance.database.objects(TopicModel.self)
  348. if topicData.count != 0 {
  349. let topicId = topicData[0].id
  350. let levelId = topicData[0].levelId
  351. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_STUDENT)/save.json"
  352. print(url)
  353. let userIdentifier = UserDefaultsConstant.getValueFromUserDefults(for: Constant.userIdentifier) ?? ""
  354. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  355. let headers: HTTPHeaders = [
  356. "Content-Type": "application/json",
  357. "Accept": "application/json",
  358. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  359. ]
  360. print(headers)
  361. if mobileTF.text?.isEmpty == false {
  362. self.mobile = mobileTF.text!
  363. } else if genderTF.text?.isEmpty == false {
  364. self.gender = genderTF.text!
  365. } else if dobTF.text?.isEmpty == false {
  366. self.dateOfBirth = dobTF.text!
  367. }
  368. let params: Parameters?
  369. params = [
  370. "appleUserIdentifier" : userIdentifier,
  371. "name" : nameTF.text ?? "",
  372. "mobile" : mobile,
  373. "email" : emailTF.text ?? "",
  374. "username" : emailTF.text?.stringBefore("@") ?? "",
  375. "password" : emailTF.text?.stringBefore("@") ?? "",
  376. "gender" : gender,
  377. "dateOfBirth" : dateOfBirth,
  378. "state" : self.stateId,
  379. "district" : self.districtId,
  380. "topic" : topicId,
  381. "level" : levelId,
  382. "role" : 2
  383. ]
  384. print(params!)
  385. AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
  386. switch response.result {
  387. case .success(let value):
  388. ActivityIndicator.stop()
  389. print(value)
  390. let userData = value as! NSDictionary
  391. let accountExpired = userData.value(forKey: "accountExpired") as! NSNumber;
  392. let accountLocked = userData.value(forKey: "accountLocked") as! NSNumber;
  393. let dateCreated = userData.value(forKey: "dateCreated") as? String ?? "";
  394. let dateOfBirth = userData.value(forKey: "dateOfBirth") as? String ?? "";
  395. let district = userData.value(forKey: "district") as? NSObject;
  396. let districtId = district?.value(forKey: "id") as! NSNumber;
  397. let email = userData.value(forKey: "email") as? String ?? "";
  398. let enabled = userData.value(forKey: "enabled") as! NSNumber;
  399. let gender = userData.value(forKey: "gender") as? String ?? "";
  400. let id = userData.value(forKey: "id") as! NSNumber;
  401. let level = userData.value(forKey: "level") as? NSObject;
  402. let levelId = level?.value(forKey: "id") as! NSNumber;
  403. let mobile = userData.value(forKey: "mobile") as? String ?? "";
  404. let name = userData.value(forKey: "name") as? String ?? "";
  405. let password = userData.value(forKey: "password") as? String ?? "";
  406. let passwordExpired = userData.value(forKey: "passwordExpired") as! NSNumber;
  407. let role = userData.value(forKey: "role") as? NSObject;
  408. let roleId = role?.value(forKey: "id") as! NSNumber;
  409. let state = userData.value(forKey: "state") as? NSObject;
  410. let stateId = state?.value(forKey: "id") as! NSNumber;
  411. let topic = userData.value(forKey: "topic") as? NSObject;
  412. let topicId = topic?.value(forKey: "id") as! NSNumber;
  413. let username = userData.value(forKey: "username") as? String ?? "";
  414. let appleUserIdentifier = userData.value(forKey: "appleUserIdentifier") as? String ?? "";
  415. var userModel = [UserDetailsModel]()
  416. let newUserModel = UserDetailsModel(accountExpired: Int(truncating: accountExpired), accountLocked: Int(truncating: accountLocked), dateCreated: dateCreated, dateOfBirth: dateOfBirth, districtId: Int(truncating: districtId), email: email, enabled: Int(truncating: enabled), gender: gender, id: Int(truncating: id), levelId: Int(truncating: levelId), mobile: mobile, name: name, password: password, passwordExpired: Int(truncating: passwordExpired), roleId: Int(truncating: roleId), stateId: Int(truncating: stateId), topicId: Int(truncating: topicId), username: username, appleIdentifier: appleUserIdentifier)
  417. userModel.append(newUserModel)
  418. DBManager.sharedInstance.addData(objs: userModel)
  419. let userModelDB = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  420. print("user count")
  421. print(userModelDB.count)
  422. UserDefaultsConstant.setIntValueInUserDefaults(objValue: Int(truncating: id), for: Constant.studentId)
  423. //Navigate to Home
  424. DispatchQueue.main.async {
  425. let loaderVC = self.storyboard?.instantiateViewController(withIdentifier:"LoadingAnimationViewController" ) as! LoadingAnimationViewController
  426. self.navigationController?.pushViewController(loaderVC, animated: true)
  427. }
  428. case .failure(let error):
  429. ActivityIndicator.stop()
  430. print(error)
  431. }
  432. }
  433. }
  434. }
  435. func updateStudent() {
  436. ActivityIndicator.start(style: .large, backColor: UIColor.black.withAlphaComponent(0.5), baseColor: UIColor.white)
  437. let topicData = DBManager.sharedInstance.database.objects(TopicModel.self)
  438. let studentId = UserDefaultsConstant.getIntValueFromUserDefults(for: Constant.studentId) as! Int
  439. if topicData.count != 0 {
  440. let topicId = topicData[0].id
  441. let levelId = topicData[0].levelId
  442. let url = "\(ApiUrl.BASE_URL + ApiUrl.API_STUDENT)/update.json"
  443. print(url)
  444. let userIdentifier = UserDefaultsConstant.getValueFromUserDefults(for: Constant.userIdentifier) ?? ""
  445. let cookieValue = UserDefaultsConstant.getValueFromUserDefults(for: "cookieValue") ?? ""
  446. let headers: HTTPHeaders = [
  447. "Content-Type": "application/json",
  448. "Accept": "application/json",
  449. "Cookie": "\(Constant.CookieName) = \(cookieValue)"
  450. ]
  451. print(headers)
  452. let params: Parameters?
  453. params = [
  454. "appleUserIdentifier" : userIdentifier,
  455. "id" : studentId,
  456. "name" : nameTF.text ?? "",
  457. "mobile" : mobileTF.text ?? "",
  458. "email" : emailTF.text ?? "",
  459. "username" : emailTF.text?.stringBefore("@") ?? "",
  460. "password" : emailTF.text?.stringBefore("@") ?? "",
  461. "gender" : genderTF.text ?? "",
  462. "dateOfBirth" : dobTF.text ?? "",
  463. "state" : self.stateId,
  464. "district" : self.districtId,
  465. "topic" : topicId,
  466. "level" : levelId,
  467. "role" : 2
  468. ]
  469. print(params!)
  470. AF.request(url, method: .put, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
  471. switch response.result {
  472. case .success(let value):
  473. ActivityIndicator.stop()
  474. print(value)
  475. let userData = value as! NSDictionary
  476. let accountExpired = userData.value(forKey: "accountExpired") as! NSNumber;
  477. let accountLocked = userData.value(forKey: "accountLocked") as! NSNumber;
  478. let dateCreated = userData.value(forKey: "dateCreated") as? String ?? "";
  479. let dateOfBirth = userData.value(forKey: "dateOfBirth") as? String ?? "";
  480. let district = userData.value(forKey: "district") as? NSObject;
  481. let districtId = district?.value(forKey: "id") as! NSNumber;
  482. let email = userData.value(forKey: "email") as? String ?? "";
  483. let enabled = userData.value(forKey: "enabled") as! NSNumber;
  484. let gender = userData.value(forKey: "gender") as? String ?? "";
  485. let id = userData.value(forKey: "id") as! NSNumber;
  486. let level = userData.value(forKey: "level") as? NSObject;
  487. let levelId = level?.value(forKey: "id") as! NSNumber;
  488. let mobile = userData.value(forKey: "mobile") as? String ?? "";
  489. let name = userData.value(forKey: "name") as? String ?? "";
  490. let password = userData.value(forKey: "password") as? String ?? "";
  491. let passwordExpired = userData.value(forKey: "passwordExpired") as! NSNumber;
  492. let role = userData.value(forKey: "role") as? NSObject;
  493. let roleId = role?.value(forKey: "id") as! NSNumber;
  494. let state = userData.value(forKey: "state") as? NSObject;
  495. let stateId = state?.value(forKey: "id") as! NSNumber;
  496. let topic = userData.value(forKey: "topic") as? NSObject;
  497. let topicId = topic?.value(forKey: "id") as! NSNumber;
  498. let username = userData.value(forKey: "username") as? String ?? "";
  499. let appleUserIdentifier = userData.value(forKey: "appleUserIdentifier") as? String ?? "";
  500. var userModel = [UserDetailsModel]()
  501. let newUserModel = UserDetailsModel(accountExpired: Int(truncating: accountExpired), accountLocked: Int(truncating: accountLocked), dateCreated: dateCreated, dateOfBirth: dateOfBirth, districtId: Int(truncating: districtId), email: email, enabled: Int(truncating: enabled), gender: gender, id: Int(truncating: id), levelId: Int(truncating: levelId), mobile: mobile, name: name, password: password, passwordExpired: Int(truncating: passwordExpired), roleId: Int(truncating: roleId), stateId: Int(truncating: stateId), topicId: Int(truncating: topicId), username: username, appleIdentifier: appleUserIdentifier)
  502. DBManager.sharedInstance.deleteFromDb(object: userModel)
  503. userModel.append(newUserModel)
  504. DBManager.sharedInstance.addData(objs: userModel)
  505. let userModelDB = DBManager.sharedInstance.database.objects(UserDetailsModel.self)
  506. print("user count")
  507. print(userModelDB.count)
  508. //Navigate to Home
  509. DispatchQueue.main.async {
  510. //self.dismiss(animated: true, completion: nil)
  511. let homeVC = self.storyboard?.instantiateViewController(withIdentifier:"DashboardViewController" ) as! DashboardViewController
  512. self.navigationController?.pushViewController(homeVC, animated: true)
  513. }
  514. case .failure(let error):
  515. ActivityIndicator.stop()
  516. print(error)
  517. self.showToastMessage(message: Helper.translateText(inputText: "Failed to update profile data, please try again later"))
  518. }
  519. }
  520. }
  521. }
  522. }
  523. extension UserDetailsViewController : UserDetailsViewProtocol {
  524. func showAlert(title: String, message: String) {
  525. Alert.showAlert(on: self, with: title, message: message)
  526. }
  527. func stopActivityIndicator() {
  528. //activityIndicator.stopAnimating()
  529. }
  530. func showToastMessage(message: String) {
  531. self.view.makeToast(message)
  532. }
  533. }