mirror of
https://github.com/MaintainTeam/LastPipeBender.git
synced 2025-03-01 13:58:20 +03:00
searchfilters: make default dialog not stretch vertically
Originally stretching vertically over the whole screen was used to avoid resizing the UI each time the selected content filter selects another set of sort filters. As it turns out fullscreen vertically dialogs can be ugly and my other attempt to use View.INVISIBLE instead of (View.GONE) in order to have some sort of placeholder, didn't work well with the Spinner Views either. So we go back and let Android resize the UI. Another resizing annoyance is now avoided. The Material's Chip with the Filter style was also resized each time a Chip was selected. To avoid resizing for the Chip views the checkmark icon is removed by using a custom chip style: "@style/ChipSearchFilter". The custom chip style also improves the contrast when a chip is selected To save some space a ChipGroup that holds more than CHIP_GROUP_ELEMENTS_THRESHOLD elements will be spanned over all cells of a row in the GridLayout.
This commit is contained in:
parent
d7bfb0ab7a
commit
a99a767b08
5 changed files with 69 additions and 10 deletions
|
@ -4,6 +4,7 @@ package org.schabi.newpipe.fragments.list.search.filter;
|
|||
|
||||
import android.content.Context;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
|
@ -23,9 +24,11 @@ import org.schabi.newpipe.util.ServiceHelper;
|
|||
import java.util.List;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerator {
|
||||
private static final int CHIP_GROUP_ELEMENTS_THRESHOLD = 2;
|
||||
private static final int CHIP_MIN_TOUCH_TARGET_SIZE_DP = 40;
|
||||
private final GridLayout globalLayout;
|
||||
|
||||
public SearchFilterDialogGenerator(
|
||||
|
@ -62,8 +65,9 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
final UiItemWrapperViews viewsWrapper = new UiItemWrapperViews(
|
||||
filterGroup.getIdentifier());
|
||||
|
||||
final TextView filterLabel;
|
||||
if (filterGroup.getNameId() != null) {
|
||||
final TextView filterLabel = new TextView(context);
|
||||
filterLabel = new TextView(context);
|
||||
|
||||
filterLabel.setId(filterGroup.getIdentifier());
|
||||
filterLabel.setText(
|
||||
|
@ -73,13 +77,16 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
setZeroPadding(filterLabel);
|
||||
|
||||
filterLabel.setLayoutParams(layoutParams);
|
||||
globalLayout.addView(filterLabel);
|
||||
viewsWrapper.add(filterLabel);
|
||||
} else {
|
||||
filterLabel = null;
|
||||
doSpanDataOverMultipleCells = true;
|
||||
}
|
||||
|
||||
if (filterGroup.isOnlyOneCheckable()) {
|
||||
if (filterLabel != null) {
|
||||
globalLayout.addView(filterLabel);
|
||||
}
|
||||
|
||||
final Spinner filterDataSpinner = new Spinner(context, Spinner.MODE_DROPDOWN);
|
||||
|
||||
|
@ -97,6 +104,9 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
|
||||
} else { // multiple items in FilterGroup selectable
|
||||
final ChipGroup chipGroup = new ChipGroup(context);
|
||||
doSpanDataOverMultipleCells = chooseParentViewForFilterLabelAndAdd(
|
||||
filterGroup, doSpanDataOverMultipleCells, filterLabel, chipGroup);
|
||||
|
||||
viewsWrapper.add(chipGroup);
|
||||
globalLayout.addView(chipGroup);
|
||||
chipGroup.setLayoutParams(
|
||||
|
@ -110,6 +120,28 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
wrapperDelegate.put(filterGroup.getIdentifier(), viewsWrapper);
|
||||
}
|
||||
|
||||
private boolean chooseParentViewForFilterLabelAndAdd(
|
||||
@NonNull final FilterGroup filterGroup,
|
||||
final boolean doSpanDataOverMultipleCells,
|
||||
@Nullable final TextView filterLabel,
|
||||
@NonNull final ChipGroup possibleParentView) {
|
||||
|
||||
boolean spanOverMultipleCells = doSpanDataOverMultipleCells;
|
||||
if (filterLabel != null) {
|
||||
// If we have more than CHIP_GROUP_ELEMENTS_THRESHOLD elements to be
|
||||
// displayed as Chips add its filterLabel as first element to ChipGroup.
|
||||
// Now the ChipGroup can be spanned over all the cells to use
|
||||
// the space better.
|
||||
if (filterGroup.getFilterItems().size() > CHIP_GROUP_ELEMENTS_THRESHOLD) {
|
||||
possibleParentView.addView(filterLabel);
|
||||
spanOverMultipleCells = true;
|
||||
} else {
|
||||
globalLayout.addView(filterLabel);
|
||||
}
|
||||
}
|
||||
return spanOverMultipleCells;
|
||||
}
|
||||
|
||||
private void createUiElementsForSingleSelectableItemsFilterGroup(
|
||||
@NonNull final FilterGroup filterGroup,
|
||||
@NonNull final UiWrapperMapDelegate wrapperDelegate,
|
||||
|
@ -143,8 +175,10 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
@NonNull final UiSelectorDelegate selectorDelegate,
|
||||
@NonNull final ChipGroup chipGroup) {
|
||||
for (final FilterItem item : filterGroup.getFilterItems()) {
|
||||
final Chip chip = new Chip(new ContextThemeWrapper(
|
||||
context, R.style.Theme_MaterialComponents_Light));
|
||||
final Chip chip = (Chip) LayoutInflater.from(context).inflate(
|
||||
R.layout.chip_search_filter, chipGroup, false);
|
||||
chip.ensureAccessibleTouchTarget(
|
||||
DeviceUtils.dpToPx(CHIP_MIN_TOUCH_TARGET_SIZE_DP, context));
|
||||
chip.setText(ServiceHelper.getTranslatedFilterString(item.getNameId(), context));
|
||||
chip.setId(item.getIdentifier());
|
||||
chip.setCheckable(true);
|
||||
|
@ -205,7 +239,7 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
@NonNull
|
||||
private GridLayout.LayoutParams getLayoutParamsViews() {
|
||||
final GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
|
||||
layoutParams.setGravity(Gravity.TOP);
|
||||
layoutParams.setGravity(Gravity.CENTER_VERTICAL);
|
||||
setDefaultMargin(layoutParams);
|
||||
return layoutParams;
|
||||
}
|
||||
|
@ -247,7 +281,6 @@ public class SearchFilterDialogGenerator extends BaseSearchFilterUiDialogGenerat
|
|||
|
||||
@Override
|
||||
public void setChecked(final boolean checked) {
|
||||
view.setSelected(checked);
|
||||
((Chip) view).setChecked(checked);
|
||||
|
||||
if (checked) {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- adding more contrast than @color/mtrl_chip_background_color if a Chip is selected -->
|
||||
<item android:alpha="0.30" android:color="?attr/colorOnSurface" android:state_enabled="true" android:state_selected="true"/>
|
||||
<item android:alpha="0.18" android:color="?attr/colorOnSurface" android:state_enabled="true" android:state_checked="true"/>
|
||||
<!-- 12% of 87% opacity -->
|
||||
<item android:alpha="0.10" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
|
||||
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
|
||||
</selector>
|
9
app/src/main/res/layout/chip_search_filter.xml
Normal file
9
app/src/main/res/layout/chip_search_filter.xml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This is used to inflate a chip with a Material theme, otherwise it would crash -->
|
||||
<!-- Theme.MaterialComponents.DayNight is used to guarantee auto day/night switching -->
|
||||
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
style="@style/ChipSearchFilter"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:theme="@style/Theme.MaterialComponents.DayNight" />
|
|
@ -1,7 +1,7 @@
|
|||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<include
|
||||
android:id="@+id/toolbar_layout"
|
||||
|
@ -9,8 +9,8 @@
|
|||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@+id/toolbar_layout">
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/toolbar_layout">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/vertical_scroll"
|
||||
|
|
|
@ -155,4 +155,12 @@
|
|||
|
||||
<style name="RouterActivityThemeDark" parent="Base.RouterActivityThemeDark" />
|
||||
|
||||
<!-- custom Chip style for SearchFilterDialogFragment -->
|
||||
<style name="ChipSearchFilter" parent="Widget.MaterialComponents.Chip.Filter">
|
||||
<item name="checkedIconEnabled">false</item>
|
||||
<item name="checkedIcon">@null</item>
|
||||
<item name="chipBackgroundColor">@color/mtrl_search_filter_chip_background_color</item>
|
||||
<item name="chipStrokeWidth">0.1dp</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Add table
Reference in a new issue