Adding list of attestations

This commit is contained in:
Mathieu Sanchez 2020-04-16 16:42:28 +02:00
parent f135425444
commit 54d2a100d0
10 changed files with 110 additions and 21 deletions

View File

@ -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<PDFView>(R.id.pdfView).also {
pdfView.also {
it.fromFile(File(pdfUtils.getPath(createAt)))
.enableAnnotationRendering(true)
.spacing(20)

View File

@ -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<Int>
) {
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"
}
}

View File

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

View File

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

View File

@ -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<Attestation>) :
RecyclerView.Adapter<AttestationsAdapter.ViewHolder>() {
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<Attestation>) {
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}"
}
}

View File

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

View File

@ -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<Event<Long>>()
private val _attestationName = MutableLiveData<String>()
private var _attestations = attestationRepository.getAttestations() as MutableLiveData<ArrayList<Attestation>>
val attestations: LiveData<ArrayList<Attestation>> =
_attestations
val startActivity: LiveData<Event<Long>> =
_startActivity
val attestationName: LiveData<String> =
_attestationName
fun onClick() {
_startActivity.value = Event(attestationCreateAt)
}
init {
val attestation = attestationRepository.getAttestations().value?.last()
attestationCreateAt = attestation?.createAt!!

View File

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

View File

@ -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">
<TextView
android:id="@+id/people_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/people_item"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
@ -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" />
<TextView
android:id="@+id/reason_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reason_item"
android:layout_marginTop="5dp"
android:layout_marginTop="3dp"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@+id/date_item"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/people_item_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/date_item_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:layout_constraintTop_toBottomOf="@id/people_item_data"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/reason_item_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:layout_constraintTop_toBottomOf="@id/date_item_data"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -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"