Update Kotlin Version
Remove Room library Adding QRUtils and PdfUtils
This commit is contained in:
		| @@ -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' | ||||
|   | ||||
							
								
								
									
										
											BIN
										
									
								
								app/src/main/assets/attestation.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/assets/attestation.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| @@ -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 | ||||
|     } | ||||
| } | ||||
| @@ -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<Int> | ||||
| ) { | ||||
|     @PrimaryKey | ||||
|     var id: Int = CURRENT_ATTESTATION_ID | ||||
|  | ||||
|     override fun toString(): String { | ||||
|         val motifs = StringBuilder() | ||||
|  | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -33,7 +33,7 @@ class AddFragment : Fragment() { | ||||
|             this.viewModel = addViewModel | ||||
|         } | ||||
|  | ||||
|         bindMessage(binding.context) | ||||
|         bindMessage(binding.constraintContext) | ||||
|         return binding.root | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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<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 { | ||||
|         return !firstName.value.isNullOrEmpty() | ||||
|                 && !lastName.value.isNullOrEmpty() | ||||
|   | ||||
| @@ -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 } | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
| } | ||||
| @@ -1,7 +1,8 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <layout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     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> | ||||
|  | ||||
| @@ -13,8 +14,7 @@ | ||||
|     <androidx.constraintlayout.widget.ConstraintLayout | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:id="@+id/context" | ||||
|         tools:context=".ui.add.AddFragment"> | ||||
|         android:id="@+id/constraintContext"> | ||||
|  | ||||
|         <androidx.core.widget.NestedScrollView | ||||
|             android:layout_width="match_parent" | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| // Top-level build file where you can add configuration options common to all sub-projects/modules. | ||||
|  | ||||
| buildscript { | ||||
|     ext.kotlin_version = '1.3.71' | ||||
|     ext.kotlin_version = '1.3.72' | ||||
|     repositories { | ||||
|         google() | ||||
|         jcenter() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user