Add menu to search and rename SortDialogFragment to FilterDialogFragment

This commit is contained in:
Clayton Wilson 2020-10-26 00:25:47 -04:00
parent 367ff12f58
commit d8bf4e02f8
5 changed files with 55 additions and 25 deletions

View File

@ -22,13 +22,13 @@ import com.crystal.crystalrangeseekbar.widgets.CrystalRangeSeekbar;
import java.util.ArrayList; import java.util.ArrayList;
public class SortDialogFragment extends DialogFragment { public class FilterDialogFragment extends DialogFragment {
public interface OnSortingListener { public interface OnFilterListener {
void sendSort(int storeSelection, int sortMode, boolean descending, double minPrice, double maxPrice); void sendSort(int storeSelection, int sortMode, boolean descending, double minPrice, double maxPrice);
} }
public OnSortingListener onSortingListener; public OnFilterListener onFilterListener;
CrystalRangeSeekbar priceSeekbar; CrystalRangeSeekbar priceSeekbar;
@ -40,7 +40,7 @@ public class SortDialogFragment extends DialogFragment {
private double minPrice; // The selected min price private double minPrice; // The selected min price
private double maxPrice; // The selected max price private double maxPrice; // The selected max price
public SortDialogFragment(int storeSelection, ArrayList<String> stores, int sortMode, boolean descending, double maxProductPrice, double minPrice, double maxPrice) { public FilterDialogFragment(int storeSelection, ArrayList<String> stores, int sortMode, boolean descending, double maxProductPrice, double minPrice, double maxPrice) {
this.storeSelection = storeSelection; this.storeSelection = storeSelection;
this.stores = stores; this.stores = stores;
this.sortMode = sortMode; this.sortMode = sortMode;
@ -59,18 +59,18 @@ public class SortDialogFragment extends DialogFragment {
// Inflate and set the layout for the dialog // Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout // Pass null as the parent view because its going in the dialog layout
View root = inflater.inflate(R.layout.dialog_sort, null); View root = inflater.inflate(R.layout.dialog_filter, null);
builder.setView(root) builder.setView(root)
// Add action buttons // Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() { .setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
onSortingListener.sendSort(storeSelection, sortMode, descending, priceSeekbar.getSelectedMinValue().doubleValue(), priceSeekbar.getSelectedMaxValue().doubleValue()); onFilterListener.sendSort(storeSelection, sortMode, descending, priceSeekbar.getSelectedMinValue().doubleValue(), priceSeekbar.getSelectedMaxValue().doubleValue());
} }
}) })
.setNegativeButton("cancel", new DialogInterface.OnClickListener() { .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
SortDialogFragment.this.getDialog().cancel(); FilterDialogFragment.this.getDialog().cancel();
} }
}); });
@ -175,9 +175,9 @@ public class SortDialogFragment extends DialogFragment {
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
try { try {
onSortingListener = (OnSortingListener) getActivity(); onFilterListener = (OnFilterListener) getActivity();
} catch (ClassCastException e) { } catch (ClassCastException e) {
Log.e("SortDialogFragment", "onAttach: ClassCastException: " + e.getMessage()); Log.e("FilterDialogFragment", "onAttach: ClassCastException: " + e.getMessage());
} }
} }
} }

View File

@ -3,6 +3,9 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.EditText; import android.widget.EditText;
@ -22,7 +25,7 @@ import java.util.Properties;
import static com.example.listify.MainActivity.am; import static com.example.listify.MainActivity.am;
public class SearchResults extends AppCompatActivity implements SortDialogFragment.OnSortingListener { public class SearchResults extends AppCompatActivity implements FilterDialogFragment.OnFilterListener {
private ListView listView; private ListView listView;
private SearchResultsListAdapter searchResultsListAdapter; private SearchResultsListAdapter searchResultsListAdapter;
private List<Product> resultsProductList = new ArrayList<>(); private List<Product> resultsProductList = new ArrayList<>();
@ -108,14 +111,26 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
return false; return false;
} }
}); });
}
// TODO: Change this to a menu in which sort and filter are two different options @Override
// TODO: Sort should be disabled until a search is made public boolean onCreateOptionsMenu(Menu menu) {
// Create a dialog for filtering and sorting search results //Inflate the menu; this adds items to the action bar if it is present.
ImageButton sortButton = (ImageButton) findViewById(R.id.results_sort_button); getMenuInflater().inflate(R.menu.search, menu);
sortButton.setOnClickListener(new View.OnClickListener() { MenuItem sortItem = menu.findItem(R.id.action_sort);
sortItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
public void onClick(View v) { public boolean onMenuItemClick(MenuItem item) {
// TODO: Create a sort dialog
return false;
}
});
// TODO: filter should be disabled until a search is made
MenuItem filterItem = menu.findItem(R.id.action_filter);
filterItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Sort the store list // Sort the store list
stores.sort(new Comparator<String>() { stores.sort(new Comparator<String>() {
@Override @Override
@ -147,11 +162,12 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
// Round up to nearest whole number for display on price seekbar // Round up to nearest whole number for display on price seekbar
maxProductPrice = Math.ceil(maxProductPrice); maxProductPrice = Math.ceil(maxProductPrice);
SortDialogFragment sortDialog = new SortDialogFragment(storeSelection, stores, sortMode, descending, maxProductPrice, minPrice, maxPrice); FilterDialogFragment sortDialog = new FilterDialogFragment(storeSelection, stores, sortMode, descending, maxProductPrice, minPrice, maxPrice);
sortDialog.show(getSupportFragmentManager(), "Sort"); sortDialog.show(getSupportFragmentManager(), "Sort");
return false;
} }
}); });
return true;
} }

View File

@ -36,13 +36,13 @@
> >
</SearchView> </SearchView>
<ImageButton <!-- <ImageButton-->
android:id="@+id/results_sort_button" <!-- android:id="@+id/results_sort_button"-->
android:layout_width="match_parent" <!-- android:layout_width="match_parent"-->
android:layout_height="wrap_content" <!-- android:layout_height="wrap_content"-->
android:contentDescription="@string/sort_button_desc" <!-- android:contentDescription="@string/sort_button_desc"-->
android:background="@null" <!-- android:background="@null"-->
app:srcCompat="@drawable/ic_baseline_sort_28" /> <!-- app:srcCompat="@drawable/ic_baseline_sort_28" />-->
</androidx.appcompat.widget.Toolbar> </androidx.appcompat.widget.Toolbar>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_sort"
android:orderInCategory="101"
android:title="Sort"
app:showAsAction="never" />
<item
android:id="@+id/action_filter"
android:orderInCategory="101"
android:title="Filter"
app:showAsAction="never" />
</menu>