Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data models, refactor local and network data sources #908

Merged
merged 8 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import androidx.test.filters.LargeTest
import com.example.android.architecture.blueprints.todoapp.HiltTestActivity
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.TodoNavGraph
import com.example.android.architecture.blueprints.todoapp.data.Task
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
import com.example.android.architecture.blueprints.todoapp.util.saveTaskBlocking
import com.google.accompanist.appcompattheme.AppCompatTheme
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -129,10 +128,9 @@ class AppNavigationTest {
}

@Test
fun taskDetailScreen_doubleUIBackButton() {
fun taskDetailScreen_doubleUIBackButton() = runTest {
val taskName = "UI <- button"
val task = Task(taskName, "Description")
tasksRepository.saveTaskBlocking(task)
tasksRepository.createTask(taskName, "Description")

setContent()

Expand All @@ -159,10 +157,9 @@ class AppNavigationTest {
}

@Test
fun taskDetailScreen_doubleBackButton() {
fun taskDetailScreen_doubleBackButton() = runTest {
val taskName = "Back button"
val task = Task(taskName, "Description")
tasksRepository.saveTaskBlocking(task)
tasksRepository.createTask(taskName, "Description")

setContent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.example.android.architecture.blueprints.todoapp.HiltTestActivity
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.data.Task
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
import com.example.android.architecture.blueprints.todoapp.util.saveTaskBlocking
import com.google.accompanist.appcompattheme.AppCompatTheme
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -50,6 +50,7 @@ import org.junit.runner.RunWith
// @LooperMode(LooperMode.Mode.PAUSED)
// @TextLayoutMode(TextLayoutMode.Mode.REALISTIC)
@HiltAndroidTest
@OptIn(ExperimentalCoroutinesApi::class)
class TasksScreenTest {

@get:Rule(order = 0)
Expand All @@ -68,9 +69,9 @@ class TasksScreenTest {
}

@Test
fun displayTask_whenRepositoryHasData() {
fun displayTask_whenRepositoryHasData() = runTest {
// GIVEN - One task already in the repository
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
repository.createTask("TITLE1", "DESCRIPTION1")

// WHEN - On startup
setContent()
Expand All @@ -80,8 +81,8 @@ class TasksScreenTest {
}

@Test
fun displayActiveTask() {
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
fun displayActiveTask() = runTest {
repository.createTask("TITLE1", "DESCRIPTION1")

setContent()

Expand All @@ -96,8 +97,10 @@ class TasksScreenTest {
}

@Test
fun displayCompletedTask() {
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1", true))
fun displayCompletedTask() = runTest {
repository.apply {
createTask("TITLE1", "DESCRIPTION1").also { completeTask(it.id) }
}

setContent()

Expand All @@ -111,8 +114,8 @@ class TasksScreenTest {
}

@Test
fun markTaskAsComplete() {
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
fun markTaskAsComplete() = runTest {
repository.createTask("TITLE1", "DESCRIPTION1")

setContent()

Expand All @@ -129,8 +132,10 @@ class TasksScreenTest {
}

@Test
fun markTaskAsActive() {
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1", true))
fun markTaskAsActive() = runTest {
repository.apply {
createTask("TITLE1", "DESCRIPTION1").also { completeTask(it.id) }
}

setContent()

Expand All @@ -147,10 +152,12 @@ class TasksScreenTest {
}

@Test
fun showAllTasks() {
fun showAllTasks() = runTest {
// Add one active task and one completed task
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
repository.saveTaskBlocking(Task("TITLE2", "DESCRIPTION2", true))
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it.id) }
}

setContent()

Expand All @@ -161,11 +168,13 @@ class TasksScreenTest {
}

@Test
fun showActiveTasks() {
fun showActiveTasks() = runTest {
// Add 2 active tasks and one completed task
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
repository.saveTaskBlocking(Task("TITLE2", "DESCRIPTION2"))
repository.saveTaskBlocking(Task("TITLE3", "DESCRIPTION3", true))
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2")
createTask("TITLE3", "DESCRIPTION3").also { completeTask(it.id) }
}

setContent()

Expand All @@ -177,11 +186,13 @@ class TasksScreenTest {
}

@Test
fun showCompletedTasks() {
fun showCompletedTasks() = runTest {
// Add one active task and 2 completed tasks
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
repository.saveTaskBlocking(Task("TITLE2", "DESCRIPTION2", true))
repository.saveTaskBlocking(Task("TITLE3", "DESCRIPTION3", true))
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it.id) }
createTask("TITLE3", "DESCRIPTION3").also { completeTask(it.id) }
}

setContent()

Expand All @@ -193,10 +204,12 @@ class TasksScreenTest {
}

@Test
fun clearCompletedTasks() {
fun clearCompletedTasks() = runTest {
// Add one active task and one completed task
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION1"))
repository.saveTaskBlocking(Task("TITLE2", "DESCRIPTION2", true))
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it.id) }
}

setContent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import androidx.test.filters.LargeTest
import com.example.android.architecture.blueprints.todoapp.HiltTestActivity
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.TodoNavGraph
import com.example.android.architecture.blueprints.todoapp.data.Task
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
import com.example.android.architecture.blueprints.todoapp.util.saveTaskBlocking
import com.google.accompanist.appcompattheme.AppCompatTheme
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -53,6 +53,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@LargeTest
@HiltAndroidTest
@OptIn(ExperimentalCoroutinesApi::class)
class TasksTest {

@get:Rule(order = 0)
Expand All @@ -75,9 +76,9 @@ class TasksTest {
}

@Test
fun editTask() {
fun editTask() = runTest {
val originalTaskTitle = "TITLE1"
repository.saveTaskBlocking(Task(originalTaskTitle, "DESCRIPTION"))
repository.createTask(originalTaskTitle, "DESCRIPTION")

setContent()

Expand Down Expand Up @@ -140,9 +141,11 @@ class TasksTest {
}

@Test
fun createTwoTasks_deleteOneTask() {
repository.saveTaskBlocking(Task("TITLE1", "DESCRIPTION"))
repository.saveTaskBlocking(Task("TITLE2", "DESCRIPTION"))
fun createTwoTasks_deleteOneTask() = runTest {
repository.apply {
createTask("TITLE1", "DESCRIPTION")
createTask("TITLE2", "DESCRIPTION")
}

setContent()

Expand All @@ -163,10 +166,10 @@ class TasksTest {
}

@Test
fun markTaskAsCompleteOnDetailScreen_taskIsCompleteInList() {
fun markTaskAsCompleteOnDetailScreen_taskIsCompleteInList() = runTest {
// Add 1 active task
val taskTitle = "COMPLETED"
repository.saveTaskBlocking(Task(taskTitle, "DESCRIPTION"))
repository.createTask(taskTitle, "DESCRIPTION")

setContent()

Expand All @@ -188,10 +191,12 @@ class TasksTest {
}

@Test
fun markTaskAsActiveOnDetailScreen_taskIsActiveInList() {
fun markTaskAsActiveOnDetailScreen_taskIsActiveInList() = runTest {
// Add 1 completed task
val taskTitle = "ACTIVE"
repository.saveTaskBlocking(Task(taskTitle, "DESCRIPTION", true))
repository.apply {
createTask(taskTitle, "DESCRIPTION").also { completeTask(it.id) }
}

setContent()

Expand All @@ -213,10 +218,10 @@ class TasksTest {
}

@Test
fun markTaskAsCompleteAndActiveOnDetailScreen_taskIsActiveInList() {
fun markTaskAsCompleteAndActiveOnDetailScreen_taskIsActiveInList() = runTest {
// Add 1 active task
val taskTitle = "ACT-COMP"
repository.saveTaskBlocking(Task(taskTitle, "DESCRIPTION"))
repository.createTask(taskTitle, "DESCRIPTION")

setContent()

Expand All @@ -240,10 +245,12 @@ class TasksTest {
}

@Test
fun markTaskAsActiveAndCompleteOnDetailScreen_taskIsCompleteInList() {
fun markTaskAsActiveAndCompleteOnDetailScreen_taskIsCompleteInList() = runTest {
// Add 1 completed task
val taskTitle = "COMP-ACT"
repository.saveTaskBlocking(Task(taskTitle, "DESCRIPTION", true))
repository.apply {
createTask(taskTitle, "DESCRIPTION").also { completeTask(it.id) }
}

setContent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.TodoDestinationsArgs
import com.example.android.architecture.blueprints.todoapp.data.Task
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
Expand Down Expand Up @@ -101,8 +100,7 @@ class AddEditTaskViewModel @Inject constructor(
}

private fun createNewTask() = viewModelScope.launch {
val newTask = Task(uiState.value.title, uiState.value.description)
tasksRepository.saveTask(newTask)
tasksRepository.createTask(uiState.value.title, uiState.value.description)
_uiState.update {
it.copy(isTaskSaved = true)
}
Expand All @@ -113,13 +111,11 @@ class AddEditTaskViewModel @Inject constructor(
throw RuntimeException("updateTask() was called but task is new.")
}
viewModelScope.launch {
val updatedTask = Task(
tasksRepository.updateTask(
taskId,
title = uiState.value.title,
description = uiState.value.description,
isCompleted = uiState.value.isTaskCompleted,
id = taskId
)
tasksRepository.saveTask(updatedTask)
_uiState.update {
it.copy(isTaskSaved = true)
}
Expand Down
Loading