-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add Creation and Completed date on Tasks
Adding a new feature to save the creation date and when the user completed the task. This will allow the "Tracker" feature to be possible, once it will show how many tasks were finished during a timestamp. Test cases added to validate new behavior.
- Loading branch information
Showing
32 changed files
with
710 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
data/domain/src/main/java/com/escodro/domain/calendar/TaskCalendar.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.escodro.domain.calendar | ||
|
||
import java.util.Calendar | ||
|
||
/** | ||
* Provide the date and time to be used on the task use cases, respecting the Inversion of Control. | ||
*/ | ||
class TaskCalendar { | ||
|
||
/** | ||
* Gets the current [Calendar]. | ||
* | ||
* @return the current [Calendar] | ||
*/ | ||
fun getCurrentCalendar(): Calendar = Calendar.getInstance() | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
data/domain/src/main/java/com/escodro/domain/usecase/task/UncompleteTask.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.escodro.domain.usecase.task | ||
|
||
import com.escodro.core.extension.applySchedulers | ||
import io.reactivex.Completable | ||
|
||
/** | ||
* Use case to set a task as uncompleted in the database. | ||
*/ | ||
class UncompleteTask(private val getTask: GetTask, private val updateTask: UpdateTask) { | ||
|
||
/** | ||
* Completes the given task. | ||
* | ||
* @param taskId the task id | ||
* | ||
* @return observable to be subscribe | ||
*/ | ||
operator fun invoke(taskId: Long): Completable = | ||
getTask(taskId).flatMapCompletable { | ||
it.completed = false | ||
it.completedDate = null | ||
updateTask(it) | ||
}.applySchedulers() | ||
} |
30 changes: 30 additions & 0 deletions
30
data/domain/src/main/java/com/escodro/domain/usecase/task/UpdateTaskStatus.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.escodro.domain.usecase.task | ||
|
||
import com.escodro.core.extension.applySchedulers | ||
import com.escodro.domain.viewdata.ViewData | ||
import io.reactivex.Completable | ||
|
||
/** | ||
* Use case to update a task as completed or uncompleted from the database. | ||
*/ | ||
class UpdateTaskStatus( | ||
private val completeTask: CompleteTask, | ||
private val uncompleteTask: UncompleteTask | ||
) { | ||
|
||
/** | ||
* Updates the task as completed or uncompleted based on the current state. | ||
* | ||
* @param task the task to be updated | ||
* | ||
* @return observable to be subscribe | ||
*/ | ||
operator fun invoke(task: ViewData.Task): Completable { | ||
task.completed = !task.completed | ||
|
||
return when (task.completed) { | ||
true -> completeTask(task.id) | ||
false -> uncompleteTask(task.id) | ||
}.applySchedulers() | ||
} | ||
} |
This file contains 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
42 changes: 42 additions & 0 deletions
42
data/domain/src/test/java/com/escodro/domain/usecase/task/CompleteTaskTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.escodro.domain.usecase.task | ||
|
||
import com.escodro.domain.calendar.TaskCalendar | ||
import com.escodro.domain.viewdata.ViewData | ||
import com.escodro.test.ImmediateSchedulerRule | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import io.reactivex.Single | ||
import java.util.Calendar | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class CompleteTaskTest { | ||
|
||
@get:Rule | ||
var testSchedulerRule = ImmediateSchedulerRule() | ||
|
||
private val mockTask = mockk<ViewData.Task>(relaxed = true) | ||
|
||
private val mockGetTask = mockk<GetTask>(relaxed = true) | ||
|
||
private val mockUpdateTask = mockk<UpdateTask>(relaxed = true) | ||
|
||
private val mockCalendar = mockk<TaskCalendar>(relaxed = true) | ||
|
||
private val completeTask = CompleteTask(mockGetTask, mockUpdateTask, mockCalendar) | ||
|
||
@Test | ||
fun `check if task was completed`() { | ||
val currentTime = Calendar.getInstance() | ||
|
||
every { mockCalendar.getCurrentCalendar() } returns currentTime | ||
every { mockGetTask.invoke(any()) } returns Single.just(mockTask) | ||
|
||
completeTask(mockTask.id).subscribe() | ||
|
||
verify { mockTask.completed = true } | ||
verify { mockTask.completedDate = currentTime } | ||
verify { mockUpdateTask.invoke(any()) } | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
data/domain/src/test/java/com/escodro/domain/usecase/task/UncompleteTaskTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.escodro.domain.usecase.task | ||
|
||
import com.escodro.domain.viewdata.ViewData | ||
import com.escodro.test.ImmediateSchedulerRule | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import io.reactivex.Single | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class UncompleteTaskTest { | ||
|
||
@get:Rule | ||
var testSchedulerRule = ImmediateSchedulerRule() | ||
|
||
private val mockTask = mockk<ViewData.Task>(relaxed = true) | ||
|
||
private val mockGetTask = mockk<GetTask>(relaxed = true) | ||
|
||
private val mockUpdateTask = mockk<UpdateTask>(relaxed = true) | ||
|
||
private val uncompleteTask = UncompleteTask(mockGetTask, mockUpdateTask) | ||
|
||
@Test | ||
fun `check if task was uncompleted`() { | ||
every { mockGetTask.invoke(any()) } returns Single.just(mockTask) | ||
|
||
uncompleteTask(mockTask.id).subscribe() | ||
|
||
verify { mockTask.completed = false } | ||
verify { mockTask.completedDate = null } | ||
verify { mockUpdateTask.invoke(any()) } | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
data/domain/src/test/java/com/escodro/domain/usecase/task/UpdateTaskStatusTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.escodro.domain.usecase.task | ||
|
||
import com.escodro.domain.viewdata.ViewData | ||
import com.escodro.test.ImmediateSchedulerRule | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class UpdateTaskStatusTest { | ||
|
||
@get:Rule | ||
var testSchedulerRule = ImmediateSchedulerRule() | ||
|
||
private val mockTask = mockk<ViewData.Task>(relaxed = true) | ||
|
||
private val mockCompleteTask = mockk<CompleteTask>(relaxed = true) | ||
|
||
private val mockUncompleteTask = mockk<UncompleteTask>(relaxed = true) | ||
|
||
private val updateTaskStatus = UpdateTaskStatus(mockCompleteTask, mockUncompleteTask) | ||
|
||
@Test | ||
fun `check if completed flag was inverted`() { | ||
updateTaskStatus(mockTask) | ||
verify { mockTask.completed = !mockTask.completed } | ||
} | ||
|
||
@Test | ||
fun `check if completed flow was called`() { | ||
every { mockTask.completed } returns false andThen true | ||
|
||
updateTaskStatus(mockTask) | ||
verify { mockTask.completed = true } | ||
verify { mockCompleteTask(mockTask.id) } | ||
} | ||
|
||
@Test | ||
fun `check if uncompleted flow was called`() { | ||
every { mockTask.completed } returns true andThen false | ||
|
||
updateTaskStatus(mockTask) | ||
verify { mockTask.completed = false } | ||
verify { mockUncompleteTask(mockTask.id) } | ||
} | ||
} |
This file contains 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
Oops, something went wrong.