From 04f03abd904a55f3330b92ec2d996b13474db122 Mon Sep 17 00:00:00 2001 From: evermind Date: Tue, 22 Nov 2022 23:35:41 +0100 Subject: [PATCH] searchfilters: Moving DividerItem from NewPipeExtractor into NewPipe DividerItem was inserted in the content filter framework in the NewPipeExtractor to have a section title for YoutubeMusic. But as UI releated stuff seems a bit out of place in the Extractor I came up with injecting the DividerItem aka section title in the frontend without having to change too much in the frontend. --- .../search/filter/FilterContainer.java | 11 +++++--- .../extractor/search/filter/FilterGroup.java | 22 ++++++++-------- .../extractor/search/filter/FilterItem.java | 10 ------- .../youtube/search/filter/YoutubeFilters.java | 3 --- .../services/DefaultFiltersTest.java | 9 +++---- .../services/DefaultSearchExtractorTest.java | 5 ++-- .../search/SoundcloudSearchQHTest.java | 8 +++--- .../youtube/search/YoutubeSearchQHTest.java | 26 +++++++++---------- 8 files changed, 42 insertions(+), 52 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterContainer.java b/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterContainer.java index 30e0aeea9b..cf4d5acc2e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterContainer.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterContainer.java @@ -2,8 +2,12 @@ package org.schabi.newpipe.extractor.search.filter; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import javax.annotation.Nonnull; @@ -20,10 +24,11 @@ public final class FilterContainer { public static final int ITEM_IDENTIFIER_UNKNOWN = -1; private final Map idToFilterItem = new HashMap<>(); - private final FilterGroup[] filterGroups; + private final List filterGroups; public FilterContainer(@Nonnull final FilterGroup[] filterGroups) { - this.filterGroups = filterGroups; + this.filterGroups = + Arrays.stream(filterGroups).collect(Collectors.toCollection(ArrayList::new)); for (final FilterGroup group : filterGroups) { for (final FilterItem item : group.getFilterItems()) { idToFilterItem.put(item.getIdentifier(), item); @@ -41,7 +46,7 @@ public FilterItem getFilterItem(final int id) { return idToFilterItem.get(id); } - public FilterGroup[] getFilterGroups() { + public List getFilterGroups() { return filterGroups; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterGroup.java b/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterGroup.java index e45b33db2b..abb4d1c30d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterGroup.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterGroup.java @@ -2,8 +2,12 @@ package org.schabi.newpipe.extractor.search.filter; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -44,7 +48,7 @@ public final class FilterGroup { /** * The filter items that belong to this {@link FilterGroup}. */ - private final FilterItem[] filterItems; + private final List filterItems; /** * {@link #getAllSortFilters()}. @@ -55,7 +59,7 @@ private FilterGroup(final int identifier, @Nullable final LibraryStringIds groupNameId, final boolean onlyOneCheckable, final int defaultSelectedFilterId, - @Nonnull final FilterItem[] filterItems, + @Nonnull final List filterItems, @Nullable final FilterContainer allSortFilters) { this.identifier = identifier; this.groupNameId = groupNameId; @@ -99,8 +103,9 @@ public int getDefaultSelectedFilterId() { /** * {@link #filterItems} + * @return */ - public FilterItem[] getFilterItems() { + public List getFilterItems() { return filterItems; } @@ -133,13 +138,6 @@ public static class Factory { void uniqueIdChecker(final Map filterItems, final FilterItem item) { - if (item.getIdentifier() == FilterContainer.ITEM_IDENTIFIER_UNKNOWN - && !(item instanceof FilterItem.DividerItem)) { - throw new InvalidFilterIdException("Filter ID " - + item.getIdentifier() + " aka FilterContainer.ITEM_IDENTIFIER_UNKNOWN" - + " for \"" + item.getNameId() + "\" not allowed"); - } - if (filterItems.containsKey(item.getIdentifier())) { throw new InvalidFilterIdException("Filter ID " + item.getIdentifier() + " for \"" @@ -170,7 +168,9 @@ public FilterGroup createFilterGroup(final int identifier, @Nonnull final FilterItem[] filterItems, @Nullable final FilterContainer allSortFilters) { return new FilterGroup(identifier, groupNameId, onlyOneCheckable, - defaultSelectedFilterId, filterItems, allSortFilters); + defaultSelectedFilterId, + Arrays.stream(filterItems).collect(Collectors.toCollection(ArrayList::new)), + allSortFilters); } /** diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterItem.java index 43b23f5402..90fbd86e9c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/search/filter/FilterItem.java @@ -51,16 +51,6 @@ public LibraryStringIds getNameId() { return this.nameId; } - /** - * This class is used to have a sub title divider between regular {@link FilterItem}s. - */ - public static class DividerItem extends FilterItem { - - public DividerItem(final LibraryStringIds nameId) { - super(FilterContainer.ITEM_IDENTIFIER_UNKNOWN, nameId); - } - } - @Override public boolean equals(final Object obj) { return (obj instanceof FilterItem) && ((FilterItem) obj).identifier == this.identifier; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFilters.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFilters.java index 5c428b3d16..709d3896cd 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFilters.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFilters.java @@ -430,9 +430,6 @@ protected void init() { groupsFactory.getFilterForId(ID_CF_MAIN_CHANNELS), groupsFactory.getFilterForId(ID_CF_MAIN_PLAYLISTS), // groupsFactory.getFilterForId(ID_CF_MAIN_MOVIES), - groupsFactory.getFilterForId(groupsFactory.addFilterItem( - new FilterItem.DividerItem( - LibraryStringIds.SEARCH_FILTERS_YOUTUBE_MUSIC))), groupsFactory.getFilterForId(ID_CF_MAIN_YOUTUBE_MUSIC_SONGS), groupsFactory.getFilterForId(ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS), groupsFactory.getFilterForId(ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS), diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java index cee7650da3..7c0611c0b3 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java @@ -11,7 +11,6 @@ import org.schabi.newpipe.extractor.search.filter.FilterItem; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -172,10 +171,10 @@ public void contentFiltersWithSortFilterTest() { contentFiltersThatHaveCorrespondingSortFilters); for (final FilterGroup cfGroup : filterContainer.getFilterGroups()) { - if (cfGroup.getFilterItems().length > 0) { + if (!cfGroup.getFilterItems().isEmpty()) { // if below id is having a sortFilterVariant it should be the same // variant than the one placed as superset to this group - final int id = cfGroup.getFilterItems()[0].getIdentifier(); + final int id = cfGroup.getFilterItems().get(0).getIdentifier(); if (contentFiltersThatHaveCorrespondingSortFilters.contains(id)) { final FilterContainer sortFilterVariant = searchFilterBase.getContentFilterSortFilterVariant(id); @@ -222,7 +221,7 @@ protected List getAllSortFiltersList() { final FilterContainer sortFilterContainer = group.getAllSortFilters(); if (null != sortFilterContainer) { for (final FilterGroup filterGroup : sortFilterContainer.getFilterGroups()) { - filterItems.addAll(Arrays.asList(filterGroup.getFilterItems())); + filterItems.addAll(filterGroup.getFilterItems()); } } } @@ -252,7 +251,7 @@ protected List getAllContentFiltersList() { protected List getAllFiltersList(final FilterContainer filterContainer) { final List filterItemList = new ArrayList<>(); for (final FilterGroup cfGroup : filterContainer.getFilterGroups()) { - filterItemList.addAll(Arrays.asList(cfGroup.getFilterItems())); + filterItemList.addAll(cfGroup.getFilterItems()); } return filterItemList; } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java index 337bc7a3ae..5f1163fe62 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java @@ -10,7 +10,6 @@ import java.net.MalformedURLException; import java.net.URL; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -43,9 +42,9 @@ public static int getNoOfSortFilterItems(final FilterContainer contentFilterCont } public static int getNoOfFilterItems(final FilterContainer filterContainer) { - return Arrays.stream(filterContainer.getFilterGroups()) + return filterContainer.getFilterGroups().stream() .map(FilterGroup::getFilterItems) - .mapToInt(filterItems -> filterItems.length) + .mapToInt(filterItems -> filterItems.size()) .sum(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/SoundcloudSearchQHTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/SoundcloudSearchQHTest.java index ce689f11eb..3b20b9df2c 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/SoundcloudSearchQHTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/SoundcloudSearchQHTest.java @@ -81,13 +81,13 @@ public void testGetAvailableContentFilter() { assertEquals(4, noOfContentFilters); assertEquals(SoundcloudFilters.ID_CF_MAIN_ALL, - contentFilter.getFilterGroups()[0].getFilterItems()[0].getIdentifier()); + contentFilter.getFilterGroups().get(0).getFilterItems().get(0).getIdentifier()); assertEquals(SoundcloudFilters.ID_CF_MAIN_TRACKS, - contentFilter.getFilterGroups()[0].getFilterItems()[1].getIdentifier()); + contentFilter.getFilterGroups().get(0).getFilterItems().get(1).getIdentifier()); assertEquals(SoundcloudFilters.ID_CF_MAIN_USERS, - contentFilter.getFilterGroups()[0].getFilterItems()[2].getIdentifier()); + contentFilter.getFilterGroups().get(0).getFilterItems().get(2).getIdentifier()); assertEquals(SoundcloudFilters.ID_CF_MAIN_PLAYLISTS, - contentFilter.getFilterGroups()[0].getFilterItems()[3].getIdentifier()); + contentFilter.getFilterGroups().get(0).getFilterItems().get(3).getIdentifier()); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java index 33e8d4269c..2cef0eb235 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.extractor.services.youtube.search; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.schabi.newpipe.extractor.ServiceList.YouTube; import static java.util.Collections.singletonList; @@ -11,6 +10,8 @@ import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest; import org.schabi.newpipe.extractor.services.youtube.search.filter.YoutubeFilters; +import java.util.List; + public class YoutubeSearchQHTest { @Test @@ -71,18 +72,17 @@ public void testGetAvailableContentFilter() { YouTube.getSearchQHFactory().getAvailableContentFilter(); final int noOfContentFilters = DefaultSearchExtractorTest.getNoOfFilterItems(contentFilter); - final FilterItem[] filterItems = contentFilter.getFilterGroups()[0].getFilterItems(); - assertEquals(10, noOfContentFilters); - assertEquals(YoutubeFilters.ID_CF_MAIN_ALL, filterItems[0].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_VIDEOS, filterItems[1].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_CHANNELS, filterItems[2].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_PLAYLISTS, filterItems[3].getIdentifier()); - assertTrue(filterItems[4] instanceof FilterItem.DividerItem); - assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS, filterItems[5].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS, filterItems[6].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS, filterItems[7].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_PLAYLISTS, filterItems[8].getIdentifier()); - assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ARTISTS, filterItems[9].getIdentifier()); + final List filterItems = contentFilter.getFilterGroups().get(0).getFilterItems(); + assertEquals(9, noOfContentFilters); + assertEquals(YoutubeFilters.ID_CF_MAIN_ALL, filterItems.get(0).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_VIDEOS, filterItems.get(1).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_CHANNELS, filterItems.get(2).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_PLAYLISTS, filterItems.get(3).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS, filterItems.get(4).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS, filterItems.get(5).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS, filterItems.get(6).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_PLAYLISTS, filterItems.get(7).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ARTISTS, filterItems.get(8).getIdentifier()); } @Test