Skip to content

Commit

Permalink
Refactor: Encapsulate Task Operations in TaskUseCase
Browse files Browse the repository at this point in the history
Introduced a sealed class `TaskUseCase` to encapsulate various operations on tasks, including:

-
 Observing all tasks (`ObserveAll`).
- Observing a task by UID (`ObserveByUid`).
- Inserting a new task (`Insert`).
- Updating a task by ID (`UpdateById`).
- Deleting a task by ID (`DeleteById`).

Each use case leverages the `TaskRepository` for data
 access. This change improves code organization and maintainability by centralizing task-related logic.
  • Loading branch information
youndon committed Dec 25, 2024
1 parent 6da7d4e commit 70537cf
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions domain/src/main/java/city/zouitel/domain/usecase/TaskUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,69 @@ package city.zouitel.domain.usecase
import city.zouitel.domain.model.Task
import city.zouitel.domain.repository.TaskRepository

/**
* Sealed class representing various use cases for interacting with tasks.
* Each use case encapsulates a specific operation on the [TaskRepository].
*/
sealed class TaskUseCase {

/**
* ObserveAll is a use case that provides a stream of all tasks from the repository.
*
* This use case delegates the responsibility of fetching and observing all tasks
* to the underlying [TaskRepository]. It exposes the data as a Flow of [Task]
* objects, allowing for reactive updates when the underlying data changes.
*
* @property repo The [TaskRepository] used to interact with the task data source.
*/
data class ObserveAll(private val repo: TaskRepository): TaskUseCase() {
operator fun invoke() = repo.observeAll
}

/**
* UseCase to observe a [Task] by its unique identifier (UID).
*
* This UseCase utilizes the [TaskRepository] to provide a Flow of [Task] objects
* that match the specified UID. It allows for observing real-time updates to a
* specific task.
*
* @property repo The [TaskRepository] instance used to access task data.
* @constructor Creates an ObserveByUid instance with the given [TaskRepository].
*/
data class ObserveByUid(private val repo: TaskRepository): TaskUseCase() {
operator fun invoke(uid: String) = repo.observeByUid(uid)
}

/**
* [Insert] is a use case responsible for inserting a new [Task] into the data layer.
*
* It utilizes a [TaskRepository] to handle the underlying data persistence.
*
* @property repo The [TaskRepository] instance used for data operations.
*/
data class Insert(private val repo: TaskRepository): TaskUseCase() {
suspend operator fun invoke(task: Task) = repo.insert(task)
}

/**
* [TaskUseCase] implementation for updating a task by its ID.
*
* This use case interacts with the [TaskRepository] to perform the actual update operation.
*
* @property repo The [TaskRepository] used to interact with the underlying data source.
*/
data class UpdateById(private val repo: TaskRepository): TaskUseCase() {
suspend operator fun invoke(id: Long) = repo.updateById(id)
}

/**
* [TaskUseCase] for deleting a task by its ID.
*
* This use case encapsulates the logic for deleting a task from the underlying
* [TaskRepository] based on the provided ID.
*
* @property repo The [TaskRepository] responsible for interacting with the data source.
*/
data class DeleteById(private val repo: TaskRepository): TaskUseCase() {
suspend operator fun invoke(id: Long) = repo.deleteById(id)
}
Expand Down

0 comments on commit 70537cf

Please sign in to comment.