From 29bea3b1c69958827db17d28878746a65d978497 Mon Sep 17 00:00:00 2001 From: jakweg Date: Wed, 2 Sep 2020 19:31:17 +0200 Subject: [PATCH] Added guidelines alert and preference entry --- .../SponsorBlockPreferenceFragment.java | 52 ++++++++++++++++++- .../pl/jakubweg/SponsorBlockSettings.java | 16 +++++- integrations/res/values/strings.xml | 7 +++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/integrations/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/integrations/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index 7b198611cf..faaf886a83 100644 --- a/integrations/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/integrations/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -1,7 +1,9 @@ package pl.jakubweg; import android.app.Activity; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; 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.adjustNewSegmentMillis; 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.uuid; import static pl.jakubweg.StringRef.str; @@ -46,7 +50,7 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); - Activity context = this.getActivity(); + final Activity context = this.getActivity(); PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(context); setPreferenceScreen(preferenceScreen); @@ -62,7 +66,8 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - enableCategoriesIfNeeded(((Boolean) newValue)); + final boolean value = (Boolean) newValue; + enableCategoriesIfNeeded(value); return true; } }); @@ -77,6 +82,26 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement preference.setTitle(str("enable_segmadding")); preference.setSummary(str("enable_segmadding_sum")); 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); @@ -86,6 +111,15 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement 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) { for (Preference preference : preferencesToDisableWhenSBDisabled) preference.setEnabled(enabled); @@ -161,6 +195,20 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement screen.addPreference(category); 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.setTitle(str("general_skiptoast")); diff --git a/integrations/java/pl/jakubweg/SponsorBlockSettings.java b/integrations/java/pl/jakubweg/SponsorBlockSettings.java index 71f9f7c279..24ef9d5e2d 100644 --- a/integrations/java/pl/jakubweg/SponsorBlockSettings.java +++ b/integrations/java/pl/jakubweg/SponsorBlockSettings.java @@ -17,13 +17,13 @@ import static pl.jakubweg.StringRef.sf; 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_KEY_SHOW_TOAST_WHEN_SKIP = "show-toast"; public static final String PREFERENCES_KEY_COUNT_SKIPS = "count-skips"; 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_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 sponsorBlockSkipSegmentsUrl = "https://sponsor.ajay.app/api/skipSegments"; 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 boolean isSponsorBlockEnabled = false; + public static boolean seenGuidelinesPopup = false; public static boolean isAddNewSegmentEnabled = false; public static boolean showToastWhenSkippedAutomatically = true; public static boolean countSkips = true; @@ -53,11 +54,22 @@ public class SponsorBlockSettings { 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) { 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); + seenGuidelinesPopup = preferences.getBoolean(PREFERENCES_KEY_SEEN_GUIDELINES, seenGuidelinesPopup); + if (!isSponsorBlockEnabled) { SkipSegmentView.hide(); NewSegmentHelperLayout.hide(); diff --git a/integrations/res/values/strings.xml b/integrations/res/values/strings.xml index 8cab44b18d..05de37042a 100644 --- a/integrations/res/values/strings.xml +++ b/integrations/res/values/strings.xml @@ -207,6 +207,13 @@ Done Cannot parse this time 😔 + View guidelines + Guidelines contain tips about submitting a segment + There are guidelines + It\'s recommended to read the Sponsor Block guidelines before submitting any segment + Already read + Show me +