Skip to content

Commit

Permalink
fix: Order by clause cannot contain a field with an equality filter p…
Browse files Browse the repository at this point in the history
…rice

Fixes issue "firebase/friendlyeats-android#118" by removing the "Sort by Price" option when user filters results by a particular Price. The "Sort by Price" option is added back when user does not filter by a particular Price, i.e., when user sets the "Price" spinner to "Any price" option.
  • Loading branch information
kaushiknsanji committed Mar 23, 2021
1 parent 6a4e79d commit f5c2295
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.google.firebase.example.fireeats.databinding.DialogFiltersBinding;
import com.google.firebase.example.fireeats.model.Restaurant;
import com.google.firebase.firestore.Query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

import androidx.annotation.NonNull;
Expand All @@ -34,12 +38,21 @@
/**
* Dialog Fragment containing filter form.
*/
public class FilterDialogFragment extends DialogFragment implements View.OnClickListener {
public class FilterDialogFragment extends DialogFragment
implements View.OnClickListener, AdapterView.OnItemSelectedListener {

// Constant used for logs
public static final String TAG = "FilterDialog";

// Constant for when "Price" filter is not selected, i.e., set to the default "Any Price"
private static final int ANY_PRICE = -1;

private DialogFiltersBinding mBinding;
private FilterListener mFilterListener;

// ArrayAdapter instance for "Sort" Spinner
private ArrayAdapter<String> mSortOptionsAdapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
Expand All @@ -48,6 +61,25 @@ public View onCreateView(@NonNull LayoutInflater inflater,
// Inflate with ViewBinding
mBinding = DialogFiltersBinding.inflate(inflater, container, false);

// Set mutable data for "Sort" Spinner
ArrayList<String> sortByOptions = new ArrayList<>(
Arrays.asList(requireContext().getResources().getStringArray(R.array.sort_by))
);
// Create Adapter for "Sort" Spinner, with default layout for Spinner item
mSortOptionsAdapter = new ArrayAdapter<>(
requireContext(),
android.R.layout.simple_spinner_item,
sortByOptions
);
// Set layout to use when list of choices appear as dropdown
mSortOptionsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Set Adapter for "Sort" Spinner
mBinding.spinnerSort.setAdapter(mSortOptionsAdapter);

// Add Item Click listener on "Price" Spinner
mBinding.spinnerPrice.setOnItemSelectedListener(this);

// Add Click listeners on Dialog Buttons
mBinding.buttonSearch.setOnClickListener(this);
mBinding.buttonCancel.setOnClickListener(this);

Expand Down Expand Up @@ -132,7 +164,7 @@ private int getSelectedPrice() {
} else if (selected.equals(getString(R.string.price_3))) {
return 3;
} else {
return -1;
return ANY_PRICE;
}
}

Expand Down Expand Up @@ -191,6 +223,67 @@ public Filters getFilters() {
return filters;
}

/**
* <p>Callback method to be invoked when an item in this view has been
* selected. This callback is invoked only when the newly selected
* position is different from the previously selected position or if
* there was no selected item.</p>
* <p>
* Implementers can call getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* @param parent The AdapterView where the selection happened
* @param view The view within the AdapterView that was clicked
* @param position The position of the view in the adapter
* @param id The row id of the item that is selected
*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Based on the "Price" Spinner selection,
// show/hide the "Sort by Price" option in "Sort" Spinner
modifySortByOptions(getSelectedPrice() == ANY_PRICE);
}

/**
* Callback method to be invoked when the selection disappears from this
* view. The selection can disappear for instance when touch is activated
* or when the adapter becomes empty.
*
* @param parent The AdapterView that now contains no selected item.
*/
@Override
public void onNothingSelected(AdapterView<?> parent) {
// no-op
}

/**
* Removes the "Sort by Price" option in "Sort" Spinner when a particular price
* is selected by the user through the "Price" Spinner.
* <p/>
* When a particular price is not selected, i.e., when "Price" Spinner is set to "Any price",
* then the "Sort by Price" option will be added back to the "Sort" Spinner if not present.
*
* @param showSortByPrice A {@link Boolean} to either add or remove the "Sort by Price" option
* in "Sort" Spinner. When {@code true}, the "Sort by Price" option will
* be added back if not present; otherwise it will be removed.
*/
private void modifySortByOptions(boolean showSortByPrice) {
// Read the position of "Sort by Price" option in "Sort" Spinner
int sortByPricePosition = mSortOptionsAdapter.getPosition(getString(R.string.sort_by_price));

if (showSortByPrice) {
// Add the "Sort by Price" option if not present in the adapter
if (sortByPricePosition == -1) {
mSortOptionsAdapter.add(getString(R.string.sort_by_price));
}
} else {
// Remove the "Sort by Price" option from the adapter
if (sortByPricePosition > -1) {
mSortOptionsAdapter.remove(mSortOptionsAdapter.getItem(sortByPricePosition));
}
}
}

interface FilterListener {

void onFilter(Filters filters);
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/dialog_filters.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/filters_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -98,7 +99,7 @@
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/icon_sort"
android:layout_toRightOf="@+id/icon_sort"
android:entries="@array/sort_by" />
tools:entries="@array/sort_by" />
</RelativeLayout>

<!-- Cancel and apply buttons -->
Expand Down

0 comments on commit f5c2295

Please sign in to comment.