diff --git a/Listify/app/src/main/java/com/example/listify/FilterDialogFragment.java b/Listify/app/src/main/java/com/example/listify/FilterDialogFragment.java index f61ea60..fa2e9a1 100644 --- a/Listify/app/src/main/java/com/example/listify/FilterDialogFragment.java +++ b/Listify/app/src/main/java/com/example/listify/FilterDialogFragment.java @@ -8,24 +8,20 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; -import android.widget.ImageButton; import android.widget.Spinner; import android.widget.TextView; - import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.DialogFragment; - import com.crystal.crystalrangeseekbar.interfaces.OnRangeSeekbarChangeListener; import com.crystal.crystalrangeseekbar.widgets.CrystalRangeSeekbar; - import java.util.ArrayList; public class FilterDialogFragment extends DialogFragment { public interface OnFilterListener { - void sendSort(int storeSelection, int sortMode, boolean descending, double minPrice, double maxPrice); + void sendFilter(int storeSelection, double minPrice, double maxPrice); } public OnFilterListener onFilterListener; @@ -33,18 +29,14 @@ public class FilterDialogFragment extends DialogFragment { CrystalRangeSeekbar priceSeekbar; private int storeSelection; - private int sortMode; - private boolean descending; private ArrayList stores; private double maxProductPrice; // The highest price on the slider private double minPrice; // The selected min price private double maxPrice; // The selected max price - public FilterDialogFragment(int storeSelection, ArrayList stores, int sortMode, boolean descending, double maxProductPrice, double minPrice, double maxPrice) { + public FilterDialogFragment(int storeSelection, ArrayList stores, double maxProductPrice, double minPrice, double maxPrice) { this.storeSelection = storeSelection; this.stores = stores; - this.sortMode = sortMode; - this.descending = descending; this.maxProductPrice = maxProductPrice; this.minPrice = minPrice; this.maxPrice = maxPrice; @@ -61,18 +53,18 @@ public class FilterDialogFragment extends DialogFragment { // Pass null as the parent view because its going in the dialog layout View root = inflater.inflate(R.layout.dialog_filter, null); builder.setView(root) - // Add action buttons - .setPositiveButton("OK", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - onFilterListener.sendSort(storeSelection, sortMode, descending, priceSeekbar.getSelectedMinValue().doubleValue(), priceSeekbar.getSelectedMaxValue().doubleValue()); - } - }) - .setNegativeButton("cancel", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - FilterDialogFragment.this.getDialog().cancel(); - } - }); + // Add action buttons + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + onFilterListener.sendFilter(storeSelection, priceSeekbar.getSelectedMinValue().doubleValue(), priceSeekbar.getSelectedMaxValue().doubleValue()); + } + }) + .setNegativeButton("cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + FilterDialogFragment.this.getDialog().cancel(); + } + }); Spinner storeDropdown = (Spinner) root.findViewById(R.id.sort_store_dropdown); String[] storeChoices = new String[stores.size() + 1]; @@ -97,57 +89,6 @@ public class FilterDialogFragment extends DialogFragment { } }); - // Change the sort arrow to be pointing up or down based on ascending or descending - final ImageButton sortDirectionButton = root.findViewById(R.id.sort_direction_button); - if (descending) { - sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_downward_50); - } else { - sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_upward_50); - } - - // Change arrow pointing direction whenever the user clicks the button - sortDirectionButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (descending) { - descending = false; - sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_upward_50); - } else { - descending = true; - sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_downward_50); - } - } - }); - - // Create the sort mode selection dropdown - Spinner sortDropdown = (Spinner) root.findViewById(R.id.sort_mode_dropdown); - String[] items = new String[] {"", "Name", "Price", "Store"}; - ArrayAdapter adapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_dropdown_item, items); - sortDropdown.setAdapter(adapter); - sortDropdown.setSelection(this.sortMode); - sortDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - sortMode = position; - - // Update the sort direction button - if (position == 0) { - sortDirectionButton.setEnabled(false); - } else { - sortDirectionButton.setEnabled(true); - } - } - - @Override - public void onNothingSelected(AdapterView parent) {} - }); - - // Disable the direction button if they have the default sorting mode selected - // Ascending and Descending are mostly irrelevant in the default sort mode - if (sortDropdown.getSelectedItemPosition() == 0) { - sortDirectionButton.setEnabled(false); - } - // Set up the seekbar for price priceSeekbar = (CrystalRangeSeekbar) root.findViewById(R.id.price_range_seekbar); final TextView tvMin = (TextView) root.findViewById(R.id.tv_min_price); diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index dd6d2bc..fef669e 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -190,18 +190,6 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp(); } - // This function only exists for the create new list option in hamburger menu - public void onClickCreateList(MenuItem m) { - m.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - CreateListDialogFragment createListDialogFragment = new CreateListDialogFragment(); - createListDialogFragment.show(getSupportFragmentManager(), "Create New List"); - return false; - } - }); - } - public void onClickSignout(MenuItem m) { m.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override diff --git a/Listify/app/src/main/java/com/example/listify/SearchResults.java b/Listify/app/src/main/java/com/example/listify/SearchResults.java index 102e3d2..4f24bcf 100644 --- a/Listify/app/src/main/java/com/example/listify/SearchResults.java +++ b/Listify/app/src/main/java/com/example/listify/SearchResults.java @@ -25,7 +25,7 @@ import java.util.Properties; import static com.example.listify.MainActivity.am; -public class SearchResults extends AppCompatActivity implements FilterDialogFragment.OnFilterListener { +public class SearchResults extends AppCompatActivity implements FilterDialogFragment.OnFilterListener, SortDialogFragment.OnSortListener { private ListView listView; private SearchResultsListAdapter searchResultsListAdapter; private List resultsProductList = new ArrayList<>(); @@ -38,15 +38,20 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag private double maxPrice = -1; @Override - public void sendSort(int storeSelection, int sortMode, boolean descending, double minPrice, double maxPrice) { + public void sendFilter(int storeSelection, double minPrice, double maxPrice) { this.storeSelection = storeSelection; - this.sortMode = sortMode; - this.descending = descending; this.minPrice = minPrice; this.maxPrice = maxPrice; sortResults(); } + @Override + public void sendSort(int sortMode, boolean descending) { + this.sortMode = sortMode; + this.descending = descending; + sortResults(); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -122,11 +127,13 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag @Override public boolean onMenuItemClick(MenuItem item) { // TODO: Create a sort dialog + SortDialogFragment sortDialog = new SortDialogFragment(sortMode, descending); + sortDialog.show(getSupportFragmentManager(), "Sort Dialog"); return false; } }); - // TODO: filter should be disabled until a search is made + // TODO: price filter should be disabled until a search is made MenuItem filterItem = menu.findItem(R.id.action_filter); filterItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override @@ -162,8 +169,8 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag // Round up to nearest whole number for display on price seekbar maxProductPrice = Math.ceil(maxProductPrice); - FilterDialogFragment sortDialog = new FilterDialogFragment(storeSelection, stores, sortMode, descending, maxProductPrice, minPrice, maxPrice); - sortDialog.show(getSupportFragmentManager(), "Sort"); + FilterDialogFragment sortDialog = new FilterDialogFragment(storeSelection, stores, maxProductPrice, minPrice, maxPrice); + sortDialog.show(getSupportFragmentManager(), "Filter Dialog"); return false; } }); diff --git a/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java b/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java new file mode 100644 index 0000000..ab46236 --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java @@ -0,0 +1,122 @@ +package com.example.listify; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ImageButton; +import android.widget.Spinner; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; + + +public class SortDialogFragment extends DialogFragment { + + public interface OnSortListener { + void sendSort(int sortMode, boolean descending); + } + + public OnSortListener onSortListener; + + private int sortMode; + private boolean descending; + + public SortDialogFragment(int sortMode, boolean descending) { + this.sortMode = sortMode; + this.descending = descending; + } + + + @Override + public Dialog onCreateDialog(final Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + // Get the layout inflater + LayoutInflater inflater = requireActivity().getLayoutInflater(); + + // Inflate and set the layout for the dialog + // Pass null as the parent view because its going in the dialog layout + View root = inflater.inflate(R.layout.dialog_sort, null); + builder.setView(root) + // Add action buttons + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + onSortListener.sendSort(sortMode, descending); + } + }) + .setNegativeButton("cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + SortDialogFragment.this.getDialog().cancel(); + } + }); + + // Change the sort arrow to be pointing up or down based on ascending or descending + final ImageButton sortDirectionButton = root.findViewById(R.id.sort_direction_button); + if (descending) { + sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_downward_50); + } else { + sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_upward_50); + } + + // Change arrow pointing direction whenever the user clicks the button + sortDirectionButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (descending) { + descending = false; + sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_upward_50); + } else { + descending = true; + sortDirectionButton.setImageResource(R.drawable.ic_baseline_arrow_downward_50); + } + } + }); + + // Create the sort mode selection dropdown + Spinner sortDropdown = (Spinner) root.findViewById(R.id.sort_mode_dropdown); + String[] items = new String[] {"", "Name", "Price", "Store"}; + ArrayAdapter adapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_dropdown_item, items); + sortDropdown.setAdapter(adapter); + sortDropdown.setSelection(this.sortMode); + sortDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + sortMode = position; + + // Update the sort direction button + if (position == 0) { + sortDirectionButton.setEnabled(false); + } else { + sortDirectionButton.setEnabled(true); + } + } + + @Override + public void onNothingSelected(AdapterView parent) {} + }); + + // Disable the direction button if they have the default sorting mode selected + // Ascending and Descending are mostly irrelevant in the default sort mode + if (sortDropdown.getSelectedItemPosition() == 0) { + sortDirectionButton.setEnabled(false); + } + + return builder.create(); + } + + // Required to extend DialogFragment + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + try { + onSortListener = (OnSortListener) getActivity(); + } catch (ClassCastException e) { + Log.e("FilterDialogFragment", "onAttach: ClassCastException: " + e.getMessage()); + } + } +} diff --git a/Listify/app/src/main/res/layout/dialog_filter.xml b/Listify/app/src/main/res/layout/dialog_filter.xml index 6a6d5b1..4783b06 100644 --- a/Listify/app/src/main/res/layout/dialog_filter.xml +++ b/Listify/app/src/main/res/layout/dialog_filter.xml @@ -20,37 +20,6 @@ android:background="@android:drawable/spinner_dropdown_background" android:spinnerMode="dropdown"/> - - - - - - - - - - + + + + + + + + + + + + + + \ No newline at end of file