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.

424 lines
17 KiB

6 years ago
6 years ago
  1. //
  2. // KYCDocumentsUploadViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 12/20/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class KYCDocumentsUploadViewController: UIViewController {
  10. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  11. var index: Int?
  12. @IBOutlet weak var viewOutlet: UIView!
  13. @IBOutlet weak var viewProgressBarOutlet: UIView!
  14. var kycviewmodel: KYCViewModel?
  15. let imagePicker = UIImagePickerController()
  16. static var internetListener = 0
  17. static var timeOutListener = 0
  18. static var apiRequestListener = 0
  19. @IBAction func submitWithDocuments(_ sender: Any) {
  20. if imageViewIDFront.tag == 1 || imageViewIDBack.tag == 2 || imageViewPassbook.tag == 3 || imageViewPassport.tag == 4 || imageViewCloseupSelfie.tag == 5 {
  21. self.popUpMessageInfo(value: 16,title:"Missing Images", message: "Please select all the images")
  22. } else {
  23. processImagesAndSubmit()
  24. }
  25. }
  26. @IBOutlet weak var imageViewCloseupSelfie: UIImageView!
  27. @IBOutlet weak var imageViewIDFront: UIImageView!
  28. @IBOutlet weak var imageViewIDBack: UIImageView!
  29. @IBOutlet weak var imageViewPassbook: UIImageView!
  30. @IBOutlet weak var imageViewPassport: UIImageView!
  31. @IBOutlet weak var kycFirstStep: UIView!
  32. @IBOutlet weak var kycProgessOne: UIView!
  33. @IBOutlet weak var kycSecondStep: UIView!
  34. @IBOutlet weak var kycProgessSecond: UIView!
  35. @IBOutlet weak var kycFinalStep: UIView!
  36. override func viewDidDisappear(_ animated: Bool) {
  37. self.kycviewmodel?.resetImageSubmission()
  38. }
  39. /**
  40. Internet Check
  41. */
  42. func setUpNetworkListener() {
  43. kycviewmodel?.internetConnection.bind { [unowned self] in
  44. guard $0 != nil else {
  45. return
  46. }
  47. self.enableUserInteractions()
  48. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  49. if $0! {
  50. self.processImagesAndSubmit()
  51. } else {
  52. if KYCDocumentsUploadViewController.internetListener == 0{
  53. KYCDocumentsUploadViewController.internetListener = KYCDocumentsUploadViewController.internetListener+1
  54. self.popUpMessage(value: 21)
  55. }
  56. }
  57. self.kycviewmodel?.internetConnection.value = nil
  58. }
  59. }
  60. /**
  61. Update the Documents of user
  62. */
  63. func setUpSubmissionListener() {
  64. kycviewmodel?.isSubmitted.bind { [unowned self] in
  65. guard $0 != nil else {
  66. return
  67. }
  68. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  69. self.enableUserInteractions()
  70. guard $0! else {
  71. if KYCDocumentsUploadViewController.apiRequestListener == 0{
  72. KYCDocumentsUploadViewController.apiRequestListener = KYCDocumentsUploadViewController.apiRequestListener+1
  73. if KYCDocumentsUploadViewController.timeOutListener == 0{
  74. KYCDocumentsUploadViewController.timeOutListener = KYCDocumentsUploadViewController.timeOutListener+1
  75. self.popUpMessageError(value: 23, message: (self.kycviewmodel?.getErrorMessage())!)
  76. }
  77. }
  78. self.kycviewmodel?.isSubmitted.value = nil
  79. return
  80. }
  81. UserDefaults.standard.set(true, forKey: "com.gmeremit.isKYCSubmitted")
  82. UserDefaults.standard.set(true, forKey: "com.gmeremit.showImageAlert")
  83. let mainWireFram = MainWireframe.shared
  84. let window = UIApplication.shared.keyWindow
  85. window?.rootViewController = mainWireFram?.getMainView()
  86. window?.makeKeyAndVisible()
  87. // self.performSegue(withIdentifier: "homeScreen", sender: nil)
  88. }
  89. }
  90. /**
  91. Process and compress the image to submit
  92. */
  93. func processImagesAndSubmit() {
  94. disableUserInteractions()
  95. showActivityIndicator(activityIndicator: activityIndicator)
  96. let imageDataCloseUpSelfie: NSData = UIImageJPEGRepresentation(imageViewCloseupSelfie.image!, 0.25)! as NSData
  97. let imageDataIDFront: NSData = UIImageJPEGRepresentation(imageViewIDFront.image!, 0.25)! as NSData
  98. let imageDataIDBack: NSData = UIImageJPEGRepresentation(imageViewIDBack.image!, 0.25)! as NSData
  99. let imageDataPassbook: NSData = UIImageJPEGRepresentation(imageViewPassbook.image!, 0.25)! as NSData
  100. let imageDataPassport: NSData = UIImageJPEGRepresentation(imageViewPassport.image!, 0.25)! as NSData
  101. let imageDataCloseUpSelfieBase64: String = imageDataCloseUpSelfie.base64EncodedString(options: .lineLength64Characters)
  102. let imageDataIDFrontBase64: String = imageDataIDFront.base64EncodedString(options: .lineLength64Characters)
  103. let imageDataIDBackBase64: String = imageDataIDBack.base64EncodedString(options: .lineLength64Characters)
  104. let imageDataPassbookBase64: String = imageDataPassbook.base64EncodedString(options: .lineLength64Characters)
  105. let imageDataPassportBase64: String = imageDataPassport.base64EncodedString(options: .lineLength64Characters)
  106. kycviewmodel?.provideImageForSubmission(docType: kycCode.Selfie.rawValue, imageBase64: imageDataCloseUpSelfieBase64)
  107. kycviewmodel?.provideImageForSubmission(docType: kycCode.IDFront.rawValue, imageBase64: imageDataIDFrontBase64)
  108. kycviewmodel?.provideImageForSubmission(docType: kycCode.IDBack.rawValue, imageBase64: imageDataIDBackBase64)
  109. kycviewmodel?.provideImageForSubmission(docType: kycCode.Passbook.rawValue, imageBase64: imageDataPassbookBase64)
  110. kycviewmodel?.provideImageForSubmission(docType: kycCode.Passport.rawValue, imageBase64: imageDataPassportBase64)
  111. }
  112. override func viewDidLoad() {
  113. super.viewDidLoad()
  114. setUpNavBar(id: 104, title: "")
  115. viewOutlet.backgroundColor = UIColor(hex:0xec1c24)
  116. viewProgressBarOutlet.backgroundColor = UIColor(hex:0xec1c24)
  117. // Do any additional setup after loading the view.
  118. // Kyc progress View
  119. kycFirstStep.layer.cornerRadius = kycFirstStep.frame.size.width/2
  120. kycFirstStep.layer.borderColor = UIColor.white.cgColor
  121. kycFirstStep.layer.borderWidth = 1.0
  122. kycSecondStep.layer.cornerRadius = kycSecondStep.frame.size.width/2
  123. kycSecondStep.layer.borderColor = UIColor.white.cgColor
  124. kycSecondStep.layer.borderWidth = 1.0
  125. kycFinalStep.layer.cornerRadius = kycFinalStep.frame.size.width/2
  126. kycFinalStep.layer.borderColor = UIColor.white.cgColor
  127. kycFinalStep.layer.borderWidth = 1.0
  128. kycviewmodel?.kycViewModelConnectionTimeOut.value = nil
  129. /**
  130. connection timeout
  131. */
  132. kycviewmodel?.kycViewModelConnectionTimeOut.bind { [unowned self] in
  133. guard $0 != nil else {
  134. return
  135. }
  136. self.enableUserInteractions()
  137. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  138. if KYCDocumentsUploadViewController.timeOutListener == 0{
  139. KYCDocumentsUploadViewController.timeOutListener = KYCDocumentsUploadViewController.timeOutListener+1
  140. self.popUpMessage(value: 22)
  141. }
  142. }
  143. kycviewmodel?.internetConnection.value = nil
  144. setUpNetworkListener()
  145. setUpSubmissionListener()
  146. guard kycviewmodel != nil else {
  147. return
  148. }
  149. setUpAnotherLoginListener(genericviewmodel: kycviewmodel!)
  150. kycviewmodel!.isSubmitted.value = nil
  151. imageViewCloseupSelfie.isUserInteractionEnabled = true
  152. imageViewIDFront.isUserInteractionEnabled = true
  153. imageViewIDBack.isUserInteractionEnabled = true
  154. imageViewPassbook.isUserInteractionEnabled = true
  155. imageViewPassport.isUserInteractionEnabled = true
  156. imageViewCloseupSelfie.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))))
  157. imageViewIDFront.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))))
  158. imageViewIDBack.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))))
  159. imageViewPassbook.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))))
  160. imageViewPassport.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))))
  161. }
  162. /**
  163. Display option to select image from camera or gallery
  164. */
  165. @objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
  166. let tappedImage = tapGestureRecognizer.view as! UIImageView
  167. switch tappedImage.tag {
  168. case 1, 10:
  169. index = 1
  170. case 2, 20:
  171. index = 2
  172. case 3, 30:
  173. index = 3
  174. case 4, 40:
  175. index = 4
  176. case 5, 50:
  177. index = 5
  178. default:
  179. return
  180. }
  181. let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
  182. let cameraAction = UIAlertAction(title: "Camera", style: .default,handler: {
  183. (action : UIAlertAction!) -> Void in
  184. self.openCamera()
  185. })
  186. let galleryAction = UIAlertAction(title: "Gallery", style: .default,handler: {
  187. (action : UIAlertAction!) -> Void in
  188. self.openGallery()
  189. })
  190. let cancelAction = UIAlertAction.init(title: "Cancel", style: .destructive, handler: nil)
  191. cancelAction.setValue(UIColor.black, forKey: "titleTextColor")
  192. cameraAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor")
  193. galleryAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor")
  194. alert.addAction(cameraAction)
  195. alert.addAction(galleryAction)
  196. alert.addAction(cancelAction)
  197. self.present(alert, animated: true, completion: nil)
  198. }
  199. /**
  200. Open the camera
  201. */
  202. func openCamera() {
  203. if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
  204. imagePicker.delegate = self
  205. imagePicker.sourceType = UIImagePickerControllerSourceType.camera
  206. if index == 5 {
  207. if(UIImagePickerController .isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.front)) {
  208. imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
  209. } else {
  210. //showCameraAlert(warningMessage: "You don't have front camera")
  211. imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
  212. }
  213. } else {
  214. imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
  215. }
  216. imagePicker.allowsEditing = true
  217. present(imagePicker, animated: true, completion: nil)
  218. }
  219. else {
  220. showCameraAlert(warningMessage: "You don't have camera")
  221. }
  222. }
  223. func showCameraAlert(warningMessage: String) {
  224. let alert = UIAlertController(title: "Warning", message: warningMessage, preferredStyle: .alert)
  225. alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  226. self.present(alert, animated: true, completion: nil)
  227. }
  228. /**
  229. Open Gallery to select he image
  230. */
  231. func openGallery() {
  232. imagePicker.delegate = self
  233. imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
  234. imagePicker.allowsEditing = true
  235. present(imagePicker, animated: true, completion: nil)
  236. }
  237. @IBAction func viewSamplePassport(_ sender: Any) {
  238. showSample(id: 4, titleString: "Passport")
  239. }
  240. @IBAction func viewSamplePassbook(_ sender: Any) {
  241. showSample(id: 3, titleString: "Passbook")
  242. }
  243. @IBAction func viewSampleBack(_ sender: Any) {
  244. showSample(id: 2, titleString: "National/ Alien Reg ID Back")
  245. }
  246. @IBAction func viewSampleFront(_ sender: Any) {
  247. showSample(id: 1, titleString: "National/ Alien Reg ID Front")
  248. }
  249. @IBAction func viewSampleCloseUpSelfie(_ sender: Any) {
  250. showSample(id: 0, titleString: "Picture with National/Alien Reg ID Front")
  251. }
  252. override func didReceiveMemoryWarning() {
  253. super.didReceiveMemoryWarning()
  254. }
  255. /**
  256. Display Sample image for user
  257. */
  258. func showSample(id: Int, titleString: String) {
  259. var imageToShow: UIImage!
  260. var imageView: UIImageView!
  261. var vertical_spacing: String!
  262. var imageHeightConstraint: CGFloat!
  263. var imageWidthConstraint: CGFloat!
  264. switch id {
  265. case 0:
  266. imageToShow = UIImage(named: "sampleSelfie")
  267. imageView = UIImageView(image: imageToShow)
  268. vertical_spacing = String(repeating: "\n", count: 15)
  269. imageHeightConstraint = imageToShow.size.height * 0.825
  270. imageWidthConstraint = imageToShow.size.width * 0.825
  271. case 1:
  272. imageToShow = UIImage(named: "sampleFront")
  273. imageView = UIImageView(image: imageToShow)
  274. vertical_spacing = String(repeating: "\n", count: 10)
  275. imageHeightConstraint = imageToShow.size.height * 0.9
  276. imageWidthConstraint = imageToShow.size.width * 0.9
  277. case 2:
  278. imageToShow = UIImage(named: "sampleBack")
  279. imageView = UIImageView(image: imageToShow)
  280. vertical_spacing = String(repeating: "\n", count: 10)
  281. imageHeightConstraint = imageToShow.size.height * 0.9 //initially 150
  282. imageWidthConstraint = imageToShow.size.width * 0.9 //initially 200
  283. case 3:
  284. imageToShow = UIImage(named: "samplePassbook")
  285. imageView = UIImageView(image: imageToShow)
  286. vertical_spacing = String(repeating: "\n", count: 12)
  287. imageHeightConstraint = imageToShow.size.height * 1.2 //initially 150
  288. imageWidthConstraint = imageToShow.size.width * 1.2 //initially 200
  289. case 4:
  290. imageToShow = UIImage(named: "samplePassport")
  291. imageView = UIImageView(image: imageToShow)
  292. vertical_spacing = String(repeating: "\n", count: 12)
  293. imageHeightConstraint = imageToShow.size.height * 1.2 //initially 150
  294. imageWidthConstraint = imageToShow.size.width * 1.2 //initially 200
  295. default:
  296. return
  297. }
  298. let alert = UIAlertController(title: titleString, message: vertical_spacing, preferredStyle: .alert)
  299. alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
  300. alert.view.addSubview(imageView)
  301. imageView.translatesAutoresizingMaskIntoConstraints = false
  302. alert.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: alert.view, attribute: .centerX, multiplier: 1, constant: 0))
  303. alert.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: alert.view, attribute: .centerY, multiplier: 1, constant: 0))
  304. alert.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: imageWidthConstraint))
  305. alert.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: imageHeightConstraint))
  306. alert.view.tintColor = UIColor(hex: 0xEC1C24)
  307. self.present(alert, animated: true, completion: nil)
  308. }
  309. }
  310. extension KYCDocumentsUploadViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  311. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  312. if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
  313. switch index! {
  314. case 1, 10:
  315. self.imageViewIDFront.image = image
  316. self.imageViewIDFront.tag = 10
  317. case 2, 20:
  318. self.imageViewIDBack.image = image
  319. self.imageViewIDBack.tag = 20
  320. case 3, 30:
  321. self.imageViewPassbook.image = image
  322. self.imageViewPassbook.tag = 30
  323. case 4, 40:
  324. self.imageViewPassport.image = image
  325. self.imageViewPassport.tag = 40
  326. case 5, 50:
  327. self.imageViewCloseupSelfie.image = image
  328. self.imageViewCloseupSelfie.tag = 50
  329. default:
  330. return
  331. }
  332. dismiss(animated: true, completion: nil)
  333. } else {
  334. }
  335. }
  336. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  337. dismiss(animated: true, completion: nil)
  338. }
  339. }