Draft
feat: Persist tasks across sessions using Room database#2
Conversation
…ence Co-authored-by: jsb-nci <212698004+jsb-nci@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Replace in-memory TaskRepositoryImpl with Room database implementation
feat: Persist tasks across sessions using Room database
Mar 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tasks were lost on app restart due to in-memory
mutableListOf<Task>()storage. ReplacesTaskRepositoryImplwith a Room-backed implementation and wires async data flow through the ViewModel via coroutines.Gradle
room = "2.7.1"andksp = "2.0.21-1.0.28"tolibs.versions.tomlwith corresponding library aliases and KSP pluginalias(libs.plugins.ksp)inapp/build.gradle.kts; addedroom-runtime,room-ktx, andksp(room-compiler)dependenciesRoom Layer (
data/local/)TaskEntity—@Entity(tableName = "tasks")mirroring theTaskdomain modelTaskDao— reactivegetAllTasks(): Flow<List<TaskEntity>>plus one-shot suspend queries for insert/update/delete andgetTaskByIdTaskDatabase— thread-safe singleton via double-checked lockingRepository
TaskRepositoryinterface updated:getTasks()now returnsFlow<List<Task>>; mutation methods aresuspendRoomTaskRepositoryImpl(dao: TaskDao)replaces in-memory impl; includesTaskEntity.toTask()/Task.toEntity()extension functionsTaskRepositoryImplretained as-is (no longer implements the interface)ViewModel & Wiring
HomeViewModelnow acceptsTaskRepositoryas a constructor parameter and collects the Flow ininitviaviewModelScope:HomeViewModelFactoryprovides the repository to the ViewModel without HiltMainActivityinstantiatesTaskDatabase → RoomTaskRepositoryImpl → HomeViewModelFactoryand passes the factory toHomeScreenHomeScreenaccepts an optionalviewModelFactory: ViewModelProvider.Factory?parameterOriginal prompt
Goal
Replace the current in-memory
TaskRepositoryImplwith a Room database implementation so tasks persist across app sessions. All changes should go on a new branch calledfeature/room-database.Project Context
com.example.composebasicsTaskRepositoryImpluses an in-memorymutableListOf<Task>()— data is lost when app is closedExisting files to be aware of:
domain/model/Task.ktdata/repository/TaskRepository.kt(interface)data/repository/TaskRepositoryImpl.kt(current in-memory impl)ui/home/HomeViewModel.kt(currently sync, no coroutines)gradle/libs.versions.toml(current — no Room versions)app/build.gradle.kts(current — no Room, no KSP)plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) } android { namespace = "com.example.composebasics" compileSdk { version = release(36) } defaultConfig { applicationId = "com.example.composebasics" minSdk = 29 targetSdk = 36 ... } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" } buildFeatures { compose = true } } dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.activity.compose) implementation(platform(libs.androidx.compose.bom)) // ... compose deps implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.10.0") }What needs to be done
1. Update
gradle/libs.versions.tomlAdd Room version and KSP plugin version:
2. Update
app/build.gradle.ktsalias(libs.plugins.ksp)to plugins block3. Create
app/src/main/java/com/example/composebasics/data/local/TaskEntity.kt4. Create
app/src/main/java/com/example/composebasics/data/local/TaskDao.kt5. Create
app/src/main/java/com/example/composebasics/data/local/TaskDatabase.kt