Update Kotlin Version

Remove Room library
Adding QRUtils and PdfUtils
This commit is contained in:
Mathieu Sanchez 2020-04-15 18:57:28 +02:00
parent c88adaef0b
commit 7614a95489
12 changed files with 137 additions and 24 deletions

View File

@ -64,11 +64,11 @@ dependencies {
implementation 'androidx.navigation:navigation-ui-ktx:2.2.1' implementation 'androidx.navigation:navigation-ui-ktx:2.2.1'
// Room // 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.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx: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 // PDF
implementation 'com.tom_roush:pdfbox-android:1.8.10.1' implementation 'com.tom_roush:pdfbox-android:1.8.10.1'
@ -76,6 +76,11 @@ dependencies {
// Gson // Gson
implementation 'com.google.code.gson:gson:2.8.6' 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' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Binary file not shown.

View File

@ -5,6 +5,9 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController 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 fr.sanchezm.attestationsCovid19.utilities.PdfUtils
import java.io.File
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -16,8 +19,17 @@ class MainActivity : AppCompatActivity() {
val navController = findNavController(R.id.nav_host_fragment) val navController = findNavController(R.id.nav_host_fragment)
PDFBoxResourceLoader.init(applicationContext)
PdfUtils.getInstance(assets, getMyFilesDir())
actionBar?.hide() actionBar?.hide()
navView.setupWithNavController(navController) navView.setupWithNavController(navController)
navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme)) navView.setBackgroundColor(resources.getColor(R.color.itemBackground, theme))
} }
private fun getMyFilesDir(): File? {
val path = getExternalFilesDir(null)
path ?: filesDir
return path
}
} }

View File

@ -1,22 +1,15 @@
package fr.sanchezm.attestationsCovid19.data.db.entity package fr.sanchezm.attestationsCovid19.data.db.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.lang.StringBuilder import java.lang.StringBuilder
const val CURRENT_ATTESTATION_ID = 0 //@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: String, val createAt: Long,
val reasons: List<Int> val reasons: List<Int>
) { ) {
@PrimaryKey
var id: Int = CURRENT_ATTESTATION_ID
override fun toString(): String { override fun toString(): String {
val motifs = StringBuilder() val motifs = StringBuilder()

View File

@ -1,11 +1,6 @@
package fr.sanchezm.attestationsCovid19.data.db.entity package fr.sanchezm.attestationsCovid19.data.db.entity
import androidx.room.Entity //@Entity(tableName = "profile")
import androidx.room.PrimaryKey
const val CURRENT_PROFILE_ID = 0
@Entity(tableName = "profile")
data class Profile( data class Profile(
val firstName: String, val firstName: String,
val lastName: String, val lastName: String,
@ -15,8 +10,8 @@ data class Profile(
val city: String, val city: String,
val postalCode: String val postalCode: String
) { ) {
@PrimaryKey // @PrimaryKey
var id: Int = CURRENT_PROFILE_ID // var id: Int = CURRENT_PROFILE_ID
override fun toString(): String { override fun toString(): String {
return "Nom: $firstName; Prenom: $lastName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city" return "Nom: $firstName; Prenom: $lastName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city"

View File

@ -1,9 +1,15 @@
package fr.sanchezm.attestationsCovid19.data.repository package fr.sanchezm.attestationsCovid19.data.repository
import fr.sanchezm.attestationsCovid19.data.db.dao.AttestationDao 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) { class AttestationRepository private constructor(private val attestationDao: AttestationDao) {
fun generateAttestation(attestation: Attestation) {
PdfUtils.getInstance()?.fillForm(attestation)
}
companion object { companion object {
@Volatile @Volatile
private var instance: AttestationRepository? = null private var instance: AttestationRepository? = null

View File

@ -33,7 +33,7 @@ class AddFragment : Fragment() {
this.viewModel = addViewModel this.viewModel = addViewModel
} }
bindMessage(binding.context) bindMessage(binding.constraintContext)
return binding.root return binding.root
} }

View File

@ -7,6 +7,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import fr.sanchezm.attestationsCovid19.R 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.db.entity.Profile
import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository
import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository import fr.sanchezm.attestationsCovid19.data.repository.ProfileRepository
@ -51,6 +52,7 @@ class AddViewModel(
fun onGenerateAttestationClick() { fun onGenerateAttestationClick() {
if (checkAllValue()) { if (checkAllValue()) {
profileRepository.insertProfile(getProfileFromView()) profileRepository.insertProfile(getProfileFromView())
attestationRepository.generateAttestation(getAttestation())
} 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")
@ -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<Int> {
val reasons = ArrayList<Int>()
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 { private fun checkAllValue(): Boolean {
return !firstName.value.isNullOrEmpty() return !firstName.value.isNullOrEmpty()
&& !lastName.value.isNullOrEmpty() && !lastName.value.isNullOrEmpty()

View File

@ -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<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()
} 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 }
}
}
}

View File

@ -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)
}

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.add.AddFragment">
<data> <data>
@ -13,8 +14,7 @@
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/context" android:id="@+id/constraintContext">
tools:context=".ui.add.AddFragment">
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext.kotlin_version = '1.3.71' ext.kotlin_version = '1.3.72'
repositories { repositories {
google() google()
jcenter() jcenter()