From 514d1dff4eaccb13e0a73ff1d8aec1d6ff6ba8b8 Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Sun, 26 Apr 2020 17:12:38 +0200 Subject: [PATCH] Adding error return if pdfUtils error --- .../ui/qrcode/QrCodeViewModel.kt | 5 ++ .../attestationsCovid19/utilities/PdfUtils.kt | 61 +++++++++++-------- app/src/main/res/values/errors.xml | 7 +++ app/src/main/res/values/strings.xml | 1 - 4 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 app/src/main/res/values/errors.xml diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt index 600c58d..099d10c 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/qrcode/QrCodeViewModel.kt @@ -5,6 +5,7 @@ import android.view.View import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import fr.sanchezm.attestationsCovid19.R import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository @@ -28,6 +29,7 @@ class QrCodeViewModel( private val _startActivity = MutableLiveData>() private val _finish = MutableLiveData>() + private val _error = MutableLiveData>() val attestation: LiveData = _attestation @@ -40,6 +42,7 @@ class QrCodeViewModel( val startActivity: LiveData> = _startActivity val finish: LiveData> = _finish + val error: LiveData> = _error fun addData(createAt: Long, toCreate: Boolean) { val att = attestationRepository.getAttestation(createAt) @@ -69,6 +72,8 @@ class QrCodeViewModel( ) { success -> if (success) displayQrCode() + else + _error.value = Event(R.string.error_failed_create_pdf) } } } diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt index 6f24a70..f254de8 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt @@ -33,14 +33,17 @@ class PdfUtils private constructor( val acroForm = docCatalog.acroForm // Adding data - setFieldsData(acroForm, attestation) - setCheckboxFields(acroForm, attestation) - addPageWithQrCode(document, attestation) + if (setFieldsData(acroForm, attestation)) { + setCheckboxFields(acroForm, attestation) + addPageWithQrCode(document, attestation) - Log.v("PdfUtils", "Save File to ${getPath(attestation.createAt)}") - document.save(getPath(attestation.createAt)) - document.close() - callback(true) + Log.v("PdfUtils", "Save File to ${getPath(attestation.createAt)}") + document.save(getPath(attestation.createAt)) + document.close() + callback(true) + } else { + callback(false) + } } catch (e: IOException) { e.printStackTrace() callback(false) @@ -106,29 +109,34 @@ class PdfUtils private constructor( } - private fun setFieldsData(acroForm: PDAcroForm, attestation: Attestation) { + private fun setFieldsData(acroForm: PDAcroForm, attestation: Attestation): Boolean { val profile = attestation.profile - // Init Fields - val namesField = acroForm.getField("Nom et prénom") as PDTextField - val birthdayField = acroForm.getField("Date de naissance") as PDTextField - val birthPlaceField = acroForm.getField("Lieu de naissance") as PDTextField - val addressField = acroForm.getField("Adresse actuelle") as PDTextField - val cityField = acroForm.getField("Ville") as PDTextField - val dateField = acroForm.getField("Date") as PDTextField - val hourField = acroForm.getField("Heure") as PDTextField - val minutesField = acroForm.getField("Minute") as PDTextField + try { + // Init Fields + val namesField = acroForm.getField("Nom et prénom") as PDTextField + val birthdayField = acroForm.getField("Date de naissance") as PDTextField + val birthPlaceField = acroForm.getField("Lieu de naissance") as PDTextField + val addressField = acroForm.getField("Adresse actuelle") as PDTextField + val cityField = acroForm.getField("Ville") as PDTextField + val dateField = acroForm.getField("Date") as PDTextField + val hourField = acroForm.getField("Heure") as PDTextField + val minutesField = acroForm.getField("Minute") as PDTextField // val signatureField = acroForm.getField("Signature") as PDTextField - namesField.value = "${profile.firstName} ${profile.lastName}" - birthdayField.value = profile.birthday - birthPlaceField.value = profile.birthPlace - addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}" - cityField.value = profile.city - dateField.value = attestation.exitDate - hourField.value = attestation.exitHour.split("h")[0] - minutesField.value = attestation.exitHour.split("h")[1] - + namesField.value = "${profile.firstName} ${profile.lastName}" + birthdayField.value = profile.birthday + birthPlaceField.value = profile.birthPlace + addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}" + cityField.value = profile.city + dateField.value = attestation.exitDate + hourField.value = attestation.exitHour.split("h")[0] + minutesField.value = attestation.exitHour.split("h")[1] + return true + } catch (e: Exception) { + Log.e(TAG, "${e.message}") + } + return false } private fun setCheckboxFields(acroForm: PDAcroForm, attestation: Attestation) { @@ -150,6 +158,7 @@ class PdfUtils private constructor( private var instance: PdfUtils? = null private val LOCK = Any() private const val folderPath = "attestations" + private val TAG = PdfUtils::class.simpleName fun getInstance() = instance diff --git a/app/src/main/res/values/errors.xml b/app/src/main/res/values/errors.xml new file mode 100644 index 0000000..066fca2 --- /dev/null +++ b/app/src/main/res/values/errors.xml @@ -0,0 +1,7 @@ + + + + Erreur lors de la génération de l\'attestation, tout les champs ne sont pas compléter + Erreur lors de la génération de l\'attestion, veuillez vérifiez vos champs ou contacter le développeur. + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 32ff0bc..ec8f45e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,7 +7,6 @@ Attestation de déplacement dérogatoire #RestonsChezNous - Erreur lors de la génération de l\'attestation, tout les champs ne sont pas compléter Attestation générer Aucune attestation, veuillez en générer une.