use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.
the class TasksRemoteDataSource method activateTask.
@Override
public void activateTask(@NonNull Task task) {
Task activeTask = new Task(task.getTitle(), task.getDescription(), task.getId());
TASKS_SERVICE_DATA.put(task.getId(), activeTask);
}
use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.
the class StatisticsPresenter method loadStatistics.
private void loadStatistics() {
mStatisticsView.setProgressIndicator(true);
// The network request might be handled in a different thread so make sure Espresso knows
// that the app is busy until the response is handled.
// App is busy until further notice
EspressoIdlingResource.increment();
Flowable<Task> tasks = mTasksRepository.getTasks().flatMap(Flowable::fromIterable);
// TODO lambda
// .flatMap(new Function<List<Task>, Publisher<? extends Task>>() {
// @Override
// public Publisher<? extends Task> apply(List<Task> source) throws Exception {
// return Flowable.fromIterable(source);
// }
// });
Flowable<Long> completedTasks = tasks.filter(task -> task.isCompleted()).count().toFlowable();
// TODO lambda
// Flowable<Long> completedTasks = tasks.filter(new Predicate<Task>() {
// @Override
// public boolean test(Task task) throws Exception {
// return task.isCompleted();
// }
// }).count().toFlowable();
Flowable<Long> activeTasks = tasks.filter(Task::isActive).count().toFlowable();
// TODO lambda
// Flowable<Long> activeTasks = tasks.filter(new Predicate<Task>() {
// @Override
// public boolean test(Task task) throws Exception {
// return task.isActive();
// }
// }).count().toFlowable();
Disposable disposable = Flowable.zip(completedTasks, activeTasks, (completed, active) -> Pair.create(active, completed)).subscribeOn(mSchedulerProvider.computation()).observeOn(mSchedulerProvider.ui()).doFinally(() -> {
if (!EspressoIdlingResource.getIdlingResource().isIdleNow()) {
// Set app as idle.
EspressoIdlingResource.decrement();
}
}).subscribe(// onNext
stats -> mStatisticsView.showStatistics(Ints.saturatedCast(stats.first), Ints.saturatedCast(stats.second)), // onError
throwable -> mStatisticsView.showLoadingStatisticsError(), // onCompleted
() -> mStatisticsView.setProgressIndicator(false));
// TODO lambda
// new Action() {
// @Override
// public void run() throws Exception {
// mStatisticsView.setProgressIndicator(false);
// }
// });
mCompositeDisposable.add(disposable);
}
use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.
the class AddEditTaskPresenterTest method populateTask_callsRepoAndUpdatesViewOnSuccess.
@Test
public void populateTask_callsRepoAndUpdatesViewOnSuccess() {
Task testTask = new Task("TITLE", "DESCRIPTION");
Optional<Task> taskOptional = Optional.of(testTask);
when(mTasksRepository.getTask(testTask.getId())).thenReturn(Flowable.just(taskOptional));
// Get a reference to the class under test
mAddEditTaskPresenter = new AddEditTaskPresenter(testTask.getId(), mTasksRepository, mAddEditTaskView, true, mSchedulerProvider);
// When the presenter is asked to populate an existing task
mAddEditTaskPresenter.populateTask();
// Then the task repository is queried and the view updated
verify(mTasksRepository).getTask(eq(testTask.getId()));
verify(mAddEditTaskView).setTitle(testTask.getTitle());
verify(mAddEditTaskView).setDescription(testTask.getDescription());
assertThat(mAddEditTaskPresenter.isDataMissing(), is(false));
}
use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.
the class TasksRepositoryTest method deleteTask_deleteTaskToServiceAPIRemovedFromCache.
@Test
public void deleteTask_deleteTaskToServiceAPIRemovedFromCache() {
// Given a task in the repository
Task newTask = new Task(TASK_TITLE, "Some Task Description", true);
mTasksRepository.saveTask(newTask);
assertThat(mTasksRepository.mCachedTasks.containsKey(newTask.getId()), is(true));
// When deleted
mTasksRepository.deleteTask(newTask.getId());
// Verify the data sources were called
verify(mTasksRemoteDataSource).deleteTask(newTask.getId());
verify(mTasksLocalDataSource).deleteTask(newTask.getId());
// Verify it's removed from repository
assertThat(mTasksRepository.mCachedTasks.containsKey(newTask.getId()), is(false));
}
use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.
the class TasksRepositoryTest method completeTask_completesTaskToServiceAPIUpdatesCache.
@Test
public void completeTask_completesTaskToServiceAPIUpdatesCache() {
// Given a stub active task with title and description added in the repository
Task newTask = new Task(TASK_TITLE, "Some Task Description");
mTasksRepository.saveTask(newTask);
// When a task is completed to the tasks repository
mTasksRepository.completeTask(newTask);
// Then the service API and persistent repository are called and the cache is updated
verify(mTasksRemoteDataSource).completeTask(newTask);
verify(mTasksLocalDataSource).completeTask(newTask);
assertThat(mTasksRepository.mCachedTasks.size(), is(1));
assertThat(mTasksRepository.mCachedTasks.get(newTask.getId()).isActive(), is(false));
}
Aggregations