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,7 +33,7 @@ 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)
@ -41,6 +41,9 @@ class PdfUtils private constructor(
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,9 +109,10 @@ 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
try {
// Init Fields // Init Fields
val namesField = acroForm.getField("Nom et prénom") as PDTextField val namesField = acroForm.getField("Nom et prénom") as PDTextField
val birthdayField = acroForm.getField("Date de naissance") as PDTextField val birthdayField = acroForm.getField("Date de naissance") as PDTextField
@ -128,7 +132,11 @@ class PdfUtils private constructor(
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>