Added guidelines alert and preference entry

This commit is contained in:
jakweg 2020-09-02 19:31:17 +02:00
parent 5614a94dd7
commit a466463e53
3 changed files with 71 additions and 4 deletions

View File

@ -1,7 +1,9 @@
package pl.jakubweg; package pl.jakubweg;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
@ -29,6 +31,8 @@ import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_UUID;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_NAME; import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_NAME;
import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis; import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis;
import static pl.jakubweg.SponsorBlockSettings.countSkips; import static pl.jakubweg.SponsorBlockSettings.countSkips;
import static pl.jakubweg.SponsorBlockSettings.getPreferences;
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically; import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically;
import static pl.jakubweg.SponsorBlockSettings.uuid; import static pl.jakubweg.SponsorBlockSettings.uuid;
import static pl.jakubweg.StringRef.str; import static pl.jakubweg.StringRef.str;
@ -46,7 +50,7 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
Activity context = this.getActivity(); final Activity context = this.getActivity();
PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(context); PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(context);
setPreferenceScreen(preferenceScreen); setPreferenceScreen(preferenceScreen);
@ -62,7 +66,8 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
enableCategoriesIfNeeded(((Boolean) newValue)); final boolean value = (Boolean) newValue;
enableCategoriesIfNeeded(value);
return true; return true;
} }
}); });
@ -77,6 +82,26 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
preference.setTitle(str("enable_segmadding")); preference.setTitle(str("enable_segmadding"));
preference.setSummary(str("enable_segmadding_sum")); preference.setSummary(str("enable_segmadding_sum"));
preferencesToDisableWhenSBDisabled.add(preference); preferencesToDisableWhenSBDisabled.add(preference);
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
final boolean value = (Boolean) o;
if (value && !SponsorBlockSettings.seenGuidelinesPopup) {
new AlertDialog.Builder(preference.getContext())
.setTitle(str("sb_guidelines_popup_title"))
.setMessage(str("sb_guidelines_popup_content"))
.setNegativeButton(str("sb_guidelines_popup_already_read"), null)
.setPositiveButton(str("sb_guidelines_popup_open"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
openGuidelines();
}
})
.show();
}
return true;
}
});
} }
addGeneralCategory(context, preferenceScreen); addGeneralCategory(context, preferenceScreen);
@ -86,6 +111,15 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
enableCategoriesIfNeeded(SponsorBlockSettings.isSponsorBlockEnabled); enableCategoriesIfNeeded(SponsorBlockSettings.isSponsorBlockEnabled);
} }
private void openGuidelines() {
final Context context = getActivity();
setSeenGuidelines(context);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://github.com/ajayyy/SponsorBlock/wiki/Guidelines"));
context.startActivity(intent);
}
private void enableCategoriesIfNeeded(boolean enabled) { private void enableCategoriesIfNeeded(boolean enabled) {
for (Preference preference : preferencesToDisableWhenSBDisabled) for (Preference preference : preferencesToDisableWhenSBDisabled)
preference.setEnabled(enabled); preference.setEnabled(enabled);
@ -161,6 +195,20 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
screen.addPreference(category); screen.addPreference(category);
category.setTitle(str("general")); category.setTitle(str("general"));
{
Preference preference = new Preference(context);
preference.setTitle(str("sb_guidelines_preference_title"));
preference.setSummary(str("sb_guidelines_preference_sum"));
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
openGuidelines();
return false;
}
});
screen.addPreference(preference);
}
{ {
Preference preference = new SwitchPreference(context); Preference preference = new SwitchPreference(context);
preference.setTitle(str("general_skiptoast")); preference.setTitle(str("general_skiptoast"));

View File

@ -17,13 +17,13 @@ import static pl.jakubweg.StringRef.sf;
public class SponsorBlockSettings { public class SponsorBlockSettings {
public static final String CACHE_DIRECTORY_NAME = "sponsor-block-segments-1";
public static final String PREFERENCES_NAME = "sponsor-block"; public static final String PREFERENCES_NAME = "sponsor-block";
public static final String PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP = "show-toast"; public static final String PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP = "show-toast";
public static final String PREFERENCES_KEY_COUNT_SKIPS = "count-skips"; public static final String PREFERENCES_KEY_COUNT_SKIPS = "count-skips";
public static final String PREFERENCES_KEY_UUID = "uuid"; public static final String PREFERENCES_KEY_UUID = "uuid";
public static final String PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP = "new-segment-step-accuracy"; public static final String PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP = "new-segment-step-accuracy";
public static final String PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED = "sb-enabled"; public static final String PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED = "sb-enabled";
public static final String PREFERENCES_KEY_SEEN_GUIDELINES = "sb-seen-gl";
public static final String PREFERENCES_KEY_NEW_SEGMENT_ENABLED = "sb-new-segment-enabled"; public static final String PREFERENCES_KEY_NEW_SEGMENT_ENABLED = "sb-new-segment-enabled";
public static final String sponsorBlockSkipSegmentsUrl = "https://sponsor.ajay.app/api/skipSegments"; public static final String sponsorBlockSkipSegmentsUrl = "https://sponsor.ajay.app/api/skipSegments";
public static final String sponsorBlockViewedUrl = "https://sponsor.ajay.app/api/viewedVideoSponsorTime"; public static final String sponsorBlockViewedUrl = "https://sponsor.ajay.app/api/viewedVideoSponsorTime";
@ -32,6 +32,7 @@ public class SponsorBlockSettings {
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SkipAutomatically; public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SkipAutomatically;
public static boolean isSponsorBlockEnabled = false; public static boolean isSponsorBlockEnabled = false;
public static boolean seenGuidelinesPopup = false;
public static boolean isAddNewSegmentEnabled = false; public static boolean isAddNewSegmentEnabled = false;
public static boolean showToastWhenSkippedAutomatically = true; public static boolean showToastWhenSkippedAutomatically = true;
public static boolean countSkips = true; public static boolean countSkips = true;
@ -53,11 +54,22 @@ public class SponsorBlockSettings {
return sponsorBlockViewedUrl + "?UUID=" + UUID; return sponsorBlockViewedUrl + "?UUID=" + UUID;
} }
public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public static void setSeenGuidelines(Context context) {
SponsorBlockSettings.seenGuidelinesPopup = true;
getPreferences(context).edit().putBoolean(PREFERENCES_KEY_SEEN_GUIDELINES, true).apply();
}
public static void update(Context context) { public static void update(Context context) {
if (context == null) return; if (context == null) return;
SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); SharedPreferences preferences = getPreferences(context);
isSponsorBlockEnabled = preferences.getBoolean(PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED, isSponsorBlockEnabled); isSponsorBlockEnabled = preferences.getBoolean(PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED, isSponsorBlockEnabled);
seenGuidelinesPopup = preferences.getBoolean(PREFERENCES_KEY_SEEN_GUIDELINES, seenGuidelinesPopup);
if (!isSponsorBlockEnabled) { if (!isSponsorBlockEnabled) {
SkipSegmentView.hide(); SkipSegmentView.hide();
NewSegmentHelperLayout.hide(); NewSegmentHelperLayout.hide();

View File

@ -207,6 +207,13 @@
<string name="new_segment_edit_by_hand_saved">Done</string> <string name="new_segment_edit_by_hand_saved">Done</string>
<string name="new_segment_edit_by_hand_parse_error">Cannot parse this time 😔</string> <string name="new_segment_edit_by_hand_parse_error">Cannot parse this time 😔</string>
<string name="sb_guidelines_preference_title">View guidelines</string>
<string name="sb_guidelines_preference_sum">Guidelines contain tips about submitting a segment</string>
<string name="sb_guidelines_popup_title">There are guidelines</string>
<string name="sb_guidelines_popup_content">It\'s recommended to read the Sponsor Block guidelines before submitting any segment</string>
<string name="sb_guidelines_popup_already_read">Already read</string>
<string name="sb_guidelines_popup_open">Show me</string>
<string name="app_name" /> <string name="app_name" />
</resources> </resources>