Skip to content

Commit a2b5f75

Browse files
committed
Merge pull request android#14 from googlesamples/todo-mvp_fixRepoLocalCache
Fixes issue with data not being stored in local data source
2 parents e488424 + d97ba16 commit a2b5f75

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/TasksRepository.java

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

1717
package com.example.android.architecture.blueprints.todoapp.data.source;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import android.support.annotation.NonNull;
2022
import android.support.annotation.Nullable;
2123

@@ -27,8 +29,6 @@
2729
import java.util.List;
2830
import java.util.Map;
2931

30-
import static com.google.common.base.Preconditions.checkNotNull;
31-
3232
/**
3333
* Concrete implementation to load tasks from the data sources into a cache.
3434
* <p>
@@ -53,7 +53,7 @@ public class TasksRepository implements TasksDataSource {
5353
* Marks the cache as invalid, to force an update the next time data is requested. This variable
5454
* has package local visibility so it can be accessed from tests.
5555
*/
56-
boolean mCacheIsDirty;
56+
boolean mCacheIsDirty = false;
5757

5858
// Prevent direct instantiation.
5959
private TasksRepository(@NonNull TasksDataSource tasksRemoteDataSource,
@@ -110,7 +110,8 @@ public void getTasks(@NonNull final LoadTasksCallback callback) {
110110
mTasksLocalDataSource.getTasks(new LoadTasksCallback() {
111111
@Override
112112
public void onTasksLoaded(List<Task> tasks) {
113-
processLoadedTasks(tasks, callback);
113+
refreshCache(tasks);
114+
callback.onTasksLoaded(new ArrayList<>(mCachedTasks.values()));
114115
}
115116

116117
@Override
@@ -268,7 +269,9 @@ private void getTasksFromRemoteDataSource(@NonNull final LoadTasksCallback callb
268269
mTasksRemoteDataSource.getTasks(new LoadTasksCallback() {
269270
@Override
270271
public void onTasksLoaded(List<Task> tasks) {
271-
processLoadedTasks(tasks, callback);
272+
refreshCache(tasks);
273+
refreshLocalDataSource(tasks);
274+
callback.onTasksLoaded(new ArrayList<>(mCachedTasks.values()));
272275
}
273276

274277
@Override
@@ -278,18 +281,24 @@ public void onDataNotAvailable() {
278281
});
279282
}
280283

281-
private void processLoadedTasks(List<Task> tasks, final LoadTasksCallback callback) {
284+
private void refreshCache(List<Task> tasks) {
282285
if (mCachedTasks == null) {
283286
mCachedTasks = new LinkedHashMap<>();
284287
}
285288
mCachedTasks.clear();
286289
for (Task task : tasks) {
287290
mCachedTasks.put(task.getId(), task);
288291
}
289-
callback.onTasksLoaded(new ArrayList<>(mCachedTasks.values()));
290292
mCacheIsDirty = false;
291293
}
292294

295+
private void refreshLocalDataSource(List<Task> tasks) {
296+
mTasksLocalDataSource.deleteAllTasks();
297+
for (Task task : tasks) {
298+
mTasksLocalDataSource.saveTask(task);
299+
}
300+
}
301+
293302
@Nullable
294303
private Task getTaskWithId(@NonNull String id) {
295304
checkNotNull(id);

todoapp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/data/source/TasksRepositoryTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp.data.source;
1818

19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.junit.Assert.assertThat;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.mockito.Matchers.any;
23+
import static org.mockito.Matchers.eq;
24+
import static org.mockito.Mockito.never;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
27+
1928
import android.content.Context;
2029

2130
import com.example.android.architecture.blueprints.todoapp.data.Task;
@@ -31,14 +40,6 @@
3140

3241
import java.util.List;
3342

34-
import static org.hamcrest.CoreMatchers.is;
35-
import static org.junit.Assert.assertThat;
36-
import static org.junit.Assert.assertTrue;
37-
import static org.mockito.Matchers.any;
38-
import static org.mockito.Matchers.eq;
39-
import static org.mockito.Mockito.never;
40-
import static org.mockito.Mockito.verify;
41-
4243
/**
4344
* Unit tests for the implementation of the in-memory repository with cache.
4445
*/
@@ -271,7 +272,7 @@ public void deleteTask_deleteTaskToServiceAPIRemovedFromCache() {
271272
@Test
272273
public void getTasksWithDirtyCache_tasksAreRetrievedFromRemote() {
273274
// When calling getTasks in the repository with dirty cache
274-
mTasksRepository.mCacheIsDirty = true;
275+
mTasksRepository.refreshTasks();
275276
mTasksRepository.getTasks(mLoadTasksCallback);
276277

277278
// And the remote data source has data available
@@ -330,6 +331,21 @@ public void getTaskWithBothDataSourcesUnavailable_firesOnDataUnavailable() {
330331
verify(mGetTaskCallback).onDataNotAvailable();
331332
}
332333

334+
@Test
335+
public void getTasks_refreshesLocalDataSource() {
336+
// Mark cache as dirty to force a reload of data from remote data source.
337+
mTasksRepository.refreshTasks();
338+
339+
// When calling getTasks in the repository
340+
mTasksRepository.getTasks(mLoadTasksCallback);
341+
342+
// Make the remote data source return data
343+
setTasksAvailable(mTasksRemoteDataSource, TASKS);
344+
345+
// Verify that the data fetched from the remote data source was saved in local.
346+
verify(mTasksLocalDataSource, times(TASKS.size())).saveTask(any(Task.class));
347+
}
348+
333349
/**
334350
* Convenience method that issues two calls to the tasks repository
335351
*/

todoapp/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ buildscript {
33
jcenter()
44
}
55
dependencies {
6-
classpath 'com.android.tools.build:gradle:2.0.0-beta6'
6+
classpath 'com.android.tools.build:gradle:2.1.0-alpha4'
77

88
// NOTE: Do not place your application dependencies here; they belong
99
// in the individual module build.gradle files

0 commit comments

Comments
 (0)