From 396501e350b23667bb70728fbd60b715beffe489 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 23 Sep 2020 19:20:41 +0800 Subject: Set NSFW, NSFW blurring and spoiler blurring on a per-user basis. Only close the account section in the navigation drawer after dismissing if the account section is open. --- .../infinityforreddit/Activity/MainActivity.java | 14 ++- .../Activity/SearchResultActivity.java | 2 +- .../Activity/SettingsActivity.java | 3 + .../Activity/ViewPostDetailActivity.java | 8 +- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 7 +- .../NavigationDrawerRecyclerViewAdapter.java | 29 ++++-- .../Adapter/PostRecyclerViewAdapter.java | 9 +- .../infinityforreddit/AppComponent.java | 3 + .../infinityforreddit/AppModule.java | 7 +- .../infinityforreddit/Fragment/PostFragment.java | 25 +++-- .../Settings/AdvancedPreferenceFragment.java | 12 +++ .../Settings/MainPreferenceFragment.java | 38 ------- .../Settings/NsfwAndBlurringFragment.java | 112 +++++++++++++++++++++ .../Utils/SharedPreferencesUtils.java | 21 ++-- 14 files changed, 206 insertions(+), 84 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NsfwAndBlurringFragment.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index 6d78ad7b..1d05e6d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -176,6 +176,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Named("main_activity_tabs") SharedPreferences mMainActivityTabsSharedPreferences; @Inject + @Named("nsfw_and_spoiler") + SharedPreferences mNsfwAndSpoilerSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; @@ -264,8 +267,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void onDrawerClosed(View drawerView) { if (adapter != null) { - adapter.closeAccountSectionWithoutChangeIconResource(); - adapter.notifyItemChanged(0); + if (adapter.closeAccountSectionWithoutChangeIconResource(true)) { + adapter.notifyItemChanged(0); + } } } }); @@ -480,7 +484,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } adapter = new NavigationDrawerRecyclerViewAdapter(this, mSharedPreferences, - mCustomThemeWrapper, mAccountName, + mNsfwAndSpoilerSharedPreferences, mCustomThemeWrapper, mAccountName, mProfileImageUrl, mBannerImageUrl, mKarma, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() { @Override @@ -536,13 +540,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb break; case R.string.enable_nsfw: if (sectionsPagerAdapter != null) { - mSharedPreferences.edit().putBoolean(SharedPreferencesUtils.NSFW_KEY, true).apply(); + mNsfwAndSpoilerSharedPreferences.edit().putBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, true).apply(); sectionsPagerAdapter.changeNSFW(true); } break; case R.string.disable_nsfw: if (sectionsPagerAdapter != null) { - mSharedPreferences.edit().putBoolean(SharedPreferencesUtils.NSFW_KEY, false).apply(); + mNsfwAndSpoilerSharedPreferences.edit().putBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false).apply(); sectionsPagerAdapter.changeNSFW(false); } break; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java index 78f39e19..a246bae1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java @@ -238,7 +238,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect tab.setText(R.string.posts); break; case 1: - tab.setText(R.string.comments); + tab.setText(R.string.subreddits); break; case 2: tab.setText(R.string.users); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java index 07abf2d5..f1c296e3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java @@ -33,6 +33,7 @@ import ml.docilealligator.infinityforreddit.Settings.FontPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.GesturesAndButtonsPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.InterfacePreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment; +import ml.docilealligator.infinityforreddit.Settings.NsfwAndBlurringFragment; public class SettingsActivity extends BaseActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { @@ -159,6 +160,8 @@ public class SettingsActivity extends BaseActivity implements pref.getFragment()); if (fragment instanceof CustomizeMainPageTabsFragment) { args.putString(CustomizeMainPageTabsFragment.EXTRA_ACCOUNT_NAME, mAccountName); + } else if (fragment instanceof NsfwAndBlurringFragment) { + args.putString(NsfwAndBlurringFragment.EXTRA_ACCOUNT_NAME, mAccountName); } fragment.setArguments(args); fragment.setTargetFragment(caller, 0); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 475d0001..cc047de9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -195,6 +195,9 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS @Named("sort_type") SharedPreferences mSortTypeSharedPreferences; @Inject + @Named("nsfw_and_spoiler") + SharedPreferences mNsfwAndSpoilerSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject ExoCreator mExoCreator; @@ -623,7 +626,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mRedditDataRoomDatabase, mGlide, mWindowWidth, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, - isSingleCommentThreadMode, mSharedPreferences, mExoCreator, + isSingleCommentThreadMode, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mExoCreator, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -786,7 +789,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mRedditDataRoomDatabase, mGlide, mWindowWidth, mAccessToken, mAccountName, mPost, mLocale, - mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences, mExoCreator, + mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences, + mNsfwAndSpoilerSharedPreferences, mExoCreator, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 4874dd31..bc476b67 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -240,7 +240,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { mSharedPreferences.edit().clear().apply(); + mainActivityTabsSharedPreferences.edit().clear().apply(); + nsfwAndBlurringSharedPreferences.edit().clear().apply(); + Toast.makeText(activity, R.string.reset_all_settings_success, Toast.LENGTH_SHORT).show(); EventBus.getDefault().post(new RecreateActivityEvent()); }) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java index ce601cf7..9ab5a099 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java @@ -17,10 +17,7 @@ import org.greenrobot.eventbus.EventBus; import javax.inject.Inject; import javax.inject.Named; -import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; -import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSavePostFeedScrolledPositionEvent; -import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; @@ -47,9 +44,6 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { Preference securityPreference = findPreference(SharedPreferencesUtils.SECURITY); SwitchPreference savePostFeedScrolledPositionSwitch = findPreference(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION); SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT); - SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); - SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); - SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_KEY); if (savePostFeedScrolledPositionSwitch != null) { savePostFeedScrolledPositionSwitch.setOnPreferenceChangeListener((preference, newValue) -> { @@ -68,38 +62,6 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { }); } - if (nsfwSwitch != null) { - nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); - if (blurNSFWSwitch != null) { - blurNSFWSwitch.setVisible((Boolean) newValue); - } - return true; - }); - } - - if (blurNSFWSwitch != null) { - boolean nsfwEnabled = sharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); - - if (nsfwEnabled) { - blurNSFWSwitch.setVisible(true); - } else { - blurNSFWSwitch.setVisible(false); - } - - blurNSFWSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeNSFWBlurEvent((Boolean) newValue)); - return true; - }); - } - - if (blurSpoilerSwitch != null) { - blurSpoilerSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeSpoilerBlurEvent((Boolean) newValue)); - return true; - }); - } - BiometricManager biometricManager = BiometricManager.from(activity); if (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) != BiometricManager.BIOMETRIC_SUCCESS) { if (securityPreference != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NsfwAndBlurringFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NsfwAndBlurringFragment.java new file mode 100644 index 00000000..470d10d5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NsfwAndBlurringFragment.java @@ -0,0 +1,112 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; + +import com.google.android.material.switchmaterial.SwitchMaterial; + +import org.greenrobot.eventbus.EventBus; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class NsfwAndBlurringFragment extends Fragment { + + public static final String EXTRA_ACCOUNT_NAME = "EAN"; + + @BindView(R.id.enable_nsfw_linear_layout_nsfw_and_spoiler_fragment) + LinearLayout enableNsfwLinearLayout; + @BindView(R.id.enable_nsfw_switch_nsfw_and_spoiler_fragment) + SwitchMaterial enableNsfwSwitchMaterial; + @BindView(R.id.blur_nsfw_linear_layout_nsfw_and_spoiler_fragment) + LinearLayout blurNsfwLinearLayout; + @BindView(R.id.blur_nsfw_switch_nsfw_and_spoiler_fragment) + SwitchMaterial blurNsfwSwitchMaterial; + @BindView(R.id.blur_spoiler_linear_layout_nsfw_and_spoiler_fragment) + LinearLayout blurSpoilerLinearLayout; + @BindView(R.id.blur_spoiler_switch_nsfw_and_spoiler_fragment) + SwitchMaterial blurSpoilerSwitchMaterial; + @Inject + @Named("nsfw_and_spoiler") + SharedPreferences nsfwAndBlurringSharedPreferences; + + private Activity activity; + + public NsfwAndBlurringFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_nsfw_and_spoiler, container, false); + + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + + ButterKnife.bind(this, rootView); + + String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); + + boolean enableNsfw = nsfwAndBlurringSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); + boolean blurNsfw = nsfwAndBlurringSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.BLUR_NSFW_BASE, true); + boolean blurSpoiler = nsfwAndBlurringSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.BLUR_SPOILER_BASE, false); + + if (enableNsfw) { + blurNsfwLinearLayout.setVisibility(View.VISIBLE); + } + + enableNsfwSwitchMaterial.setChecked(enableNsfw); + blurNsfwSwitchMaterial.setChecked(blurNsfw); + blurSpoilerSwitchMaterial.setChecked(blurSpoiler); + + enableNsfwLinearLayout.setOnClickListener(view -> enableNsfwSwitchMaterial.performClick()); + enableNsfwSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> { + nsfwAndBlurringSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, b).apply(); + if (b) { + blurNsfwLinearLayout.setVisibility(View.VISIBLE); + } else { + blurNsfwLinearLayout.setVisibility(View.GONE); + } + EventBus.getDefault().post(new ChangeNSFWEvent(b)); + }); + + blurNsfwLinearLayout.setOnClickListener(view -> blurNsfwSwitchMaterial.performClick()); + blurNsfwSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> { + nsfwAndBlurringSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.BLUR_NSFW_BASE, b).apply(); + EventBus.getDefault().post(new ChangeNSFWBlurEvent(b)); + }); + + blurSpoilerLinearLayout.setOnClickListener(view -> blurSpoilerSwitchMaterial.performClick()); + blurSpoilerSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> { + nsfwAndBlurringSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.BLUR_SPOILER_BASE, b).apply(); + EventBus.getDefault().post(new ChangeSpoilerBlurEvent(b)); + }); + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + this.activity = (AppCompatActivity) context; + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index 351088c3..598c3d98 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -8,9 +8,6 @@ public class SharedPreferencesUtils { public static final String ENABLE_NOTIFICATION_KEY = "enable_notification"; public static final String NOTIFICATION_INTERVAL_KEY = "notificaiton_interval"; public static final String LAZY_MODE_INTERVAL_KEY = "lazy_mode_interval"; - public static final String NSFW_KEY = "nsfw"; - public static final String BLUR_NSFW_KEY = "blur_nsfw"; - public static final String BLUR_SPOILER_KEY = "blur_spoiler"; public static final String THEME_KEY = "theme"; public static final String ICON_FOREGROUND_KEY = "icon_foreground"; public static final String ICON_BACKGROUND_KEY = "icon_background"; @@ -127,6 +124,11 @@ public class SharedPreferencesUtils { public static final String LONG_PRESS_TO_HIDE_TOOLBAR_IN_COMPACT_LAYOUT = "long_press_to_hide_toolbar_in_compact_layout"; public static final String POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT = "post_compact_layout_toolbar_hidden_by_default"; public static final String SECURITY = "security"; + public static final String START_AUTOPLAY_VISIBLE_AREA_OFFSET_PORTRAIT = "start_autoplay_visible_area_offset_portrait"; + public static final String START_AUTOPLAY_VISIBLE_AREA_OFFSET_LANDSCAPE = "start_autoplay_visible_area_offset_landscape"; + public static final String MUTE_NSFW_VIDEO = "mute_nsfw_video"; + public static final String VIDEO_PLAYER_IGNORE_NAV_BAR = "video_player_ignore_nav_bar"; + public static final String SAVE_FRONT_PAGE_SCROLLED_POSITION = "save_front_page_scrolled_position"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title"; @@ -145,11 +147,10 @@ public class SharedPreferencesUtils { public static final int MAIN_PAGE_TAB_POST_TYPE_MULTIREDDIT = 4; public static final int MAIN_PAGE_TAB_POST_TYPE_USER = 5; - public static final String START_AUTOPLAY_VISIBLE_AREA_OFFSET_PORTRAIT = "start_autoplay_visible_area_offset_portrait"; - public static final String START_AUTOPLAY_VISIBLE_AREA_OFFSET_LANDSCAPE = "start_autoplay_visible_area_offset_landscape"; - public static final String MUTE_NSFW_VIDEO = "mute_nsfw_video"; - public static final String VIDEO_PLAYER_IGNORE_NAV_BAR = "video_player_ignore_nav_bar"; - public static final String SAVE_FRONT_PAGE_SCROLLED_POSITION = "save_front_page_scrolled_position"; + public static final String NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.nsfw_and_spoiler"; + public static final String NSFW_BASE = "_nsfw"; + public static final String BLUR_NSFW_BASE = "_blur_nsfw"; + public static final String BLUR_SPOILER_BASE = "_blur_spoiler"; //Legacy Settings public static final String MAIN_PAGE_TAB_1_TITLE_LEGACY = "main_page_tab_1_title"; @@ -169,4 +170,8 @@ public class SharedPreferencesUtils { public static final String POST_LAYOUT_POPULAR_POST_LEGACY = "post_layout_popular_post"; public static final String POST_LAYOUT_ALL_POST_LEGACY = "post_layout_all_post"; + + public static final String NSFW_KEY_LEGACY = "nsfw"; + public static final String BLUR_NSFW_KEY_LEGACY = "blur_nsfw"; + public static final String BLUR_SPOILER_KEY_LEGACY = "blur_spoiler"; } -- cgit v1.2.3