From ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 17 Sep 2019 15:04:37 +0800 Subject: Unbluring NSFW images is now an option in settings. --- .../main/java/Settings/MainPreferenceFragment.java | 95 +++++++++++++++------- .../infinityforreddit/AppComponent.java | 2 + .../infinityforreddit/ChangeNSFWBlurEvent.java | 8 ++ .../infinityforreddit/ChangeNSFWEvent.java | 2 +- .../CommentAndPostRecyclerViewAdapter.java | 10 ++- .../infinityforreddit/PostFragment.java | 32 +++++++- .../infinityforreddit/PostRecyclerViewAdapter.java | 13 +-- .../infinityforreddit/SharedPreferencesUtils.java | 1 + .../infinityforreddit/ViewPostDetailActivity.java | 27 +++++- 9 files changed, 145 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWBlurEvent.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/Settings/MainPreferenceFragment.java b/app/src/main/java/Settings/MainPreferenceFragment.java index fbe97347..11093bf6 100644 --- a/app/src/main/java/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/Settings/MainPreferenceFragment.java @@ -1,6 +1,8 @@ package Settings; +import android.app.Activity; +import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; @@ -11,7 +13,11 @@ import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; +import javax.inject.Inject; + +import ml.docilealligator.infinityforreddit.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.ChangeNSFWEvent; +import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; @@ -25,47 +31,74 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; */ public class MainPreferenceFragment extends PreferenceFragmentCompat { + @Inject + SharedPreferences sharedPreferences; + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.main_preferences, rootKey); - SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); - ListPreference listPreference = findPreference(SharedPreferencesUtils.THEME_KEY); + Activity activity = getActivity(); + if(activity != null) { + ((Infinity) activity.getApplication()).getAppComponent().inject(this); - if(nsfwSwitch != null) { - nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); - return true; - }); - } + SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); + SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); + ListPreference listPreference = findPreference(SharedPreferencesUtils.THEME_KEY); + + if(nsfwSwitch != null) { + nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); + if(blurNSFWSwitch != null) { + blurNSFWSwitch.setVisible((Boolean) newValue); + } + return true; + }); + } - boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + if(blurNSFWSwitch != null) { + boolean nsfwEnabled = sharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); - if(listPreference != null) { - if(systemDefault) { - listPreference.setEntries(R.array.settings_theme_q); - } else { - listPreference.setEntries(R.array.settings_theme); + if(nsfwEnabled) { + blurNSFWSwitch.setVisible(true); + } else { + blurNSFWSwitch.setVisible(false); + } + + blurNSFWSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeNSFWBlurEvent((Boolean) newValue)); + return true; + }); } - listPreference.setOnPreferenceChangeListener((preference, newValue) -> { - int option = Integer.parseInt((String) newValue); - switch (option) { - case 0: - AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); - break; - case 1: - AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); - break; - case 2: - if(systemDefault) { - AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); - } else { - AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); - } + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + + if(listPreference != null) { + if(systemDefault) { + listPreference.setEntries(R.array.settings_theme_q); + } else { + listPreference.setEntries(R.array.settings_theme); } - return true; - }); + + listPreference.setOnPreferenceChangeListener((preference, newValue) -> { + int option = Integer.parseInt((String) newValue); + switch (option) { + case 0: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + break; + case 1: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + break; + case 2: + if(systemDefault) { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); + } else { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); + } + } + return true; + }); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 0c66bcf3..2e6617f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit; import javax.inject.Singleton; +import Settings.MainPreferenceFragment; import Settings.NotificationPreferenceFragment; import dagger.Component; @@ -41,4 +42,5 @@ public interface AppComponent { void inject(LinkResolverActivity linkResolverActivity); void inject(SearchActivity searchActivity); void inject(SettingsActivity settingsActivity); + void inject(MainPreferenceFragment mainPreferenceFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWBlurEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWBlurEvent.java new file mode 100644 index 00000000..506c1627 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWBlurEvent.java @@ -0,0 +1,8 @@ +package ml.docilealligator.infinityforreddit; + +public class ChangeNSFWBlurEvent { + public boolean needBlurNSFW; + public ChangeNSFWBlurEvent(boolean needBlurNSFW) { + this.needBlurNSFW = needBlurNSFW; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java index ee3b182b..5d4b5a5c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java @@ -1,7 +1,7 @@ package ml.docilealligator.infinityforreddit; public class ChangeNSFWEvent { - boolean nsfw; + public boolean nsfw; public ChangeNSFWEvent(boolean nsfw) { this.nsfw = nsfw; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 28249f40..158e48a0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -78,6 +78,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 0) { + mPostRecyclerView.scrollToPosition(previousPosition); + } + } + @Override public void onStart() { super.onStart(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 04a81c87..e671bda5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -40,7 +40,6 @@ import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; import CustomView.AspectRatioGifImageView; -import SubredditDatabase.SubredditDao; import User.UserDao; import butterknife.BindView; import butterknife.ButterKnife; @@ -59,11 +58,11 @@ class PostRecyclerViewAdapter extends PagedListAdapter 0) { + mRecyclerView.scrollToPosition(previousPosition); + } + } + @Subscribe public void onAccountSwitchEvent(SwitchAccountEvent event) { if(!getClass().getName().equals(event.excludeActivityClassName)) { -- cgit v1.2.3