@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
    View root = inflater.inflate(R.layout.fragment_aggregate_report, container, false);

    mProgressBar = (SmoothProgressBar) root.findViewById(R.id.progress_bar);
    mCategoriesList = (ListView) root.findViewById(R.id.categories_list_view);

    View header =
        inflater.inflate(R.layout.fragment_aggregate_report_header, mCategoriesList, false);
    View footer =
        inflater.inflate(R.layout.fragment_aggregate_report_footer, mCategoriesList, false);
    mOrgUnitButton = (CardTextViewButton) header.findViewById(R.id.unit_button);
    mDataSetButton = (CardTextViewButton) header.findViewById(R.id.dataset_button);
    mPeriodButton = (CardTextViewButton) header.findViewById(R.id.period_button);
    mButton = (CardDetailedButton) footer.findViewById(R.id.data_entry_button);

    mOrgUnitButton.setOnClickListener(this);
    mDataSetButton.setOnClickListener(this);
    mPeriodButton.setOnClickListener(this);
    mDataSetButton.setOnClickListener(this);

    mCategoriesList.addHeaderView(header, null, false);
    mCategoriesList.addFooterView(footer, null, false);

    return root;
  }
  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);
  }
  @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);
  }
 private void handleViews(int level) {
   switch (level) {
     case 0:
       mPeriodButton.setEnabled(false);
     case 1:
       mAdapter.swapData(null);
     case 2:
       mButton.hide(true);
       break;
     case 3:
       mButton.show(true);
   }
 }
  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);
    }
  }