finished the implementation

This commit is contained in:
caneleex 2021-07-27 17:26:59 +02:00
parent 4fc6d6630e
commit 5bd9e3472f
4 changed files with 60 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
@ -15,6 +16,7 @@ import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.text.InputType;
import android.widget.EditText;
import android.widget.Toast;
import java.text.DecimalFormat;
@ -24,6 +26,7 @@ import pl.jakubweg.requests.Requester;
import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_NEW_SEGMENT_ENABLED;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS;
@ -38,6 +41,7 @@ import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
import static pl.jakubweg.SponsorBlockSettings.showTimeWithoutSegments;
import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically;
import static pl.jakubweg.SponsorBlockSettings.uuid;
import static pl.jakubweg.SponsorBlockUtils.formatColorString;
import static pl.jakubweg.StringRef.str;
@SuppressWarnings({"unused", "deprecation"}) // injected
@ -162,17 +166,45 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
preference.setEntries(entries);
preference.setEntryValues(entryValues);
EditTextPreference colorPreference = new EditTextPreference(context);
colorPreference.setTitle("Set " + segmentInfo.title + " color");
colorPreference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
colorPreference.setKey(segmentInfo.key + "_color");
colorPreference.setDefaultValue(segmentInfo.color);
String key = segmentInfo.key + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX;
Preference colorPreference = new Preference(context);
colorPreference.setTitle(str("color_change"));
colorPreference.setOnPreferenceClickListener(preference1 -> {
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"), (dialog, which) -> {
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"), (dialog, which) -> {
int defaultColor = segmentInfo.defaultColor;
segmentInfo.setColor(defaultColor);
Toast.makeText(applicationContext, str("color_reset"), Toast.LENGTH_SHORT).show();
preferences.edit().putString(key, formatColorString(defaultColor)).apply();
})
.show();
return true;
});
preferencesToDisableWhenSBDisabled.add(colorPreference);
category.addPreference(preference);
category.addPreference(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)
@ -111,9 +116,6 @@ public class SponsorBlockSettings {
segment.behaviour = behaviour;
if (behaviour.showOnTimeBar)
enabledCategories.add(segment.key);
String tmp = preferences.getString(segment.key + "_color", String.valueOf(segment.color));
segment.setColor(Integer.parseInt(tmp));
}
//"[%22sponsor%22,%22outro%22,%22music_offtopic%22,%22intro%22,%22selfpromo%22,%22interaction%22,%22preview%22]";
@ -195,23 +197,24 @@ public class SponsorBlockSettings {
public final StringRef skipMessage;
public final StringRef description;
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;
this.defaultColor = defaultColor;
this.color = defaultColor;
this.paint = new Paint();
}
@ -224,15 +227,14 @@ public class SponsorBlockSettings {
}
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

@ -444,6 +444,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,13 @@
<string name="skip_showbutton">Show a skip button</string>
<string name="skip_ignore">Don\'t do anything</string>
<string name="color_change">Change color</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="reset">Reset</string>
<string name="change">Change</string>
<string name="stats">Stats</string>
<string name="stats_loading">Loading..</string>
<string name="stats_sb_disabled">SponsorBlock is disabled</string>