https://github.com/syedpiyal/roomdatabasecontactsapp
An Android app demonstrating the use of Room Database for managing a simple contact list with Kotlin Coroutines and LiveData.
https://github.com/syedpiyal/roomdatabasecontactsapp
android-application coroutines kotlin roomdatabase
Last synced: about 2 months ago
JSON representation
An Android app demonstrating the use of Room Database for managing a simple contact list with Kotlin Coroutines and LiveData.
- Host: GitHub
- URL: https://github.com/syedpiyal/roomdatabasecontactsapp
- Owner: SyedPiyal
- Created: 2024-11-25T11:52:38.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-25T11:55:25.000Z (over 1 year ago)
- Last Synced: 2025-04-23T19:14:21.807Z (about 1 year ago)
- Topics: android-application, coroutines, kotlin, roomdatabase
- Language: Kotlin
- Homepage:
- Size: 42 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RoomDatabaseContactsApp
This is an Android application that demonstrates how to use **Room Database** with **Kotlin Coroutines** to manage a simple contact list. The app allows users to add, update, delete, and retrieve contacts using Room persistence library.
## Features
- **Room Database** to persist contact data.
- **Kotlin Coroutines** for background operations (inserting, updating, deleting).
- **LiveData** to observe changes in the database and update the UI automatically.
- Simple contact management UI with a button to fetch all contacts.
- **Edge-to-Edge** design support (using `ViewCompat` for proper padding on modern devices).
## Architecture
- **Room Database**: A local database to store contact details like name and phone number.
- **Dao (Data Access Object)**: Interface to define methods for database operations such as inserting, updating, deleting, and querying contacts.
- **LiveData**: Used to observe the data from the database and update the UI accordingly.
- **Coroutines**: Used for performing long-running database operations in the background to ensure smooth performance.
## Setup Instructions
1. Clone the repository:
```bash
git clone https://github.com/yourusername/RoomDatabaseContactsApp.git
```
2. Open the project in **Android Studio**.
3. Make sure you have **Kotlin** and **Room dependencies** included in your `build.gradle` files:
- In your `app/build.gradle`:
```gradle
dependencies {
implementation(libs.androidx.room.runtime)
ksp("androidx.room:room-compiler:2.5.0")
implementation(libs.androidx.room.ktx)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.android)
}
```
4. Sync the project with Gradle.
5. Run the app on an emulator or physical device.
## Code Explanation
### Room Database
The `ContactDatabase` class is a singleton class that provides access to the Room database. It uses `@Database` annotation to define the entities (Contact table) and database version.
```kotlin
@Database(entities = [Contact::class], version = 1)
abstract class ContactDatabase : RoomDatabase() {
abstract fun contactDao(): ContactDao
}