Skip to content

Commit a8dabba

Browse files
committed
New BaseView and BasePresenter + add/edit presenter lifecycle
1 parent d06e7ba commit a8dabba

File tree

19 files changed

+128
-59
lines changed

19 files changed

+128
-59
lines changed

todo-mvp/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksScreenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public void orientationChange_FilterCompletedPersists() {
377377
onView(withText(TITLE1)).check(matches(isDisplayed()));
378378

379379
// when rotating the screen
380-
TestUtils.rotateToLandscape(mTasksActivityTestRule);
380+
TestUtils.rotateOrientation(mTasksActivityTestRule);
381381

382382
// then nothing changes
383383
onView(withText(TITLE1)).check(matches(isDisplayed()));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2016, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.architecture.blueprints.todoapp;
18+
19+
public interface BasePresenter {
20+
21+
void start();
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2016, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.architecture.blueprints.todoapp;
18+
19+
public interface BaseView<T> {
20+
21+
void setPresenter(T presenter);
22+
23+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ protected void onCreate(Bundle savedInstanceState) {
5050
AddEditTaskFragment addEditTaskFragment =
5151
(AddEditTaskFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
5252

53+
String taskId = null;
5354
if (addEditTaskFragment == null) {
5455
addEditTaskFragment = AddEditTaskFragment.newInstance();
5556

5657
if (getIntent().hasExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID)) {
57-
String taskId = getIntent().getStringExtra(
58+
taskId = getIntent().getStringExtra(
5859
AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID);
5960
actionBar.setTitle(R.string.edit_task);
6061
Bundle bundle = new Bundle();
@@ -69,9 +70,10 @@ protected void onCreate(Bundle savedInstanceState) {
6970
}
7071

7172
// Create the presenter
72-
AddEditTaskPresenter addEditTaskPresenter = new AddEditTaskPresenter(
73-
Injection.provideTasksRepository(getApplicationContext()), addEditTaskFragment);
74-
addEditTaskFragment.setPresenter(addEditTaskPresenter);
73+
new AddEditTaskPresenter(
74+
taskId,
75+
Injection.provideTasksRepository(getApplicationContext()),
76+
addEditTaskFragment);
7577
}
7678

7779
@Override

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
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

@@ -34,12 +37,12 @@ interface View {
3437
boolean isActive();
3538
}
3639

37-
interface Presenter {
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
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class AddEditTaskFragment extends Fragment implements AddEditTaskContract
3939

4040
public static final String ARGUMENT_EDIT_TASK_ID = "EDIT_TASK_ID";
4141

42-
private AddEditTaskContract.Presenter mActionListener;
42+
private AddEditTaskContract.Presenter mPresenter;
4343

4444
private TextView mTitle;
4545

@@ -58,13 +58,12 @@ public AddEditTaskFragment() {
5858
@Override
5959
public void onResume() {
6060
super.onResume();
61-
if (!isNewTask()) {
62-
mActionListener.populateTask(mEditedTaskId);
63-
}
61+
mPresenter.start();
6462
}
6563

66-
public void setPresenter(@NonNull AddEditTaskContract.Presenter listener) {
67-
mActionListener = checkNotNull(listener);
64+
@Override
65+
public void setPresenter(@NonNull AddEditTaskContract.Presenter presenter) {
66+
mPresenter = checkNotNull(presenter);
6867
}
6968

7069
@Override
@@ -80,13 +79,12 @@ public void onActivityCreated(Bundle savedInstanceState) {
8079
@Override
8180
public void onClick(View v) {
8281
if (isNewTask()) {
83-
mActionListener.createTask(
82+
mPresenter.createTask(
8483
mTitle.getText().toString(),
8584
mDescription.getText().toString()
8685
);
8786
} else {
88-
mActionListener.updateTask(
89-
getArguments().getString(ARGUMENT_EDIT_TASK_ID),
87+
mPresenter.updateTask(
9088
mTitle.getText().toString(),
9189
mDescription.getText().toString()
9290
);

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

Lines changed: 32 additions & 5 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;
@@ -36,10 +37,30 @@ public class AddEditTaskPresenter implements AddEditTaskContract.Presenter,
3637
@NonNull
3738
private final AddEditTaskContract.View mAddTaskView;
3839

39-
public AddEditTaskPresenter(@NonNull TasksDataSource tasksRepository,
40+
@Nullable
41+
private String mTaskId;
42+
43+
/**
44+
* Creates a presenter for the add/edit view.
45+
*
46+
* @param taskId ID of the task to edit or null for a new task
47+
* @param tasksRepository a repository of data for tasks
48+
* @param addTaskView the add/edit view
49+
*/
50+
public AddEditTaskPresenter(@Nullable String taskId, @NonNull TasksDataSource tasksRepository,
4051
@NonNull AddEditTaskContract.View addTaskView) {
52+
mTaskId = taskId;
4153
mTasksRepository = checkNotNull(tasksRepository);
4254
mAddTaskView = checkNotNull(addTaskView);
55+
56+
mAddTaskView.setPresenter(this);
57+
}
58+
59+
@Override
60+
public void start() {
61+
if (mTaskId != null) {
62+
populateTask();
63+
}
4364
}
4465

4566
@Override
@@ -54,14 +75,20 @@ public void createTask(String title, String description) {
5475
}
5576

5677
@Override
57-
public void updateTask(String taskId, String title, String description) {
58-
mTasksRepository.saveTask(new Task(title, description, taskId));
78+
public void updateTask(String title, String description) {
79+
if (mTaskId == null) {
80+
throw new RuntimeException("updateTask() was called but task is new.");
81+
}
82+
mTasksRepository.saveTask(new Task(title, description, mTaskId));
5983
mAddTaskView.showTasksList(); // After an edit, go back to the list.
6084
}
6185

6286
@Override
63-
public void populateTask(String taskId) {
64-
mTasksRepository.getTask(taskId, this);
87+
public void populateTask() {
88+
if (mTaskId == null) {
89+
throw new RuntimeException("populateTask() was called but task is new.");
90+
}
91+
mTasksRepository.getTask(mTaskId, this);
6592
}
6693

6794
@Override

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsActivity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ protected void onCreate(Bundle savedInstanceState) {
6969
);
7070
}
7171

72-
StatisticsPresenter statisticsPresenter = new StatisticsPresenter(
72+
new StatisticsPresenter(
7373
Injection.provideTasksRepository(getApplicationContext()), statisticsFragment);
74-
75-
statisticsFragment.setPresenter(statisticsPresenter);
7674
}
7775

7876
@Override

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

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

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

19-
import com.example.android.architecture.blueprints.todoapp.util.BasePresenter;
20-
import com.example.android.architecture.blueprints.todoapp.util.BaseView;
19+
import com.example.android.architecture.blueprints.todoapp.BasePresenter;
20+
import com.example.android.architecture.blueprints.todoapp.BaseView;
2121

2222
/**
2323
* This specifies the contract between the view and the presenter.

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsPresenter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public StatisticsPresenter(@NonNull TasksRepository tasksRepository,
4242
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
4343
mStatisticsView = checkNotNull(statisticsView, "StatisticsView cannot be null!");
4444

45+
mStatisticsView.setPresenter(this);
4546
}
4647

4748
@Override

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailContract.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

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

19-
import com.example.android.architecture.blueprints.todoapp.util.BasePresenter;
19+
import com.example.android.architecture.blueprints.todoapp.BasePresenter;
20+
import com.example.android.architecture.blueprints.todoapp.BaseView;
2021

2122
/**
2223
* This specifies the contract between the view and the presenter.
2324
*/
2425
public interface TaskDetailContract {
2526

26-
interface View {
27+
interface View extends BaseView<Presenter> {
2728

2829
void setLoadingIndicator(boolean active);
2930

@@ -47,12 +48,10 @@ interface View {
4748

4849
void showTaskMarkedActive();
4950

50-
void setPresenter(Presenter listener);
51-
5251
boolean isActive();
5352
}
5453

55-
interface Presenter extends BasePresenter{
54+
interface Presenter extends BasePresenter {
5655

5756
void editTask();
5857

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailPresenter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public TaskDetailPresenter(@Nullable String taskId,
4444
this.mTaskId = taskId;
4545
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null!");
4646
mTaskDetailView = checkNotNull(taskDetailView, "taskDetailView cannot be null!");
47+
4748
mTaskDetailView.setPresenter(this);
4849
}
4950

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ protected void onCreate(Bundle savedInstanceState) {
8181
(TasksFilterType) savedInstanceState.getSerializable(CURRENT_FILTERING_KEY);
8282
mTasksPresenter.setFiltering(currentFiltering);
8383
}
84-
85-
tasksFragment.setPresenter(mTasksPresenter);
8684
}
8785

8886
@Override

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksContract.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
import android.support.annotation.NonNull;
2020

21+
import com.example.android.architecture.blueprints.todoapp.BaseView;
2122
import com.example.android.architecture.blueprints.todoapp.data.Task;
22-
import com.example.android.architecture.blueprints.todoapp.util.BasePresenter;
23+
import com.example.android.architecture.blueprints.todoapp.BasePresenter;
2324

2425
import java.util.List;
2526

@@ -28,7 +29,7 @@
2829
*/
2930
public interface TasksContract {
3031

31-
interface View {
32+
interface View extends BaseView<Presenter> {
3233

3334
void setLoadingIndicator(boolean active);
3435

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksPresenter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class TasksPresenter implements TasksContract.Presenter {
4848
public TasksPresenter(@NonNull TasksRepository tasksRepository, @NonNull TasksContract.View tasksView) {
4949
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
5050
mTasksView = checkNotNull(tasksView, "tasksView cannot be null!");
51+
52+
mTasksView.setPresenter(this);
5153
}
5254

5355
@Override

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/util/BasePresenter.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

todo-mvp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/util/BaseView.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)