From 54d2a100d0f6eaaf27830db97465bae559ebba98 Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Thu, 16 Apr 2020 16:42:28 +0200 Subject: [PATCH] Adding list of attestations --- .../attestationsCovid19/PdfViewerActivity.kt | 3 +- .../data/db/entity/Attestation.kt | 16 ++++--- .../data/db/entity/Profile.kt | 2 +- .../ui/add/AddViewModel.kt | 2 +- .../ui/attestations/AttestationsAdapter.kt | 45 +++++++++++++++++++ .../ui/attestations/AttestationsFragment.kt | 11 +++++ .../ui/attestations/AttestationsViewModel.kt | 13 +++--- .../attestationsCovid19/utilities/PdfUtils.kt | 4 +- app/src/main/res/layout/attestation_item.xml | 34 ++++++++++++-- .../main/res/layout/fragment_attestations.xml | 1 + 10 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/PdfViewerActivity.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/PdfViewerActivity.kt index 0f91858..1e838a8 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/PdfViewerActivity.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/PdfViewerActivity.kt @@ -4,6 +4,7 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.github.barteksc.pdfviewer.PDFView import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils +import kotlinx.android.synthetic.main.activity_pdf_viewer.* import java.io.File class PdfViewerActivity : AppCompatActivity() { @@ -14,7 +15,7 @@ class PdfViewerActivity : AppCompatActivity() { val createAt = intent.getLongExtra("createAt", 0) val pdfUtils = InjectorUtils.providePdfUtils(applicationContext) - findViewById(R.id.pdfView).also { + pdfView.also { it.fromFile(File(pdfUtils.getPath(createAt))) .enableAnnotationRendering(true) .spacing(20) 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 2af8b61..6e0397c 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 @@ -3,7 +3,7 @@ package fr.sanchezm.attestationsCovid19.data.db.entity import java.text.SimpleDateFormat import java.util.* -const val PATTERN = "dd/MM/yyyy 'a' HH:mm" +const val PATTERN = "dd/MM/yyyy 'a' HH'h'mm" //@Entity(tableName = "attestation") data class Attestation( @@ -14,14 +14,18 @@ data class Attestation( val reasons: List ) { override fun toString(): String { - val motifs = StringBuilder() - - repeat(reasons.size) { motifs.append(getMotifText(it), "-") }.also { motifs.dropLast(1) } - return "Cree le: ${getDate(createAt)}; $profile; Sortie: $exitDate a $exitHour; Motifs: $motifs" + return "Cree le: ${getDate(createAt)}; $profile; Sortie: $exitDate a $exitHour; Motifs: ${getMotifsText()}" } fun getCreatedAtFormatedDate(): String = getDate(createAt) + fun getMotifsText(): String { + val motifs = StringBuilder() + + reasons.forEach { motifs.append(getMotifText(it), "-") } + return motifs.toString().dropLast(1) + } + private fun getDate(dateTime: Long): String = SimpleDateFormat(PATTERN, Locale.FRANCE).format(Date(dateTime)) private fun getMotifText(i: Int): String { @@ -33,7 +37,7 @@ data class Attestation( 5 -> "sport" 6 -> "judiciare" 7 -> "missions" - else -> "" + else -> "Error $i not found" } } 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 23a8d36..6d3ae1d 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 @@ -14,6 +14,6 @@ data class Profile( // var id: Int = CURRENT_PROFILE_ID override fun toString(): String { - return "Nom: $firstName; Prenom: $lastName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city" + return "Nom: $lastName; Prenom: $firstName; Naissance: $birthday a $birthPlace; Adresse: $address $postalCode $city" } } \ No newline at end of file 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 bde8727..9fe61e4 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 @@ -50,7 +50,7 @@ class AddViewModel( val reason7 = MutableLiveData(false) private val datePattern = "dd/MM/yyyy" - private val timePattern = "HH:mm" + private val timePattern = "HH'h'mm" @SuppressLint("LongLogTag") fun onGenerateAttestationClick() { diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt new file mode 100644 index 0000000..eb22c7d --- /dev/null +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsAdapter.kt @@ -0,0 +1,45 @@ +package fr.sanchezm.attestationsCovid19.ui.attestations + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import fr.sanchezm.attestationsCovid19.R +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation +import kotlinx.android.synthetic.main.attestation_item.view.* + +class AttestationsAdapter(private val items: ArrayList) : + RecyclerView.Adapter() { + + override fun getItemCount(): Int { + return items.size + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = + ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.attestation_item, parent, false)) + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.bind(items[(items.size - 1) - position]) + } + + fun updateAttestations(attestations: List) { + items.clear() + items.addAll(attestations) + notifyDataSetChanged() + } + + class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + private val peopleItem = view.people_item_data + private val dateItem = view.date_item_data + private val reasonItem = view.reason_item_data + + fun bind(attestation: Attestation) { + peopleItem.text = getName(attestation) + dateItem.text = attestation.getCreatedAtFormatedDate() + reasonItem.text = attestation.getMotifsText() + } + + private fun getName(attestation: Attestation) = "${attestation.profile.firstName} ${attestation.profile.lastName}" + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt index e1f1e11..b01caae 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsFragment.kt @@ -9,14 +9,17 @@ import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import androidx.recyclerview.widget.LinearLayoutManager import fr.sanchezm.attestationsCovid19.PdfViewerActivity import fr.sanchezm.attestationsCovid19.R import fr.sanchezm.attestationsCovid19.databinding.FragmentAttestationsBinding import fr.sanchezm.attestationsCovid19.utilities.InjectorUtils +import kotlinx.android.synthetic.main.fragment_attestations.view.* class AttestationsFragment : Fragment() { private lateinit var attestationsViewModel: AttestationsViewModel + private val attestationsAdapter = AttestationsAdapter(arrayListOf()) override fun onCreateView( inflater: LayoutInflater, @@ -34,6 +37,10 @@ class AttestationsFragment : Fragment() { this.viewModel = attestationsViewModel } + binding.root.attestation_recycler_view.apply { + layoutManager = LinearLayoutManager(context) + adapter = attestationsAdapter + } bindMessage() return binding.root } @@ -47,6 +54,10 @@ class AttestationsFragment : Fragment() { startActivity(intent) } }) + + attestationsViewModel.attestations.observe(viewLifecycleOwner, Observer { + attestationsAdapter.updateAttestations(it) + }) } private fun initializeUi() { diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt index 1d56f32..53ca775 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/ui/attestations/AttestationsViewModel.kt @@ -3,6 +3,7 @@ package fr.sanchezm.attestationsCovid19.ui.attestations import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import fr.sanchezm.attestationsCovid19.data.db.entity.Attestation import fr.sanchezm.attestationsCovid19.data.repository.AttestationRepository import fr.sanchezm.attestationsCovid19.utilities.Event @@ -12,16 +13,14 @@ class AttestationsViewModel(private val attestationRepository: AttestationReposi private val _startActivity = MutableLiveData>() private val _attestationName = MutableLiveData() + private var _attestations = attestationRepository.getAttestations() as MutableLiveData> + + val attestations: LiveData> = + _attestations + val startActivity: LiveData> = _startActivity - val attestationName: LiveData = - _attestationName - - fun onClick() { - _startActivity.value = Event(attestationCreateAt) - } - init { val attestation = attestationRepository.getAttestations().value?.last() attestationCreateAt = attestation?.createAt!! diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt index 6b22691..b3c24c4 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/utilities/PdfUtils.kt @@ -128,8 +128,8 @@ class PdfUtils private constructor( 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] + hourField.value = attestation.exitHour.split("h")[0] + minutesField.value = attestation.exitHour.split("h")[1] } diff --git a/app/src/main/res/layout/attestation_item.xml b/app/src/main/res/layout/attestation_item.xml index d5eb6fc..8301124 100644 --- a/app/src/main/res/layout/attestation_item.xml +++ b/app/src/main/res/layout/attestation_item.xml @@ -4,13 +4,15 @@ android:background="@drawable/custom_rectangle" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_margin="10dp"> + android:layout_marginBottom="15dp" + android:padding="15dp"> @@ -19,16 +21,42 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/date_item" - android:layout_marginTop="5dp" + android:layout_marginTop="3dp" + android:textStyle="bold" app:layout_constraintTop_toBottomOf="@+id/people_item" app:layout_constraintStart_toStartOf="parent" /> + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_attestations.xml b/app/src/main/res/layout/fragment_attestations.xml index ea657fc..f2f0f47 100644 --- a/app/src/main/res/layout/fragment_attestations.xml +++ b/app/src/main/res/layout/fragment_attestations.xml @@ -33,6 +33,7 @@ android:id="@+id/attestation_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginTop="80dp" app:layout_constraintTop_toBottomOf="@id/title" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"