Price range filtering finished

This commit is contained in:
Clayton Wilson 2020-10-25 21:19:02 -04:00
parent c9d40d0eae
commit 9462bc6723
2 changed files with 22 additions and 22 deletions

View File

@ -32,17 +32,15 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
private int sortMode; private int sortMode;
private boolean descending; private boolean descending;
private double minPrice = 0; private double minPrice = 0;
private double maxPrice = 0; private double maxPrice = -1;
@Override @Override
public void sendSort(int storeSelection, int sortMode, boolean descending, double maxPrice, double minPrice) { public void sendSort(int storeSelection, int sortMode, boolean descending, double minPrice, double maxPrice) {
this.storeSelection = storeSelection; this.storeSelection = storeSelection;
this.sortMode = sortMode; this.sortMode = sortMode;
this.descending = descending; this.descending = descending;
this.minPrice = minPrice; this.minPrice = minPrice;
this.maxPrice = maxPrice; this.maxPrice = maxPrice;
System.out.println(minPrice);
System.out.println(maxPrice);
sortResults(); sortResults();
} }
@ -89,7 +87,6 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(SearchResults.this, resultsProductListSorted.get(position).getItemName(), Toast.LENGTH_SHORT).show();
Intent itemDetailsPage = new Intent(SearchResults.this, ItemDetails.class); Intent itemDetailsPage = new Intent(SearchResults.this, ItemDetails.class);
// Send the selected product // Send the selected product
@ -112,6 +109,8 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
} }
}); });
// TODO: Change this to a menu in which sort and filter are two different options
// TODO: Sort should be disabled until a search is made
// Create a dialog for filtering and sorting search results // Create a dialog for filtering and sorting search results
ImageButton sortButton = (ImageButton) findViewById(R.id.results_sort_button); ImageButton sortButton = (ImageButton) findViewById(R.id.results_sort_button);
sortButton.setOnClickListener(new View.OnClickListener() { sortButton.setOnClickListener(new View.OnClickListener() {
@ -140,10 +139,14 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
maxProductPrice = resultsProductList.get(i).getPrice().doubleValue(); maxProductPrice = resultsProductList.get(i).getPrice().doubleValue();
} }
} }
if (maxPrice == 0) { if (maxPrice == -1) {
maxPrice = maxProductPrice; maxPrice = maxProductPrice;
} }
} }
// Round up to nearest whole number for display on price seekbar
maxProductPrice = Math.ceil(maxProductPrice);
SortDialogFragment sortDialog = new SortDialogFragment(storeSelection, stores, sortMode, descending, maxProductPrice, minPrice, maxPrice); SortDialogFragment sortDialog = new SortDialogFragment(storeSelection, stores, sortMode, descending, maxProductPrice, minPrice, maxPrice);
sortDialog.show(getSupportFragmentManager(), "Sort"); sortDialog.show(getSupportFragmentManager(), "Sort");
} }
@ -264,6 +267,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
break; break;
} }
// Flip the list if descending is selected
if (this.sortMode != 0 & this.descending) { if (this.sortMode != 0 & this.descending) {
for (int i = 0; i < resultsProductListSorted.size() / 2; i++) { for (int i = 0; i < resultsProductListSorted.size() / 2; i++) {
Product temp = resultsProductListSorted.get(i); Product temp = resultsProductListSorted.get(i);
@ -284,6 +288,17 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
resultsProductListSorted.addAll(temp); resultsProductListSorted.addAll(temp);
} }
// Filter out products that don't fit price restraints
ArrayList<Product> temp = new ArrayList<>();
resultsProductListSorted.forEach(product -> {
if (product.getPrice().doubleValue() >= this.minPrice &&
(this.maxPrice == -1 || product.getPrice().doubleValue() <= this.maxPrice)) {
temp.add(product);
}
});
resultsProductListSorted.clear();
resultsProductListSorted.addAll(temp);
searchResultsListAdapter.notifyDataSetChanged(); searchResultsListAdapter.notifyDataSetChanged();
} }
} }

View File

@ -17,7 +17,6 @@ import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import com.crystal.crystalrangeseekbar.interfaces.OnRangeSeekbarChangeListener; import com.crystal.crystalrangeseekbar.interfaces.OnRangeSeekbarChangeListener;
import com.crystal.crystalrangeseekbar.interfaces.OnRangeSeekbarFinalValueListener;
import com.crystal.crystalrangeseekbar.widgets.CrystalRangeSeekbar; import com.crystal.crystalrangeseekbar.widgets.CrystalRangeSeekbar;
import java.util.ArrayList; import java.util.ArrayList;
@ -140,9 +139,7 @@ public class SortDialogFragment extends DialogFragment {
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {}
}
}); });
// Disable the direction button if they have the default sorting mode selected // Disable the direction button if they have the default sorting mode selected
@ -157,7 +154,6 @@ public class SortDialogFragment extends DialogFragment {
final TextView tvMax = (TextView) root.findViewById(R.id.tv_max_price); final TextView tvMax = (TextView) root.findViewById(R.id.tv_max_price);
priceSeekbar.setMaxValue((float) this.maxProductPrice); priceSeekbar.setMaxValue((float) this.maxProductPrice);
System.out.println(String.format("%f : %f", this.minPrice, this.maxPrice));
priceSeekbar.setMinStartValue((float) this.minPrice); priceSeekbar.setMinStartValue((float) this.minPrice);
priceSeekbar.setMaxStartValue((float) this.maxPrice); priceSeekbar.setMaxStartValue((float) this.maxPrice);
priceSeekbar.apply(); priceSeekbar.apply();
@ -171,17 +167,6 @@ public class SortDialogFragment extends DialogFragment {
} }
}); });
// // Save price values when user finishes moving the slider
// priceSeekbar.setOnRangeSeekbarFinalValueListener(new OnRangeSeekbarFinalValueListener() {
// @Override
// public void finalValue(Number minValue, Number maxValue) {
// minPrice = minValue.doubleValue();
// maxPrice = maxValue.doubleValue();
//// System.out.println(String.format("Min: $%.2f, Max: $%.2f", minValue.doubleValue(), maxValue.doubleValue()));
// }
// });
return builder.create(); return builder.create();
} }