Search in sources :

Example 16 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class AddEditTaskPresenter method updateTask.

private void updateTask(String title, String description) {
    if (isNewTask()) {
        throw new RuntimeException("updateTask() was called but task is new.");
    }
    mTasksRepository.saveTask(new Task(title, description, mTaskId));
    // After an edit, go back to the list.
    mAddTaskView.showTasksList();
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task)

Example 17 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class TasksRepository method getTask.

/**
 * Gets tasks from local data source (sqlite) unless the table is new or empty. In that case it
 * uses the network data source. This is done to simplify the sample.
 */
@Override
public Flowable<Optional<Task>> getTask(@NonNull final String taskId) {
    checkNotNull(taskId);
    final Task cachedTask = getTaskWithId(taskId);
    // Respond immediately with cache if available
    if (cachedTask != null) {
        return Flowable.just(Optional.of(cachedTask));
    }
    // Do in memory cache update to keep the app UI up to date
    if (mCachedTasks == null) {
        mCachedTasks = new LinkedHashMap<>();
    }
    // Is the task in the local data source? If not, query the network.
    Flowable<Optional<Task>> localTask = getTaskWithIdFromLocalRepository(taskId);
    Flowable<Optional<Task>> remoteTask = mTasksRemoteDataSource.getTask(taskId).doOnNext(taskOptional -> {
        if (taskOptional.isPresent()) {
            Task task = taskOptional.get();
            mTasksLocalDataSource.saveTask(task);
            mCachedTasks.put(task.getId(), task);
        }
    });
    return Flowable.concat(localTask, remoteTask).firstElement().toFlowable();
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Optional(com.google.common.base.Optional)

Example 18 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class TasksRemoteDataSource method addTask.

private static void addTask(String title, String description) {
    Task newTask = new Task(title, description);
    TASKS_SERVICE_DATA.put(newTask.getId(), newTask);
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task)

Example 19 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class TasksRemoteDataSource method completeTask.

@Override
public void completeTask(@NonNull Task task) {
    Task completedTask = new Task(task.getTitle(), task.getDescription(), task.getId(), true);
    TASKS_SERVICE_DATA.put(task.getId(), completedTask);
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task)

Example 20 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class TasksPresenter method loadTasks.

/**
 * @param forceUpdate   Pass in true to refresh the data in the {@link TasksDataSource}
 * @param showLoadingUI Pass in true to display a loading icon in the UI
 */
private void loadTasks(final boolean forceUpdate, final boolean showLoadingUI) {
    if (showLoadingUI) {
        mTasksView.setLoadingIndicator(true);
    }
    if (forceUpdate) {
        mTasksRepository.refreshTasks();
    }
    // 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();
    mCompositeDisposable.clear();
    Disposable disposable = mTasksRepository.getTasks().flatMap(Flowable::fromIterable).filter(task -> {
        switch(mCurrentFiltering) {
            case ACTIVE_TASKS:
                return task.isActive();
            case COMPLETED_TASKS:
                return task.isCompleted();
            case ALL_TASKS:
            default:
                return true;
        }
    }).toList().subscribeOn(mSchedulerProvider.io()).observeOn(mSchedulerProvider.ui()).doFinally(() -> {
        if (!EspressoIdlingResource.getIdlingResource().isIdleNow()) {
            // Set app as idle.
            EspressoIdlingResource.decrement();
        }
    }).subscribe(// onNext
    tasks -> {
        processTasks(tasks);
        mTasksView.setLoadingIndicator(false);
    }, // onError
    throwable -> mTasksView.showLoadingTasksError());
    mCompositeDisposable.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TasksDataSource(com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource) BaseSchedulerProvider(com.example.android.architecture.blueprints.todoapp.util.schedulers.BaseSchedulerProvider) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) NonNull(android.support.annotation.NonNull) EspressoIdlingResource(com.example.android.architecture.blueprints.todoapp.util.EspressoIdlingResource) TasksRepository(com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Flowable(io.reactivex.Flowable) AddEditTaskActivity(com.example.android.architecture.blueprints.todoapp.addedittask.AddEditTaskActivity) Activity(android.app.Activity) Task(com.example.android.architecture.blueprints.todoapp.data.Task) Flowable(io.reactivex.Flowable)

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