Skip to content

Commit f915a40

Browse files
committed
refactoring statistics page
1 parent 7a4e77a commit f915a40

File tree

11 files changed

+54
-42
lines changed

11 files changed

+54
-42
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
7272
TasksLoader tasksLoader = new TasksLoader(getApplicationContext(),
7373
Injection.provideTasksRepository(getApplicationContext()));
7474

75-
StatisticsPresenter statisticsPresenter = new StatisticsPresenter(statisticsFragment,
76-
tasksLoader);
75+
StatisticsPresenter statisticsPresenter = new StatisticsPresenter(statisticsFragment, tasksLoader, getSupportLoaderManager());
7776

7877
statisticsFragment.setPresenter(statisticsPresenter);
7978
}

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

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

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

19+
import com.example.android.architecture.blueprints.todoapp.util.BasePresenter;
20+
1921
/**
2022
* This specifies the contract between the view and the presenter.
2123
*/
@@ -29,4 +31,8 @@ interface View {
2931

3032
void showLoadingStatisticsError();
3133
}
34+
35+
interface Presenter extends BasePresenter {
36+
37+
}
3238
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5858
@Override
5959
public void onResume() {
6060
super.onResume();
61-
mTasksPresenter.startLoader(this);
61+
mTasksPresenter.resume();
6262
}
6363

