diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-09-17 07:04:37 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-09-17 07:04:37 +0000 |
commit | ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd (patch) | |
tree | 70eef13261988a993f4324d7e2cc17ba74d8e618 /app/src/main/java | |
parent | 3592dc29dabe1fbc41f4af5536e395987963c810 (diff) | |
download | infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.tar infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.tar.gz infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.tar.bz2 infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.tar.lz infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.tar.xz infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.tar.zst infinity-for-reddit-ee5fc0a5fc652c9e0b83b4f82ec80f1ec81380fd.zip |
Unbluring NSFW images is now an option in settings.
Diffstat (limited to '')
9 files changed, 145 insertions, 45 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; + }); + } } } } 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<RecyclerVie private Locale mLocale; private String mSingleCommentId; private boolean mIsSingleCommentThreadMode; + private boolean mNeedBlurNSFW; private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback; private boolean isInitiallyLoading; private boolean isInitiallyLoadingFailed; @@ -94,7 +95,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide, String accessToken, String accountName, Post post, Locale locale, String singleCommentId, boolean isSingleCommentThreadMode, - CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { + boolean needBlurNSFW, CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { mActivity = activity; mRetrofit = retrofit; mOauthRetrofit = oauthRetrofit; @@ -127,6 +128,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie mLocale = locale; mSingleCommentId = singleCommentId; mIsSingleCommentThreadMode = isSingleCommentThreadMode; + mNeedBlurNSFW = needBlurNSFW; mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback; isInitiallyLoading = true; isInitiallyLoadingFailed = false; @@ -761,7 +763,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie } }); - if(mPost.isNSFW()) { + if(mPost.isNSFW() && mNeedBlurNSFW) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2))) .into(holder.mImageView); } else { @@ -977,6 +979,10 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie } } + void setBlurNSFW(boolean needBlurNSFW) { + mNeedBlurNSFW = needBlurNSFW; + } + @Override public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { if (holder instanceof CommentViewHolder) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 2bb47053..67a0b247 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -258,6 +258,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { int filter = getArguments().getInt(EXTRA_FILTER); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); + boolean needBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true); PostViewModel.Factory factory; @@ -266,7 +267,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String query = getArguments().getString(EXTRA_QUERY); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { + accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -298,7 +299,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all")); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - accessToken, postType, displaySubredditName, new PostRecyclerViewAdapter.Callback() { + accessToken, postType, displaySubredditName, needBlurNsfw, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -334,7 +335,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { + accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -363,7 +364,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } else { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { + accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -553,6 +554,29 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Subscribe + public void onChangeNSFWBlurEvent(ChangeNSFWBlurEvent event) { + mAdapter.setBlurNSFW(event.needBlurNSFW); + + int previousPosition = -1; + if(mLinearLayoutManager != null) { + previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + } else if(mStaggeredGridLayoutManager != null) { + int[] into = new int[2]; + previousPosition = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]; + } + + RecyclerView.LayoutManager layoutManager = mPostRecyclerView.getLayoutManager(); + mPostRecyclerView.setAdapter(null); + mPostRecyclerView.setLayoutManager(null); + mPostRecyclerView.setAdapter(mAdapter); + mPostRecyclerView.setLayoutManager(layoutManager); + + if(previousPosition > 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<Post, RecyclerView.ViewHo private String mAccessToken; private RequestManager mGlide; private RedditDataRoomDatabase mRedditDataRoomDatabase; - private SubredditDao mSubredditDao; private UserDao mUserDao; private boolean canStartActivity = true; private int mPostType; private boolean mDisplaySubredditName; + private boolean mNeedBlurNSFW; private static final int VIEW_TYPE_DATA = 0; private static final int VIEW_TYPE_ERROR = 1; @@ -80,7 +79,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase, String accessToken, int postType, - boolean displaySubredditName, Callback callback) { + boolean displaySubredditName, boolean needBlurNSFW, Callback callback) { super(DIFF_CALLBACK); if(context != null) { mContext = context; @@ -89,9 +88,9 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo mAccessToken = accessToken; mPostType = postType; mDisplaySubredditName = displaySubredditName; + mNeedBlurNSFW = needBlurNSFW; mGlide = Glide.with(mContext.getApplicationContext()); mRedditDataRoomDatabase = redditDataRoomDatabase; - mSubredditDao = redditDataRoomDatabase.subredditDao(); mUserDao = redditDataRoomDatabase.userDao(); mCallback = callback; } @@ -652,7 +651,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo } }); - if(post.isNSFW()) { + if(post.isNSFW() && mNeedBlurNSFW) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2))) .into(((DataViewHolder) holder).imageView); } else { @@ -682,6 +681,10 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo return super.getItemCount(); } + void setBlurNSFW(boolean needBlurNSFW) { + mNeedBlurNSFW = needBlurNSFW; + } + private boolean hasExtraRow() { return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java index 6c4ca027..e3103796 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java @@ -9,6 +9,7 @@ public class SharedPreferencesUtils { 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 THEME_KEY = "theme"; public static final String ICON_FOREGROUND_KEY = "icon_foreground"; public static final String ICON_BACKGROUND_KEY = "icon_background"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 04e013fe..3063e0c0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -79,6 +79,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo private int orientation; private int postListPosition = -1; private String mSingleCommentId; + private boolean mNeedBlurNsfw; @State boolean mNullAccessToken = false; @@ -216,6 +217,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo toolbar.setTitle(""); setSupportActionBar(toolbar); + mNeedBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true); + mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; @@ -359,7 +362,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, - mLocale, mSingleCommentId, isSingleCommentThreadMode, + mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -471,7 +474,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, - isSingleCommentThreadMode, + isSingleCommentThreadMode, mNeedBlurNsfw, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -902,6 +905,26 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } @Subscribe + public void onChangeNSFWBlurEvent(ChangeNSFWBlurEvent event) { + mAdapter.setBlurNSFW(event.needBlurNSFW); + + int previousPosition = -1; + if(mLinearLayoutManager != null) { + previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + } + + RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); + mRecyclerView.setAdapter(null); + mRecyclerView.setLayoutManager(null); + mRecyclerView.setAdapter(mAdapter); + mRecyclerView.setLayoutManager(layoutManager); + + if(previousPosition > 0) { + mRecyclerView.scrollToPosition(previousPosition); + } + } + + @Subscribe public void onAccountSwitchEvent(SwitchAccountEvent event) { if(!getClass().getName().equals(event.excludeActivityClassName)) { finish(); |