content: /*
   * Copyright (C) 2015 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.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  
  import <%= appPackage %>.R;
  import <%= appPackage %>.addedittask.AddEditTaskActivity;
  import <%= appPackage %>.addedittask.AddEditTaskFragment;
  import <%= appPackage %>.data.Task;
  import <%= appPackage %>.databinding.TaskdetailFragBinding;
  
  
  /**
   * Main UI for the task detail screen.
   */
  public class TaskDetailFragment extends Fragment implements TaskDetailContract.View {
  
      public static final String ARGUMENT_TASK_ID = "TASK_ID";
  
      public static final int REQUEST_EDIT_TASK = 1;
  
      private TaskdetailFragBinding mViewDataBinding;
  
      private TaskDetailContract.Presenter mPresenter;
  
      public static TaskDetailFragment newInstance(String taskId) {
          Bundle arguments = new Bundle();
          arguments.putString(ARGUMENT_TASK_ID, taskId);
          TaskDetailFragment fragment = new TaskDetailFragment();
          fragment.setArguments(arguments);
          return fragment;
      }
  
  
      @Override
      public void setPresenter(TaskDetailContract.Presenter presenter) {
          mPresenter = presenter;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          mViewDataBinding.setPresenter(mPresenter);
      }
  
      @Override
      public void onResume() {
          super.onResume();
          mPresenter.start();
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.taskdetail_frag, container, false);
  
          mViewDataBinding = TaskdetailFragBinding.bind(view);
  
          setHasOptionsMenu(true);
  
          setRetainInstance(true);
  
          // Set up floating action button
          FloatingActionButton fab =
                  (FloatingActionButton) getActivity().findViewById(R.id.fab_edit_task);
  
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  String taskId = getArguments().getString(ARGUMENT_TASK_ID);
                  Intent intent = new Intent(getContext(), AddEditTaskActivity.class);
                  intent.putExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID, taskId);
                  startActivityForResult(intent, REQUEST_EDIT_TASK);
              }
          });
  
          return view;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.menu_delete:
                  String taskId = getArguments().getString(ARGUMENT_TASK_ID);
                  mPresenter.deleteTask();
                  return true;
          }
          return false;
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.taskdetail_fragment_menu, menu);
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == REQUEST_EDIT_TASK) {
              // If the task was edited successfully, go back to the list.
              if (resultCode == Activity.RESULT_OK) {
                  getActivity().finish();
                  return;
              }
          }
      }
  
      @Override
      public void showTask(Task task) {
          mViewDataBinding.setTask(task);
      }
  
      @Override
      public void showError() {
          // If an error occurred, simply show an empty task.
          Task emptyTask = new Task("", getString(R.string.no_data));
          mViewDataBinding.setTask(emptyTask);
      }
  
      @Override
      public void showTaskDeleted() {
          getActivity().finish();
      }
  
      @Override
      public void showTaskMarkedComplete() {
          Snackbar.make(getView(), getString(R.string.task_marked_complete), Snackbar.LENGTH_LONG)
                  .show();
      }
  
      @Override
      public void showTaskMarkedActive() {
          Snackbar.make(getView(), getString(R.string.task_marked_active), Snackbar.LENGTH_LONG)
                  .show();
      }
  
      @Override
      public boolean isActive() {
          return isAdded();
      }
  }
  