Merge pull request #21 from caneleex/feature/configurable-colors

add support for changing segments' color
This commit is contained in:
caneleex 2021-07-28 16:47:54 +02:00 committed by GitHub
commit 0027cb341f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 10 deletions

View File

@ -153,7 +153,9 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
entryValues[i] = behaviour.key;
}
for (SponsorBlockSettings.SegmentInfo segmentInfo : SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted()) {
SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted();
for (SponsorBlockSettings.SegmentInfo segmentInfo : categories) {
ListPreference preference = new ListPreference(context);
preference.setTitle(segmentInfo.getTitleWithDot());
preference.setSummary(segmentInfo.description.toString());
@ -161,9 +163,27 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
preference.setDefaultValue(defaultValue);
preference.setEntries(entries);
preference.setEntryValues(entryValues);
category.addPreference(preference);
}
Preference colorPreference = new Preference(context);
screen.addPreference(colorPreference);
colorPreference.setTitle(str("color_change"));
colorPreference.setOnPreferenceClickListener(preference1 -> {
CharSequence[] items = new CharSequence[categories.length];
for (int i = 0; i < items.length; i++) {
items[i] = categories[i].getTitleWithDot();
}
new AlertDialog.Builder(context)
.setTitle(str("color_choose_category"))
.setItems(items, SponsorBlockUtils.categoryColorChangeClickListener)
.show();
return true;
});
preferencesToDisableWhenSBDisabled.add(colorPreference);
}
private void addStatsCategory(Context context, PreferenceScreen screen) {

View File

@ -2,6 +2,7 @@ package pl.jakubweg;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
@ -28,6 +29,7 @@ public class SponsorBlockSettings {
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS = "sb-skipped-segments";
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME = "sb-skipped-segments-time";
public static final String PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS = "sb-length-without-segments";
public static final String PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX = "_color";
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SKIP_AUTOMATICALLY;
@ -93,6 +95,9 @@ public class SponsorBlockSettings {
SegmentBehaviour[] possibleBehaviours = SegmentBehaviour.values();
final ArrayList<String> enabledCategories = new ArrayList<>(possibleBehaviours.length);
for (SegmentInfo segment : SegmentInfo.valuesWithoutUnsubmitted()) {
String categoryColor = preferences.getString(segment.key + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX, SponsorBlockUtils.formatColorString(segment.defaultColor));
segment.setColor(Color.parseColor(categoryColor));
SegmentBehaviour behaviour = null;
String value = preferences.getString(segment.key, null);
if (value == null)
@ -191,26 +196,26 @@ public class SponsorBlockSettings {
public final StringRef title;
public final StringRef skipMessage;
public final StringRef description;
public final int color;
public final Paint paint;
public final int defaultColor;
public int color;
public SegmentBehaviour behaviour;
private CharSequence lazyTitleWithDot;
SegmentInfo(String key,
StringRef title,
StringRef skipMessage,
StringRef description,
SegmentBehaviour behaviour,
int color) {
int defaultColor) {
this.key = key;
this.title = title;
this.skipMessage = skipMessage;
this.description = description;
this.behaviour = behaviour;
this.color = color & 0xFFFFFF;
paint = new Paint();
paint.setColor(color);
this.defaultColor = defaultColor;
this.color = defaultColor;
this.paint = new Paint();
}
public static SegmentInfo[] valuesWithoutUnsubmitted() {
@ -221,10 +226,15 @@ public class SponsorBlockSettings {
return mValuesMap.get(key);
}
public void setColor(int color) {
color = color & 0xFFFFFF;
this.color = color;
paint.setColor(color);
paint.setAlpha(255);
}
public CharSequence getTitleWithDot() {
return (lazyTitleWithDot == null) ?
lazyTitleWithDot = Html.fromHtml(String.format("<font color=\"#%06X\">⬤</font> %s", color, title))
: lazyTitleWithDot;
return Html.fromHtml(String.format("<font color=\"#%06X\">⬤</font> %s", color, title));
}
}
}

View File

@ -5,11 +5,14 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
@ -41,6 +44,7 @@ import static pl.jakubweg.PlayerController.getLastKnownVideoTime;
import static pl.jakubweg.PlayerController.sponsorSegmentsOfCurrentVideo;
import static pl.jakubweg.SponsorBlockPreferenceFragment.FORMATTER;
import static pl.jakubweg.SponsorBlockPreferenceFragment.SAVED_TEMPLATE;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX;
import static pl.jakubweg.SponsorBlockSettings.isSponsorBlockEnabled;
import static pl.jakubweg.SponsorBlockSettings.showTimeWithoutSegments;
import static pl.jakubweg.SponsorBlockSettings.skippedSegments;
@ -220,6 +224,40 @@ public abstract class SponsorBlockUtils {
})
.show();
};
public static final DialogInterface.OnClickListener categoryColorChangeClickListener = (dialog, which) -> {
SponsorBlockSettings.SegmentInfo segmentInfo = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted()[which];
String key = segmentInfo.key + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX;
Context context = ((AlertDialog) dialog).getContext();
EditText editText = new EditText(context);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setText(formatColorString(segmentInfo.color));
Context applicationContext = context.getApplicationContext();
SharedPreferences preferences = SponsorBlockSettings.getPreferences(context);
new AlertDialog.Builder(context)
.setView(editText)
.setPositiveButton(str("change"), (dialog1, which1) -> {
try {
int color = Color.parseColor(editText.getText().toString());
segmentInfo.setColor(color);
Toast.makeText(applicationContext, str("color_changed"), Toast.LENGTH_SHORT).show();
preferences.edit().putString(key, formatColorString(color)).apply();
}
catch (Exception ex) {
Toast.makeText(applicationContext, str("color_invalid"), Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton(str("reset"), (dialog1, which1) -> {
int defaultColor = segmentInfo.defaultColor;
segmentInfo.setColor(defaultColor);
Toast.makeText(applicationContext, str("color_reset"), Toast.LENGTH_SHORT).show();
preferences.edit().putString(key, formatColorString(defaultColor)).apply();
})
.setNegativeButton(android.R.string.cancel, null)
.show();
};
private static final Runnable submitRunnable = () -> {
messageToToast = null;
final String uuid = SponsorBlockSettings.uuid;
@ -445,6 +483,10 @@ public abstract class SponsorBlockUtils {
return count;
}
public static String formatColorString(int color) {
return String.format("#%06X", color);
}
@SuppressWarnings("deprecation")
public static void addUserStats(PreferenceCategory category, Preference loadingPreference, UserStats stats) {
category.removePreference(loadingPreference);

View File

@ -183,6 +183,14 @@
<string name="skip_showbutton">Show a skip button</string>
<string name="skip_ignore">Don\'t do anything</string>
<string name="color_change">Change colors</string>
<string name="color_choose_category">Choose the category</string>
<string name="color_changed">Color changed</string>
<string name="color_reset">Color reset</string>
<string name="color_invalid">Invalid hex code</string>
<string name="change">Change</string>
<string name="reset">Reset</string>
<string name="stats">Stats</string>
<string name="stats_loading">Loading..</string>
<string name="stats_sb_disabled">SponsorBlock is disabled</string>