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();
}
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();
}
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);
}
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);
}
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);
}
Aggregations