0
0
Fork 0
mirror of https://github.com/YTVanced/Integrations synced 2024-11-27 05:33:00 +00:00

add whitelist menus

This commit is contained in:
caneleex 2022-02-22 21:25:09 +01:00
parent cb9006190f
commit 1b436e0405
4 changed files with 142 additions and 6 deletions

View file

@ -19,7 +19,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import fi.vanced.libraries.youtube.player.ChannelModel;
@ -117,8 +116,7 @@ public class Whitelist {
}
return false;
}
List<ChannelModel> whitelistedChannels = whitelistMap.get(whitelistType);
for (ChannelModel channel : whitelistedChannels) {
for (ChannelModel channel : getWhitelistedChannels(whitelistType)) {
if (channel.getAuthor().equals(channelName)) {
if (debug) {
Log.d(TAG, String.format("Whitelist for channel %s for type %s", channelName, whitelistType));
@ -130,7 +128,7 @@ public class Whitelist {
}
public static boolean addToWhitelist(WhitelistType whitelistType, Context context, ChannelModel channel) {
ArrayList<ChannelModel> whitelisted = whitelistMap.get(whitelistType);
ArrayList<ChannelModel> whitelisted = getWhitelistedChannels(whitelistType);
for (ChannelModel whitelistedChannel : whitelisted) {
String channelId = channel.getChannelId();
if (whitelistedChannel.getChannelId().equals(channelId)) {
@ -146,7 +144,7 @@ public class Whitelist {
}
public static void removeFromWhitelist(WhitelistType whitelistType, Context context, String channelName) {
ArrayList<ChannelModel> channels = whitelistMap.get(whitelistType);
ArrayList<ChannelModel> channels = getWhitelistedChannels(whitelistType);
Iterator<ChannelModel> iterator = channels.iterator();
while (iterator.hasNext()) {
ChannelModel channel = iterator.next();
@ -165,7 +163,7 @@ public class Whitelist {
}
}
private static boolean updateWhitelist(WhitelistType whitelistType, ArrayList<ChannelModel> channels, Context context) {
public static boolean updateWhitelist(WhitelistType whitelistType, ArrayList<ChannelModel> channels, Context context) {
if (context == null) {
return false;
}
@ -185,4 +183,8 @@ public class Whitelist {
public static void setEnabled(WhitelistType whitelistType, boolean enabled) {
enabledMap.put(whitelistType, enabled);
}
public static ArrayList<ChannelModel> getWhitelistedChannels(WhitelistType whitelistType) {
return whitelistMap.get(whitelistType);
}
}

View file

@ -52,6 +52,7 @@ import java.util.ArrayList;
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
import fi.vanced.utils.SharedPrefUtils;
import pl.jakubweg.objects.EditTextListPreference;
import pl.jakubweg.objects.WhitelistedChannelsPreference;
import pl.jakubweg.requests.SBRequester;
@SuppressWarnings({"unused", "deprecation"}) // injected
@ -381,6 +382,14 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
{
WhitelistedChannelsPreference preference = new WhitelistedChannelsPreference(context);
preference.setTitle("Manage your whitelisted channels");
preference.setWhitelistType(WhitelistType.SPONSORBLOCK);
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
}
private static class APIURLChangeListener implements DialogInterface.OnClickListener {

View file

@ -0,0 +1,120 @@
package pl.jakubweg.objects;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import fi.vanced.libraries.youtube.player.ChannelModel;
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
import fi.vanced.utils.VancedUtils;
public class WhitelistedChannelsPreference extends DialogPreference {
private ArrayList<ChannelModel> mEntries;
private WhitelistType whitelistType;
public WhitelistedChannelsPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public WhitelistedChannelsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public WhitelistedChannelsPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WhitelistedChannelsPreference(Context context) {
super(context);
}
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
mEntries = Whitelist.getWhitelistedChannels(whitelistType);
Context context = getContext();
if (mEntries.isEmpty()) {
TextView emptyView = new TextView(context);
emptyView.setText("There are no whitelisted channels.");
emptyView.setTextSize(18);
builder.setView(emptyView);
}
else {
builder.setView(getEntriesListView(context));
}
}
private View getEntriesListView(Context context) {
LinearLayout entriesContainer = new LinearLayout(context);
entriesContainer.setOrientation(LinearLayout.VERTICAL);
for (final ChannelModel entry : mEntries) {
String author = entry.getAuthor();
View entryView = getEntryView(context, author, v -> {
entriesContainer.removeView(entriesContainer.findViewWithTag(author));
mEntries.remove(entry);
});
entryView.setTag(author);
entriesContainer.addView(entryView);
}
return entriesContainer;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult && mEntries != null) {
Whitelist.updateWhitelist(whitelistType, mEntries, getContext());
}
}
private View getEntryView(Context context, CharSequence entry,
View.OnClickListener onDeleteClickListener) {
int horizontalPadding = 80;
Drawable divider = context.getResources().getDrawable(android.R.drawable.divider_horizontal_dark);
Drawable deleteIcon = context.getResources().getDrawable(VancedUtils.getIdentifier("ic_baseline_delete_24", "drawable"));
LinearLayout.LayoutParams entryContainerParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
entryContainerParams.setMargins(horizontalPadding, 0, horizontalPadding, 0);
LinearLayout.LayoutParams entryLabelLayoutParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
entryLabelLayoutParams.gravity = Gravity.CENTER;
LinearLayout entryContainer = new LinearLayout(context);
entryContainer.setOrientation(LinearLayout.HORIZONTAL);
entryContainer.setDividerDrawable(divider);
entryContainer.setLayoutParams(entryContainerParams);
TextView entryLabel = new TextView(context);
entryLabel.setText(entry);
entryLabel.setLayoutParams(entryLabelLayoutParams);
entryLabel.setTextSize(18);
ImageButton deleteButton = new ImageButton(context);
deleteButton.setImageDrawable(deleteIcon);
deleteButton.setOnClickListener(onDeleteClickListener);
deleteButton.setBackground(null);
entryContainer.addView(entryLabel);
entryContainer.addView(deleteButton);
return entryContainer;
}
public void setWhitelistType(WhitelistType whitelistType) {
this.whitelistType = whitelistType;
}
}

View file

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>