Skip to content

Commit 2502501

Browse files
committed
Sync AddEdit and Stats. All test pass
1 parent c2cc491 commit 2502501

File tree

12 files changed

+253
-128
lines changed

12 files changed

+253
-128
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.support.v7.widget.Toolbar;
2525

2626
import com.example.android.architecture.blueprints.todoapp.R;
27+
import com.example.android.architecture.blueprints.todoapp.ToDoApplication;
2728
import com.example.android.architecture.blueprints.todoapp.util.ActivityUtils;
2829
import com.example.android.architecture.blueprints.todoapp.util.EspressoIdlingResource;
2930

@@ -46,24 +47,35 @@ protected void onCreate(Bundle savedInstanceState) {
4647
actionBar.setDisplayHomeAsUpEnabled(true);
4748
actionBar.setDisplayShowHomeEnabled(true);
4849

49-
if (null == savedInstanceState) {
50-
// If there's a Task ID in the Bundle, this is an edit. Otherwise it's a new task.
50+
AddEditTaskFragment addEditTaskFragment =
51+
(AddEditTaskFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
52+
53+
String taskId = null;
54+
if (addEditTaskFragment == null) {
55+
addEditTaskFragment = AddEditTaskFragment.newInstance();
56+
5157
if (getIntent().hasExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID)) {
52-
String taskId = getIntent().getStringExtra(
58+
taskId = getIntent().getStringExtra(
5359
AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID);
5460
actionBar.setTitle(R.string.edit_task);
55-
AddEditTaskFragment addEditTaskFragment = AddEditTaskFragment.newInstance();
5661
Bundle bundle = new Bundle();
5762
bundle.putString(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID, taskId);
5863
addEditTaskFragment.setArguments(bundle);
59-
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
60-
addEditTaskFragment, R.id.contentFrame);
6164
} else {
6265
actionBar.setTitle(R.string.add_task);
63-
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
64-
AddEditTaskFragment.newInstance(), R.id.contentFrame);
6566
}
67+
68+
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
69+
addEditTaskFragment, R.id.contentFrame);
6670
}
71+
72+
// Create the presenter
73+
DaggerAddEditTaskFragmentComponent.builder()
74+
.addEditTaskPresenterModule(
75+
new AddEditTaskPresenterModule(addEditTaskFragment, taskId))
76+
.tasksRepositoryComponent(
77+
((ToDoApplication) getApplication()).getTasksRepositoryComponent()).build()
78+
.getAddEditTaskPresenter();
6779
}
6880

6981
@Override

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,33 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp.addedittask;
1818

