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 ArrayList<String> 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<Product>() {
@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<Product>() {
@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<Product>() {
@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<Product>() {
@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));

View File

@ -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());
}
}
}

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

View File

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