From 70537cfb2bbde76cc8190d5ae433290d9629b799 Mon Sep 17 00:00:00 2001 From: yon Date: Wed, 25 Dec 2024 17:21:00 +0100 Subject: [PATCH] Refactor: Encapsulate Task Operations in TaskUseCase 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. --- .../zouitel/domain/usecase/TaskUseCase.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/domain/src/main/java/city/zouitel/domain/usecase/TaskUseCase.kt b/domain/src/main/java/city/zouitel/domain/usecase/TaskUseCase.kt index 92a44e52..acef42bf 100644 --- a/domain/src/main/java/city/zouitel/domain/usecase/TaskUseCase.kt +++ b/domain/src/main/java/city/zouitel/domain/usecase/TaskUseCase.kt @@ -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) }