diff --git a/app/build.gradle b/app/build.gradle index acbca27..2866797 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -63,7 +63,8 @@ dependencies { debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version" // Add Room Database Dependencies - implementation "androidx.room:room-runtime:2.5.0" - kapt "androidx.room:room-compiler:2.5.0" - implementation "androidx.room:room-ktx:2.5.0" + def room_version = "2.5.0" + implementation "androidx.room:room-runtime:$room_version" + kapt "androidx.room:room-compiler:$room_version" + implementation "androidx.room:room-ktx:$room_version" } \ No newline at end of file diff --git a/app/src/main/java/com/medmapper/v33001/dao/MedicineDAO.kt b/app/src/main/java/com/medmapper/v33001/dao/MedicineDAO.kt index 271f155..14691aa 100644 --- a/app/src/main/java/com/medmapper/v33001/dao/MedicineDAO.kt +++ b/app/src/main/java/com/medmapper/v33001/dao/MedicineDAO.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow @Dao interface MedicineDAO { @Query("SELECT * FROM Medicine ORDER BY name ASC") - fun getAlphabetizedMedicine(): Flow> + fun getAllAlphabetizedMedicine(): Flow> @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun insert(medicine: Medicine) diff --git a/app/src/main/java/com/medmapper/v33001/dto/Medicine.kt b/app/src/main/java/com/medmapper/v33001/dto/Medicine.kt index 8094740..d1a7293 100644 --- a/app/src/main/java/com/medmapper/v33001/dto/Medicine.kt +++ b/app/src/main/java/com/medmapper/v33001/dto/Medicine.kt @@ -1,5 +1,6 @@ package com.medmapper.v33001.dto +import androidx.annotation.NonNull import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey @@ -7,9 +8,9 @@ import androidx.room.PrimaryKey @Entity(tableName = "Medicine") data class Medicine( @PrimaryKey(autoGenerate = true) val medID: Int, - @ColumnInfo(name = "name") val name: String?, + @NonNull @ColumnInfo(name = "name") val name: String, @ColumnInfo(name = "strength") val strength: String?, - @ColumnInfo(name = "start date") val startDate: String?, - @ColumnInfo(name = "prescription length") val lengthInDays: Int + @NonNull @ColumnInfo(name = "start date") val startDate: String, + @NonNull @ColumnInfo(name = "prescription length") val lengthInDays: Int //,@ColumnInfo() val endDate: Date = startDate ) \ No newline at end of file diff --git a/app/src/main/java/com/medmapper/v33001/repository/MedicineRepository.kt b/app/src/main/java/com/medmapper/v33001/repository/MedicineRepository.kt index c2f8470..b3f1ff4 100644 --- a/app/src/main/java/com/medmapper/v33001/repository/MedicineRepository.kt +++ b/app/src/main/java/com/medmapper/v33001/repository/MedicineRepository.kt @@ -5,17 +5,21 @@ import com.medmapper.v33001.dao.MedicineDAO import com.medmapper.v33001.dto.Medicine import kotlinx.coroutines.flow.Flow -// Declares teh DAO as private property in the constructor. Pass in the DAO -// instead of the whole database, because you only need access to the DAO +/** + * Declares the DAO as private property in the constructor. + * Pass in the DAO instead of the whole database, because you only need access to the DAO + */ class MedicineRepository(private val medicineDAO: MedicineDAO) { - // Room executes all queries on a separate thread. - // Observed Flow will notify the observer when the data has changed. - val allMedicines: Flow> = medicineDAO.getAlphabetizedMedicine() + // Room executes all queries on a separate thread. + // Observed Flow will notify the observer when the data has changed. + + val allMedicines: Flow> = medicineDAO.getAllAlphabetizedMedicine() + + // By default Room runs suspend queries off the main thread, therefore + // we don't need to implement anything else to ensure we're not doing + // long running database work off the main thread - // By default Room runs suspend queries off the main thread, therefore - // we don't need to implement anything else to ensure we're not doing - // long running database work off the main thread @Suppress("RedundantSuspendModifier") @WorkerThread suspend fun insert(medicine: Medicine) { diff --git a/app/src/main/java/com/medmapper/v33001/room/MedicineRoomDatabase.kt b/app/src/main/java/com/medmapper/v33001/room/MedicineRoomDatabase.kt index a37fcf9..3d069e2 100644 --- a/app/src/main/java/com/medmapper/v33001/room/MedicineRoomDatabase.kt +++ b/app/src/main/java/com/medmapper/v33001/room/MedicineRoomDatabase.kt @@ -7,13 +7,16 @@ import androidx.room.RoomDatabase import com.medmapper.v33001.dao.MedicineDAO import com.medmapper.v33001.dto.Medicine +/** + * + */ @Database(entities = arrayOf(Medicine::class), version = 1, exportSchema = false) public abstract class MedicineRoomDatabase : RoomDatabase() { abstract fun medicineDao(): MedicineDAO + //Singleton prevents multiple instances of database opening at the same time. companion object { - // Singleton prevents multiple instances of database opening at the same time. @Volatile private var INSTANCE: MedicineRoomDatabase? = null fun getDatabase(context: Context): MedicineRoomDatabase {