diff --git a/app/src/main/java/com/nivesh/production/bajajfd/ui/fragment/StepThreeBajajFDFragment.kt b/app/src/main/java/com/nivesh/production/bajajfd/ui/fragment/StepThreeBajajFDFragment.kt index 7260ac6..0abdaac 100644 --- a/app/src/main/java/com/nivesh/production/bajajfd/ui/fragment/StepThreeBajajFDFragment.kt +++ b/app/src/main/java/com/nivesh/production/bajajfd/ui/fragment/StepThreeBajajFDFragment.kt @@ -45,25 +45,36 @@ class StepThreeBajajFDFragment : BaseFragment() { private var _binding: FragmentBajajfdStepThreeBinding? = null private val binding get() = _binding!! private lateinit var listOfDocType: List + lateinit var getCodes: GetCodes private lateinit var stepThreeBajajFDViewModel: StepThreeBajajFDViewModel private lateinit var dialog: Dialog private var takeImageResult: ActivityResultLauncher? = null private var selectImageIntent: ActivityResultLauncher? = null - private val mapImage: HashMap = HashMap() - private var front: String = "" - private var fileExt: String? = "" + private var panString: String = "" + private var photoString: String = "" + private var docString: String = "" + private var docString2: String = "" + + private var panFileExt: String? = "" + private var photoFileExt: String? = "" + private var doc1FileExt: String? = "" + private var doc2fileExt: String? = "" + private var uniqueId: String? = "" - private var selectedImage: String? = "" - private var checkPANUploadFile: Boolean? = false - private var checkPhotoUploadFile: Boolean? = false - private var actionType: AccountCaptureType? = null - enum class AccountCaptureType { - MAIN_PAN_UPLOAD,MAIN_PHOTO_UPLOAD,FIRST_UPLOAD,SECOND_UPLOAD - } + // private var actionType: AccountCaptureType? = null +// +// enum class AccountCaptureType { +// MAIN_PAN_UPLOAD, MAIN_PHOTO_UPLOAD, FIRST_UPLOAD, SECOND_UPLOAD +// } + val MAIN_PAN_UPLOAD: Int = 1 + val MAIN_PHOTO_UPLOAD: Int = 2 + val FIRST_UPLOAD: Int = 3 + val SECOND_UPLOAD: Int = 4 + var actionType: Int = -1 private val requestPermission = registerForActivityResult( ActivityResultContracts.RequestPermission() @@ -109,15 +120,11 @@ class StepThreeBajajFDFragment : BaseFragment() { binding.btnNext.setOnClickListener { if (validate()) { - var uploadPosition = 0 - for (entry in mapImage.iterator()) { - uploadPosition++ - if (entry.key.contains("Main ")) { - uploadDocApi(entry.key.replace("Main ", ""), entry.value, uploadPosition) - } else { - uploadDocApi(entry.key, entry.value, uploadPosition) - } - } + uploadDocApi( + "PAN", + "data:image/.".plus(panFileExt).plus(";base64").plus(panString), + 1 + ) } } binding.btnBack.setOnClickListener { @@ -125,37 +132,30 @@ class StepThreeBajajFDFragment : BaseFragment() { } binding.btnPANUpload.setOnClickListener { - checkPANUploadFile = true - checkPhotoUploadFile = false - actionType = AccountCaptureType.MAIN_PAN_UPLOAD - selectImage("PAN") + actionType = MAIN_PAN_UPLOAD + selectImage() } binding.btnPhotoUpload.setOnClickListener { - checkPhotoUploadFile = true - checkPANUploadFile = false - actionType = AccountCaptureType.MAIN_PHOTO_UPLOAD - selectImage("Photograph") + actionType = MAIN_PHOTO_UPLOAD + selectImage() } binding.btnAadhaarFrontUpload.setOnClickListener { - checkPANUploadFile = false - checkPhotoUploadFile = false - actionType = AccountCaptureType.FIRST_UPLOAD - selectImage(binding.spDocType.text.toString()) + actionType = FIRST_UPLOAD + selectImage() } binding.btnAadhaarBackUpload.setOnClickListener { - checkPANUploadFile = false - checkPhotoUploadFile = false - actionType = AccountCaptureType.SECOND_UPLOAD - selectImage(binding.spDocType.text.toString()) + actionType = SECOND_UPLOAD + selectImage() } binding.spDocType.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ -> - - val getCodes: GetCodes = parent.getItemAtPosition(position) as GetCodes + getCodes = parent.getItemAtPosition(position) as GetCodes + docString = "" + docString2 = "" when (getCodes.Label) { resources.getString(R.string.aadhar) -> setAadhaarUploadLayout() @@ -190,8 +190,7 @@ class StepThreeBajajFDFragment : BaseFragment() { return result } - private fun selectImage(fileType: String) { - selectedImage = fileType + private fun selectImage() { dialog = Dialog(activity as BajajFdMainActivity) dialog.setContentView(R.layout.camera_gallery_layout) val ivCut: ImageView = dialog.findViewById(R.id.ivCut) @@ -256,27 +255,74 @@ class StepThreeBajajFDFragment : BaseFragment() { } private fun uploadDocument(uri: Uri) { - val fileDir: File = requireActivity().cacheDir - val fileExtension = File(fileDir.toString().plus("/").plus(getFileName(uri))) - fileExt = fileExtension.extension - encodedFileToBase64(fileExtension) - if (checkPANUploadFile == true || checkPhotoUploadFile == true) { - mapImage["Main ".plus(selectedImage)] = front - } else { - selectedImage?.let { mapImage.put(it, front) } - } - when(actionType){ - AccountCaptureType.MAIN_PAN_UPLOAD -> binding.ivPan.visibility = View.VISIBLE - AccountCaptureType.MAIN_PHOTO_UPLOAD -> binding.ivPhotograph.visibility = View.VISIBLE - AccountCaptureType.FIRST_UPLOAD -> binding.ivAadharFront.visibility = View.VISIBLE - else ->{ + when (actionType) { + MAIN_PAN_UPLOAD -> { + binding.ivPan.visibility = View.VISIBLE + val fileDir: File = requireActivity().cacheDir + val fileExtension = File(fileDir.toString().plus("/").plus(getFileName(uri))) + panFileExt = fileExtension.extension + encodedPANBase64(fileExtension) + } + MAIN_PHOTO_UPLOAD -> { + binding.ivPhotograph.visibility = View.VISIBLE + binding.ivPan.visibility = View.VISIBLE + val fileDir: File = requireActivity().cacheDir + val fileExtension = File(fileDir.toString().plus("/").plus(getFileName(uri))) + photoFileExt = fileExtension.extension + encodedPhotoBase64(fileExtension) + } + FIRST_UPLOAD -> { + binding.ivAadharFront.visibility = View.VISIBLE + val fileDir: File = requireActivity().cacheDir + val fileExtension = File(fileDir.toString().plus("/").plus(getFileName(uri))) + doc1FileExt = fileExtension.extension + encodedUpload1Base64(fileExtension) + } + else -> { binding.ivAadharBack.visibility = View.VISIBLE + val fileDir: File = requireActivity().cacheDir + val fileExtension = File(fileDir.toString().plus("/").plus(getFileName(uri))) + doc2fileExt = fileExtension.extension + encodedFileToBase64(fileExtension) } } } + private fun encodedPANBase64(fileName: File?) { + panString = try { + val bytes: ByteArray? = fileName?.let { loadFile(it) } + Base64.encodeToString(bytes, Base64.DEFAULT).trim { it <= ' ' } + .replace("\n".toRegex(), "").replace("\\s+".toRegex(), "") + } catch (e: Exception) { + e.printStackTrace() + "" + } + } + + private fun encodedPhotoBase64(fileName: File?) { + photoString = try { + val bytes: ByteArray? = fileName?.let { loadFile(it) } + Base64.encodeToString(bytes, Base64.DEFAULT).trim { it <= ' ' } + .replace("\n".toRegex(), "").replace("\\s+".toRegex(), "") + } catch (e: Exception) { + e.printStackTrace() + "" + } + } + + private fun encodedUpload1Base64(fileName: File?) { + docString = try { + val bytes: ByteArray? = fileName?.let { loadFile(it) } + Base64.encodeToString(bytes, Base64.DEFAULT).trim { it <= ' ' } + .replace("\n".toRegex(), "").replace("\\s+".toRegex(), "") + } catch (e: Exception) { + e.printStackTrace() + "" + } + } + private fun encodedFileToBase64(fileName: File?) { - front = try { + docString2 = try { val bytes: ByteArray? = fileName?.let { loadFile(it) } Base64.encodeToString(bytes, Base64.DEFAULT).trim { it <= ' ' } .replace("\n".toRegex(), "").replace("\\s+".toRegex(), "") @@ -310,8 +356,7 @@ class StepThreeBajajFDFragment : BaseFragment() { du.Description = key du.DocumentType = key du.FDProvider = getString(R.string.bajaj) - du.ImageEncodeToBase64 = - "data:image/" + "." + fileExt + ";base64,".plus(imageBase64) //"data:image/png;base64" + du.ImageEncodeToBase64 = imageBase64 du.NiveshClientCode = (activity as BajajFdMainActivity).getClientDetailsResponse.ObjectResponse?.clientDetails?.clientMasterMFD?.CLIENT_CODE du.UniqueId = uniqueId @@ -330,17 +375,36 @@ class StepThreeBajajFDFragment : BaseFragment() { when (code) { 200 -> { Log.e("check_upload_res", response.message.toString()) - if (uploadPosition == mapImage.size) { - Toast.makeText( - requireActivity(), - "".plus(mapImage.size.toString() + " Last Image Uploaded ") + - uploadPosition, - Toast.LENGTH_LONG - ).show() - // bajajFDInterface.stepThreeApi((activity as BajajFdMainActivity).createFDRequest) + if (uploadPosition == 1) { + uploadDocApi( + "Photograph", + "data:image/.".plus(photoFileExt).plus(";base64") + .plus(photoString), + 2 + ) + } else if (uploadPosition == 2) { + uploadDocApi( + getCodes.Value, + "data:image/.".plus(doc1FileExt).plus(";base64") + .plus(docString), + 3 + ) + } else if (uploadPosition == 3) { + if (getCodes.Value == "Aadhar") { + uploadDocApi( + getCodes.Value, + "data:image/.".plus(doc2fileExt).plus(";base64") + .plus(docString2), + 4 + ) + } else { + createFDApi((activity as BajajFdMainActivity).createFDRequest) + } + } else if (uploadPosition == 4) { createFDApi((activity as BajajFdMainActivity).createFDRequest) } } + // 650 -> refreshToken() else -> { Common.showDialogValidation( @@ -368,51 +432,51 @@ class StepThreeBajajFDFragment : BaseFragment() { } private fun createFDApi(data: CreateFDRequest) { - stepThreeBajajFDViewModel.createFDApi( - data, - Constants.token, - activity as BajajFdMainActivity - ) - stepThreeBajajFDViewModel.getFDResponseMutableData.observe(viewLifecycleOwner) { response -> - when (response) { - is Resource.Success -> { - Log.e("createFDApi", "response--> " + response.data.toString()) - val createFDApplicationResponse: CreateFDApplicationResponse = - Gson().fromJson( - response.data?.toString(), - CreateFDApplicationResponse::class.java - ) - createFDApplicationResponse.Response.StatusCode.let { code -> - when (code) { - 200 -> { - (activity as BajajFdMainActivity).stepThreeApi( - createFDApplicationResponse - ) - } - // 650 -> refreshToken() - else -> { - Common.showDialogValidation( - activity as BajajFdMainActivity, - createFDApplicationResponse.Response.Errors[0].ErrorMessage - ) - } + stepThreeBajajFDViewModel.createFDApi( + data, + Constants.token, + activity as BajajFdMainActivity + ) + stepThreeBajajFDViewModel.getFDResponseMutableData.observe(viewLifecycleOwner) { response -> + when (response) { + is Resource.Success -> { + Log.e("createFDApi", "response--> " + response.data.toString()) + val createFDApplicationResponse: CreateFDApplicationResponse = + Gson().fromJson( + response.data?.toString(), + CreateFDApplicationResponse::class.java + ) + createFDApplicationResponse.Response.StatusCode.let { code -> + when (code) { + 200 -> { + (activity as BajajFdMainActivity).stepThreeApi( + createFDApplicationResponse + ) + } + // 650 -> refreshToken() + else -> { + Common.showDialogValidation( + activity as BajajFdMainActivity, + createFDApplicationResponse.Response.Errors[0].ErrorMessage + ) } } } + } - is Resource.Error -> { - response.message?.let { message -> - Common.showDialogValidation(activity as BajajFdMainActivity, message) - } + is Resource.Error -> { + response.message?.let { message -> + Common.showDialogValidation(activity as BajajFdMainActivity, message) } - is Resource.Loading -> { + } + is Resource.Loading -> { - } - is Resource.DataError -> { + } + is Resource.DataError -> { - } } } + } } @@ -481,22 +545,28 @@ class StepThreeBajajFDFragment : BaseFragment() { private fun validate(): Boolean { - if (binding.spDocType.text.equals(R.string.aadhar) && mapImage.size < 3) { + if (panString.isEmpty()) { + Common.showDialogValidation( + activity as BajajFdMainActivity, + "Upload PAN Document" + ) + return false + } else if (photoString.isEmpty()) { Common.showDialogValidation( activity as BajajFdMainActivity, - "Upload Aadhaar Images" + "Upload Photo" ) return false - } else if (!binding.spDocType.text.equals(R.string.aadhar) && mapImage.size == 3) { + } else if (docString.isEmpty()) { Common.showDialogValidation( activity as BajajFdMainActivity, - "Upload Aadhaar Images" + "Upload " + getCodes.Value + " Image" ) return false - } else if (mapImage.size < 3) { + } else if (getCodes.Value == "Aadhar" && docString2.isEmpty()) { Common.showDialogValidation( activity as BajajFdMainActivity, - "Upload All Images" + "Upload Aadhar Back Image" ) return false }