I'll help you create a prescription storing app called "My Prescription" in Kotlin with Jetpack Compose. Here's the complete structure:
Dependencies (build.gradle.kts - Module level)
kotlin Copy code
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation("androidx.compose.ui:ui:1.5.8")
implementation("androidx.compose.ui:ui-tooling-preview:1.5.8")
implementation("androidx.compose.material3:material3:1.1.2")
implementation("androidx.compose.material:material-icons-extended:1.5.8")
implementation("androidx.navigation:navigation-compose:2.7.6")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0")
implementation("androidx.room:room-runtime:2.6.1")
implementation("androidx.room:room-ktx:2.6.1")
kapt("androidx.room:room-compiler:2.6.1")
debugImplementation("androidx.compose.ui:ui-tooling:1.5.8")
debugImplementation("androidx.compose.ui:ui-test-manifest:1.5.8")
}
File Structure and Code
1. MainActivity.kt
kotlin Copy code
package com.example.myprescription
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.example.myprescription.navigation.AppNavigation
import com.example.myprescription.ui.theme.MyPrescriptionTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyPrescriptionTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
AppNavigation()
}
}
}
}
}
2. data/FamilyMember.kt
kotlin Copy code
package com.example.myprescription.data
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "family_members")
data class FamilyMember(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
val name: String,
val age: Int,
val relation: String,
val isSelected: Boolean = false
)
3. data/Prescription.kt
kotlin Copy code
package com.example.myprescription.data
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "prescriptions")
data class Prescription(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
val memberId: Int,
val medicineName: String,
val dosage: String,
val frequency: String,
val duration: String,
val doctorName: String,
val date: String,
val notes: String = ""
)
4. data/Report.kt
kotlin Copy code
package com.example.myprescription.data
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "reports")
data class Report(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
val memberId: Int,
val reportType: String,
val reportName: String,
val date: String,
val doctorName: String,
val notes: String = ""
)
5. data/AppDao.kt
kotlin Copy code
package com.example.myprescription.data
import androidx.room.*
import kotlinx.coroutines.flow.Flow
@Dao
interface AppDao {
// Family Members
@Query("SELECT * FROM family_members")
fun getAllMembers(): Flow<List<FamilyMember>>
@Insert
suspend fun insertMember(member: FamilyMember)
@Delete
suspend fun deleteMember(member: FamilyMember)
@Update
suspend fun updateMember(member: FamilyMember)
@Query("SELECT * FROM family_members WHERE isSelected = 1 LIMIT 1")
suspend fun getSelectedMember(): FamilyMember?
// Prescriptions
@Query("SELECT * FROM prescriptions WHERE memberId = :memberId")
fun getPrescriptionsForMember(memberId: Int): Flow<List<Prescription>>
@Insert
suspend fun insertPrescription(prescription: Prescription)
@Delete
suspend fun deletePrescription(prescription: Prescription)
// Reports
@Query("SELECT * FROM reports WHERE memberId = :memberId")
fun getReportsForMember(memberId: Int): Flow<List<Report>>
@Insert
suspend fun insertReport(report: Report)
@Delete
suspend fun deleteReport(report: Report)
}
6. data/AppDatabase.kt
kotlin Copy code
package com.example.myprescription.data
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import android.content.Context
@Database(
entities = [FamilyMember::class, Prescription::class, Report::class],
version = 1,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
abstract fun dao(): AppDao
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"prescription_database"
).build()
INSTANCE = instance
instance
}
}
}
}
7. viewmodel/FamilyViewModel.kt
kotlin Copy code
package com.example.myprescription.viewmodel
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.example.myprescription.data.AppDatabase
import com.example.myprescription.data.FamilyMember
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
class FamilyViewModel(application: Application) : AndroidViewModel(application) {
private val dao = AppDatabase.getDatabase(application).dao()
val familyMembers = dao.getAllMembers()
private val _selectedMember = MutableStateFlow<FamilyMember?>(null)
val selectedMember: StateFlow<FamilyMember?> = _selectedMember.asStateFlow()
fun addMember(name: String, age: Int, relation: String) {
viewModelScope.launch {
dao.insertMember(FamilyMember(name = name, age = age, relation = relation))
}
}
fun deleteMember(member: FamilyMember) {
viewModelScope.launch {
dao.deleteMember(member)
}
}
fun selectMember(member: FamilyMember) {
viewModelScope.launch {
// Deselect all members first
familyMembers.value.forEach {
if (it.isSelected) {
dao.updateMember(it.copy(isSelected = false))
}
}
// Select the chosen member
dao.updateMember(member.copy(isSelected = true))
_selectedMember.value = member.copy(isSelected = true)
}
}
init {
viewModelScope.launch {
_selectedMember.value = dao.getSelectedMember()
}
}
}
8. viewmodel/PrescriptionViewModel.kt
kotlin Copy code
package com.example.myprescription.viewmodel
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.example.myprescription.data.AppDatabase
import com.example.myprescription.data.Prescription
import com.example.myprescription.data.Report
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
class PrescriptionViewModel(application: Application) : AndroidViewModel(application) {
private val dao = AppDatabase.getDatabase(application).dao()
private val _prescriptions = MutableStateFlow<List<Prescription>>(emptyList())
val prescriptions: StateFlow<List<Prescription>> = _prescriptions.asStateFlow()
private val _reports = MutableStateFlow<List<Report>>(emptyList())
val reports: StateFlow<List<Report>> = _reports.asStateFlow()
fun loadDataForMember(memberId: Int) {
viewModelScope.launch {
dao.getPrescriptionsForMember(memberId).collect {
_prescriptions.value = it
}
}
viewModelScope.launch {
dao.getReportsForMember(memberId).collect {
_reports.value = it
}
}
}
fun addPrescription(
memberId: Int,
medicineName: String,
dosage: String,
frequency: String,
duration: String,
doctorName: String,
date: String,
notes: String
) {
viewModelScope.launch {
dao.insertPrescription(
Prescription(
memberId = memberId,
medicineName = medicineName,
dosage = dosage,
frequency = frequency,
duration = duration,
doctorName = doctorName,
date = date,
notes = notes
)
)
}
}
fun addReport(
memberId: Int,
reportType: String,
reportName: String,
date: String,
doctorName: String,
notes: String
) {
viewModelScope.launch {
dao.insertReport(
Report(
memberId = memberId,
reportType = reportType,
reportName = reportName,
date = date,
doctorName = doctorName,
notes = notes
)
)
}
}
fun deletePrescription(prescription: Prescription) {
viewModelScope.launch {
dao.deletePrescription(prescription)
}
}
fun deleteReport(report: Report) {
viewModelScope.launch {
dao.deleteReport(report)
}
}
}
9. navigation/AppNavigation.kt
kotlin Copy code
package com.example.myprescription.navigation
import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.myprescription.screens.FamilyMembersScreen
import com.example.myprescription.screens.PrescriptionScreen
import com.example.myprescription.viewmodel.FamilyViewModel
import com.example.myprescription.viewmodel.PrescriptionViewModel
@Composable
fun AppNavigation() {
val navController = rememberNavController()
val familyViewModel: FamilyViewModel = viewModel()
val prescriptionViewModel: PrescriptionViewModel = viewModel()
NavHost(
navController = navController,
startDestination = "family_members"
) {
composable("family_members") {
FamilyMembersScreen(
viewModel = familyViewModel,
onNavigateToPrescriptions = {
navController.navigate("prescriptions")
}
)
}
composable("prescriptions") {
PrescriptionScreen(
familyViewModel = familyViewModel,
prescriptionViewModel = prescriptionViewModel,
onNavigateBack = {
navController.popBackStack()
}
)
}
}
}
10. screens/FamilyMembersScreen.kt
kotlin Copy code
package com.example.myprescription.screens
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.myprescription.data.FamilyMember
import com.example.myprescription.viewmodel.FamilyViewModel
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FamilyMembersScreen(
viewModel: FamilyViewModel,
onNavigateToPrescriptions: () -> Unit
) {
val familyMembers by viewModel.familyMembers.collectAsStateWithLifecycle(initialValue = emptyList())
val selectedMember by viewModel.selectedMember.collectAsStateWithLifecycle()
var showAddDialog by remember { mutableStateOf(false) }
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Family Members",
style = MaterialTheme.typography.headlineMedium
)
FloatingActionButton(
onClick = { showAddDialog = true },
modifier = Modifier.size(56.dp)
) {
Icon(Icons.Default.Add, contentDescription = "Add Member")
}
}
Spacer(modifier = Modifier.height(16.dp))
LazyColumn {
items(familyMembers) { member ->
MemberCard(
member = member,
isSelected = member.id == selectedMember?.id,
onSelect = { viewModel.selectMember(member) },
onDelete = { viewModel.deleteMember(member) }
)
Spacer(modifier = Modifier.height(8.dp))
}
}
Spacer(modifier = Modifier.weight(1f))
Button(
onClick = onNavigateToPrescriptions,
modifier = Modifier.fillMaxWidth(),
enabled = selectedMember != null
) {
Text("Continue to Prescriptions")
}
}
if (showAddDialog) {
AddMemberDialog(
onDismiss = { showAddDialog = false },
onAdd = { name, age, relation ->
viewModel.addMember(name, age, relation)
showAddDialog = false
}
)
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MemberCard(
member: FamilyMember,
isSelected: Boolean,
onSelect: () -> Unit,
onDelete: () -> Unit
) {
Card(
modifier = Modifier.fillMaxWidth(),
colors