6464
@Override

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,27 @@
3232
* Listens to user actions from the UI ({@link StatisticsFragment}), retrieves the data and updates
3333
* the UI as required.
3434
*/
35-
public class StatisticsPresenter implements LoaderManager.LoaderCallbacks<List<Task>> {
35+
public class StatisticsPresenter implements StatisticsContract.Presenter, LoaderManager.LoaderCallbacks<List<Task>> {
3636

3737
private static final int TASK_QUERY = 3;
3838

3939
private StatisticsContract.View mStatisticsView;
4040

4141
private TasksLoader mTasksLoader;
4242

43+
private LoaderManager mLoaderManager;
44+
4345
public StatisticsPresenter(@NonNull StatisticsContract.View statisticsView,
44-
@NonNull TasksLoader tasksLoader) {
46+
@NonNull TasksLoader tasksLoader,
47+
@NonNull LoaderManager loaderManager) {
4548
mStatisticsView = checkNotNull(statisticsView, "StatisticsView cannot be null!");
4649
mTasksLoader = checkNotNull(tasksLoader, "tasksLoader cannot be null!");
50+
mLoaderManager = checkNotNull(loaderManager, "loaderManager cannot be null!");
4751
}
4852

49-
/**
50-
* This starts the {@link LoaderManager}, querying the list of tasks. It returns the
51-
* StatisticsPresenter so it can be chained with the constructor. This isn't called from the
52-
* constructor to enable writing unit tests for the non loader methods in the
53-
* StatisticsPresenter (creating an instance from a unit test would fail if this method were
54-
* called from it).
55-
*/
56-
public StatisticsPresenter startLoader(StatisticsFragment fragment) {
57-
fragment.getLoaderManager().initLoader(TASK_QUERY, null, this);
58-
return this;
53+
@Override
54+
public void resume() {
55+
mLoaderManager.initLoader(TASK_QUERY, null, this);
5956
}
6057

6158
@Override
@@ -75,7 +72,6 @@ public void onLoaderReset(Loader<List<Task>> loader) {
7572
}
7673

7774
private void loadStatistics(List<Task> tasks) {
78-
7975
if (tasks == null) {
8076
mStatisticsView.showLoadingStatisticsError();
8177
} else {
@@ -96,4 +92,5 @@ private void loadStatistics(List<Task> tasks) {
9692
mStatisticsView.showStatistics(activeTasks, completedTasks);
9793
}
9894
}
95+
9996
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ protected void onCreate(Bundle savedInstanceState) {
7575

7676
// Create the presenter
7777
TasksRepository repository = Injection.provideTasksRepository(getApplicationContext());
78+
TasksLoader tasksLoader = new TasksLoader(getApplicationContext(), repository);
79+
7880
mTasksPresenter = new TasksPresenter(
79-
new TasksLoader(getApplicationContext(), repository),
81+
tasksLoader,
8082
getSupportLoaderManager(),
8183
repository,
82-
tasksFragment);
84+
tasksFragment
85+
);
8386

8487
// Load previously saved state, if available.
8588
if (savedInstanceState != null) {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp.statistics;
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.google.common.collect.Lists;
2425

26+
import java.util.List;
27+
2528
import org.junit.Before;
2629
import org.junit.Test;
2730
import org.mockito.Mock;
2831
import org.mockito.MockitoAnnotations;
2932

30-
import java.util.List;
31-
3233
import static org.mockito.Mockito.mock;
3334
import static org.mockito.Mockito.verify;
3435

@@ -45,6 +46,9 @@ public class StatisticsPresenterTest {
4546
@Mock
4647
private TasksLoader mTasksLoader;
4748

49+
@Mock
50+
private LoaderManager mLoaderManager;
51+
4852
private StatisticsPresenter mStatisticsPresenter;
4953

5054
@Before
@@ -54,7 +58,7 @@ public void setupStatisticsPresenter() {
5458
MockitoAnnotations.initMocks(this);
5559

5660
// Get a reference to the class under test
57-
mStatisticsPresenter = new StatisticsPresenter(mStatisticsView, mTasksLoader);
61+
mStatisticsPresenter = new StatisticsPresenter(mStatisticsView, mTasksLoader, mLoaderManager);
5862

5963
// We initialise the tasks to 3, with one active and two completed
6064
TASKS = Lists.newArrayList(new Task("Title1", "Description1"),

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public class StatisticsActivity extends AppCompatActivity {
3838

3939
private DrawerLayout mDrawerLayout;
4040

41-
private StatisticsPresenter mStatisticsPresenter;
42-
4341
@Override
4442
protected void onCreate(Bundle savedInstanceState) {
4543
super.onCreate(savedInstanceState);
@@ -67,11 +65,14 @@ protected void onCreate(Bundle savedInstanceState) {
6765
if (statisticsFragment == null) {
6866
statisticsFragment = StatisticsFragment.newInstance();
6967
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
70-
statisticsFragment, R.id.contentFrame);
68+
statisticsFragment, R.id.contentFrame
69+
);
7170
}
7271

73-
mStatisticsPresenter = new StatisticsPresenter(
72+
StatisticsPresenter statisticsPresenter = new StatisticsPresenter(
7473
Injection.provideTasksRepository(getApplicationContext()), statisticsFragment);
74+
75+
statisticsFragment.setPresenter(statisticsPresenter);
7576
}
7677

7778
@Override

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

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

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

19+
import com.example.android.architecture.blueprints.todoapp.util.BasePresenter;
20+
1921
/**
2022
* This specifies the contract between the view and the presenter.
2123
*/
@@ -29,13 +31,10 @@ interface View {
2931

3032
void showLoadingStatisticsError();
3133

32-
void setActionListener(UserActionsListener listener);
33-
3434
boolean isActive();
3535
}
3636

37-
interface UserActionsListener {
37+
interface Presenter extends BasePresenter {
3838

39-
void loadStatistics();
4039
}
4140
}

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

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

3737
private TextView mStatisticsTV;
3838

39-
private StatisticsContract.UserActionsListener mUserActionsListener;
39+
private StatisticsContract.Presenter mPresenter;
4040

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

45-
@Override
46-
public void setActionListener(@NonNull StatisticsContract.UserActionsListener listener) {
47-
mUserActionsListener = checkNotNull(listener);
45+
public void setPresenter(@NonNull StatisticsContract.Presenter listener) {
46+
mPresenter = checkNotNull(listener);
4847
}
4948

5049
@Nullable
@@ -59,7 +58,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5958
@Override
6059
public void onResume() {
6160
super.onResume();
62-
mUserActionsListener.loadStatistics();
61+
mPresenter.resume();
6362
}
6463

6564
@Override

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Listens to user actions from the UI ({@link StatisticsFragment}), retrieves the data and updates
3232
* the UI as required.
3333
*/
34-
public class StatisticsPresenter implements StatisticsContract.UserActionsListener {
34+
public class StatisticsPresenter implements StatisticsContract.Presenter {
3535

3636
private final TasksRepository mTasksRepository;
3737

@@ -41,11 +41,14 @@ 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-
mStatisticsView.setActionListener(this);
4544
}
4645

4746
@Override
48-
public void loadStatistics() {
47+
public void resume() {
48+
loadStatistics();
49+
}
50+
51+
private void loadStatistics() {
4952
mStatisticsView.setProgressIndicator(true);
5053

5154
// The network request might be handled in a different thread so make sure Espresso knows
@@ -92,4 +95,5 @@ public void onDataNotAvailable() {
9295
}
9396
});
9497
}
98+
9599
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository;
2222
import com.google.common.collect.Lists;
2323

24+
import java.util.List;
25+
2426
import org.junit.Before;
2527
import org.junit.Test;
2628
import org.mockito.ArgumentCaptor;
2729
import org.mockito.Captor;
2830
import org.mockito.Mock;
2931
import org.mockito.MockitoAnnotations;
3032

31-
import java.util.List;
32-
3333
import static org.mockito.Mockito.verify;
3434
import static org.mockito.Mockito.when;
3535

@@ -79,7 +79,7 @@ public void loadEmptyTasksFromRepository_CallViewToDisplay() {
7979
TASKS.clear();
8080

8181
// When loading of Tasks is requested
82-
mStatisticsPresenter.loadStatistics();
82+
mStatisticsPresenter.resume();
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.loadStatistics();
101+
mStatisticsPresenter.resume();
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.loadStatistics();
118+
mStatisticsPresenter.resume();
119119

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

0 commit comments

Comments
 (0)