@Override
 public void onClick(View view) {
   switch (view.getId()) {
     case R.id.unit_button:
       {
         OrgUnitDialogFragment fragment = OrgUnitDialogFragment.newInstance(this);
         fragment.show(getChildFragmentManager());
         break;
       }
     case R.id.dataset_button:
       {
         DataSetDialogFragment fragment =
             DataSetDialogFragment.newInstance(this, mState.getOrgUnitId());
         fragment.show(getChildFragmentManager());
         break;
       }
     case R.id.period_button:
       {
         PeriodDialogFragment fragment =
             PeriodDialogFragment.newInstance(this, mState.getDataSetId());
         fragment.show(getChildFragmentManager());
         break;
       }
     case R.id.data_entry_button:
       {
         // startReportEntryActivity();
         break;
       }
   }
 }
 private void handleButton() {
   String orgUnit = getString(R.string.organisation_unit) + ": " + mState.getOrgUnitLabel();
   String dataSet = getString(R.string.dataset) + ": " + mState.getDataSetLabel();
   String period = getString(R.string.period) + ": " + mState.getPeriod().getLabel();
   mButton.setFirstLineText(orgUnit);
   mButton.setSecondLineText(dataSet);
   mButton.setThirdLineText(period);
 }
  public void onDataSetSelected(String dataSetId, String dataSetLabel, String categoryComboId) {
    mDataSetButton.setText(dataSetLabel);
    mPeriodButton.setEnabled(true);

    mState.setDataSet(dataSetId, dataSetLabel, categoryComboId);
    mState.resetPeriod();
    mState.resetCategoryStates();
    handleViews(1);
  }
  public void onUnitSelected(String orgUnitId, String orgUnitLabel) {
    mOrgUnitButton.setText(orgUnitLabel);
    mDataSetButton.setEnabled(true);

    mState.setOrgUnit(orgUnitId, orgUnitLabel);
    mState.resetDataSet();
    mState.resetPeriod();
    mState.resetCategoryStates();
    handleViews(0);
  }
  public void onCategorySelected(
      String categoryId, String categoryOptionId, String categoryOptionName) {
    for (CategoryState state : mState.getCategoryStates()) {
      if (state.getCategoryId().equals(categoryId)) {
        state.setCategoryOptionId(categoryOptionId);
        state.setCategoryOptionName(categoryOptionName);
        break;
      }
    }

    onCategoriesSelected(mState.getCategoryStates());
  }
  public void onPeriodSelected(DateHolder dateHolder, boolean loadCategoriesFromDb) {
    mPeriodButton.setText(dateHolder.getLabel());

    mState.setPeriod(dateHolder);
    mState.resetCategoryStates();
    handleViews(2);

    if (loadCategoriesFromDb) {
      // we need to put the categoryComboId inside of Bundle in order to
      // enable its (categoryComboId) survival through configuration changes
      Bundle arguments = new Bundle();
      arguments.putString(CATEGORY_COMBO_ID, mState.getDataSetCategoryComboId());

      // load dataset categories
      getLoaderManager().restartLoader(CATEGORIES_LOADER_ID, arguments, CATEGORY_LOADER_CALLBACK);
    }
  }
  public void onCategoriesSelected(List<CategoryState> states) {
    mState.setCategoryStates(states);
    mAdapter.swapData(new ArrayList<>(states));

    if (areAllCategoriesChosen()) {
      handleButton();
      handleViews(3);
    } else {
      handleViews(2);
    }
  }
  private boolean areAllCategoriesChosen() {
    List<CategoryState> categoryStates = mState.getCategoryStates();

    if (categoryStates == null || categoryStates.isEmpty()) {
      return false;
    }
    for (CategoryState categoryState : categoryStates) {
      if (!categoryState.isCategoryOptionSelected()) {
        return false;
      }
    }
    return true;
  }
  private void startReportEntryActivity() {
    String orgUnitId = mState.getOrgUnitId();
    String orgUnitLabel = mState.getOrgUnitLabel();

    String dataSetId = mState.getDataSetId();
    String dataSetLabel = mState.getDataSetLabel();

    String period = mState.getPeriod().getDate();
    String periodLabel = mState.getPeriod().getLabel();

    Intent intent =
        ReportEntryActivity.newIntent(
            getActivity(), orgUnitId, orgUnitLabel, dataSetId, dataSetLabel, period, periodLabel);
    startActivity(intent);
  }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    mOrgUnitButton.setEnabled(false);
    mDataSetButton.setEnabled(false);
    mPeriodButton.setEnabled(false);
    mButton.hide(false);

    if (savedInstanceState != null && savedInstanceState.getParcelable(STATE) != null) {
      mState = savedInstanceState.getParcelable(STATE);
    }

    if (mState == null) {
      mState = new AggregateReportFragmentState();
    }

    mAdapter =
        new CategoryAdapter(LayoutInflater.from(getActivity()), getChildFragmentManager(), this);
    mCategoriesList.setAdapter(mAdapter);

    mProgressBar.setVisibility(mState.isSyncInProcess() ? View.VISIBLE : View.INVISIBLE);
  }
  public void onRestoreInstanceState() {
    AggregateReportFragmentState backedUpState = new AggregateReportFragmentState(mState);
    if (!backedUpState.isOrgUnitEmpty()) {
      onUnitSelected(backedUpState.getOrgUnitId(), backedUpState.getOrgUnitLabel());

      if (!backedUpState.isDataSetEmpty()) {
        onDataSetSelected(
            backedUpState.getDataSetId(),
            backedUpState.getDataSetLabel(),
            backedUpState.getDataSetCategoryComboId());

        if (!backedUpState.isPeriodEmpty()) {
          onPeriodSelected(backedUpState.getPeriod(), false);

          if (!backedUpState.areCategoryStatesEmpty()) {
            onCategoriesSelected(backedUpState.getCategoryStates());
          }
        }
      }
    }
  }