diff --git a/app/build.gradle b/app/build.gradle index ee40606..2ab8985 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,11 +11,6 @@ android { enabled = true } - configurations.all() { - resolutionStrategy.force "org.antlr:antlr4-runtime:4.5.3" - resolutionStrategy.force "org.antlr:antlr4-tool:4.5.3" - } - defaultConfig { applicationId "fr.sanchezm.attestationsCovid19" minSdkVersion 23 diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt index 1f8bb58..523697b 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt @@ -7,7 +7,6 @@ import androidx.navigation.ui.setupWithNavController import com.google.android.material.bottomnavigation.BottomNavigationView import com.tom_roush.pdfbox.util.PDFBoxResourceLoader import fr.sanchezm.attestationsCovid19.utilities.PdfUtils -import java.io.File class MainActivity : AppCompatActivity() { @@ -26,10 +25,6 @@ class MainActivity : AppCompatActivity() { navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme)) } - private fun getMyFilesDir(): File? { - val path = getExternalFilesDir(null) + private fun getMyFilesDir(): String = filesDir.path - path ?: filesDir - return path - } } \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt index a83435d..fb5181f 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Attestation.kt @@ -1,13 +1,11 @@ package fr.sanchezm.attestationsCovid19.data.db.entity -import java.lang.StringBuilder - //@Entity(tableName = "attestation") data class Attestation( val profile: Profile, val exitDate: String, val exitHour: String, - val createAt: Long, + var createAt: Long, val reasons: List ) { override fun toString(): String { diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt index 85665a9..e041ab1 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/repository/AttestationRepository.kt @@ -6,9 +6,8 @@ import fr.sanchezm.attestationsCovid19.utilities.PdfUtils class AttestationRepository private constructor(private val attestationDao: AttestationDao) { - fun generateAttestation(attestation: Attestation) { + fun generateAttestation(attestation: Attestation) = PdfUtils.getInstance()?.fillForm(attestation) - } companion object { @Volatile diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt index 220b735..f5c7eb2 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddFragment.kt @@ -42,7 +42,13 @@ class AddFragment : Fragment() { it.getContentIfNotHandled()?.let { stringId -> Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT ).show() } - }); + }) + + addViewModel.dataMessage.observe(viewLifecycleOwner, Observer { + it.getContentIfNotHandled()?.let { stringId -> + Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT).show() + } + }) } private fun initializeUi() { diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt index 8aa17c1..9ec7cfb 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/add/AddViewModel.kt @@ -23,10 +23,14 @@ class AddViewModel( ) : ViewModel() { private val _errorMessage = MutableLiveData>() + private val _dataMessage = MutableLiveData>() val errorMessage: LiveData> get() = _errorMessage + val dataMessage: LiveData> + get() = _dataMessage + val firstName = MutableLiveData() val lastName = MutableLiveData() val birthday = MutableLiveData() @@ -53,6 +57,7 @@ class AddViewModel( if (checkAllValue()) { profileRepository.insertProfile(getProfileFromView()) attestationRepository.generateAttestation(getAttestation()) + _dataMessage.value = Event(R.string.attestation_generated) } else { _errorMessage.value = Event(R.string.error_cannot_create_attestation) Log.e("onGenerateAttestationClick", "Cannot generate Attestation") 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 b04c51d..c9159e3 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt @@ -1,49 +1,88 @@ package fr.sanchezm.attestationsCovid19.utilities import android.content.res.AssetManager +import android.util.Log import com.tom_roush.pdfbox.pdmodel.PDDocument -import com.tom_roush.pdfbox.pdmodel.interactive.form.* +import com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm +import com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckbox +import com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import java.io.File import java.io.IOException +import java.util.* +import kotlin.collections.ArrayList -class PdfUtils private constructor(private val assetManager: AssetManager, private val path: File?) { - private val folderPath = "/attestations/" +class PdfUtils private constructor( + private val assetManager: AssetManager, + private val path: String +) { - fun fillForm (attestation: Attestation) { + private val folderPath = "attestations" + private val checkboxListNames = ArrayList() + + fun fillForm(attestation: Attestation) { + checkFolder() try { // Load the document and get the AcroForm val document: PDDocument = PDDocument.load(assetManager.open("attestation.pdf")) val docCatalog = document.documentCatalog val acroForm = docCatalog.acroForm - // Fill the text field - val field = acroForm.getField("TextField") as PDTextField - field.value = "Filled Text Field" - // Optional: don't allow this field to be edited - field.isReadOnly = true - val checkbox = acroForm.getField("Checkbox") - (checkbox as PDCheckbox).check() -// val radio = acroForm.getField("Radio") -// (radio as PDRadioButton).value = "Second" -// val listbox = acroForm.getField("ListBox") -// val listValues: MutableList = ArrayList() -// listValues.add(1) -// listValues.add(2) -// (listbox as PDListBox).selectedOptionsIndex = listValues -// val dropdown = acroForm.getField("Dropdown") -// (dropdown as PDComboBox).setValue("Hello") - val path = "" -// val path: String = root.getAbsolutePath().toString() + "/Download/FilledForm.pdf" -// tv.setText("Saved filled form to $path") - document.save(path) + // Adding data + setFieldsData(acroForm, attestation) + setCheckboxFields(acroForm, attestation) + attestation.createAt = Date().time + + val savePath = "$path/$folderPath/${attestation.createAt}.pdf" + + Log.v("PdfUtils", "Save File to $savePath") + document.save(savePath) document.close() } catch (e: IOException) { e.printStackTrace() } } + private fun setFieldsData(acroForm: PDAcroForm, attestation: Attestation) { + 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 +// val signatureField = acroForm.getField("Signature") as PDTextField + + namesField.value = "${profile.firstName} ${profile.lastName}" + birthdayField.value = profile.birthPlace + 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(":")[0] + minutesField.value = attestation.exitHour.split(":")[1] + + } + + private fun setCheckboxFields(acroForm: PDAcroForm, attestation: Attestation) { + attestation.reasons.forEach { + (acroForm.getField(checkboxListNames[it - 1]) as PDCheckbox).check() + } + } + + private fun checkFolder() { + val file = "$path/$folderPath" + + if (!File(file).exists()) { + File(file).mkdir() + } + } + companion object { @Volatile private var instance: PdfUtils? = null @@ -51,9 +90,18 @@ class PdfUtils private constructor(private val assetManager: AssetManager, priva fun getInstance() = instance - fun getInstance(assetManager: AssetManager, path: File?) = instance ?: synchronized(LOCK) { - instance ?: PdfUtils(assetManager, path).also { instance = it } + fun getInstance(assetManager: AssetManager, path: String) = instance ?: synchronized(LOCK) { + instance ?: PdfUtils(assetManager, path).also { instance = it; it.initList() } } } + private fun initList() { + checkboxListNames.add("Déplacements entre domicile et travail") + checkboxListNames.add("Déplacements achats nécéssaires") + checkboxListNames.add("Consultations et soins") + checkboxListNames.add("Déplacements pour motif familial") + checkboxListNames.add("Déplacements brefs (activité physique et animaux)") + checkboxListNames.add("Convcation judiciaire ou administrative") + checkboxListNames.add("Mission d'intérêt général") + } } \ 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 7577999..0c14c75 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,6 +8,7 @@ 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 Prénom