content: package com.google.firebase.example.fireeats.ui.main;
  
  import android.content.Context;
  import android.databinding.DataBindingUtil;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.DialogFragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.google.firebase.example.fireeats.R;
  import com.google.firebase.example.fireeats.databinding.DialogFiltersBinding;
  import com.google.firebase.example.fireeats.model.Restaurant;
  import com.google.firebase.firestore.Query;
  
  /**
   * Dialog Fragment containing filter form.
   */
  public class FilterDialogFragment extends DialogFragment {
  
      public static final String TAG = "FilterDialog";
  
      public interface FilterListener {
          void onFilter(Filters filters);
      }
  
      public interface FiltersDialogHandler {
          void addFilters(boolean doCancel);
      }
  
  
      private View mRootView;
      private FilterListener mFilterListener;
      private DialogFiltersBinding binding;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater,
                               @Nullable ViewGroup container,
                               @Nullable Bundle savedInstanceState) {
          binding = DataBindingUtil.inflate(inflater, R.layout.dialog_filters, container, false);
          binding.setHandler(doCancel -> {
              if (! doCancel) {
                  if (mFilterListener != null) {
                      mFilterListener.onFilter(getFilters());
                  }
              }
  
              dismiss();
          });
  
          mRootView = binding.getRoot();
          return mRootView;
      }
  
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
  
          if (context instanceof FilterListener) {
              mFilterListener = (FilterListener) context;
          }
      }
  
      @Override
      public void onResume() {
          super.onResume();
          getDialog().getWindow().setLayout(
                  ViewGroup.LayoutParams.MATCH_PARENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT);
      }
  
      @Nullable
      private String getSelectedCategory() {
          String selected = (String) binding.spinnerCategory.getSelectedItem();
          if (getString(R.string.value_any_category).equals(selected)) {
              return null;
          } else {
              return selected;
          }
      }
  
      @Nullable
      private String getSelectedCity() {
          String selected = (String) binding.spinnerCity.getSelectedItem();
          if (getString(R.string.value_any_city).equals(selected)) {
              return null;
          } else {
              return selected;
          }
      }
  
      private int getSelectedPrice() {
          String selected = (String) binding.spinnerPrice.getSelectedItem();
          if (selected.equals(getString(R.string.price_1))) {
              return 1;
          } else if (selected.equals(getString(R.string.price_2))) {
              return 2;
          } else if (selected.equals(getString(R.string.price_3))) {
              return 3;
          } else {
              return - 1;
          }
      }
  
      @Nullable
      private String getSelectedSortBy() {
          String selected = (String) binding.spinnerSort.getSelectedItem();
          if (getString(R.string.sort_by_rating).equals(selected)) {
              return Restaurant.FIELD_AVG_RATING;
          }
          if (getString(R.string.sort_by_price).equals(selected)) {
              return Restaurant.FIELD_PRICE;
          }
          if (getString(R.string.sort_by_popularity).equals(selected)) {
              return Restaurant.FIELD_POPULARITY;
          }
  
          return null;
      }
  
      @Nullable
      private Query.Direction getSortDirection() {
          String selected = (String) binding.spinnerSort.getSelectedItem();
          if (getString(R.string.sort_by_rating).equals(selected)) {
              return Query.Direction.DESCENDING;
          }
          if (getString(R.string.sort_by_price).equals(selected)) {
              return Query.Direction.ASCENDING;
          }
          if (getString(R.string.sort_by_popularity).equals(selected)) {
              return Query.Direction.DESCENDING;
          }
  
          return null;
      }
  
      public void resetFilters() {
          if (mRootView != null) {
              binding.spinnerCategory.setSelection(0);
              binding.spinnerCategory.setSelection(0);
              binding.spinnerPrice.setSelection(0);
              binding.spinnerSort.setSelection(0);
          }
      }
  
      public Filters getFilters() {
          Filters filters = new Filters();
  
          if (mRootView != null) {
              filters.setCategory(getSelectedCategory());
              filters.setCity(getSelectedCity());
              filters.setPrice(getSelectedPrice());
              filters.setSortBy(getSelectedSortBy());
              filters.setSortDirection(getSortDirection());
          }
  
          return filters;
      }
  }
  