19+
import com.example.android.architecture.blueprints.todoapp.BasePresenter;
20+
import com.example.android.architecture.blueprints.todoapp.BaseView;
21+
1922
/**
2023
* This specifies the contract between the view and the presenter.
2124
*/
2225
public interface AddEditTaskContract {
2326

24-
interface View {
27+
interface View extends BaseView<Presenter> {
2528

2629
void showEmptyTaskError();
2730

2831
void showTasksList();
2932

30-
void setUserActionListener(UserActionsListener listener);
31-
3233
void setTitle(String title);
3334

3435
void setDescription(String description);
36+
37+
boolean isActive();
3538
}
3639

37-
interface UserActionsListener {
40+
interface Presenter extends BasePresenter {
3841

3942
void createTask(String title, String description);
4043

41-
void updateTask(String taskId, String title, String description);
44+
void updateTask( String title, String description);
4245

43-
void populateTask(String taskId);
46+
void populateTask();
4447
}
4548
}

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp.addedittask;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import android.app.Activity;
2022
import android.os.Bundle;
23+
import android.support.annotation.NonNull;
2124
import android.support.annotation.Nullable;
2225
import android.support.design.widget.FloatingActionButton;
2326
import android.support.design.widget.Snackbar;
@@ -28,7 +31,6 @@
2831
import android.widget.TextView;
2932

3033
import com.example.android.architecture.blueprints.todoapp.R;
31-
import com.example.android.architecture.blueprints.todoapp.ToDoApplication;
3234

3335
/**
3436
* Main UI for the add task screen. Users can enter a task title and description.
@@ -37,12 +39,14 @@ public class AddEditTaskFragment extends Fragment implements AddEditTaskContract
3739

3840
public static final String ARGUMENT_EDIT_TASK_ID = "EDIT_TASK_ID";
3941

40-
private AddEditTaskContract.UserActionsListener mActionListener;
42+
private AddEditTaskContract.Presenter mPresenter;
4143

4244
private TextView mTitle;
4345

4446
private TextView mDescription;
4547

48+
private String mEditedTaskId;
49+
4650
public static AddEditTaskFragment newInstance() {
4751
return new AddEditTaskFragment();
4852
}
@@ -54,21 +58,19 @@ public AddEditTaskFragment() {
5458
@Override
5559
public void onResume() {
5660
super.onResume();
57-
if (!isNewTask()) {
58-
String taskId = getArguments().getString(ARGUMENT_EDIT_TASK_ID);
59-
mActionListener.populateTask(taskId);
60-
}
61+
mPresenter.start();
62+
}
63+
64+
@Override
65+
public void setPresenter(@NonNull AddEditTaskContract.Presenter presenter) {
66+
mPresenter = checkNotNull(presenter);
6167
}
6268

6369
@Override
6470
public void onActivityCreated(Bundle savedInstanceState) {
6571
super.onActivityCreated(savedInstanceState);
66-
AddEditTaskFragmentComponent featureComponent = DaggerAddEditTaskFragmentComponent.builder()
67-
.addEditTaskPresenterModule(new AddEditTaskPresenterModule(this))
68-
.tasksRepositoryComponent(((ToDoApplication) getActivity().getApplication())
69-
.getTasksRepositoryComponent())
70-
.build();
71-
mActionListener = featureComponent.getAddEditTaskPresenter();
72+
73+
setTaskIdIfAny();
7274

7375
FloatingActionButton fab =
7476
(FloatingActionButton) getActivity().findViewById(R.id.fab_edit_task_done);
@@ -77,12 +79,11 @@ public void onActivityCreated(Bundle savedInstanceState) {
7779
@Override
7880
public void onClick(View v) {
7981
if (isNewTask()) {
80-
mActionListener.createTask(
82+
mPresenter.createTask(
8183
mTitle.getText().toString(),
8284
mDescription.getText().toString());
8385
} else {
84-
mActionListener.updateTask(
85-
getArguments().getString(ARGUMENT_EDIT_TASK_ID),
86+
mPresenter.updateTask(
8687
mTitle.getText().toString(),
8788
mDescription.getText().toString());
8889
}
@@ -115,11 +116,6 @@ public void showTasksList() {
115116
getActivity().finish();
116117
}
117118

118-
@Override
119-
public void setUserActionListener(AddEditTaskContract.UserActionsListener listener) {
120-
mActionListener = listener;
121-
}
122-
123119
@Override
124120
public void setTitle(String title) {
125121
mTitle.setText(title);
@@ -130,7 +126,18 @@ public void setDescription(String description) {
130126
mDescription.setText(description);
131127
}
132128

129+
@Override
130+
public boolean isActive() {
131+
return isAdded();
132+
}
133+
134+
private void setTaskIdIfAny() {
135+
if (getArguments() != null && getArguments().containsKey(ARGUMENT_EDIT_TASK_ID)) {
136+
mEditedTaskId = getArguments().getString(ARGUMENT_EDIT_TASK_ID);
137+
}
138+
}
139+
133140
private boolean isNewTask() {
134-
return getArguments() == null || !getArguments().containsKey(ARGUMENT_EDIT_TASK_ID);
141+
return mEditedTaskId == null;
135142
}
136143
}

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.android.architecture.blueprints.todoapp.addedittask;
1818

1919
import android.support.annotation.NonNull;
20+
import android.support.annotation.Nullable;
2021

2122
import com.example.android.architecture.blueprints.todoapp.data.Task;
2223
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource;
@@ -38,22 +39,26 @@
3839
* therefore, to ensure the developer doesn't instantiate the class manually bypassing Dagger,
3940
* it's good practice minimise the visibility of the class/constructor as much as possible.
4041
*/
41-
final class AddEditTaskPresenter implements AddEditTaskContract.UserActionsListener,
42+
final class AddEditTaskPresenter implements AddEditTaskContract.Presenter,
4243
TasksDataSource.GetTaskCallback {
4344

4445
@NonNull
45-
private final TasksRepository mTasksRepository;
46+
private final TasksDataSource mTasksRepository;
4647

4748
@NonNull
4849
private final AddEditTaskContract.View mAddTaskView;
4950

51+
@Nullable
52+
private String mTaskId;
53+
5054
/**
5155
* Dagger strictly enforces that arguments not marked with {@code @Nullable} are not injected
5256
* with {@code @Nullable} values.
5357
*/
5458
@Inject
55-
AddEditTaskPresenter(TasksRepository tasksRepository,
59+
AddEditTaskPresenter(@Nullable String taskId, TasksRepository tasksRepository,
5660
AddEditTaskContract.View addTaskView) {
61+
mTaskId = taskId;
5762
mTasksRepository = tasksRepository;
5863
mAddTaskView = addTaskView;
5964
}
@@ -64,7 +69,14 @@ final class AddEditTaskPresenter implements AddEditTaskContract.UserActionsListe
6469
*/
6570
@Inject
6671
void setupListeners() {
67-
mAddTaskView.setUserActionListener(this);
72+
mAddTaskView.setPresenter(this);
73+
}
74+
75+
@Override
76+
public void start() {
77+
if (mTaskId != null) {
78+
populateTask();
79+
}
6880
}
6981

7082
@Override
@@ -79,24 +91,36 @@ public void createTask(String title, String description) {
7991
}
8092

8193
@Override
82-
public void updateTask(String taskId, String title, String description) {
83-
mTasksRepository.saveTask(new Task(title, description, taskId));
94+
public void updateTask(String title, String description) {
95+
if (mTaskId == null) {
96+
throw new RuntimeException("updateTask() was called but task is new.");
97+
}
98+
mTasksRepository.saveTask(new Task(title, description, mTaskId));
8499
mAddTaskView.showTasksList(); // After an edit, go back to the list.
85100
}
86101

87102
@Override
88-
public void populateTask(String taskId) {
89-
mTasksRepository.getTask(taskId, this);
103+
public void populateTask() {
104+
if (mTaskId == null) {
105+
throw new RuntimeException("populateTask() was called but task is new.");
106+
}
107+
mTasksRepository.getTask(mTaskId, this);
90108
}
91109

92110
@Override
93111
public void onTaskLoaded(Task task) {
94-
mAddTaskView.setTitle(task.getTitle());
95-
mAddTaskView.setDescription(task.getDescription());
112+
// The view may not be able to handle UI updates anymore
113+
if (mAddTaskView.isActive()) {
114+
mAddTaskView.setTitle(task.getTitle());
115+
mAddTaskView.setDescription(task.getDescription());
116+
}
96117
}
97118

98119
@Override
99120
public void onDataNotAvailable() {
100-
mAddTaskView.showEmptyTaskError();
121+
// The view may not be able to handle UI updates anymore
122+
if (mAddTaskView.isActive()) {
123+
mAddTaskView.showEmptyTaskError();
124+
}
101125
}
102126
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.android.architecture.blueprints.todoapp.addedittask;
22

3+
import android.support.annotation.Nullable;
4+
35
import dagger.Module;
46
import dagger.Provides;
57

@@ -12,12 +14,21 @@ public class AddEditTaskPresenterModule {
1214

1315
private final AddEditTaskContract.View mView;
1416

15-
public AddEditTaskPresenterModule(AddEditTaskContract.View view) {
17+
private String mTaskId;
18+
19+
public AddEditTaskPresenterModule(AddEditTaskContract.View view, @Nullable String taskId) {
1620
mView = view;
21+
mTaskId = taskId;
1722
}
1823

1924
@Provides
2025
AddEditTaskContract.View provideAddEditTaskContractView() {
2126
return mView;
2227
}
28+
29+
@Provides
30+
@Nullable
31+
String provideTaskId() {
32+
return mTaskId;
33+
}
2334
}

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsContract.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,26 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp.statistics;
1818

19+
import com.example.android.architecture.blueprints.todoapp.BasePresenter;
20+
import com.example.android.architecture.blueprints.todoapp.BaseView;
21+
1922
/**
2023
* This specifies the contract between the view and the presenter.
2124
*/
2225
public interface StatisticsContract {
2326

24-
interface View {
27+
interface View extends BaseView<Presenter> {
2528

2629
void setProgressIndicator(boolean active);
2730

28-
void displayStatistics(int numberOfIncompleteTasks, int numberOfCompletedTasks);
31+
void showStatistics(int numberOfIncompleteTasks, int numberOfCompletedTasks);
2932

3033
void showLoadingStatisticsError();
3134

32-
boolean isInactive();
35+
boolean isActive();
3336
}
3437

35-
interface UserActionsListener {
38+
interface Presenter extends BasePresenter {
3639

37-
void loadStatistics();
3840
}
3941
}

0 commit comments

Comments
 (0)