/** * Listens to user actions from the UI ({@link AddEditTaskFragment}), retrieves the data and updates * the UI as required. */ publicclassAddEditTaskPresenterimplementsAddEditTaskContract.Presenter {
@NonNull private CompositeDisposable mCompositeDisposable; /** * Creates a presenter for the add/edit view. * * @param taskId ID of the task to edit or null for a new task * @param tasksRepository a repository of data for tasks * @param addTaskView the add/edit view * @param shouldLoadDataFromRepo whether data needs to be loaded or not (for config changes) */ publicAddEditTaskPresenter(@Nullable String taskId, @NonNull TasksDataSource tasksRepository, @NonNull AddEditTaskContract.View addTaskView, boolean shouldLoadDataFromRepo, @NonNull BaseSchedulerProvider schedulerProvider) { mTaskId = taskId; mTasksRepository = checkNotNull(tasksRepository); mAddTaskView = checkNotNull(addTaskView); mIsDataMissing = shouldLoadDataFromRepo;
mSchedulerProvider = checkNotNull(schedulerProvider, "schedulerProvider cannot be null!");
@Override publicvoidpopulateTask() { if (isNewTask()) { thrownewRuntimeException("populateTask() was called but task is new."); } mCompositeDisposable.add(mTasksRepository .getTask(mTaskId) .subscribeOn(mSchedulerProvider.computation()) .observeOn(mSchedulerProvider.ui()) .subscribe( // onNext taskOptional -> { if (taskOptional.isPresent()) { Tasktask= taskOptional.get(); if (mAddTaskView.isActive()) { mAddTaskView.setTitle(task.getTitle()); mAddTaskView.setDescription(task.getDescription());
privatevoidupdateTask(String title, String description) { if (isNewTask()) { thrownewRuntimeException("updateTask() was called but task is new."); } mTasksRepository.saveTask(newTask(title, description, mTaskId)); mAddTaskView.showTasksList(); // After an edit, go back to the list. } }