Search in sources :

Example 6 with Task

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);
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task)

Example 7 with Task

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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Task(com.example.android.architecture.blueprints.todoapp.data.Task) Flowable(io.reactivex.Flowable)

Example 8 with Task

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));
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Test(org.junit.Test)

Example 9 with Task

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));
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Test(org.junit.Test)

Example 10 with Task

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));
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Test(org.junit.Test)

Aggregations

Task (com.example.android.architecture.blueprints.todoapp.data.Task)35 Test (org.junit.Test)20 Optional (com.google.common.base.Optional)3 Before (org.junit.Before)3 TasksRepository (com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository)2 ImmediateSchedulerProvider (com.example.android.architecture.blueprints.todoapp.util.schedulers.ImmediateSchedulerProvider)2 Flowable (io.reactivex.Flowable)2 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)2 Disposable (io.reactivex.disposables.Disposable)2 TestSubscriber (io.reactivex.subscribers.TestSubscriber)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 LargeTest (android.support.test.filters.LargeTest)1 AddEditTaskActivity (com.example.android.architecture.blueprints.todoapp.addedittask.AddEditTaskActivity)1 TasksDataSource (com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource)1 EspressoIdlingResource (com.example.android.architecture.blueprints.todoapp.util.EspressoIdlingResource)1 BaseSchedulerProvider (com.example.android.architecture.blueprints.todoapp.util.schedulers.BaseSchedulerProvider)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 List (java.util.List)1