Adding generating PDF + message
This commit is contained in:
parent
7614a95489
commit
5ce2bb2fe4
@ -11,11 +11,6 @@ android {
|
|||||||
enabled = true
|
enabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
configurations.all() {
|
|
||||||
resolutionStrategy.force "org.antlr:antlr4-runtime:4.5.3"
|
|
||||||
resolutionStrategy.force "org.antlr:antlr4-tool:4.5.3"
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "fr.sanchezm.attestationsCovid19"
|
applicationId "fr.sanchezm.attestationsCovid19"
|
||||||
minSdkVersion 23
|
minSdkVersion 23
|
||||||
|
@ -7,7 +7,6 @@ import androidx.navigation.ui.setupWithNavController
|
|||||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||||
import com.tom_roush.pdfbox.util.PDFBoxResourceLoader
|
import com.tom_roush.pdfbox.util.PDFBoxResourceLoader
|
||||||
import fr.sanchezm.attestationsCovid19.utilities.PdfUtils
|
import fr.sanchezm.attestationsCovid19.utilities.PdfUtils
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@ -26,10 +25,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme))
|
navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMyFilesDir(): File? {
|
private fun getMyFilesDir(): String = filesDir.path
|
||||||
val path = getExternalFilesDir(null)
|
|
||||||
|
|
||||||
path ?: filesDir
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,13 +1,11 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.data.db.entity
|
package fr.sanchezm.attestationsCovid19.data.db.entity
|
||||||
|
|
||||||
import java.lang.StringBuilder
|
|
||||||
|
|
||||||
//@Entity(tableName = "attestation")
|
//@Entity(tableName = "attestation")
|
||||||
data class Attestation(
|
data class Attestation(
|
||||||
val profile: Profile,
|
val profile: Profile,
|
||||||
val exitDate: String,
|
val exitDate: String,
|
||||||
val exitHour: String,
|
val exitHour: String,
|
||||||
val createAt: Long,
|
var createAt: Long,
|
||||||
val reasons: List<Int>
|
val reasons: List<Int>
|
||||||
) {
|
) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
|
@ -6,9 +6,8 @@ import fr.sanchezm.attestationsCovid19.utilities.PdfUtils
|
|||||||
|
|
||||||
class AttestationRepository private constructor(private val attestationDao: AttestationDao) {
|
class AttestationRepository private constructor(private val attestationDao: AttestationDao) {
|
||||||
|
|
||||||
fun generateAttestation(attestation: Attestation) {
|
fun generateAttestation(attestation: Attestation) =
|
||||||
PdfUtils.getInstance()?.fillForm(attestation)
|
PdfUtils.getInstance()?.fillForm(attestation)
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Volatile
|
@Volatile
|
||||||
|
@ -42,7 +42,13 @@ class AddFragment : Fragment() {
|
|||||||
it.getContentIfNotHandled()?.let { stringId ->
|
it.getContentIfNotHandled()?.let { stringId ->
|
||||||
Snackbar.make(context, stringId, Snackbar.LENGTH_SHORT ).show()
|
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() {
|
private fun initializeUi() {
|
||||||
|
@ -23,10 +23,14 @@ class AddViewModel(
|
|||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val _errorMessage = MutableLiveData<Event<Int>>()
|
private val _errorMessage = MutableLiveData<Event<Int>>()
|
||||||
|
private val _dataMessage = MutableLiveData<Event<Int>>()
|
||||||
|
|
||||||
val errorMessage: LiveData<Event<Int>>
|
val errorMessage: LiveData<Event<Int>>
|
||||||
get() = _errorMessage
|
get() = _errorMessage
|
||||||
|
|
||||||
|
val dataMessage: LiveData<Event<Int>>
|
||||||
|
get() = _dataMessage
|
||||||
|
|
||||||
val firstName = MutableLiveData<String>()
|
val firstName = MutableLiveData<String>()
|
||||||
val lastName = MutableLiveData<String>()
|
val lastName = MutableLiveData<String>()
|
||||||
val birthday = MutableLiveData<String>()
|
val birthday = MutableLiveData<String>()
|
||||||
@ -53,6 +57,7 @@ class AddViewModel(
|
|||||||
if (checkAllValue()) {
|
if (checkAllValue()) {
|
||||||
profileRepository.insertProfile(getProfileFromView())
|
profileRepository.insertProfile(getProfileFromView())
|
||||||
attestationRepository.generateAttestation(getAttestation())
|
attestationRepository.generateAttestation(getAttestation())
|
||||||
|
_dataMessage.value = Event(R.string.attestation_generated)
|
||||||
} else {
|
} else {
|
||||||
_errorMessage.value = Event(R.string.error_cannot_create_attestation)
|
_errorMessage.value = Event(R.string.error_cannot_create_attestation)
|
||||||
Log.e("onGenerateAttestationClick", "Cannot generate Attestation")
|
Log.e("onGenerateAttestationClick", "Cannot generate Attestation")
|
||||||
|
@ -1,49 +1,88 @@
|
|||||||
package fr.sanchezm.attestationsCovid19.utilities
|
package fr.sanchezm.attestationsCovid19.utilities
|
||||||
|
|
||||||
import android.content.res.AssetManager
|
import android.content.res.AssetManager
|
||||||
|
import android.util.Log
|
||||||
import com.tom_roush.pdfbox.pdmodel.PDDocument
|
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 fr.sanchezm.attestationsCovid19.data.db.entity.Attestation
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
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<String>()
|
||||||
|
|
||||||
|
fun fillForm(attestation: Attestation) {
|
||||||
|
checkFolder()
|
||||||
try {
|
try {
|
||||||
// Load the document and get the AcroForm
|
// Load the document and get the AcroForm
|
||||||
val document: PDDocument = PDDocument.load(assetManager.open("attestation.pdf"))
|
val document: PDDocument = PDDocument.load(assetManager.open("attestation.pdf"))
|
||||||
val docCatalog = document.documentCatalog
|
val docCatalog = document.documentCatalog
|
||||||
val acroForm = docCatalog.acroForm
|
val acroForm = docCatalog.acroForm
|
||||||
|
|
||||||
// Fill the text field
|
// Adding data
|
||||||
val field = acroForm.getField("TextField") as PDTextField
|
setFieldsData(acroForm, attestation)
|
||||||
field.value = "Filled Text Field"
|
setCheckboxFields(acroForm, attestation)
|
||||||
// Optional: don't allow this field to be edited
|
attestation.createAt = Date().time
|
||||||
field.isReadOnly = true
|
|
||||||
val checkbox = acroForm.getField("Checkbox")
|
val savePath = "$path/$folderPath/${attestation.createAt}.pdf"
|
||||||
(checkbox as PDCheckbox).check()
|
|
||||||
// val radio = acroForm.getField("Radio")
|
Log.v("PdfUtils", "Save File to $savePath")
|
||||||
// (radio as PDRadioButton).value = "Second"
|
document.save(savePath)
|
||||||
// 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)
|
|
||||||
document.close()
|
document.close()
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
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 {
|
companion object {
|
||||||
@Volatile
|
@Volatile
|
||||||
private var instance: PdfUtils? = null
|
private var instance: PdfUtils? = null
|
||||||
@ -51,9 +90,18 @@ class PdfUtils private constructor(private val assetManager: AssetManager, priva
|
|||||||
|
|
||||||
fun getInstance() = instance
|
fun getInstance() = instance
|
||||||
|
|
||||||
fun getInstance(assetManager: AssetManager, path: File?) = instance ?: synchronized(LOCK) {
|
fun getInstance(assetManager: AssetManager, path: String) = instance ?: synchronized(LOCK) {
|
||||||
instance ?: PdfUtils(assetManager, path).also { instance = it }
|
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_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="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 -->
|
<!-- Field for attestation -->
|
||||||
<string name="first_name">Prénom</string>
|
<string name="first_name">Prénom</string>
|
||||||
|
Loading…
Reference in New Issue
Block a user