Search in sources :

Example 1 with TasksDataSource

use of com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource 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

Activity (android.app.Activity)1 NonNull (android.support.annotation.NonNull)1 AddEditTaskActivity (com.example.android.architecture.blueprints.todoapp.addedittask.AddEditTaskActivity)1 Task (com.example.android.architecture.blueprints.todoapp.data.Task)1 TasksDataSource (com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource)1 TasksRepository (com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository)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 Flowable (io.reactivex.Flowable)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Disposable (io.reactivex.disposables.Disposable)1 List (java.util.List)1