|
| 1 | +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later |
| 2 | + |
| 3 | +package org.schabi.newpipe.fragments.list.search.filter; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.GridLayout; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import com.google.android.material.chip.ChipGroup; |
| 12 | + |
| 13 | +import org.schabi.newpipe.extractor.search.filter.FilterGroup; |
| 14 | +import org.schabi.newpipe.util.DeviceUtils; |
| 15 | + |
| 16 | +import androidx.annotation.NonNull; |
| 17 | + |
| 18 | +public class SearchFilterChipDialogGenerator extends SearchFilterDialogGenerator { |
| 19 | + |
| 20 | + public SearchFilterChipDialogGenerator( |
| 21 | + @NonNull final SearchFilterLogic logic, |
| 22 | + @NonNull final ViewGroup root, |
| 23 | + @NonNull final Context context) { |
| 24 | + super(logic, root, context); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void createFilterGroup(@NonNull final FilterGroup filterGroup, |
| 29 | + @NonNull final UiWrapperMapDelegate wrapperDelegate, |
| 30 | + @NonNull final UiSelectorDelegate selectorDelegate) { |
| 31 | + final boolean doSpanDataOverMultipleCells = true; |
| 32 | + final UiItemWrapperViews viewsWrapper = new UiItemWrapperViews( |
| 33 | + filterGroup.getIdentifier()); |
| 34 | + |
| 35 | + if (filterGroup.getNameId() != null) { |
| 36 | + final GridLayout.LayoutParams layoutParams = |
| 37 | + clipFreeRightColumnLayoutParams(doSpanDataOverMultipleCells); |
| 38 | + final TextView filterLabel = createFilterLabel(filterGroup, layoutParams); |
| 39 | + globalLayout.addView(filterLabel); |
| 40 | + viewsWrapper.add(filterLabel); |
| 41 | + } else if (doWeNeedASeparatorView()) { |
| 42 | + final SeparatorLineView separatorLineView = createSeparatorLine(); |
| 43 | + globalLayout.addView(separatorLineView); |
| 44 | + viewsWrapper.add(separatorLineView); |
| 45 | + } |
| 46 | + |
| 47 | + final ChipGroup chipGroup = new ChipGroup(context); |
| 48 | + chipGroup.setLayoutParams( |
| 49 | + setDefaultMarginInDp(clipFreeRightColumnLayoutParams(doSpanDataOverMultipleCells), |
| 50 | + 8, 2, 4, 2)); |
| 51 | + chipGroup.setSingleLine(false); |
| 52 | + chipGroup.setSingleSelection(filterGroup.isOnlyOneCheckable()); |
| 53 | + |
| 54 | + createUiChipElementsForFilterGroupItems( |
| 55 | + filterGroup, wrapperDelegate, selectorDelegate, chipGroup); |
| 56 | + |
| 57 | + |
| 58 | + wrapperDelegate.put(filterGroup.getIdentifier(), viewsWrapper); |
| 59 | + globalLayout.addView(chipGroup); |
| 60 | + viewsWrapper.add(chipGroup); |
| 61 | + } |
| 62 | + |
| 63 | + private boolean doWeNeedASeparatorView() { |
| 64 | + // if 0 than there is nothing to separate |
| 65 | + if (globalLayout.getChildCount() == 0) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + final View lastView = globalLayout.getChildAt(globalLayout.getChildCount() - 1); |
| 69 | + return !(lastView instanceof SeparatorLineView); |
| 70 | + } |
| 71 | + |
| 72 | + private ViewGroup.MarginLayoutParams setDefaultMarginInDp( |
| 73 | + @NonNull final ViewGroup.MarginLayoutParams layoutParams, |
| 74 | + final int left, final int top, final int right, final int bottom) { |
| 75 | + layoutParams.setMargins( |
| 76 | + DeviceUtils.dpToPx(left, context), |
| 77 | + DeviceUtils.dpToPx(top, context), |
| 78 | + DeviceUtils.dpToPx(right, context), |
| 79 | + DeviceUtils.dpToPx(bottom, context) |
| 80 | + ); |
| 81 | + return layoutParams; |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments