Skip to content

My Code Review Suggestions.#21

Open
prxtxks wants to merge 1 commit intomasterfrom
chaudhpr_codeReview1
Open

My Code Review Suggestions.#21
prxtxks wants to merge 1 commit intomasterfrom
chaudhpr_codeReview1

Conversation

@prxtxks
Copy link
Copy Markdown
Collaborator

@prxtxks prxtxks commented Mar 5, 2023

Analysis of the program:

The program is a medicine tracking app that uses Room database to store medicine information. It consists of several files including the entity class, DAO, database, repository, and view model. The app follows the MVVM architecture pattern and uses Kotlin coroutines and Flow for asynchronous operations.

Was the program available in UC Github on time?

Yes

Is the program documented/commented well enough for you to understand?

Overall, the program was documented well enough for me to understand the code and make the suggested improvements.

Does the program compile?

I had to make some adjustments to make it compile.

Rationale behind your changes:

The suggestions I provided aim to improve the code's readability, maintainability, and performance. For example, using the @TypeConverters annotation and specifying the class name of the DateConverter class would ensure that Room can convert Date objects to a format that can be stored in the database. Using the @insert, @delete, and @update annotations instead of @query for database operations would make the code more readable and maintainable. Using the suspend keyword for all database operations that could potentially block the UI thread ensures that these operations are executed asynchronously and do not block the UI thread, thus improving the app's performance. I have included single comments for every change I made, and can be viewed in the Files changed section.

Sources:

  1. https://developer.android.com/training/data-storage/room/defining-data
  2. https://developer.android.com/training/data-storage/room/referencing-data
  3. https://developer.android.com/training/data-storage/room/accessing-data
  4. https://kotlinlang.org/docs/coroutines-basics.html#suspending-functions
  5. https://kotlinlang.org/docs/functions.html#variable-number-of-arguments-varargs

Links to three specific commits that you made to your own group's GitHub repository before the end of sprint deadline.

  1. chintavs/Workout-App@02e0ff1
  2. chintavs/Workout-App@9921feb
  3. chintavs/Workout-App@e13c099

Three specific technical concepts that you learned from this code review:

  • Using the @TypeConverters annotation and specifying a class name for the TypeConverter class to ensure that Room can convert custom data types to a format that can be stored in the database.
  • Using the suspend keyword for database operations that could potentially block the UI thread to ensure that these operations are executed asynchronously and do not block the UI thread.
  • Using the @insert, @delete, and @update annotations instead of @query for database operations to make the code more readable and maintainable.

Comment on lines +1 to +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
}
}

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

Comment on lines +16 to +21
@Delete
suspend fun delete(medicine: Medicine)

@Update
suspend fun update(medicine: Medicine)

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

@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

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

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

Comment on lines +20 to +33
val allMedicine: Flow<List<Medicine>> = medicineDAO.getAlphabetizedMedicine()

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

suspend fun insert(medicine: Medicine) {
medicineDAO.insert(medicine)
}
} No newline at end of file

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

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

}
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.

Added these functions to provide access to the data stored in the database and to allow for inserting, deleting, and updating Medicine objects in the database.

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

@discospiff
Copy link
Copy Markdown

Good suggestions and citations here. I recommend that the group review these suggestions, test them, and consider merging this branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants