Adding generating PDF + message
This commit is contained in:
parent
7614a95489
commit
5ce2bb2fe4
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
@ -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<Int>
|
||||
) {
|
||||
override fun toString(): String {
|
||||
|
@ -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
|
||||
|
@ -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() {
|
||||
|
@ -23,10 +23,14 @@ class AddViewModel(
|
||||
) : ViewModel() {
|
||||
|
||||
private val _errorMessage = MutableLiveData<Event<Int>>()
|
||||
private val _dataMessage = MutableLiveData<Event<Int>>()
|
||||
|
||||
val errorMessage: LiveData<Event<Int>>
|
||||
get() = _errorMessage
|
||||
|
||||
val dataMessage: LiveData<Event<Int>>
|
||||
get() = _dataMessage
|
||||
|
||||
val firstName = MutableLiveData<String>()
|
||||
val lastName = MutableLiveData<String>()
|
||||
val birthday = MutableLiveData<String>()
|
||||
@ -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")
|
||||
|
@ -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
|
||||
) {
|
||||
|
||||
private val folderPath = "attestations"
|
||||
private val checkboxListNames = ArrayList<String>()
|
||||
|
||||
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<Int> = 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")
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
<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>
|
||||
|
||||
<!-- Field for attestation -->
|
||||
<string name="first_name">Prénom</string>
|
||||
|
Loading…
Reference in New Issue
Block a user