Adding error return if pdfUtils error
This commit is contained in:
		@@ -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<Event<Long>>()
 | 
			
		||||
    private val _finish = MutableLiveData<Event<Boolean>>()
 | 
			
		||||
    private val _error = MutableLiveData<Event<Int>>()
 | 
			
		||||
 | 
			
		||||
    val attestation: LiveData<Attestation> = _attestation
 | 
			
		||||
 | 
			
		||||
@@ -40,6 +42,7 @@ class QrCodeViewModel(
 | 
			
		||||
 | 
			
		||||
    val startActivity: LiveData<Event<Long>> = _startActivity
 | 
			
		||||
    val finish: LiveData<Event<Boolean>> = _finish
 | 
			
		||||
    val error: LiveData<Event<Int>> = _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)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								app/src/main/res/values/errors.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								app/src/main/res/values/errors.xml
									
									
									
									
									
										Normal 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>
 | 
			
		||||
@@ -7,7 +7,6 @@
 | 
			
		||||
    <!-- Attestation Fragment -->
 | 
			
		||||
    <string name="attestation_title">Attestation de déplacement dérogatoire</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="no_attestation">Aucune attestation, veuillez en générer une.</string>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user