@Test public void getTasksWithLocalDataSourceUnavailable_tasksAreRetrievedFromRemote() { setTasksNotAvailable(mTasksLocalDataSource); setTasksAvailable(mTasksRemoteDataSource, TASKS); mTasksRepository.getTasks().subscribe(mTasksTestSubscriber); Mockito.verify(mTasksRemoteDataSource).getTasks(); mTasksTestSubscriber.assertValue(TASKS); }
@Override public Flowable<List<Task>> getTasks() { if (mCachedTasks != null && !mCacheIsDirty) { return Flowable.fromIterable(mCachedTasks.values()).toList().toFlowable(); } else if (mCachedTasks == null) { mCachedTasks = new LinkedHashMap<>(); } Flowable<List<Task>> remoteTasks = getAndSaveRemoteTasks(); if (mCacheIsDirty) { return remoteTasks; } else { Flowable<List<Task>> localTasks = getAndCacheLocalTasks(); return Flowable.concat(localTasks, remoteTasks) .filter(tasks -> !tasks.isEmpty()) .firstOrError() .toFlowable(); } }
TasksRepository implements TasksDataSource { @Override public Flowable<List<Task>> getTasks() { if (mCachedTasks != null && !mCacheIsDirty) { return Flowable.fromIterable(mCachedTasks.values()).toList().toFlowable(); } else if (mCachedTasks == null) { mCachedTasks = new LinkedHashMap<>(); } Flowable<List<Task>> remoteTasks = getAndSaveRemoteTasks(); if (mCacheIsDirty) { return remoteTasks; } else { Flowable<List<Task>> localTasks = getAndCacheLocalTasks(); return Flowable.concat(localTasks, remoteTasks) .filter(tasks -> !tasks.isEmpty()) .firstOrError() .toFlowable(); } } }
TasksRepository implements TasksDataSource { @Override public Flowable<List<Task>> getTasks() { if (mCachedTasks != null && !mCacheIsDirty) { return Flowable.fromIterable(mCachedTasks.values()).toList().toFlowable(); } else if (mCachedTasks == null) { mCachedTasks = new LinkedHashMap<>(); } Flowable<List<Task>> remoteTasks = getAndSaveRemoteTasks(); if (mCacheIsDirty) { return remoteTasks; } else { Flowable<List<Task>> localTasks = getAndCacheLocalTasks(); return Flowable.concat(localTasks, remoteTasks) .filter(tasks -> !tasks.isEmpty()) .firstOrError() .toFlowable(); } } private  TasksRepository(@NonNull TasksDataSource tasksRemoteDataSource,
                            @NonNull TasksDataSource tasksLocalDataSource); }
TasksRepository implements TasksDataSource { @Override public Flowable<List<Task>> getTasks() { if (mCachedTasks != null && !mCacheIsDirty) { return Flowable.fromIterable(mCachedTasks.values()).toList().toFlowable(); } else if (mCachedTasks == null) { mCachedTasks = new LinkedHashMap<>(); } Flowable<List<Task>> remoteTasks = getAndSaveRemoteTasks(); if (mCacheIsDirty) { return remoteTasks; } else { Flowable<List<Task>> localTasks = getAndCacheLocalTasks(); return Flowable.concat(localTasks, remoteTasks) .filter(tasks -> !tasks.isEmpty()) .firstOrError() .toFlowable(); } } private  TasksRepository(@NonNull TasksDataSource tasksRemoteDataSource,
                            @NonNull TasksDataSource tasksLocalDataSource); static TasksRepository getInstance(@NonNull TasksDataSource tasksRemoteDataSource,
                                              @NonNull TasksDataSource tasksLocalDataSource); static void destroyInstance(); @Override Flowable<List<Task>> getTasks(); @Override void saveTask(@NonNull Task task); @Override void completeTask(@NonNull Task task); @Override void completeTask(@NonNull String taskId); @Override void activateTask(@NonNull Task task); @Override void activateTask(@NonNull String taskId); @Override void clearCompletedTasks(); @Override Flowable<Optional<Task>> getTask(@NonNull final String taskId); @Override void refreshTasks(); @Override void deleteAllTasks(); @Override void deleteTask(@NonNull String taskId); }
TasksRepository implements TasksDataSource { @Override public Flowable<List<Task>> getTasks() { if (mCachedTasks != null && !mCacheIsDirty) { return Flowable.fromIterable(mCachedTasks.values()).toList().toFlowable(); } else if (mCachedTasks == null) { mCachedTasks = new LinkedHashMap<>(); } Flowable<List<Task>> remoteTasks = getAndSaveRemoteTasks(); if (mCacheIsDirty) { return remoteTasks; } else { Flowable<List<Task>> localTasks = getAndCacheLocalTasks(); return Flowable.concat(localTasks, remoteTasks) .filter(tasks -> !tasks.isEmpty()) .firstOrError() .toFlowable(); } } private  TasksRepository(@NonNull TasksDataSource tasksRemoteDataSource,
                            @NonNull TasksDataSource tasksLocalDataSource); static TasksRepository getInstance(@NonNull TasksDataSource tasksRemoteDataSource,
                                              @NonNull TasksDataSource tasksLocalDataSource); static void destroyInstance(); @Override Flowable<List<Task>> getTasks(); @Override void saveTask(@NonNull Task task); @Override void completeTask(@NonNull Task task); @Override void completeTask(@NonNull String taskId); @Override void activateTask(@NonNull Task task); @Override void activateTask(@NonNull String taskId); @Override void clearCompletedTasks(); @Override Flowable<Optional<Task>> getTask(@NonNull final String taskId); @Override void refreshTasks(); @Override void deleteAllTasks(); @Override void deleteTask(@NonNull String taskId);  }