New sort dialog fully implemented

This commit is contained in:
Clayton Wilson 2020-10-26 19:15:58 -04:00
parent 20b86c573a
commit 32dd8c56ca
5 changed files with 101 additions and 24 deletions

View File

@ -32,7 +32,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
private List<Product> resultsProductListSorted = new ArrayList<>(); private List<Product> resultsProductListSorted = new ArrayList<>();
private ArrayList<String> stores = new ArrayList<>(); private ArrayList<String> stores = new ArrayList<>();
private int storeSelection; private int storeSelection;
private int sortMode; private SortModes sortMode = SortModes.NONE;
private boolean descending; private boolean descending;
private double minPrice = 0; private double minPrice = 0;
private double maxPrice = -1; private double maxPrice = -1;
@ -46,7 +46,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
} }
@Override @Override
public void sendSort(int sortMode, boolean descending) { public void sendSort(SortModes sortMode, boolean descending) {
this.sortMode = sortMode; this.sortMode = sortMode;
this.descending = descending; this.descending = descending;
sortResults(); sortResults();
@ -243,10 +243,10 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
// Sort based on mode // Sort based on mode
switch (this.sortMode) { switch (this.sortMode) {
case 0: case NONE:
// Do nothing // Do nothing
break; break;
case 1: case NAME:
resultsProductListSorted.sort(new Comparator<Product>() { resultsProductListSorted.sort(new Comparator<Product>() {
@Override @Override
public int compare(Product a, Product b) { public int compare(Product a, Product b) {
@ -254,9 +254,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
} }
}); });
break; break;
case PRICE:
// TODO: May need to change this depending on if price is stored as a string or a double
case 2:
resultsProductListSorted.sort(new Comparator<Product>() { resultsProductListSorted.sort(new Comparator<Product>() {
@Override @Override
public int compare(Product a, Product b) { public int compare(Product a, Product b) {
@ -271,7 +269,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
}); });
break; break;
case 3: case STORE:
resultsProductListSorted.sort(new Comparator<Product>() { resultsProductListSorted.sort(new Comparator<Product>() {
@Override @Override
public int compare(Product a, Product b) { public int compare(Product a, Product b) {
@ -280,7 +278,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
}); });
break; break;
case 4: case UPC:
resultsProductListSorted.sort(new Comparator<Product>() { resultsProductListSorted.sort(new Comparator<Product>() {
@Override @Override
public int compare(Product a, Product b) { 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 // 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++) { for (int i = 0; i < resultsProductListSorted.size() / 2; i++) {
Product temp = resultsProductListSorted.get(i); Product temp = resultsProductListSorted.get(i);
resultsProductListSorted.set(i, resultsProductListSorted.get(resultsProductListSorted.size() - i - 1)); resultsProductListSorted.set(i, resultsProductListSorted.get(resultsProductListSorted.size() - i - 1));

View File

@ -5,23 +5,31 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
public class SortDialogFragment extends DialogFragment { public class SortDialogFragment extends DialogFragment {
public interface OnSortListener { public interface OnSortListener {
void sendSort(int sortMode, boolean descending); void sendSort(SortModes sortMode, boolean descending);
} }
public OnSortListener onSortListener; public OnSortListener onSortListener;
private int sortMode; private SortModes sortMode;
private boolean descending; 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.sortMode = sortMode;
this.descending = descending; this.descending = descending;
} }
@ -38,42 +46,97 @@ public class SortDialogFragment extends DialogFragment {
View root = inflater.inflate(R.layout.dialog_sort, null); View root = inflater.inflate(R.layout.dialog_sort, null);
builder.setView(root); builder.setView(root);
TextView tvSortNone = root.findViewById(R.id.sort_none); tvSortNone = (TextView) root.findViewById(R.id.sort_none);
TextView tvSortName = root.findViewById(R.id.sort_name); tvSortName = (TextView) root.findViewById(R.id.sort_name);
TextView tvSortPrice = root.findViewById(R.id.sort_price); tvSortPrice = (TextView) root.findViewById(R.id.sort_price);
TextView tvSortStore = root.findViewById(R.id.sort_store); 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() { tvSortNone.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
handleClicked(tvSortNone, SortModes.NONE, swDescending.isChecked());
} }
}); });
tvSortName.setOnClickListener(new View.OnClickListener() { tvSortName.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
handleClicked(tvSortName, SortModes.NAME, swDescending.isChecked());
} }
}); });
tvSortPrice.setOnClickListener(new View.OnClickListener() { tvSortPrice.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
handleClicked(tvSortPrice, SortModes.PRICE, swDescending.isChecked());
} }
}); });
tvSortStore.setOnClickListener(new View.OnClickListener() { tvSortStore.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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(); 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 // Required to extend DialogFragment
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
@ -81,7 +144,7 @@ public class SortDialogFragment extends DialogFragment {
try { try {
onSortListener = (OnSortListener) getActivity(); onSortListener = (OnSortListener) getActivity();
} catch (ClassCastException e) { } catch (ClassCastException e) {
Log.e("FilterDialogFragment", "onAttach: ClassCastException: " + e.getMessage()); Log.e("SortDialogFragment", "onAttach: ClassCastException: " + e.getMessage());
} }
} }
} }

View File

@ -0,0 +1,9 @@
package com.example.listify;
public enum SortModes {
NONE,
NAME,
PRICE,
STORE,
UPC
}

View File

@ -4,6 +4,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"> android:layout_marginHorizontal="30dp">
<!-- I would've use a ListView here, but it doesn't offer enough customization -->
<TextView <TextView
android:id="@+id/sort_none" android:id="@+id/sort_none"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -15,8 +16,7 @@
android:gravity="center_vertical" android:gravity="center_vertical"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless" android:foreground="?attr/selectableItemBackgroundBorderless" />
android:soundEffectsEnabled="true"/>
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
@ -78,9 +78,14 @@
android:background="@color/list_divider" /> android:background="@color/list_divider" />
<LinearLayout <LinearLayout
android:id="@+id/descending_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:gravity="center_vertical"> android:gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:soundEffectsEnabled="false">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -91,6 +96,7 @@
android:paddingStart="16dp"/> android:paddingStart="16dp"/>
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_descending"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>

View File

@ -11,4 +11,5 @@
<color name="list_row_hover_start_color">#ebeef0</color> <color name="list_row_hover_start_color">#ebeef0</color>
<color name="list_row_hover_end_color">#1c9ef4</color> <color name="list_row_hover_end_color">#1c9ef4</color>
<color name="light_gray">#e6e6e6</color> <color name="light_gray">#e6e6e6</color>
<color name="white">#ffffffff</color>
</resources> </resources>