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.

96 lines
3.9 KiB

6 years ago
  1. //
  2. // UploadImageViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 4/24/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class UploadImageViewModel: ModelExtension {
  10. var isImageSubmitted: Box<Bool?> = Box(nil)
  11. var uploadImageConnectionTimeOut: Box<Bool?> = Box(nil)
  12. let userId = UserDefaults.standard.object(forKey: "com.gmeremit.username") as! String
  13. /**
  14. To upload image file
  15. - parameter docType: image of document type
  16. - parameter imageBase64: Image data
  17. */
  18. func uploadProfileImage(docType: String, imageBase64: String){
  19. if !Reachability.isConnectedToNetwork() {
  20. self.internetConnection.value = false
  21. } else {
  22. let docParam = ["documentType": docType, "file": imageBase64]
  23. RestApiMananger.sharedInstance.submitDocument(param: docParam, userId: userId) { result in
  24. switch result {
  25. case let .success(documentJSON):
  26. self.submitUserProfileImage(imageUrl: documentJSON["documentUrl"].stringValue)
  27. case let .failure(errorJSON):
  28. self.setErrorMessage(message: errorJSON["message"].stringValue)
  29. self.isImageSubmitted.value = false
  30. return
  31. case .updateAccessCode:
  32. RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId, password: self.getLoginPassword()) {
  33. result in
  34. if result != "Error"{
  35. let uuid = RestApiMananger.sharedInstance.getUUID()
  36. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  37. self.uploadProfileImage(docType: docType, imageBase64: imageBase64)
  38. }
  39. }
  40. case .logOutUser():
  41. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  42. self.anotherLogin.value = true
  43. case .timeOut:
  44. self.uploadImageConnectionTimeOut.value = false
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. To submit image url
  51. - parameter imageUrl: url
  52. */
  53. func submitUserProfileImage(imageUrl: String){
  54. if !Reachability.isConnectedToNetwork() {
  55. self.internetConnection.value = false
  56. } else{
  57. let param = ["dpUrl": imageUrl]
  58. RestApiMananger.sharedInstance.updateUserDetail (userId: userId, param: param) { result in
  59. switch result {
  60. case let .success(documentJSON):
  61. self.isImageSubmitted.value = true
  62. UserDefaults.standard.set(documentJSON["dpUrl"].stringValue, forKey: "com.gmeremit.dpUrl")
  63. case let .failure(errorJSON):
  64. self.setErrorMessage(message: errorJSON["message"].stringValue)
  65. self.isImageSubmitted.value = false
  66. return
  67. case .updateAccessCode:
  68. RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId, password: self.getLoginPassword()) {
  69. result in
  70. if result != "Error"{
  71. let uuid = RestApiMananger.sharedInstance.getUUID()
  72. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  73. self.submitUserProfileImage(imageUrl: imageUrl)
  74. }
  75. }
  76. case .logOutUser():
  77. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  78. self.anotherLogin.value = true
  79. case .timeOut:
  80. self.uploadImageConnectionTimeOut.value = false
  81. }
  82. }
  83. }
  84. }
  85. }