TrackTask is an Android application that allows users to manage their to-do tasks efficiently. It offers features to create, edit, delete, and mark tasks as completed. The app is designed with a user-friendly interface and employs modern Android development principles and libraries.
The TrackTask app follows the Model-View-ViewModel (MVVM) architecture pattern. This separation of concerns ensures a clear and maintainable structure for the app. Below are the key components of the app's architecture:
-
Model: Represents the data and business logic. In this app, the primary model is the
Task
class, which defines the structure of a task. -
View: Represents the user interface components. Activities and XML layout files are part of the view in this app.
-
ViewModel: Acts as an intermediary between the model and view. It prepares and manages the data for the view. The
TaskViewModel
class handles interactions between theMainActivity
(view) and theTaskRepo
(model).
The TrackTask app uses Kotlin Coroutines to manage background tasks and asynchronous operations. This ensures that database operations, which can be time-consuming, don't block the main UI thread, providing a smooth user experience.
- Introduction
- Features
- Getting Started
- 3.1. Prerequisites
- 3.2. Installation
- App Structure
- 4.1. XML Layouts
- 4.2. Kotlin Code
- 4.3. Database
- ViewModels and Repository
- Dependencies
- Room Database
- 7.1. Task Entity
- 7.2. Task DAO (Data Access Object)
- 7.3. TaskDatabase
- 7.4. TaskRepo (Repository)
- Usage
- Conclusion
The TrackTask app is a straightforward Android application designed to assist users in managing their tasks. It enables users to create, edit, and delete tasks, all while displaying tasks in a list. This documentation provides a detailed understanding of how the app is structured and how it operates.
The app boasts the following features:
- Task Management: Create, edit, and delete tasks.
- Task List: Display tasks in a scrollable list.
- Splash Screen: An attractive splash screen for a smooth startup experience.
Before you begin working with the TrackTask app, ensure you have the following prerequisites:
- Android Studio: Install Android Studio on your computer.
- Basic Knowledge: A fundamental understanding of Android app development concepts.
Follow these steps to install and run the TrackTask app:
- Clone the Project: Clone or download the project from the repository.
- Open in Android Studio: Launch Android Studio and open the project directory.
- Build and Run: Build and run the app on an emulator or physical Android device.
<!-- Main activity layout with RecyclerView for displaying tasks -->
- This XML layout defines the main activity's UI, which includes a
RecyclerView
for displaying the list of tasks and two floating action buttons for adding new tasks and clearing all tasks. TheRecyclerView
is styled with a green background.
<!-- Layout for individual task items in the RecyclerView -->
- This layout represents each individual task item in the
RecyclerView
. It includes aCheckBox
for task completion, an "Edit" button for editing tasks, and a "Delete" button for deleting tasks.
<!-- Layout for creating or editing tasks -->
- This layout is used for creating or editing tasks. It includes an
EditText
for entering task descriptions and a "Save" button for saving or updating tasks.
<!-- Splash screen layout displayed during app startup -->
- This layout defines the splash screen UI, which displays an image while the app loads.
// Main activity for task management
- This is the main activity of the app. It sets up the
RecyclerView
to display tasks, handles user interactions, and communicates with the ViewModel to manage tasks. It performs the following functions: - Initializes and configures the RecyclerView to display tasks.
- Manages user interactions for adding, editing, and clearing tasks.
- Observes changes in the task data using LiveData and updates the UI accordingly.
- Communicates with the ViewModel to perform CRUD operations on tasks.
- deleteListItemClicked(): Deletes a selected task when the delete button is clicked.
- listItemClicked(): Opens the NewTaskActivity for editing a selected task.
- newTaskLauncher: Handles the result of the NewTaskActivity, updating or saving tasks.
- deleteAllTasks(): Deletes all tasks when the clear button is clicked.
-
Description: The
TaskAdapter
is an adapter for the RecyclerView in theMainActivity
. It binds the data from the Room database to the individual views in the RecyclerView. -
Key Code: The adapter defines view holders for tasks, inflates the
task_list.xml
layout, and handles user interactions for editing and deleting tasks. -
Functions:
onCreateViewHolder()
: Creates newTaskViewHolder
instances when needed.onBindViewHolder()
: Binds data to theTaskViewHolder
and handles user interactions.TaskViewHolder
: Represents the individual views for each task item and sets click listeners.TaskComparator
: ComparesTask
objects for efficient updates.
// Activity for creating or editing tasks
- This activity is used for creating or editing tasks. It communicates with the ViewModel to save or update tasks.
// Splash screen activity
- This activity displays a splash screen with a timer, then launches the main activity.
// Data class representing a task
- This data class defines the data structure for tasks. It is used by Room, the local database library, to create and manage the task table.
// Data Access Object (DAO) for tasks
- This interface defines the database access methods for tasks, including inserting, updating, deleting, and querying tasks.
// Room database class
- This class represents the Room database. It defines the database version, entities, and provides a reference to the
TaskDao
.
The ViewModel uses LiveData to observe changes in task data and communicates with the TaskRepo
.
-
TaskViewModel.kt
: This ViewModel class handles UI-related tasks, such as observing changes in the task list and providing methods for performing CRUD (Create, Read, Update, Delete) operations on tasks. -
TaskViewModelFactory.kt
: This class provides instances of theTaskViewModel
. It is used to pass the repository to the ViewModel. -
TaskRepo.kt
: The repository class abstracts the data sources and exposes methods for interacting with tasks. It utilizes theTaskDao
to perform database operations.TheTaskRepo
class is responsible for managing data operations related to tasks. It serves as an abstraction layer between the ViewModel and the Room database.
The app relies on various Android libraries and dependencies, including AndroidX components, Room, Kotlin, Material Design components, and more. Here are the dependencies used in the TrackTask Android app project:
dependencies {
// AndroidX Libraries
implementation "androidx.appcompat:appcompat:1.6.1"
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
implementation "androidx.core:core-ktx:1.12.0"
implementation "com.google.android.material:material:1.9.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.2"
implementation "androidx.recyclerview:recyclerview:1.2.1"
// Room Persistence Library
implementation "androidx.room:room-runtime:2.5.2"
kapt "androidx.room:room-compiler:2.5.2"
implementation "androidx.room:room-ktx:2.5.2"
// Testing Dependencies
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit:1.1.5"
androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
// Core Splash Screen Library
implementation "androidx.core:core-splashscreen:1.0.1"
// Kotlin Standard Library
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
}
These dependencies cover a wide range of functionalities, including UI components, database management, testing, and Kotlin language support. They help create a modern and efficient Android app.
Room is used in the TrackTask app for managing the local database. Let's explore how Room is implemented.
@Entity(tableName = "Task_table")
data class Task(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "Task_id")
val id: Int = 0,
@ColumnInfo(name = "Task_desc")
val task: String
)
- The
Task
class is annotated with@Entity
, indicating that it represents a database entity. - It has two fields:
id
(the primary key) andtask
(the task description).
@Dao
interface TaskDao {
@Query("SELECT * FROM Task_table")
fun getAllTask(): Flow<List<Task>>
@Query("DELETE FROM Task_table")
suspend fun deleteAllTasks()
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(task: Task)
@Update
suspend fun update(task: Task)
@Delete
suspend fun delete(task: Task)
}
- The
TaskDao
interface defines the methods for interacting with tasks in the database. - It includes methods for querying all tasks, deleting all tasks, inserting, updating, and deleting individual tasks.
@Database(entities = [Task::class], version = 2, exportSchema = false)
abstract class TaskDatabase : RoomDatabase() {
abstract fun taskDao(): TaskDao
companion object {
@Volatile
private var INSTANCE: TaskDatabase? = null
fun getDatabase(context: Context, scope: CoroutineScope): TaskDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext, TaskDatabase::class.java, "TASK_DATABASE"
).allow
MainThreadQueries()
.addCallback(TaskDatabaseCallback(scope))
.build()
INSTANCE = instance
instance
}
}
}
}
TaskDatabase
is an abstract class annotated with@Database
, indicating that it is a Room database.- It defines the database version and provides an abstract method
taskDao()
to access theTaskDao
.
class TaskRepo(private val taskDao: TaskDao) {
val allTasks: Flow<List<Task>> = taskDao.getAllTask()
suspend fun insert(task: Task) {
taskDao.insert(task)
}
suspend fun update(task: Task) {
taskDao.update(task)
}
suspend fun delete(task: Task) {
taskDao.delete(task)
}
suspend fun deleteAllTasks() {
taskDao.deleteAllTasks()
}
}
TaskRepo
is the repository class responsible for abstracting data source access.- It exposes the
allTasks
property as a Flow for observing changes in the task list. - This class provides methods to insert, update, delete individual tasks, and delete all tasks.
- Launch the app on your Android device.
- The splash screen appears briefly, followed by the main activity.
- Click the floating action button with a plus icon to add a new task.
- Enter a task description and click "Save" to create the task.
- View the list of tasks in the main activity.
- Click the checkbox to mark a task as done.
- Click the "Edit" button to edit a task.
- Click the "Delete" button to delete a task.
- Click the trash can icon (floating action button) to delete all tasks.
The TrackTask Android app follows the MVVM architecture and utilizes modern Android development libraries like Room, LiveData, ViewModel, and Kotlin Coroutines. It provides a user-friendly interface for managing tasks efficiently, from creation and editing to deletion and completion. The separation of concerns in the architecture ensures maintainability and scalability for future enhancements.
You have now explored the detailed documentation for the TrackTask Android app, including a comprehensive explanation of how Room is utilized for database operations. This app offers a simple yet practical way to manage tasks on your Android device. Feel free to modify and extend it to suit your specific needs or use it as a learning resource for Android app development. Happy coding!