Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.

Commit 280c9be

Browse files
riggarooJoseAlcerreca
authored andcommitted
Removed the unnecessary passing of the title and description back into the ViewModel from the Fragment (android#358)
The AddEditTaskViewModel already contains the title and description of the task. In order to be a more accurate example and depict exactly where the information lives and flows, the title and description is now just obtained from the ViewModel itself instead of being passed through from the Fragment.
1 parent 15a6ed7 commit 280c9be

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private void setupFab() {
108108
fab.setOnClickListener(new View.OnClickListener() {
109109
@Override
110110
public void onClick(View v) {
111-
mViewModel.saveTask(mViewModel.title.get(), mViewModel.description.get());
111+
mViewModel.saveTask();
112112
}
113113
});
114114
}

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskViewModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ public void onDataNotAvailable() {
105105
}
106106

107107
// Called when clicking on fab.
108-
void saveTask(String title, String description) {
109-
Task task = new Task(title, description);
108+
void saveTask() {
109+
Task task = new Task(title.get(), description.get());
110110
if (task.isEmpty()) {
111111
mSnackbarText.setValue(R.string.empty_task_message);
112112
return;
113113
}
114114
if (isNewTask() || mTaskId == null) {
115115
createTask(task);
116116
} else {
117-
task = new Task(title, description, mTaskId, mTaskCompleted);
117+
task = new Task(title.get(), description.get(), mTaskId, mTaskCompleted);
118118
updateTask(task);
119119
}
120120
}

todoapp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskViewModelTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public void setupAddEditTaskViewModel() {
7474
@Test
7575
public void saveNewTaskToRepository_showsSuccessMessageUi() {
7676
// When the ViewModel is asked to save a task
77-
mAddEditTaskViewModel.saveTask("New Task Title", "Some Task Description");
77+
mAddEditTaskViewModel.description.set("Some Task Description");
78+
mAddEditTaskViewModel.title.set("New Task Title");
79+
mAddEditTaskViewModel.saveTask();
7880

7981
// Then a task is saved in the repository and the view updated
8082
verify(mTasksRepository).saveTask(any(Task.class)); // saved to the model

0 commit comments

Comments
 (0)