Skip to content

Commit 6fb86e7

Browse files
authored
Merge pull request #16 from anrouxel/AddPrescription-View
Add prescription view
2 parents f8c31ee + dad5e5e commit 6fb86e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2733
-379
lines changed

.idea/sonarlint/issuestore/index.pb

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/securityhotspotstore/index.pb

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ dependencies {
116116
implementation("androidx.room:room-runtime:2.5.0")
117117
ksp("androidx.room:room-compiler:2.5.0")
118118

119+
// Alarm
120+
implementation("com.github.ColdTea-Projects:SmplrAlarm:2.1.0")
121+
119122
testImplementation("junit:junit:4.13.2")
120123
androidTestImplementation("androidx.test.ext:junit:1.1.5")
121124
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

app/src/main/AndroidManifest.xml

+5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
<uses-feature android:name="android.hardware.camera" />
66
<uses-permission android:name="android.permission.CAMERA" />
7+
<uses-permission android:name="android.permission.INTERNET"/>
8+
9+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
10+
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
711

812
<application
913
android:allowBackup="true"
14+
tools:replace="android:allowBackup"
1015
android:dataExtractionRules="@xml/data_extraction_rules"
1116
android:fullBackupContent="@xml/backup_rules"
1217
android:icon="@mipmap/ic_launcher"

app/src/main/assets

Submodule assets updated from 614c01e to 2bf2fd0

app/src/main/java/fr/medicapp/medicapp/MainActivity.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package fr.medicapp.medicapp
22

3+
import android.os.Build
34
import android.os.Bundle
45
import androidx.activity.ComponentActivity
56
import androidx.activity.compose.setContent
7+
import androidx.annotation.RequiresApi
8+
import androidx.compose.runtime.getValue
69
import androidx.navigation.compose.rememberNavController
10+
import fr.medicapp.medicapp.ai.PrescriptionAI
711
import fr.medicapp.medicapp.database.AppDatabase
8-
import fr.medicapp.medicapp.entity.DoctorEntity
9-
import fr.medicapp.medicapp.entity.PrescriptionEntity
1012
import fr.medicapp.medicapp.ui.navigation.RootNavGraph
1113
import fr.medicapp.medicapp.ui.theme.MedicAppTheme
1214

1315
class MainActivity : ComponentActivity() {
16+
@RequiresApi(Build.VERSION_CODES.O)
1417
override fun onCreate(savedInstanceState: Bundle?) {
1518
super.onCreate(savedInstanceState)
1619

17-
val db = AppDatabase.getInstance(this)
20+
AppDatabase.getInstance(this)
21+
PrescriptionAI.getInstance(this)
1822

1923
setContent {
2024
MedicAppTheme {

app/src/main/java/fr/medicapp/medicapp/ai/PrescriptionAI.kt

+47-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package fr.medicapp.medicapp.ai
22

33
import android.content.Context
44
import android.content.res.AssetManager
5+
import android.net.Uri
56
import android.os.Handler
67
import android.os.HandlerThread
78
import android.util.Log
89
import androidx.annotation.WorkerThread
10+
import com.google.mlkit.vision.common.InputImage
11+
import com.google.mlkit.vision.text.TextRecognition
12+
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
913
import fr.medicapp.medicapp.tokenization.Feature
1014
import fr.medicapp.medicapp.tokenization.FeatureConverter
1115
import org.pytorch.IValue
@@ -39,23 +43,50 @@ class PrescriptionAI(
3943
private val PREDICT_ANS_NUM = 5
4044
private val NUM_LITE_THREADS = 4
4145

42-
/*val modelRunnable = Runnable {
46+
private val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
4347

44-
}*/
45-
46-
fun analyse(visionText: String): MutableList<Pair<String, String>> {
47-
val latch = CountDownLatch(1)
48-
var result = mutableListOf<Pair<String, String>>()
48+
init {
4949
mBackgroundThread = HandlerThread("BackgroundThread")
5050
mBackgroundThread.start()
5151
mHandle = Handler(mBackgroundThread.looper)
5252
mHandle.post {
5353
loadModel()
54-
result = runModel(visionText)
55-
latch.countDown() // Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
5654
}
57-
latch.await() // Causes the current thread to wait until the latch has counted down to zero.
58-
return result
55+
}
56+
57+
fun analyse(
58+
imageUri: Uri,
59+
onPrediction: (MutableList<Pair<String, String>>) -> Unit,
60+
onDismiss: () -> Unit
61+
) {
62+
mHandle.post {
63+
while (mModule == null) {
64+
Thread.sleep(100)
65+
}
66+
val visionText = recognizeText(imageUri)
67+
if (visionText != null) {
68+
val sentenceTokenized = runModel(visionText)
69+
onPrediction(sentenceTokenized)
70+
}
71+
onDismiss()
72+
}
73+
}
74+
75+
@WorkerThread
76+
private fun recognizeText(imageUri: Uri): String? {
77+
var visionText: String? = null
78+
val latch = CountDownLatch(1)
79+
val image = InputImage.fromFilePath(context, imageUri)
80+
recognizer.process(image)
81+
.addOnSuccessListener {
82+
visionText = it.text
83+
latch.countDown()
84+
}
85+
.addOnFailureListener {
86+
latch.countDown()
87+
}
88+
latch.await()
89+
return visionText
5990
}
6091

6192
@WorkerThread
@@ -76,7 +107,7 @@ class PrescriptionAI(
76107
}
77108

78109
val moduleFileAbsoluteFilePath: String? = assetFilePath(context.assets)
79-
mModule = Module.load(moduleFileAbsoluteFilePath)//, mutableMapOf(), Device.CPU)
110+
mModule = Module.load(moduleFileAbsoluteFilePath) // , mutableMapOf(), Device.CPU)
80111
Log.v(TAG, "Model loaded.")
81112
}
82113
}
@@ -150,7 +181,10 @@ class PrescriptionAI(
150181

151182
for (i in predictionsLabelList.indices) {
152183
if (predictionsLabelList[i] != "O") {
153-
Log.d("AIModel", "Prediction: ${feature.origTokens[i]} -> ${predictionsLabelList[i]}")
184+
Log.d(
185+
"AIModel",
186+
"Prediction: ${feature.origTokens[i]} -> ${predictionsLabelList[i]}"
187+
)
154188
sentenceTokenized.add(Pair(feature.origTokens[i], predictionsLabelList[i]))
155189
}
156190
}
@@ -209,4 +243,4 @@ class PrescriptionAI(
209243
}
210244
}
211245
}
212-
}
246+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fr.medicapp.medicapp.dao
2+
3+
import androidx.room.Dao
4+
import androidx.room.Delete
5+
import androidx.room.Insert
6+
import androidx.room.Query
7+
import androidx.room.Update
8+
import fr.medicapp.medicapp.entity.MedicationEntity
9+
import fr.medicapp.medicapp.entity.UserEntity
10+
11+
@Dao
12+
interface MedicationDAO {
13+
@Query("SELECT * FROM Medications")
14+
fun getAll(): List<MedicationEntity>
15+
16+
@Query("SELECT * FROM Medications WHERE CommercializationStatus NOT LIKE 'NON COMMERCIALISÉE'")
17+
fun getAllWithoutNotTreadings(): List<MedicationEntity>
18+
19+
@Query("SELECT * FROM Medications WHERE cisCode = :id")
20+
fun getOne(id: String): MedicationEntity
21+
22+
@Insert
23+
fun add(t: MedicationEntity)
24+
25+
@Insert
26+
fun addAll(vararg t: MedicationEntity)
27+
28+
@Delete
29+
fun delete(t: MedicationEntity)
30+
31+
@Delete
32+
fun deleteAll(vararg t: MedicationEntity)
33+
34+
@Update
35+
fun update(t: MedicationEntity)
36+
37+
@Update
38+
fun updateAll(vararg t: MedicationEntity)
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package fr.medicapp.medicapp.dao
2+
3+
import androidx.room.Dao
4+
import androidx.room.Delete
5+
import androidx.room.Insert
6+
import androidx.room.Query
7+
import androidx.room.Update
8+
import fr.medicapp.medicapp.entity.DoctorEntity
9+
import fr.medicapp.medicapp.entity.NotificationEntity
10+
import fr.medicapp.medicapp.entity.SideEffectEntity
11+
12+
@Dao
13+
interface NotificationDAO {
14+
@Query("SELECT * FROM NotificationEntity")
15+
fun getAll(): List<NotificationEntity>
16+
17+
@Query("SELECT * FROM NotificationEntity WHERE id = :id")
18+
fun getOne(id: String): NotificationEntity
19+
20+
@Query("SELECT * FROM NotificationEntity WHERE medicationName = :medicament")
21+
fun getByMedicament(medicament: String): List<NotificationEntity>
22+
23+
24+
@Insert
25+
fun add(t: NotificationEntity)
26+
27+
@Insert
28+
fun addAll(vararg t: NotificationEntity)
29+
30+
@Delete
31+
fun delete(t: NotificationEntity)
32+
33+
@Delete
34+
fun deleteAll(vararg t: NotificationEntity)
35+
36+
@Update
37+
fun update(t: NotificationEntity)
38+
39+
@Update
40+
fun updateAll(vararg t: NotificationEntity)
41+
}

app/src/main/java/fr/medicapp/medicapp/dao/SideEffectDAO.kt

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ interface SideEffectDAO {
1515
@Query("SELECT * FROM SideEffectEntity WHERE id = :id")
1616
fun getOne(id: String): SideEffectEntity
1717

18+
@Query("SELECT * FROM SideEffectEntity WHERE medicament = :medicament")
19+
fun getByMedicament(medicament: String): List<SideEffectEntity>
20+
1821
@Insert
1922
fun add(t: SideEffectEntity)
2023

app/src/main/java/fr/medicapp/medicapp/dao/TreatmentDAO.kt

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ interface TreatmentDAO {
1616
@Query("SELECT * FROM TreatmentEntity WHERE id = :id")
1717
fun getOne(id: String): TreatmentEntity
1818

19+
@Query("SELECT * FROM TreatmentEntity WHERE notification = 1")
20+
fun getWithNotification(): List<TreatmentEntity>
21+
1922
@Insert
2023
fun add(t: TreatmentEntity)
2124

Binary file not shown.

0 commit comments

Comments
 (0)