Skip to content

Commit 8e65642

Browse files
committed
Sync MVP/MVP-Loaders
1 parent 1aabaf4 commit 8e65642

File tree

28 files changed

+119
-82
lines changed

28 files changed

+119
-82
lines changed

todo-mvp-loaders/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/TestUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
*/
2727
public class TestUtils {
2828

29-
public static void rotateToLandscape(ActivityTestRule<? extends Activity> activityTestRule) {
29+
private static void rotateToLandscape(ActivityTestRule<? extends Activity> activityTestRule) {
3030
activityTestRule.getActivity()
3131
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
3232
}
3333

34-
public static void rotateToPortrait(ActivityTestRule<? extends Activity> activityTestRule) {
34+
private static void rotateToPortrait(ActivityTestRule<? extends Activity> activityTestRule) {
3535
activityTestRule.getActivity()
3636
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
3737
}

todo-mvp-loaders/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()));

todo-mvp-loaders/app/src/androidTestMock/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailScreenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void orientationChange_menuAndTaskPersist() {
133133
// Check delete menu item is displayed and is unique
134134
onView(withId(R.id.menu_delete)).check(matches(isDisplayed()));
135135

136-
TestUtils.rotateToLandscape(mTaskDetailActivityTestRule);
136+
TestUtils.rotateOrientation(mTaskDetailActivityTestRule);
137137

138138
onView(withId(R.id.task_detail_title)).check(matches(withText(TASK_TITLE)));
139139
onView(withId(R.id.task_detail_description)).check(matches(withText(TASK_DESCRIPTION)));
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-loaders/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskActivity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ protected void onCreate(Bundle savedInstanceState) {
7272

7373
// Create the loader and presenter
7474
TaskLoader taskLoader = new TaskLoader(taskId, getApplicationContext());
75-
AddEditTaskPresenter addEditTaskPresenter = new AddEditTaskPresenter(
75+
new AddEditTaskPresenter(
7676
taskId,
7777
Injection.provideTasksRepository(getApplicationContext()),
7878
addEditTaskFragment,
79-
taskLoader);
80-
81-
addEditTaskFragment.setPresenter(addEditTaskPresenter);
79+
taskLoader,
80+
getSupportLoaderManager());
8281
}
8382

8483
@Override

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

Lines changed: 5 additions & 2 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

@@ -32,7 +35,7 @@ interface View {
3235
void setDescription(String description);
3336
}
3437

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

3740
void createTask(String title, String description);
3841

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

Lines changed: 7 additions & 6 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 AddEditTaskPresenter mTasksPresenter;
42+
private AddEditTaskContract.Presenter mPresenter;
4343

4444
private TextView mTitle;
4545

@@ -58,11 +58,12 @@ public AddEditTaskFragment() {
5858
@Override
5959
public void onResume() {
6060
super.onResume();
61-
mTasksPresenter.startLoader(this);
61+
mPresenter.start();
6262
}
6363

64-
public void setPresenter(@NonNull AddEditTaskPresenter presenter) {
65-
mTasksPresenter = checkNotNull(presenter);
64+
@Override
65+
public void setPresenter(@NonNull AddEditTaskContract.Presenter presenter) {
66+
mPresenter = checkNotNull(presenter);
6667
}
6768

6869
@Override
@@ -78,11 +79,11 @@ public void onActivityCreated(Bundle savedInstanceState) {
7879
@Override
7980
public void onClick(View v) {
8081
if (isNewTask()) {
81-
mTasksPresenter.createTask(
82+
mPresenter.createTask(
8283
mTitle.getText().toString(),
8384
mDescription.getText().toString());
8485
} else {
85-
mTasksPresenter.updateTask(
86+
mPresenter.updateTask(
8687
mEditedTaskId,
8788
mTitle.getText().toString(),
8889
mDescription.getText().toString());

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
public class AddEditTaskPresenter implements AddEditTaskContract.Presenter,
3636
LoaderManager.LoaderCallbacks<Task> {
3737

38+
private static final int TASK_QUERY = 2;
39+
3840
@NonNull
3941
private TasksDataSource mTasksRepository;
4042

@@ -46,25 +48,23 @@ public class AddEditTaskPresenter implements AddEditTaskContract.Presenter,
4648

4749
private TaskLoader mTaskLoader;
4850

49-
private int TASK_QUERY = 2;
51+
private final LoaderManager mLoaderManager;
5052

5153
public AddEditTaskPresenter(@Nullable String taskId, @NonNull TasksDataSource tasksRepository,
52-
@NonNull AddEditTaskContract.View addTaskView, @NonNull TaskLoader taskLoader) {
54+
@NonNull AddEditTaskContract.View addTaskView, @NonNull TaskLoader taskLoader,
55+
@NonNull LoaderManager loaderManager) {
5356
mTaskId = taskId;
5457
mTasksRepository = checkNotNull(tasksRepository);
5558
mAddTaskView = checkNotNull(addTaskView);
5659
mTaskLoader = checkNotNull(taskLoader);
60+
mLoaderManager = checkNotNull(loaderManager, "loaderManager cannot be null!");
61+
62+
mAddTaskView.setPresenter(this);
5763
}
5864

59-
/**
60-
* This starts the {@link LoaderManager}, querying the task. It returns the AddEditTaskPresenter
61-
* so it can be chained with the constructor. This isn't called from the constructor to enable
62-
* writing unit tests for the non loader methods in the AddEditTaskPresenter (creating an
63-
* instance from a unit test would fail if this method were called from it).
64-
*/
65-
public AddEditTaskPresenter startLoader(AddEditTaskFragment fragment) {
66-
fragment.getLoaderManager().initLoader(TASK_QUERY, null, this);
67-
return this;
65+
@Override
66+
public void start() {
67+
mLoaderManager.initLoader(TASK_QUERY, null, this);
6868
}
6969

7070
@Override

todo-mvp-loaders/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
@@ -72,9 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
7272
TasksLoader tasksLoader = new TasksLoader(getApplicationContext(),
7373
Injection.provideTasksRepository(getApplicationContext()));
7474

75-
StatisticsPresenter statisticsPresenter = new StatisticsPresenter(statisticsFragment, tasksLoader, getSupportLoaderManager());
76-
77-
statisticsFragment.setPresenter(statisticsPresenter);
75+
new StatisticsPresenter(statisticsFragment, tasksLoader, getSupportLoaderManager());
7876
}
7977

8078
@Override

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

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

1717
package com.example.android.architecture.blueprints.todoapp.statistics;
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 StatisticsContract {
2526

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

2829
void setProgressIndicator(boolean active);
2930

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public class StatisticsFragment extends Fragment implements StatisticsContract.V
3636

3737
private TextView mStatisticsTV;
3838

39-
private StatisticsPresenter mTasksPresenter;
39+
private StatisticsContract.Presenter mPresenter;
4040

4141
public static StatisticsFragment newInstance() {
4242
return new StatisticsFragment();
4343
}
4444

45-
public void setPresenter(@NonNull StatisticsPresenter presenter) {
46-
mTasksPresenter = checkNotNull(presenter);
45+
@Override
46+
public void setPresenter(@NonNull StatisticsContract.Presenter presenter) {
47+
mPresenter = checkNotNull(presenter);
4748
}
4849

4950
@Nullable
@@ -58,7 +59,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5859
@Override
5960
public void onResume() {
6061
super.onResume();
61-
mTasksPresenter.resume();
62+
mPresenter.start();
6263
}
6364

6465
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ public StatisticsPresenter(@NonNull StatisticsContract.View statisticsView,
4848
mStatisticsView = checkNotNull(statisticsView, "StatisticsView cannot be null!");
4949
mTasksLoader = checkNotNull(tasksLoader, "tasksLoader cannot be null!");
5050
mLoaderManager = checkNotNull(loaderManager, "loaderManager cannot be null!");
51+
52+
mStatisticsView.setPresenter(this);
5153
}
5254

5355
@Override
54-
public void resume() {
56+
public void start() {
5557
mLoaderManager.initLoader(TASK_QUERY, null, this);
5658
}
5759

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ protected void onCreate(Bundle savedInstanceState) {
6363
}
6464

6565
// Create the presenter
66-
TaskDetailPresenter mTaskDetailPresenter = new TaskDetailPresenter(
66+
new TaskDetailPresenter(
6767
taskId,
6868
Injection.provideTasksRepository(getApplicationContext()),
6969
taskDetailFragment,
7070
new TaskLoader(taskId, getApplicationContext()),
7171
getSupportLoaderManager());
72-
73-
taskDetailFragment.setPresenter(mTaskDetailPresenter);
7472
}
7573

7674
@Override

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

Lines changed: 3 additions & 2 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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static TaskDetailFragment newInstance(String taskId) {
6767
@Override
6868
public void onResume() {
6969
super.onResume();
70-
mPresenter.resume();
70+
mPresenter.start();
7171
}
7272

7373
@Nullable
@@ -94,7 +94,8 @@ public void onClick(View v) {
9494
return root;
9595
}
9696

97-
public void setPresenter(@NonNull TaskDetailPresenter presenter) {
97+
@Override
98+
public void setPresenter(@NonNull TaskDetailContract.Presenter presenter) {
9899
mPresenter = checkNotNull(presenter);
99100
}
100101

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ public TaskDetailPresenter(@Nullable String taskId,
5858
mTaskDetailView = checkNotNull(taskDetailView, "taskDetailView cannot be null!");
5959
mTaskLoader = checkNotNull(taskLoader, "taskLoader cannot be null!");
6060
mLoaderManager = checkNotNull(loaderManager, "loaderManager cannot be null!");
61+
62+
mTaskDetailView.setPresenter(this);
6163
}
6264

6365
@Override
64-
public void resume() {
66+
public void start() {
6567
openTask();
6668
}
6769

todo-mvp-loaders/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
@@ -90,8 +90,6 @@ protected void onCreate(Bundle savedInstanceState) {
9090
(TasksFilterType) savedInstanceState.getSerializable(CURRENT_FILTERING_KEY);
9191
mTasksPresenter.setFiltering(currentFiltering);
9292
}
93-
94-
tasksFragment.setPresenter(mTasksPresenter);
9593
}
9694

9795
@Override

todo-mvp-loaders/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

0 commit comments

Comments
 (0)