Skip to content

Commit

Permalink
Merge pull request #90 from anrouxel/DownloadFromApi
Browse files Browse the repository at this point in the history
Download from api
  • Loading branch information
gueguefun authored Feb 29, 2024
2 parents 60da92d + b284b3f commit 85e0d63
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ interface RelationsDAO {
@Query("SELECT * FROM RelationInfo")
fun getAll(): List<Relations>

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

@Insert
fun insert(relation: RelationInfo): Long

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ 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<Relations> {
return db.RelationsDAO().getBySubstance(substance)
}

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

relation.interactions.forEach {
it.relationInfoId = id
}

db.InteractionsDAO().insert(relation.interactions)

return id
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class Relations(

@Relation(
parentColumn = "relation_info_id",
entityColumn = "interaction_id"
entityColumn = "relation_info_id"
)
val interactions: List<Interactions> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,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 +38,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 +51,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 +105,16 @@ fun PrescriptionHome(
} == true
} && isNewMedicationAdded

alertRelationOpen = withContext(Dispatchers.IO) {
controlRelation(prescriptions, relations)
} && isNewMedicationAdded

}
PrescriptionList(
prescriptions = prescriptions,
onPrescriptionClick = onPrescriptionClick
)

}

else -> {
PrescriptionList(
prescriptions = prescriptions,
Expand Down Expand Up @@ -136,6 +145,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 +225,81 @@ fun NoPrescriptionAvailable() {
)
}

suspend fun controlRelation(
prescriptions: List<Prescription>,
relationRepo : RelationRepository
): Boolean {
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
}
}

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) {
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 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.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 } ?.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
}
}
}
}
}

}
}
return withContext(Dispatchers.IO) {
false
}
}

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
Expand Down

0 comments on commit 85e0d63

Please sign in to comment.