diff options
Diffstat (limited to 'app/src/main/java/Settings/MainPreferenceFragment.java')
-rw-r--r-- | app/src/main/java/Settings/MainPreferenceFragment.java | 95 |
1 files changed, 64 insertions, 31 deletions
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; + }); + } } } } |