content: /*
   * Copyright 2016, The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package <%= appPackage %>.taskdetail;
  
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  
  import <%= appPackage %>.UseCase;
  import <%= appPackage %>.UseCaseHandler;
  import <%= appPackage %>.addedittask.domain.usecase.DeleteTask;
  import <%= appPackage %>.addedittask.domain.usecase.GetTask;
  import <%= appPackage %>.tasks.domain.model.Task;
  import <%= appPackage %>.tasks.domain.usecase.ActivateTask;
  import <%= appPackage %>.tasks.domain.usecase.CompleteTask;
  import com.google.common.base.Strings;
  
  import static com.google.common.base.Preconditions.checkNotNull;
  
  /**
   * Listens to user actions from the UI ({@link TaskDetailFragment}), retrieves the data and updates
   * the UI as required.
   */
  public class TaskDetailPresenter implements TaskDetailContract.Presenter {
  
      private final TaskDetailContract.View mTaskDetailView;
      private final UseCaseHandler mUseCaseHandler;
      private final GetTask mGetTask;
      private final CompleteTask mCompleteTask;
      private final ActivateTask mActivateTask;
      private final DeleteTask mDeleteTask;
  
      @Nullable
      private String mTaskId;
  
      public TaskDetailPresenter(@NonNull UseCaseHandler useCaseHandler,
              @Nullable String taskId,
              @NonNull TaskDetailContract.View taskDetailView,
              @NonNull GetTask getTask,
              @NonNull CompleteTask completeTask,
              @NonNull ActivateTask activateTask,
              @NonNull DeleteTask deleteTask) {
          mTaskId = taskId;
          mUseCaseHandler = checkNotNull(useCaseHandler, "useCaseHandler cannot be null!");
          mTaskDetailView = checkNotNull(taskDetailView, "taskDetailView cannot be null!");
          mGetTask = checkNotNull(getTask, "getTask cannot be null!");
          mCompleteTask = checkNotNull(completeTask, "completeTask cannot be null!");
          mActivateTask = checkNotNull(activateTask, "activateTask cannot be null!");
          mDeleteTask = checkNotNull(deleteTask, "deleteTask cannot be null!");
          mTaskDetailView.setPresenter(this);
      }
  
      @Override
      public void start() {
          openTask();
      }
  
      private void openTask() {
          if (Strings.isNullOrEmpty(mTaskId)) {
              mTaskDetailView.showMissingTask();
              return;
          }
  
          mTaskDetailView.setLoadingIndicator(true);
  
          mUseCaseHandler.execute(mGetTask, new GetTask.RequestValues(mTaskId),
                  new UseCase.UseCaseCallback<GetTask.ResponseValue>() {
                      @Override
                      public void onSuccess(GetTask.ResponseValue response) {
                          Task task = response.getTask();
  
                          // The view may not be able to handle UI updates anymore
                          if (!mTaskDetailView.isActive()) {
                              return;
                          }
                          mTaskDetailView.setLoadingIndicator(false);
                          showTask(task);
                      }
  
                      @Override
                      public void onError() {
                          // The view may not be able to handle UI updates anymore
                          if (!mTaskDetailView.isActive()) {
                              return;
                          }
                          mTaskDetailView.showMissingTask();
                      }
                  });
      }
  
      @Override
      public void editTask() {
          if (Strings.isNullOrEmpty(mTaskId)) {
              mTaskDetailView.showMissingTask();
              return;
          }
          mTaskDetailView.showEditTask(mTaskId);
      }
  
      @Override
      public void deleteTask() {
          mUseCaseHandler.execute(mDeleteTask, new DeleteTask.RequestValues(mTaskId),
                  new UseCase.UseCaseCallback<DeleteTask.ResponseValue>() {
                      @Override
                      public void onSuccess(DeleteTask.ResponseValue response) {
                          mTaskDetailView.showTaskDeleted();
                      }
  
                      @Override
                      public void onError() {
                          // Show error, log, etc.
                      }
                  });
      }
  
      @Override
      public void completeTask() {
          if (Strings.isNullOrEmpty(mTaskId)) {
              mTaskDetailView.showMissingTask();
              return;
          }
  
          mUseCaseHandler.execute(mCompleteTask, new CompleteTask.RequestValues(mTaskId),
                  new UseCase.UseCaseCallback<CompleteTask.ResponseValue>() {
                      @Override
                      public void onSuccess(CompleteTask.ResponseValue response) {
                          mTaskDetailView.showTaskMarkedComplete();
                      }
  
                      @Override
                      public void onError() {
                          // Show error, log, etc.
                      }
                  });
      }
  
      @Override
      public void activateTask() {
          if (Strings.isNullOrEmpty(mTaskId)) {
              mTaskDetailView.showMissingTask();
              return;
          }
          mUseCaseHandler.execute(mActivateTask, new ActivateTask.RequestValues(mTaskId),
                  new UseCase.UseCaseCallback<ActivateTask.ResponseValue>() {
                      @Override
                      public void onSuccess(ActivateTask.ResponseValue response) {
                          mTaskDetailView.showTaskMarkedActive();
                      }
  
                      @Override
                      public void onError() {
                          // Show error, log, etc.
                      }
                  });
      }
  
      private void showTask(@NonNull Task task) {
          String title = task.getTitle();
          String description = task.getDescription();
  
          if (Strings.isNullOrEmpty(title)) {
              mTaskDetailView.hideTitle();
          } else {
              mTaskDetailView.showTitle(title);
          }
  
          if (Strings.isNullOrEmpty(description)) {
              mTaskDetailView.hideDescription();
          } else {
              mTaskDetailView.showDescription(description);
          }
          mTaskDetailView.showCompletionStatus(task.isCompleted());
      }
  }
  