From c2ee3a5492e8bc81e4948bcea5631efbf47d68f9 Mon Sep 17 00:00:00 2001 From: marine Date: Thu, 29 Feb 2024 04:35:20 +0100 Subject: [PATCH 1/3] Affichage ne fonctionne pas, mais presque + j'ai sommeil --- .../database/dao/relations/RelationsDAO.kt | 4 ++ .../relations/RelationRepository.kt | 4 ++ .../screen/prescription/PrescriptionHome.kt | 67 ++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt b/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt index a9766b8f..84f78e47 100644 --- a/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt +++ b/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt @@ -15,6 +15,10 @@ interface RelationsDAO { @Query("SELECT * FROM RelationInfo") fun getAll() : List + @Transaction + @Query("SELECT * FROM RelationInfo WHERE substance = :substance") + fun getBySubstance(substance: String) : List + @Insert fun insert(relation: RelationInfo): Long diff --git a/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt b/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt index 5fda7ea4..03a97e87 100644 --- a/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt +++ b/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt @@ -11,6 +11,10 @@ class RelationRepository(contexte : Context) : Repository(contexte) { return db.RelationsDAO().getAll() } + fun getBySubstance(substance: String): List { + return db.RelationsDAO().getBySubstance(substance) + } + fun insert(relation: Relations): Long { val id = db.RelationsDAO().insert(relation.relationInfo) diff --git a/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt b/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt index c5273f14..be96a62c 100644 --- a/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt +++ b/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import fr.medicapp.medicapp.database.repositories.relations.RelationRepository import fr.medicapp.medicapp.model.prescription.Doctor import fr.medicapp.medicapp.model.prescription.relationship.Prescription import fr.medicapp.medicapp.ui.components.button.FloatingActionButtons @@ -36,6 +37,9 @@ import fr.medicapp.medicapp.ui.components.modal.ConfirmReportModal import fr.medicapp.medicapp.ui.components.screen.Home import fr.medicapp.medicapp.ui.theme.EUPurpleColorShema import fr.medicapp.medicapp.ui.theme.MedicAppTheme +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import okhttp3.Dispatcher @RequiresApi(Build.VERSION_CODES.Q) @Composable @@ -46,8 +50,10 @@ fun PrescriptionHome( ) { val noPrescriptionDialog = remember { mutableStateOf(false) } var alertRedondantOpen by remember { mutableStateOf(false) } + var alertRelationOpen by remember { mutableStateOf(false) } var isReportModalOpen by remember { mutableStateOf(false) } val context = LocalContext.current + val relations = RelationRepository(context) val sharedPrefs = context.getSharedPreferences("medicapp", Context.MODE_PRIVATE) val isNewMedicationAdded = sharedPrefs .getBoolean("isNewMedicationAdded", false) @@ -98,14 +104,16 @@ fun PrescriptionHome( } == true } && isNewMedicationAdded + alertRelationOpen = withContext(Dispatchers.IO) { + controlRelation(prescriptions.dropLast(1), prescriptions.last(), relations) + } + } PrescriptionList( prescriptions = prescriptions, onPrescriptionClick = onPrescriptionClick ) - } - else -> { PrescriptionList( prescriptions = prescriptions, @@ -136,6 +144,21 @@ fun PrescriptionHome( } ) } + if (alertRelationOpen) { + AlertModal( + title = "Relation médicamenteuse dangereuse", + content = "Attention, le médicament que vous venez de rajouter contient un principe actif qui peut interagir avec un autre médicament déjà présent dans vos traitements.", + dismissText = "", + confirmText = "Compris", + onConfirm = { + alertRelationOpen = false + with(sharedPrefs.edit()) { + putBoolean("isNewMedicationAdded", false) + apply() + } + } + ) + } if (noPrescriptionDialog.value) { AlertModal( title = "Erreur", @@ -201,6 +224,46 @@ fun NoPrescriptionAvailable() { ) } +suspend fun controlRelation( + prescriptions: List, + newPrescription : Prescription, + relationRepo : RelationRepository +): Boolean { + val substanceNames = newPrescription.medication?.medicationCompositions?.map { it.substanceName } + if (substanceNames.isNullOrEmpty()) { + return withContext(Dispatchers.IO) { + false + } + } + + val relations = relationRepo.getAll().map { it.relationInfo.substance } + for (substance in substanceNames) { + if (relations.contains(substance)) { + //On cherche si il y a intéraction avec un autre produit + //Interactions est une liste de substance + val interactions = relationRepo.getBySubstance(substance) + .map { it.interactions } + .flatten() + .map { it.substance } + for (prescription in prescriptions) { + val prescriptionSubstanceNames = prescription.medication?.medicationCompositions?.map { it.substanceName } + if (!prescriptionSubstanceNames.isNullOrEmpty()) { + for (prescriptionSubstance in prescriptionSubstanceNames) { + if (interactions.contains(prescriptionSubstance)) { + return withContext(Dispatchers.IO) { + true + } + } + } + } + } + } + } + return withContext(Dispatchers.IO) { + false + } +} + @RequiresApi(Build.VERSION_CODES.Q) @Preview(name = "Light Theme", showSystemUi = true) @Composable From 41f17d01e70afea3ad58dca70ef555365c04e331 Mon Sep 17 00:00:00 2001 From: marine Date: Thu, 29 Feb 2024 10:26:21 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Afficahge=20fonctionne=20pas=20mais=20c'est?= =?UTF-8?q?=20=C3=A0=20cause=20d'antonin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/screen/prescription/PrescriptionHome.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt b/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt index be96a62c..3c3ebd03 100644 --- a/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt +++ b/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt @@ -2,6 +2,7 @@ package fr.medicapp.medicapp.ui.screen.prescription import android.content.Context import android.os.Build +import android.util.Log import androidx.annotation.RequiresApi import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer @@ -105,7 +106,7 @@ fun PrescriptionHome( } && isNewMedicationAdded alertRelationOpen = withContext(Dispatchers.IO) { - controlRelation(prescriptions.dropLast(1), prescriptions.last(), relations) + controlRelation(prescriptions.dropLast(1), relations) } } @@ -226,10 +227,10 @@ fun NoPrescriptionAvailable() { suspend fun controlRelation( prescriptions: List, - newPrescription : Prescription, relationRepo : RelationRepository ): Boolean { - val substanceNames = newPrescription.medication?.medicationCompositions?.map { it.substanceName } + Log.d("GuegueTest", "ça rentre dans la méthode") + val substanceNames = prescriptions.last().medication?.medicationCompositions?.map { it.substanceName } if (substanceNames.isNullOrEmpty()) { return withContext(Dispatchers.IO) { false @@ -237,15 +238,20 @@ suspend fun controlRelation( } val relations = relationRepo.getAll().map { it.relationInfo.substance } + Log.d("Relation", relations.toString()) + Log.d("Relation", substanceNames.toString()) for (substance in substanceNames) { if (relations.contains(substance)) { //On cherche si il y a intéraction avec un autre produit //Interactions est une liste de substance + Log.d("GuegueTest", "ça rentre dans la boucle = relation trouvée") val interactions = relationRepo.getBySubstance(substance) .map { it.interactions } .flatten() .map { it.substance } + Log.d("GuegueTest", "voici les interactions : $interactions") for (prescription in prescriptions) { + Log.d("GuegueTest", "ça rentre dans la boucle2") val prescriptionSubstanceNames = prescription.medication?.medicationCompositions?.map { it.substanceName } if (!prescriptionSubstanceNames.isNullOrEmpty()) { for (prescriptionSubstance in prescriptionSubstanceNames) { From 96bb70a4e0cdcb3ba9460321436d809d149cbd42 Mon Sep 17 00:00:00 2001 From: marine Date: Thu, 29 Feb 2024 12:14:17 +0100 Subject: [PATCH 3/3] Affichage qui fonctionne --- .../database/dao/relations/RelationsDAO.kt | 4 ++ .../relations/RelationRepository.kt | 6 +++ .../medicapp/model/relations/RelationInfo.kt | 1 + .../model/relations/crossRef/Relations.kt | 2 +- .../screen/prescription/PrescriptionHome.kt | 48 +++++++++++++++---- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt b/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt index 84f78e47..62939c92 100644 --- a/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt +++ b/app/src/main/java/fr/medicapp/medicapp/database/dao/relations/RelationsDAO.kt @@ -15,6 +15,10 @@ interface RelationsDAO { @Query("SELECT * FROM RelationInfo") fun getAll() : List + @Transaction + @Query("SELECT * FROM RelationInfo WHERE relation_info_id = :id") + fun getById(id: Long) : Relations + @Transaction @Query("SELECT * FROM RelationInfo WHERE substance = :substance") fun getBySubstance(substance: String) : List diff --git a/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt b/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt index 03a97e87..4efa1ac5 100644 --- a/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt +++ b/app/src/main/java/fr/medicapp/medicapp/database/repositories/relations/RelationRepository.kt @@ -11,6 +11,10 @@ class RelationRepository(contexte : Context) : Repository(contexte) { return db.RelationsDAO().getAll() } + fun getById(id: Long): Relations { + return db.RelationsDAO().getById(id) + } + fun getBySubstance(substance: String): List { return db.RelationsDAO().getBySubstance(substance) } @@ -22,6 +26,8 @@ class RelationRepository(contexte : Context) : Repository(contexte) { it.relationInfoId = id } + db.InteractionsDAO().insert(relation.interactions) + return id } diff --git a/app/src/main/java/fr/medicapp/medicapp/model/relations/RelationInfo.kt b/app/src/main/java/fr/medicapp/medicapp/model/relations/RelationInfo.kt index 4f8d8502..382970eb 100644 --- a/app/src/main/java/fr/medicapp/medicapp/model/relations/RelationInfo.kt +++ b/app/src/main/java/fr/medicapp/medicapp/model/relations/RelationInfo.kt @@ -9,6 +9,7 @@ data class RelationInfo( @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "relation_info_id") val id: Long = 0L, + val substance: String = "", val com: String = "" ) \ No newline at end of file diff --git a/app/src/main/java/fr/medicapp/medicapp/model/relations/crossRef/Relations.kt b/app/src/main/java/fr/medicapp/medicapp/model/relations/crossRef/Relations.kt index 89b4885c..dce364e0 100644 --- a/app/src/main/java/fr/medicapp/medicapp/model/relations/crossRef/Relations.kt +++ b/app/src/main/java/fr/medicapp/medicapp/model/relations/crossRef/Relations.kt @@ -13,7 +13,7 @@ data class Relations( @Relation( parentColumn = "relation_info_id", - entityColumn = "interaction_id" + entityColumn = "relation_info_id" ) val interactions: List = emptyList() ) \ No newline at end of file diff --git a/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt b/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt index 3c3ebd03..25ac1412 100644 --- a/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt +++ b/app/src/main/java/fr/medicapp/medicapp/ui/screen/prescription/PrescriptionHome.kt @@ -106,8 +106,8 @@ fun PrescriptionHome( } && isNewMedicationAdded alertRelationOpen = withContext(Dispatchers.IO) { - controlRelation(prescriptions.dropLast(1), relations) - } + controlRelation(prescriptions, relations) + } && isNewMedicationAdded } PrescriptionList( @@ -230,32 +230,43 @@ suspend fun controlRelation( relationRepo : RelationRepository ): Boolean { Log.d("GuegueTest", "ça rentre dans la méthode") - val substanceNames = prescriptions.last().medication?.medicationCompositions?.map { it.substanceName } + val substanceNames = prescriptions.last().medication?.medicationCompositions?.map{ it.substanceName } if (substanceNames.isNullOrEmpty()) { return withContext(Dispatchers.IO) { false } } - val relations = relationRepo.getAll().map { it.relationInfo.substance } - Log.d("Relation", relations.toString()) + val relationsNoAccent = relationRepo.getAll().map { it.relationInfo.substance }.map { it.removeAccents() } + val relationsNorm = relationRepo.getAll().map { it.relationInfo.substance } + Log.d("Relation", relationsNorm.toString()) Log.d("Relation", substanceNames.toString()) for (substance in substanceNames) { - if (relations.contains(substance)) { + val substanceCheck = substance.removeAccents() + if (relationsNoAccent.contains(substanceCheck)) { //On cherche si il y a intéraction avec un autre produit //Interactions est une liste de substance Log.d("GuegueTest", "ça rentre dans la boucle = relation trouvée") - val interactions = relationRepo.getBySubstance(substance) + val index = relationsNoAccent.indexOf(substanceCheck) + Log.d("GuegueTest", "Substance qui pose problème : ${relationsNorm[index]}") + val interactions = relationRepo.getBySubstance(relationsNorm[index]) .map { it.interactions } .flatten() .map { it.substance } + .map { it.removeAccents() } + + Log.d("GuegueTest", "voici les interactions : $interactions") - for (prescription in prescriptions) { + + for (prescription in prescriptions.dropLast(1)) { + Log.d("GuegueTest", "taille de prescription : ${prescriptions.dropLast(1).size}") Log.d("GuegueTest", "ça rentre dans la boucle2") - val prescriptionSubstanceNames = prescription.medication?.medicationCompositions?.map { it.substanceName } + val prescriptionSubstanceNames = prescription.medication?.medicationCompositions?.map { it.substanceName } ?.map { it.removeAccents() } if (!prescriptionSubstanceNames.isNullOrEmpty()) { + Log.d("GuegueTest", "prescriptionSubstanceNames : $prescriptionSubstanceNames") for (prescriptionSubstance in prescriptionSubstanceNames) { if (interactions.contains(prescriptionSubstance)) { + Log.d("GuegueTest", "aie aie aie") return withContext(Dispatchers.IO) { true } @@ -263,6 +274,7 @@ suspend fun controlRelation( } } } + } } return withContext(Dispatchers.IO) { @@ -270,6 +282,24 @@ suspend fun controlRelation( } } +fun String.removeAccents(): String { + return this + .replace("é".uppercase(), "E") + .replace("è".uppercase(), "E") + .replace("ê".uppercase(), "E") + .replace("à".uppercase(), "A") + .replace("â".uppercase(), "A") + .replace("ù".uppercase(), "U") + .replace("û".uppercase(), "U") + .replace("ô".uppercase(), "O") + .replace("î".uppercase(), "I") + .replace("ï".uppercase(), "I") + .replace("ç".uppercase(), "C") + .replace("ë".uppercase(), "E") + .replace("ü".uppercase(), "U") + .replace("ö".uppercase(), "O") +} + @RequiresApi(Build.VERSION_CODES.Q) @Preview(name = "Light Theme", showSystemUi = true) @Composable