mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 10:48:46 +00:00
Moving sort and filter code to the correct files
This commit is contained in:
parent
d8bf4e02f8
commit
e7c1ab85ea
@ -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<String> 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<String> stores, int sortMode, boolean descending, double maxProductPrice, double minPrice, double maxPrice) {
|
||||
public FilterDialogFragment(int storeSelection, ArrayList<String> 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[] {"<Default>", "Name", "Price", "Store"};
|
||||
ArrayAdapter<String> 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);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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<Product> 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;
|
||||
}
|
||||
});
|
||||
|
||||
@ -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[] {"<Default>", "Name", "Price", "Store"};
|
||||
ArrayAdapter<String> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,37 +20,6 @@
|
||||
android:background="@android:drawable/spinner_dropdown_background"
|
||||
android:spinnerMode="dropdown"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_sort_by"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="15dp"
|
||||
android:text="Sort by" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="15dp">
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/sort_mode_dropdown"
|
||||
android:layout_width="265dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:drawable/spinner_dropdown_background"
|
||||
android:spinnerMode="dropdown"/>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/sort_direction_button"
|
||||
android:layout_marginTop="-6dp"
|
||||
android:layout_width="57dp"
|
||||
android:layout_height="70dp"
|
||||
android:background="@null"
|
||||
android:src="@drawable/ic_baseline_arrow_upward_50"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_price_label"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
41
Listify/app/src/main/res/layout/dialog_sort.xml
Normal file
41
Listify/app/src/main/res/layout/dialog_sort.xml
Normal file
@ -0,0 +1,41 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_sort_by"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="15dp"
|
||||
android:text="Sort by" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="265dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="15dp">
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/sort_mode_dropdown"
|
||||
android:layout_width="265dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:drawable/spinner_dropdown_background"
|
||||
android:spinnerMode="dropdown"/>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/sort_direction_button"
|
||||
android:layout_marginTop="-6dp"
|
||||
android:layout_width="57dp"
|
||||
android:layout_height="70dp"
|
||||
android:background="@null"
|
||||
android:src="@drawable/ic_baseline_arrow_upward_50"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<ListView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</LinearLayout>
|
||||
Loading…
Reference in New Issue
Block a user