Skip to content

Commit 13910df

Browse files
committed
adding BaseView and BasePresenter to the root
1 parent f915a40 commit 13910df

File tree

20 files changed

+46
-37
lines changed

20 files changed

+46
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface View {
3232
void setDescription(String description);
3333
}
3434

35-
interface UserActionsListener {
35+
interface Presenter {
3636

3737
void createTask(String title, String description);
3838

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* Listens to user actions from the UI ({@link AddEditTaskFragment}), retrieves the data and updates
3333
* the UI as required.
3434
*/
35-
public class AddEditTaskPresenter implements AddEditTaskContract.UserActionsListener,
35+
public class AddEditTaskPresenter implements AddEditTaskContract.Presenter,
3636
LoaderManager.LoaderCallbacks<Task> {
3737

3838
@NonNull

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ protected void onCreate(Bundle savedInstanceState) {
6969
}
7070

7171
// Create the presenter
72-
new AddEditTaskPresenter(
72+
AddEditTaskPresenter addEditTaskPresenter = new AddEditTaskPresenter(
7373
Injection.provideTasksRepository(getApplicationContext()), addEditTaskFragment);
74+
addEditTaskFragment.setPresenter(addEditTaskPresenter);
7475
}
7576

7677
@Override

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ interface View {
3131

3232
void setDescription(String description);
3333

34-
void setActionListener(UserActionsListener listener);
35-
3634
boolean isActive();
3735
}
3836

39-
interface UserActionsListener {
37+
interface Presenter {
4038

4139
void createTask(String title, String description);
4240

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

Lines changed: 6 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 AddEditTaskContract.UserActionsListener mActionListener;
42+
private AddEditTaskContract.Presenter mActionListener;
4343

4444
private TextView mTitle;
4545

@@ -63,8 +63,7 @@ public void onResume() {
6363
}
6464
}
6565

66-
@Override
67-
public void setActionListener(@NonNull AddEditTaskContract.UserActionsListener listener) {
66+
public void setPresenter(@NonNull AddEditTaskContract.Presenter listener) {
6867
mActionListener = checkNotNull(listener);
6968
}
7069

@@ -83,12 +82,14 @@ public void onClick(View v) {
8382
if (isNewTask()) {
8483
mActionListener.createTask(
8584
mTitle.getText().toString(),
86-
mDescription.getText().toString());
85+
mDescription.getText().toString()
86+
);
8787
} else {
8888
mActionListener.updateTask(
8989
getArguments().getString(ARGUMENT_EDIT_TASK_ID),
9090
mTitle.getText().toString(),
91-
mDescription.getText().toString());
91+
mDescription.getText().toString()
92+
);
9293
}
9394

9495
}
@@ -134,7 +135,6 @@ public boolean isActive() {
134135
return isAdded();
135136
}
136137

137-
138138
private void setTaskIdIfAny() {
139139
if (getArguments() != null && getArguments().containsKey(ARGUMENT_EDIT_TASK_ID)) {
140140
mEditedTaskId = getArguments().getString(ARGUMENT_EDIT_TASK_ID);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Listens to user actions from the UI ({@link AddEditTaskFragment}), retrieves the data and updates
2828
* the UI as required.
2929
*/
30-
public class AddEditTaskPresenter implements AddEditTaskContract.UserActionsListener,
30+
public class AddEditTaskPresenter implements AddEditTaskContract.Presenter,
3131
TasksDataSource.GetTaskCallback {
3232

3333
@NonNull
@@ -40,7 +40,6 @@ public AddEditTaskPresenter(@NonNull TasksDataSource tasksRepository,
4040
@NonNull AddEditTaskContract.View addTaskView) {
4141
mTasksRepository = checkNotNull(tasksRepository);
4242
mAddTaskView = checkNotNull(addTaskView);
43-
addTaskView.setActionListener(this);
4443
}
4544

4645
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
package com.example.android.architecture.blueprints.todoapp.statistics;
1818

1919
import com.example.android.architecture.blueprints.todoapp.util.BasePresenter;
20+
import com.example.android.architecture.blueprints.todoapp.util.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/app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsFragment.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public static StatisticsFragment newInstance() {
4242
return new StatisticsFragment();
4343
}
4444

45-
public void setPresenter(@NonNull StatisticsContract.Presenter listener) {
46-
mPresenter = checkNotNull(listener);
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-
mPresenter.resume();
62+
mPresenter.start();
6263
}
6364

6465
@Override
@@ -91,4 +92,5 @@ public void showLoadingStatisticsError() {
9192
public boolean isActive() {
9293
return isAdded();
9394
}
95+
9496
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public StatisticsPresenter(@NonNull TasksRepository tasksRepository,
4141
@NonNull StatisticsContract.View statisticsView) {
4242
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
4343
mStatisticsView = checkNotNull(statisticsView, "StatisticsView cannot be null!");
44+
4445
}
4546

4647
@Override
47-
public void resume() {
48+
public void start() {
4849
loadStatistics();
4950
}
5051

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

Lines changed: 1 addition & 1 deletion
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TaskDetailPresenter(@Nullable String taskId,
4848
}
4949

5050
@Override
51-
public void resume() {
51+
public void start() {
5252
openTask();
5353
}
5454

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
8787
@Override
8888
public void onResume() {
8989
super.onResume();
90-
mPresenter.resume();
90+
mPresenter.start();
9191
}
9292

9393
public void setPresenter(@NonNull TasksContract.Presenter listener) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public TasksPresenter(@NonNull TasksRepository tasksRepository, @NonNull TasksCo
5151
}
5252

5353
@Override
54-
public void resume() {
54+
public void start() {
5555
loadTasks(false);
5656
}
5757

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface BasePresenter {
44

5-
void resume();
5+
void start();
66

77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.android.architecture.blueprints.todoapp.util;
2+
3+
public interface BaseView<T> {
4+
5+
void setPresenter(T presenter);
6+
7+
}

todo-mvp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsPresenterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void setupStatisticsPresenter() {
6868
// The presenter won't update the view unless it's active.
6969
when(mStatisticsView.isActive()).thenReturn(true);
7070

71-
// We initialise the tasks to 3, with one active and two completed
71+
// We start the tasks to 3, with one active and two completed
7272
TASKS = Lists.newArrayList(new Task("Title1", "Description1"),
7373
new Task("Title2", "Description2", true), new Task("Title3", "Description3", true));
7474
}
@@ -79,7 +79,7 @@ public void loadEmptyTasksFromRepository_CallViewToDisplay() {
7979
TASKS.clear();
8080

8181
// When loading of Tasks is requested
82-
mStatisticsPresenter.resume();
82+
mStatisticsPresenter.start();
8383

8484
//Then progress indicator is shown
8585
verify(mStatisticsView).setProgressIndicator(true);
@@ -98,7 +98,7 @@ public void loadNonEmptyTasksFromRepository_CallViewToDisplay() {
9898
// Given an initialized StatisticsPresenter with 1 active and 2 completed tasks
9999

100100
// When loading of Tasks is requested
101-
mStatisticsPresenter.resume();
101+
mStatisticsPresenter.start();
102102

103103
//Then progress indicator is shown
104104
verify(mStatisticsView).setProgressIndicator(true);
@@ -115,7 +115,7 @@ public void loadNonEmptyTasksFromRepository_CallViewToDisplay() {
115115
@Test
116116
public void loadStatisticsWhenTasksAreUnavailable_CallErrorToDisplay() {
117117
// When statistics are loaded
118-
mStatisticsPresenter.resume();
118+
mStatisticsPresenter.start();
119119

120120
// And tasks data isn't available
121121
verify(mTasksRepository).getTasks(mLoadTasksCallbackCaptor.capture());

todo-mvp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailPresenterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void setup() {
7878
public void getActiveTaskFromRepositoryAndLoadIntoView() {
7979
// When tasks presenter is asked to open a task
8080
mTaskDetailPresenter = new TaskDetailPresenter(ACTIVE_TASK.getId(), mTasksRepository, mTaskDetailView);
81-
mTaskDetailPresenter.resume();
81+
mTaskDetailPresenter.start();
8282

8383
// Then task is loaded from model, callback is captured and progress indicator is shown
8484
verify(mTasksRepository).getTask(eq(ACTIVE_TASK.getId()), mGetTaskCallbackCaptor.capture());
@@ -98,7 +98,7 @@ public void getActiveTaskFromRepositoryAndLoadIntoView() {
9898
@Test
9999
public void getCompletedTaskFromRepositoryAndLoadIntoView() {
100100
mTaskDetailPresenter = new TaskDetailPresenter(COMPLETED_TASK.getId(), mTasksRepository, mTaskDetailView);
101-
mTaskDetailPresenter.resume();
101+
mTaskDetailPresenter.start();
102102

103103
// Then task is loaded from model, callback is captured and progress indicator is shown
104104
verify(mTasksRepository).getTask(eq(COMPLETED_TASK.getId()), mGetTaskCallbackCaptor.capture());
@@ -119,7 +119,7 @@ public void getCompletedTaskFromRepositoryAndLoadIntoView() {
119119
public void getUnknownTaskFromRepositoryAndLoadIntoView() {
120120
// When loading of a task is requested with an invalid task ID.
121121
mTaskDetailPresenter = new TaskDetailPresenter(INVALID_TASK_ID, mTasksRepository, mTaskDetailView);
122-
mTaskDetailPresenter.resume();
122+
mTaskDetailPresenter.start();
123123
verify(mTaskDetailView).showMissingTask();
124124
}
125125

@@ -142,7 +142,7 @@ public void completeTask() {
142142
// Given an initialized presenter with an active task
143143
Task task = new Task(TITLE_TEST, DESCRIPTION_TEST);
144144
mTaskDetailPresenter = new TaskDetailPresenter(task.getId(), mTasksRepository, mTaskDetailView);
145-
mTaskDetailPresenter.resume();
145+
mTaskDetailPresenter.start();
146146

147147
// When the presenter is asked to complete the task
148148
mTaskDetailPresenter.completeTask();
@@ -157,7 +157,7 @@ public void activateTask() {
157157
// Given an initialized presenter with a completed task
158158
Task task = new Task(TITLE_TEST, DESCRIPTION_TEST, true);
159159
mTaskDetailPresenter = new TaskDetailPresenter(task.getId(), mTasksRepository, mTaskDetailView);
160-
mTaskDetailPresenter.resume();
160+
mTaskDetailPresenter.start();
161161

162162
// When the presenter is asked to activate the task
163163
mTaskDetailPresenter.activateTask();

todo-mvp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksPresenterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setupTasksPresenter() {
6969
// The presenter won't update the view unless it's active.
7070
when(mTasksView.isActive()).thenReturn(true);
7171

72-
// We initialise the tasks to 3, with one active and two completed
72+
// We start the tasks to 3, with one active and two completed
7373
TASKS = Lists.newArrayList(new Task("Title1", "Description1"),
7474
new Task("Title2", "Description2", true), new Task("Title3", "Description3", true));
7575
}

0 commit comments

Comments
 (0)