Skip to content

Commit

Permalink
Affichage ne fonctionne pas, mais presque
Browse files Browse the repository at this point in the history
+ j'ai sommeil
  • Loading branch information
marine committed Feb 29, 2024
1 parent 02b430e commit c2ee3a5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface RelationsDAO {
@Query("SELECT * FROM RelationInfo")
fun getAll() : List<Relations>

@Transaction
@Query("SELECT * FROM RelationInfo WHERE substance = :substance")
fun getBySubstance(substance: String) : List<Relations>

@Insert
fun insert(relation: RelationInfo): Long

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class RelationRepository(contexte : Context) : Repository(contexte) {
return db.RelationsDAO().getAll()
}

fun getBySubstance(substance: String): List<Relations> {
return db.RelationsDAO().getBySubstance(substance)
}

fun insert(relation: Relations): Long {
val id = db.RelationsDAO().insert(relation.relationInfo)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -201,6 +224,46 @@ fun NoPrescriptionAvailable() {
)
}

suspend fun controlRelation(
prescriptions: List<Prescription>,
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
Expand Down

0 comments on commit c2ee3a5

Please sign in to comment.