Skip to content

Commit d494af2

Browse files
committed
task presenters in mvp and loaders are now set the same way
1 parent 2b74807 commit d494af2

File tree

8 files changed

+21
-18
lines changed

8 files changed

+21
-18
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ interface View {
6565

6666
interface Presenter extends BasePresenter {
6767

68+
void result(int requestCode, int resultCode);
69+
6870
void loadTasks(boolean forceUpdate);
6971

7072
void addNewTask();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public TasksPresenter(@NonNull TasksLoader loader, @NonNull LoaderManager loader
6565
mLoaderManager = checkNotNull(loaderManager, "loader manager cannot be null");
6666
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
6767
mTasksView = checkNotNull(tasksView, "tasksView cannot be null!");
68+
6869
}
6970

7071
@Override

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
public interface BasePresenter {
44

5-
void result(int requestCode, int resultCode);
6-
75
void resume();
86

97
void pause();

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616

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

19+
import android.support.v4.app.LoaderManager;
1920
import android.support.v4.content.Loader;
2021

2122
import com.example.android.architecture.blueprints.todoapp.data.Task;
2223
import com.example.android.architecture.blueprints.todoapp.data.source.TasksLoader;
2324
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository;
2425
import com.google.common.collect.Lists;
2526

27+
import java.util.List;
28+
2629
import org.junit.Before;
2730
import org.junit.Test;
2831
import org.mockito.ArgumentCaptor;
2932
import org.mockito.Captor;
3033
import org.mockito.Mock;
3134
import org.mockito.MockitoAnnotations;
3235

33-
import java.util.List;
34-
3536
import static org.hamcrest.core.Is.is;
3637
import static org.junit.Assert.assertThat;
3738
import static org.mockito.Matchers.any;
@@ -61,6 +62,9 @@ public class TasksPresenterTest {
6162
@Mock
6263
private TasksLoader mTasksLoader;
6364

65+
@Mock
66+
private LoaderManager mLoaderManager;
67+
6468
private TasksPresenter mTasksPresenter;
6569

6670
@Before
@@ -70,11 +74,12 @@ public void setupTasksPresenter() {
7074
MockitoAnnotations.initMocks(this);
7175

7276
// Get a reference to the class under test
73-
mTasksPresenter = new TasksPresenter(mTasksLoader, mTasksRepository, mTasksView);
77+
mTasksPresenter = new TasksPresenter(mTasksLoader, mLoaderManager, mTasksRepository, mTasksView);
7478

7579
// We initialise the tasks to 3, with one active and two completed
7680
TASKS = Lists.newArrayList(new Task("Title1", "Description1"),
77-
new Task("Title2", "Description2", true), new Task("Title3", "Description3", true));
81+
new Task("Title2", "Description2", true), new Task("Title3", "Description3", true)
82+
);
7883
}
7984

8085
@Test

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

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

8688
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ interface View {
6060

6161
void showSuccessfullySavedMessage();
6262

63-
void setPresenter(Presenter listener);
64-
6563
boolean isActive();
6664

6765
void showFilteringPopUpMenu();
6866
}
6967

7068
interface Presenter extends BasePresenter {
7169

70+
void result(int requestCode, int resultCode);
71+
7272
void loadTasks(boolean forceUpdate);
7373

7474
void addNewTask();

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import static com.google.common.base.Preconditions.checkNotNull;
3333

34-
3534
/**
3635
* Listens to user actions from the UI ({@link TasksFragment}), retrieves the data and updates the
3736
* UI as required.
@@ -46,11 +45,9 @@ public class TasksPresenter implements TasksContract.Presenter {
4645

4746
private boolean mFirstLoad = true;
4847

49-
public TasksPresenter(
50-
@NonNull TasksRepository tasksRepository, @NonNull TasksContract.View tasksView) {
48+
public TasksPresenter(@NonNull TasksRepository tasksRepository, @NonNull TasksContract.View tasksView) {
5149
mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
5250
mTasksView = checkNotNull(tasksView, "tasksView cannot be null!");
53-
tasksView.setPresenter(this);
5451
}
5552

5653
@Override
@@ -79,7 +76,7 @@ public void loadTasks(boolean forceUpdate) {
7976
}
8077

8178
/**
82-
* @param forceUpdate Pass in true to refresh the data in the {@link TasksDataSource}
79+
* @param forceUpdate Pass in true to refresh the data in the {@link TasksDataSource}
8380
* @param showLoadingUI Pass in true to display a loading icon in the UI
8481
*/
8582
private void loadTasks(boolean forceUpdate, final boolean showLoadingUI) {
@@ -107,7 +104,7 @@ public void onTasksLoaded(List<Task> tasks) {
107104
}
108105

109106
// We filter the tasks based on the requestType
110-
for (Task task: tasks) {
107+
for (Task task : tasks) {
111108
switch (mCurrentFiltering) {
112109
case ALL_TASKS:
113110
tasksToShow.add(task);
@@ -135,7 +132,7 @@ public void onTasksLoaded(List<Task> tasks) {
135132
mTasksView.setLoadingIndicator(false);
136133
}
137134

138-
processTasks(tasksToShow);
135+
processTasks(tasksToShow);
139136
}
140137

141138
@Override
@@ -227,7 +224,7 @@ public void clearCompletedTasks() {
227224
* Sets the current task filtering type.
228225
*
229226
* @param requestType Can be {@link TasksFilterType#ALL_TASKS},
230-
* {@link TasksFilterType#COMPLETED_TASKS}, or {@link TasksFilterType#ACTIVE_TASKS}
227+
* {@link TasksFilterType#COMPLETED_TASKS}, or {@link TasksFilterType#ACTIVE_TASKS}
231228
*/
232229
@Override
233230
public void setFiltering(TasksFilterType requestType) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
public interface BasePresenter {
44

5-
void result(int requestCode, int resultCode);
6-
75
void resume();
86

97
void pause();

0 commit comments

Comments
 (0)