Adding error return if pdfUtils error

This commit is contained in:
Mathieu Sanchez 2020-04-26 17:12:38 +02:00
parent 1f255951a0
commit 514d1dff4e
4 changed files with 47 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import android.view.View
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import fr.sanchezm.attestationsCovid19.R
import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository import fr.sanchezm.attestationsCovid19.data.repository.ConfigRepository
@ -28,6 +29,7 @@ class QrCodeViewModel(
private val _startActivity = MutableLiveData<Event<Long>>() private val _startActivity = MutableLiveData<Event<Long>>()
private val _finish = MutableLiveData<Event<Boolean>>() private val _finish = MutableLiveData<Event<Boolean>>()
private val _error = MutableLiveData<Event<Int>>()
val attestation: LiveData<Attestation> = _attestation val attestation: LiveData<Attestation> = _attestation
@ -40,6 +42,7 @@ class QrCodeViewModel(
val startActivity: LiveData<Event<Long>> = _startActivity val startActivity: LiveData<Event<Long>> = _startActivity
val finish: LiveData<Event<Boolean>> = _finish val finish: LiveData<Event<Boolean>> = _finish
val error: LiveData<Event<Int>> = _error
fun addData(createAt: Long, toCreate: Boolean) { fun addData(createAt: Long, toCreate: Boolean) {
val att = attestationRepository.getAttestation(createAt) val att = attestationRepository.getAttestation(createAt)
@ -69,6 +72,8 @@ class QrCodeViewModel(
) { success -> ) { success ->
if (success) if (success)
displayQrCode() displayQrCode()
else
_error.value = Event(R.string.error_failed_create_pdf)
} }
} }
} }

View File

@ -33,14 +33,17 @@ class PdfUtils private constructor(
val acroForm = docCatalog.acroForm val acroForm = docCatalog.acroForm
// Adding data // Adding data
setFieldsData(acroForm, attestation) if (setFieldsData(acroForm, attestation)) {
setCheckboxFields(acroForm, attestation) setCheckboxFields(acroForm, attestation)
addPageWithQrCode(document, attestation) addPageWithQrCode(document, attestation)
Log.v("PdfUtils", "Save File to ${getPath(attestation.createAt)}") Log.v("PdfUtils", "Save File to ${getPath(attestation.createAt)}")
document.save(getPath(attestation.createAt)) document.save(getPath(attestation.createAt))
document.close() document.close()
callback(true) callback(true)
} else {
callback(false)
}
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
callback(false) 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 val profile = attestation.profile
// Init Fields try {
val namesField = acroForm.getField("Nom et prénom") as PDTextField // Init Fields
val birthdayField = acroForm.getField("Date de naissance") as PDTextField val namesField = acroForm.getField("Nom et prénom") as PDTextField
val birthPlaceField = acroForm.getField("Lieu de naissance") as PDTextField val birthdayField = acroForm.getField("Date de naissance") as PDTextField
val addressField = acroForm.getField("Adresse actuelle") as PDTextField val birthPlaceField = acroForm.getField("Lieu de naissance") as PDTextField
val cityField = acroForm.getField("Ville") as PDTextField val addressField = acroForm.getField("Adresse actuelle") as PDTextField
val dateField = acroForm.getField("Date") as PDTextField val cityField = acroForm.getField("Ville") as PDTextField
val hourField = acroForm.getField("Heure") as PDTextField val dateField = acroForm.getField("Date") as PDTextField
val minutesField = acroForm.getField("Minute") as PDTextField val hourField = acroForm.getField("Heure") as PDTextField
val minutesField = acroForm.getField("Minute") as PDTextField
// val signatureField = acroForm.getField("Signature") as PDTextField // val signatureField = acroForm.getField("Signature") as PDTextField
namesField.value = "${profile.firstName} ${profile.lastName}" namesField.value = "${profile.firstName} ${profile.lastName}"
birthdayField.value = profile.birthday birthdayField.value = profile.birthday
birthPlaceField.value = profile.birthPlace birthPlaceField.value = profile.birthPlace
addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}" addressField.value = "${profile.address} ${profile.postalCode} ${profile.city}"
cityField.value = profile.city cityField.value = profile.city
dateField.value = attestation.exitDate dateField.value = attestation.exitDate
hourField.value = attestation.exitHour.split("h")[0] hourField.value = attestation.exitHour.split("h")[0]
minutesField.value = attestation.exitHour.split("h")[1] 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) { private fun setCheckboxFields(acroForm: PDAcroForm, attestation: Attestation) {
@ -150,6 +158,7 @@ class PdfUtils private constructor(
private var instance: PdfUtils? = null private var instance: PdfUtils? = null
private val LOCK = Any() private val LOCK = Any()
private const val folderPath = "attestations" private const val folderPath = "attestations"
private val TAG = PdfUtils::class.simpleName
fun getInstance() = instance fun getInstance() = instance

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="error_cannot_create_attestation">Erreur lors de la génération de l\'attestation, tout les champs ne sont pas compléter</string>
<string name="error_failed_create_pdf">Erreur lors de la génération de l\'attestion, veuillez vérifiez vos champs ou contacter le développeur.</string>
</resources>

View File

@ -7,7 +7,6 @@
<!-- Attestation Fragment --> <!-- Attestation Fragment -->
<string name="attestation_title">Attestation de déplacement dérogatoire</string> <string name="attestation_title">Attestation de déplacement dérogatoire</string>
<string name="attestation_subtitle">#RestonsChezNous</string> <string name="attestation_subtitle">#RestonsChezNous</string>
<string name="error_cannot_create_attestation">Erreur lors de la génération de l\'attestation, tout les champs ne sont pas compléter</string>
<string name="attestation_generated">Attestation générer</string> <string name="attestation_generated">Attestation générer</string>
<string name="no_attestation">Aucune attestation, veuillez en générer une.</string> <string name="no_attestation">Aucune attestation, veuillez en générer une.</string>