Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file, as:

  • This file does not belong here. Manifest should be in app/manifests directory.
  • This file is incomplete.


<application>

</application>

</manifest>
17 changes: 17 additions & 0 deletions app/src/main/java/com/medmapper/v33001/dao/DateConverter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.medmapper.v33001.dao

import androidx.room.TypeConverter
import java.util.Date

class DateConverter {
@TypeConverter
fun fromTimestamp(value: Long?): Date? {
return value?.let { Date(it) }
}

@TypeConverter
fun dateToTimestamp(date: Date?): Long? {
return date?.time
}
}

Comment on lines +1 to +17
Copy link
Copy Markdown
Collaborator Author

@prxtxks prxtxks Mar 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the DateConverter.kt class file to enable Room to store and retrieve Date objects from the database (refer Medicine.kt). This ensures proper storage and retrieval of dates from the database. This is a common practice in Android development when working with Room databases.

Source: https://developer.android.com/training/data-storage/room/referencing-data#type-converters

9 changes: 7 additions & 2 deletions app/src/main/java/com/medmapper/v33001/dao/MedicineDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import kotlinx.coroutines.flow.Flow

@Dao
interface MedicineDAO {

@Query("SELECT * FROM Medicine ORDER BY name ASC")
fun getAlphabetizedMedicine(): Flow<List<Medicine>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(medicine: Medicine)

@Query("DELETE FROM Medicine")
suspend fun deleteAll()
@Delete
suspend fun delete(medicine: Medicine)

@Update
suspend fun update(medicine: Medicine)

Comment on lines +16 to +21
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Room provides specific annotations for DELETE and UPDATE. Using @delete and @update annotations instead of @query can make the code more concise and easier to read.

Source: https://developer.android.com/training/data-storage/room/accessing-data

}
5 changes: 4 additions & 1 deletion app/src/main/java/com/medmapper/v33001/dto/Medicine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package com.medmapper.v33001.dto
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.medmapper.v33001.dao.DateConverter

@Entity(tableName = "Medicine")
@TypeConverters(DateConverter::class)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Room does not support non-primitive data types such as Date, so we need to define a TypeConverter to convert them to and from primitive data types that Room can handle. The @TypeConverter annotation can convert a custom type to a supported database type

Source: https://developer.android.com/training/data-storage/room/referencing-data.html#type-converters

data class Medicine(
@PrimaryKey(autoGenerate = true) val medID: Int,
@ColumnInfo(name = "name") val name: String?,
@ColumnInfo(name = "strength") val strength: String?,
@ColumnInfo(name = "start date") val startDate: String?,
@ColumnInfo(name = "start_date") val startDate: String?,
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Column names should be lowercase, with words separated by underscores ("_").

Source: https://developer.android.com/training/data-storage/room/defining-data

@ColumnInfo(name = "prescription length") val lengthInDays: Int
//,@ColumnInfo() val endDate: Date = startDate
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ 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<List<Medicine>> = medicineDAO.getAlphabetizedMedicine()

// 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
val allMedicine: Flow<List<Medicine>> = medicineDAO.getAlphabetizedMedicine()

fun getMedicineList(): Flow<List<Medicine>> {
return medicineDAO.getAlphabetizedMedicine()
}

suspend fun insert(medicine: Medicine) {
medicineDAO.insert(medicine)
}
}

suspend fun delete(medicine: Medicine) {
medicineDAO.delete(medicine)
}

suspend fun update(medicine: Medicine) {
medicineDAO.update(medicine)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.medmapper.v33001.dao.DateConverter
import com.medmapper.v33001.dao.MedicineDAO
import com.medmapper.v33001.dto.Medicine

@Database(entities = arrayOf(Medicine::class), version = 1, exportSchema = false)
@Database(entities = [Medicine::class], version = 1, exportSchema = false)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spread operator * is preferred over arrayOf when passing vararg arguments because it avoids the overhead of creating an additional array.

Source: https://kotlinlang.org/docs/functions.html#variable-number-of-arguments-varargs

@TypeConverters(DateConverter::class)
public abstract class MedicineRoomDatabase : RoomDatabase() {

abstract fun medicineDao(): MedicineDAO
Expand Down