From 32dd8c56ca4e78e23aafa168b2615d6e963dcbd5 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Mon, 26 Oct 2020 19:15:58 -0400 Subject: [PATCH] New sort dialog fully implemented --- .../com/example/listify/SearchResults.java | 18 ++-- .../example/listify/SortDialogFragment.java | 85 ++++++++++++++++--- .../java/com/example/listify/SortModes.java | 9 ++ .../app/src/main/res/layout/dialog_sort.xml | 12 ++- Listify/app/src/main/res/values/colors.xml | 1 + 5 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 Listify/app/src/main/java/com/example/listify/SortModes.java 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 4f24bcf..36542f2 100644 --- a/Listify/app/src/main/java/com/example/listify/SearchResults.java +++ b/Listify/app/src/main/java/com/example/listify/SearchResults.java @@ -32,7 +32,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag private List resultsProductListSorted = new ArrayList<>(); private ArrayList stores = new ArrayList<>(); private int storeSelection; - private int sortMode; + private SortModes sortMode = SortModes.NONE; private boolean descending; private double minPrice = 0; private double maxPrice = -1; @@ -46,7 +46,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag } @Override - public void sendSort(int sortMode, boolean descending) { + public void sendSort(SortModes sortMode, boolean descending) { this.sortMode = sortMode; this.descending = descending; sortResults(); @@ -243,10 +243,10 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag // Sort based on mode switch (this.sortMode) { - case 0: + case NONE: // Do nothing break; - case 1: + case NAME: resultsProductListSorted.sort(new Comparator() { @Override public int compare(Product a, Product b) { @@ -254,9 +254,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag } }); break; - - // TODO: May need to change this depending on if price is stored as a string or a double - case 2: + case PRICE: resultsProductListSorted.sort(new Comparator() { @Override public int compare(Product a, Product b) { @@ -271,7 +269,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag }); break; - case 3: + case STORE: resultsProductListSorted.sort(new Comparator() { @Override public int compare(Product a, Product b) { @@ -280,7 +278,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag }); break; - case 4: + case UPC: resultsProductListSorted.sort(new Comparator() { @Override public int compare(Product a, Product b) { @@ -291,7 +289,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag } // Flip the list if descending is selected - if (this.sortMode != 0 & this.descending) { + if (this.sortMode != SortModes.NONE & this.descending) { for (int i = 0; i < resultsProductListSorted.size() / 2; i++) { Product temp = resultsProductListSorted.get(i); resultsProductListSorted.set(i, resultsProductListSorted.get(resultsProductListSorted.size() - i - 1)); diff --git a/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java b/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java index a97cd1f..0fbf7f2 100644 --- a/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java +++ b/Listify/app/src/main/java/com/example/listify/SortDialogFragment.java @@ -5,23 +5,31 @@ import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.widget.SwitchCompat; import androidx.fragment.app.DialogFragment; public class SortDialogFragment extends DialogFragment { public interface OnSortListener { - void sendSort(int sortMode, boolean descending); + void sendSort(SortModes sortMode, boolean descending); } public OnSortListener onSortListener; - private int sortMode; + private SortModes sortMode; private boolean descending; - public SortDialogFragment(int sortMode, boolean descending) { + TextView tvSortNone; + TextView tvSortName; + TextView tvSortPrice; + TextView tvSortStore; + SwitchCompat swDescending; + + public SortDialogFragment(SortModes sortMode, boolean descending) { this.sortMode = sortMode; this.descending = descending; } @@ -38,42 +46,97 @@ public class SortDialogFragment extends DialogFragment { View root = inflater.inflate(R.layout.dialog_sort, null); builder.setView(root); - TextView tvSortNone = root.findViewById(R.id.sort_none); - TextView tvSortName = root.findViewById(R.id.sort_name); - TextView tvSortPrice = root.findViewById(R.id.sort_price); - TextView tvSortStore = root.findViewById(R.id.sort_store); + tvSortNone = (TextView) root.findViewById(R.id.sort_none); + tvSortName = (TextView) root.findViewById(R.id.sort_name); + tvSortPrice = (TextView) root.findViewById(R.id.sort_price); + tvSortStore = (TextView) root.findViewById(R.id.sort_store); + LinearLayout llDescendingContainer = (LinearLayout) root.findViewById(R.id.descending_container); + swDescending = (SwitchCompat) root.findViewById(R.id.switch_descending); + + switch (this.sortMode) { + case NONE: + tvSortNone.setBackgroundColor(getResources().getColor(R.color.colorAccent)); + break; + case NAME: + tvSortName.setBackgroundColor(getResources().getColor(R.color.colorAccent)); + break; + case PRICE: + tvSortPrice.setBackgroundColor(getResources().getColor(R.color.colorAccent)); + break; + case STORE: + tvSortStore.setBackgroundColor(getResources().getColor(R.color.colorAccent)); + break; + } + + if (this.descending) { + swDescending.setChecked(true); + } tvSortNone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - + handleClicked(tvSortNone, SortModes.NONE, swDescending.isChecked()); } }); tvSortName.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - + handleClicked(tvSortName, SortModes.NAME, swDescending.isChecked()); } }); tvSortPrice.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - + handleClicked(tvSortPrice, SortModes.PRICE, swDescending.isChecked()); } }); tvSortStore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + handleClicked(tvSortStore, SortModes.STORE, swDescending.isChecked()); + } + }); + // TODO: set onclick listener for descending switch + llDescendingContainer.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + System.out.println("linear"); + swDescending.performClick(); +// handleClicked(sortMode, swDescending.isChecked()); + } + }); + + swDescending.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + System.out.println("switch"); + descending = swDescending.isChecked(); + onSortListener.sendSort(sortMode, swDescending.isChecked()); } }); return builder.create(); } + void handleClicked(TextView tvSelected, SortModes sortMode, boolean descending) { + this.sortMode = sortMode; + this.descending = descending; + + tvSortNone.setBackgroundColor(getResources().getColor(R.color.white)); + tvSortName.setBackgroundColor(getResources().getColor(R.color.white)); + tvSortPrice.setBackgroundColor(getResources().getColor(R.color.white)); + tvSortStore.setBackgroundColor(getResources().getColor(R.color.white)); + + tvSelected.setBackgroundColor(getResources().getColor(R.color.colorAccent)); + onSortListener.sendSort(sortMode, descending); + +// SortDialogFragment.this.getDialog().cancel(); + } + // Required to extend DialogFragment @Override public void onAttach(@NonNull Context context) { @@ -81,7 +144,7 @@ public class SortDialogFragment extends DialogFragment { try { onSortListener = (OnSortListener) getActivity(); } catch (ClassCastException e) { - Log.e("FilterDialogFragment", "onAttach: ClassCastException: " + e.getMessage()); + Log.e("SortDialogFragment", "onAttach: ClassCastException: " + e.getMessage()); } } } diff --git a/Listify/app/src/main/java/com/example/listify/SortModes.java b/Listify/app/src/main/java/com/example/listify/SortModes.java new file mode 100644 index 0000000..6c490a3 --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/SortModes.java @@ -0,0 +1,9 @@ +package com.example.listify; + +public enum SortModes { + NONE, + NAME, + PRICE, + STORE, + UPC +} diff --git a/Listify/app/src/main/res/layout/dialog_sort.xml b/Listify/app/src/main/res/layout/dialog_sort.xml index 09f4f03..af88f7d 100644 --- a/Listify/app/src/main/res/layout/dialog_sort.xml +++ b/Listify/app/src/main/res/layout/dialog_sort.xml @@ -4,6 +4,7 @@ android:layout_height="wrap_content" android:layout_marginHorizontal="30dp"> + + android:foreground="?attr/selectableItemBackgroundBorderless" /> + android:gravity="center_vertical" + android:clickable="true" + android:focusable="true" + android:foreground="?attr/selectableItemBackgroundBorderless" + android:soundEffectsEnabled="false"> diff --git a/Listify/app/src/main/res/values/colors.xml b/Listify/app/src/main/res/values/colors.xml index a9055a6..84a11b5 100644 --- a/Listify/app/src/main/res/values/colors.xml +++ b/Listify/app/src/main/res/values/colors.xml @@ -11,4 +11,5 @@ #ebeef0 #1c9ef4 #e6e6e6 + #ffffffff \ No newline at end of file