From 9bbc364c11988cc029e3e82aca5d7520ba83194c Mon Sep 17 00:00:00 2001 From: Mathieu Sanchez Date: Thu, 16 Apr 2020 10:29:46 +0200 Subject: [PATCH] Setting good date in attestation for toString method, adding save after ad attestation --- .../attestationsCovid19/data/db/dao/AttestationDao.kt | 1 + .../attestationsCovid19/data/db/entity/Attestation.kt | 11 ++++++++++- .../attestationsCovid19/ui/add/AddViewModel.kt | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt index 8ea0a27..eef5377 100644 --- a/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt +++ b/app/src/main/java/fr/sanchezm/attestationsCovid19/data/db/dao/AttestationDao.kt @@ -21,6 +21,7 @@ class AttestationDao(private val path: String) { fun addAttestation(attestation: Attestation) { _attestations.value?.add(attestation) + save() } init { 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 fb5181f..e9d0380 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,5 +1,8 @@ package fr.sanchezm.attestationsCovid19.data.db.entity +import java.text.SimpleDateFormat +import java.util.* + //@Entity(tableName = "attestation") data class Attestation( val profile: Profile, @@ -8,11 +11,17 @@ data class Attestation( var createAt: Long, val reasons: List ) { + private val pattern = "dd/MM/yyyy 'a' HH:mm" + override fun toString(): String { val motifs = StringBuilder() repeat(reasons.size) { motifs.append(getMotifText(it), "-") }.also { motifs.dropLast(1) } - return "Cree le: $createAt; $profile; Sortie: $exitDate a $exitHour; Motifs: $motifs" + return "Cree le: ${getDate(createAt)}; $profile; Sortie: $exitDate a $exitHour; Motifs: $motifs" + } + + private fun getDate(dateTime: Long): String { + return SimpleDateFormat(pattern, Locale.FRANCE).format(Date(dateTime)) } private fun getMotifText(i: Int): String { 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 82460b2..bde8727 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 @@ -113,7 +113,7 @@ class AddViewModel( getProfileFromView(), exitDate.value.toString(), exitHour.value.toString(), - Date().time, + 0, getReasons() ) }