Skip to content

Commit

Permalink
searchfilters: Moving DividerItem from NewPipeExtractor into NewPipe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evermind-zz authored and Stypox committed Dec 30, 2023
1 parent 0985afa commit 04f03ab
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,10 +24,11 @@ public final class FilterContainer {
public static final int ITEM_IDENTIFIER_UNKNOWN = -1;

private final Map<Integer, FilterItem> idToFilterItem = new HashMap<>();
private final FilterGroup[] filterGroups;
private final List<FilterGroup> 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);
Expand All @@ -41,7 +46,7 @@ public FilterItem getFilterItem(final int id) {
return idToFilterItem.get(id);
}

public FilterGroup[] getFilterGroups() {
public List<FilterGroup> getFilterGroups() {
return filterGroups;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +48,7 @@ public final class FilterGroup {
/**
* The filter items that belong to this {@link FilterGroup}.
*/
private final FilterItem[] filterItems;
private final List<FilterItem> filterItems;

/**
* {@link #getAllSortFilters()}.
Expand All @@ -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<FilterItem> filterItems,
@Nullable final FilterContainer allSortFilters) {
this.identifier = identifier;
this.groupNameId = groupNameId;
Expand Down Expand Up @@ -99,8 +103,9 @@ public int getDefaultSelectedFilterId() {

/**
* {@link #filterItems}
* @return
*/
public FilterItem[] getFilterItems() {
public List<FilterItem> getFilterItems() {
return filterItems;
}

Expand Down Expand Up @@ -133,13 +138,6 @@ public static class Factory {
void uniqueIdChecker(final Map<Integer, FilterItem> 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 \""
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -222,7 +221,7 @@ protected List<FilterItem> 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());
}
}
}
Expand Down Expand Up @@ -252,7 +251,7 @@ protected List<FilterItem> getAllContentFiltersList() {
protected List<FilterItem> getAllFiltersList(final FilterContainer filterContainer) {
final List<FilterItem> filterItemList = new ArrayList<>();
for (final FilterGroup cfGroup : filterContainer.getFilterGroups()) {
filterItemList.addAll(Arrays.asList(cfGroup.getFilterItems()));
filterItemList.addAll(cfGroup.getFilterItems());
}
return filterItemList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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<FilterItem> 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
Expand Down

0 comments on commit 04f03ab

Please sign in to comment.