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.

201 lines
7.2 KiB

6 years ago
  1. //
  2. // PopUpAddUserProfileViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 2/28/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class PopUpAddUserProfileViewController: UIViewController {
  10. // var dialogDismissed: Box<Bool?> = Box(nil)
  11. @IBOutlet weak var buttonProfileImage: UIButton!
  12. private var uploadimageviewmodel = UploadImageViewModel()
  13. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  14. private var selectedImage: UIImage!
  15. public static var uploadImageConnectionTimeOutCheckDialog = 0
  16. let imagePicker = UIImagePickerController()
  17. @IBOutlet weak var addLater: UIButton!
  18. @IBOutlet weak var userName: UILabel!
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. setUpAnotherLoginListener(genericviewmodel: uploadimageviewmodel)
  22. self.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
  23. self.showAnimate()
  24. userName.text = "Namaste, \(getFullName())"
  25. uploadimageviewmodel.uploadImageConnectionTimeOut.value = nil
  26. /**
  27. connection timeout
  28. */
  29. uploadimageviewmodel.uploadImageConnectionTimeOut.bind { [unowned self] in
  30. guard $0 != nil else {
  31. return
  32. }
  33. self.enableUserInteractions()
  34. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  35. if PopUpAddUserProfileViewController.uploadImageConnectionTimeOutCheckDialog == 0{
  36. PopUpAddUserProfileViewController.uploadImageConnectionTimeOutCheckDialog = PopUpAddUserProfileViewController.uploadImageConnectionTimeOutCheckDialog+1
  37. self.popUpMessage(value: 45)
  38. }
  39. }
  40. uploadimageviewmodel.isImageSubmitted.bind { [unowned self] in
  41. guard $0 != nil else {
  42. return
  43. }
  44. self.enableUserInteractions()
  45. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  46. guard $0! else {
  47. if PopUpAddUserProfileViewController.uploadImageConnectionTimeOutCheckDialog == 0{
  48. PopUpAddUserProfileViewController.uploadImageConnectionTimeOutCheckDialog = PopUpAddUserProfileViewController.uploadImageConnectionTimeOutCheckDialog+1
  49. self.popUpMessageError(value: 10, message: self.uploadimageviewmodel.getErrorMessage())
  50. }
  51. self.uploadimageviewmodel.isImageSubmitted.value = nil
  52. return
  53. }
  54. self.addLater.setTitle("Done", for: .normal)
  55. self.addLater.setTitleColor(UIColor(hex: 0xec1c24), for: .normal)
  56. self.buttonProfileImage.setImage(self.selectedImage, for: .normal)
  57. self.uploadimageviewmodel.isImageSubmitted.value = nil
  58. }
  59. }
  60. // override func showAnimate() {
  61. // super.showAnimate()
  62. // self.navigationController?.navigationBar.isHidden = true
  63. // self.tabBarController?.tabBar.isHidden = true
  64. // self.tabBarController?.tabBar.isUserInteractionEnabled = false
  65. //
  66. // }
  67. func showAnimate()
  68. {
  69. self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
  70. self.view.alpha = 0.0
  71. self.view.alpha = 1.0
  72. self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
  73. UIView.animate(withDuration: 0.1, animations: {
  74. self.view.alpha = 1.0
  75. self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
  76. });
  77. }
  78. func dismissDialog()
  79. {
  80. self.navigationController?.navigationBar.isUserInteractionEnabled = true
  81. UIView.animate(withDuration: 0.0, animations: {
  82. self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
  83. self.view.alpha = 0.0;
  84. }, completion:{(finished : Bool) in
  85. if (finished)
  86. {
  87. self.view.removeFromSuperview()
  88. }
  89. });
  90. UserDefaults.standard.set(false, forKey: "com.gmeremit.showImageAlert")
  91. // dialogDismissed.value = true
  92. }
  93. override func didReceiveMemoryWarning() {
  94. super.didReceiveMemoryWarning()
  95. // Dispose of any resources that can be recreated.
  96. }
  97. @IBAction func addLaterAction(_ sender: Any) {
  98. UserDefaults.standard.set(false, forKey: "com.gmeremit.showImageAlert")
  99. self.dismissDialog()
  100. }
  101. @IBAction func selectProfileImage(_ sender: Any) {
  102. let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
  103. alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
  104. self.openCamera()
  105. }))
  106. alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
  107. self.openGallery()
  108. }))
  109. alert.addAction(UIAlertAction.init(title: "Cancel", style: .destructive, handler: {_ in
  110. self.dismiss(animated: true, completion: nil)
  111. }))
  112. self.present(alert, animated: true, completion: nil)
  113. }
  114. func openCamera(){
  115. if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
  116. imagePicker.delegate = self
  117. imagePicker.sourceType = UIImagePickerControllerSourceType.camera
  118. imagePicker.allowsEditing = true
  119. present(imagePicker, animated: true, completion: nil)
  120. }
  121. else {
  122. let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
  123. alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  124. self.present(alert, animated: true, completion: nil)
  125. }
  126. }
  127. func openGallery(){
  128. imagePicker.delegate = self
  129. imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
  130. imagePicker.allowsEditing = true
  131. present(imagePicker, animated: true, completion: nil)
  132. }
  133. }
  134. extension PopUpAddUserProfileViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  135. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  136. selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
  137. let imageData: NSData = UIImageJPEGRepresentation(selectedImage, 0.25)! as NSData
  138. let imageDataBase64: String = imageData.base64EncodedString(options: .lineLength64Characters)
  139. uploadUserProfileImage(profileImage: imageDataBase64);
  140. dismiss(animated: true, completion: nil)
  141. showActivityIndicator(activityIndicator: activityIndicator)
  142. disableUserInteractions()
  143. }
  144. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  145. dismiss(animated: true, completion: nil)
  146. }
  147. func uploadUserProfileImage(profileImage: String){
  148. uploadimageviewmodel.uploadProfileImage(docType: kycCode.UserImage.rawValue, imageBase64: profileImage)
  149. }
  150. }