diff --git a/app/build.gradle b/app/build.gradle index 7426c18..ee40606 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,11 +64,11 @@ dependencies { implementation 'androidx.navigation:navigation-ui-ktx:2.2.1' // Room - implementation 'androidx.room:room-runtime:2.2.5' +// implementation 'androidx.room:room-runtime:2.2.5' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' - kapt 'androidx.room:room-compiler:2.2.5' +// kapt 'androidx.room:room-compiler:2.2.5' // PDF implementation 'com.tom_roush:pdfbox-android:1.8.10.1' @@ -76,6 +76,11 @@ dependencies { // Gson implementation 'com.google.code.gson:gson:2.8.6' + // QRCode + implementation 'com.google.zxing:core:3.4.0' + //noinspection GradleDependency + implementation 'com.journeyapps:zxing-android-embedded:3.6.0' + testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/assets/attestation.pdf b/app/src/main/assets/attestation.pdf new file mode 100644 index 0000000..22e0e02 Binary files /dev/null and b/app/src/main/assets/attestation.pdf differ diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt index bb20fda..1f8bb58 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/MainActivity.kt @@ -5,6 +5,9 @@ import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController 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() { @@ -16,8 +19,17 @@ class MainActivity : AppCompatActivity() { val navController = findNavController(R.id.nav_host_fragment) + PDFBoxResourceLoader.init(applicationContext) + PdfUtils.getInstance(assets, getMyFilesDir()) actionBar?.hide() navView.setupWithNavController(navController) navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme)) } + + private fun getMyFilesDir(): File? { + val path = getExternalFilesDir(null) + + 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 f1037e1..a83435d 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,22 +1,15 @@ package fr.sanchezm.attestationsCovid19.data.db.entity -import androidx.room.Entity -import androidx.room.PrimaryKey import java.lang.StringBuilder -const val CURRENT_ATTESTATION_ID = 0 - -@Entity(tableName = "attestation") +//@Entity(tableName = "attestation") data class Attestation( val profile: Profile, val exitDate: String, val exitHour: String, - val createAt: String, + val createAt: Long, val reasons: List ) { - @PrimaryKey - var id: Int = CURRENT_ATTESTATION_ID - override fun toString(): String { val motifs = StringBuilder() diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt index 310c9da..23a8d36 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/entity/Profile.kt @@ -1,11 +1,6 @@ package fr.sanchezm.attestationsCovid19.data.db.entity -import androidx.room.Entity -import androidx.room.PrimaryKey - -const val CURRENT_PROFILE_ID = 0 - -@Entity(tableName = "profile") +//@Entity(tableName = "profile") data class Profile( val firstName: String, val lastName: String, @@ -15,8 +10,8 @@ data class Profile( val city: String, val postalCode: String ) { - @PrimaryKey - var id: Int = CURRENT_PROFILE_ID +// @PrimaryKey +// var id: Int = CURRENT_PROFILE_ID override fun toString(): String { return "Nom: $firstName; Prenom: $lastName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city" 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 caaf8d6..85665a9 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 @@ -1,9 +1,15 @@ package fr.sanchezm.attestationsCovid19.data.repository import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation +import fr.sanchezm.attestationsCovid19.utilities.PdfUtils class AttestationRepository private constructor(private val attestationDao: AttestationDao) { + fun generateAttestation(attestation: Attestation) { + PdfUtils.getInstance()?.fillForm(attestation) + } + companion object { @Volatile private var instance: AttestationRepository? = null 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 e2bfd50..220b735 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 @@ -33,7 +33,7 @@ class AddFragment : Fragment() { this.viewModel = addViewModel } - bindMessage(binding.context) + bindMessage(binding.constraintContext) return binding.root } 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 d4bcb14..8aa17c1 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 @@ -7,6 +7,7 @@ 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.db.entity.Profile import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository @@ -51,6 +52,7 @@ class AddViewModel( fun onGenerateAttestationClick() { if (checkAllValue()) { profileRepository.insertProfile(getProfileFromView()) + attestationRepository.generateAttestation(getAttestation()) } else { _errorMessage.value = Event(R.string.error_cannot_create_attestation) Log.e("onGenerateAttestationClick", "Cannot generate Attestation") @@ -99,6 +101,30 @@ class AddViewModel( ) } + private fun getAttestation(): Attestation { + return Attestation( + getProfileFromView(), + exitDate.value.toString(), + exitHour.value.toString(), + Date().time, + getReasons() + ) + } + + private fun getReasons(): List { + val reasons = ArrayList() + + if (reason1.value!!) reasons.add(1) + if (reason2.value!!) reasons.add(2) + if (reason3.value!!) reasons.add(3) + if (reason4.value!!) reasons.add(4) + if (reason5.value!!) reasons.add(5) + if (reason6.value!!) reasons.add(6) + if (reason7.value!!) reasons.add(7) + + return reasons + } + private fun checkAllValue(): Boolean { return !firstName.value.isNullOrEmpty() && !lastName.value.isNullOrEmpty() diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt new file mode 100644 index 0000000..b04c51d --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt @@ -0,0 +1,59 @@ +package fr.sanchezm.attestationsCovid19.utilities + +import android.content.res.AssetManager +import com.tom_roush.pdfbox.pdmodel.PDDocument +import com.tom_roush.pdfbox.pdmodel.interactive.form.* +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation +import java.io.File +import java.io.IOException + +class PdfUtils private constructor(private val assetManager: AssetManager, private val path: File?) { + + private val folderPath = "/attestations/" + + fun fillForm (attestation: Attestation) { + 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) + document.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + companion object { + @Volatile + private var instance: PdfUtils? = null + private val LOCK = Any() + + fun getInstance() = instance + + fun getInstance(assetManager: AssetManager, path: File?) = instance ?: synchronized(LOCK) { + instance ?: PdfUtils(assetManager, path).also { instance = it } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/QRCodeUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/QRCodeUtils.kt new file mode 100644 index 0000000..fab1679 --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/QRCodeUtils.kt @@ -0,0 +1,17 @@ +package fr.sanchezm.attestationsCovid19.utilities + +import android.graphics.Bitmap +import com.google.zxing.BarcodeFormat +import com.google.zxing.MultiFormatWriter +import com.journeyapps.barcodescanner.BarcodeEncoder +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation + + +class QRCodeUtils { + + fun getQrCode(attestation: Attestation): Bitmap = + BarcodeEncoder().createBitmap(getMultiFormatWriter(attestation)) + + private fun getMultiFormatWriter(attestation: Attestation) = + MultiFormatWriter().encode(attestation.toString(), BarcodeFormat.QR_CODE, 150, 150) +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_add_attestation.xml b/app/src/main/res/layout/fragment_add_attestation.xml index d97a72f..258b519 100644 --- a/app/src/main/res/layout/fragment_add_attestation.xml +++ b/app/src/main/res/layout/fragment_add_attestation.xml @@ -1,7 +1,8 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + tools:context=".ui.add.AddFragment"> @@ -13,8 +14,7 @@ + android:id="@+id/constraintContext">