Unbluring NSFW images is now an option in settings.

This commit is contained in:
Alex Ning 2019-09-17 15:04:37 +08:00
parent 3592dc29da
commit ee5fc0a5fc
11 changed files with 154 additions and 47 deletions

View File

@ -1,6 +1,8 @@
package Settings; package Settings;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -11,7 +13,11 @@ import androidx.preference.SwitchPreference;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject;
import ml.docilealligator.infinityforreddit.ChangeNSFWBlurEvent;
import ml.docilealligator.infinityforreddit.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.ChangeNSFWEvent;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.SharedPreferencesUtils;
@ -25,16 +31,42 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
*/ */
public class MainPreferenceFragment extends PreferenceFragmentCompat { public class MainPreferenceFragment extends PreferenceFragmentCompat {
@Inject
SharedPreferences sharedPreferences;
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.main_preferences, rootKey); setPreferencesFromResource(R.xml.main_preferences, rootKey);
Activity activity = getActivity();
if(activity != null) {
((Infinity) activity.getApplication()).getAppComponent().inject(this);
SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY);
SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY);
ListPreference listPreference = findPreference(SharedPreferencesUtils.THEME_KEY); ListPreference listPreference = findPreference(SharedPreferencesUtils.THEME_KEY);
if(nsfwSwitch != null) { if(nsfwSwitch != null) {
nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) 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; return true;
}); });
} }
@ -68,4 +100,5 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat {
}); });
} }
} }
}
} }

View File

@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
import javax.inject.Singleton; import javax.inject.Singleton;
import Settings.MainPreferenceFragment;
import Settings.NotificationPreferenceFragment; import Settings.NotificationPreferenceFragment;
import dagger.Component; import dagger.Component;
@ -41,4 +42,5 @@ public interface AppComponent {
void inject(LinkResolverActivity linkResolverActivity); void inject(LinkResolverActivity linkResolverActivity);
void inject(SearchActivity searchActivity); void inject(SearchActivity searchActivity);
void inject(SettingsActivity settingsActivity); void inject(SettingsActivity settingsActivity);
void inject(MainPreferenceFragment mainPreferenceFragment);
} }

View File

@ -0,0 +1,8 @@
package ml.docilealligator.infinityforreddit;
public class ChangeNSFWBlurEvent {
public boolean needBlurNSFW;
public ChangeNSFWBlurEvent(boolean needBlurNSFW) {
this.needBlurNSFW = needBlurNSFW;
}
}

View File

@ -1,7 +1,7 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
public class ChangeNSFWEvent { public class ChangeNSFWEvent {
boolean nsfw; public boolean nsfw;
public ChangeNSFWEvent(boolean nsfw) { public ChangeNSFWEvent(boolean nsfw) {
this.nsfw = nsfw; this.nsfw = nsfw;
} }

View File

@ -78,6 +78,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
private Locale mLocale; private Locale mLocale;
private String mSingleCommentId; private String mSingleCommentId;
private boolean mIsSingleCommentThreadMode; private boolean mIsSingleCommentThreadMode;
private boolean mNeedBlurNSFW;
private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback; private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback;
private boolean isInitiallyLoading; private boolean isInitiallyLoading;
private boolean isInitiallyLoadingFailed; private boolean isInitiallyLoadingFailed;
@ -94,7 +95,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide, RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide,
String accessToken, String accountName, Post post, String accessToken, String accountName, Post post,
Locale locale, String singleCommentId, boolean isSingleCommentThreadMode, Locale locale, String singleCommentId, boolean isSingleCommentThreadMode,
CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { boolean needBlurNSFW, CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) {
mActivity = activity; mActivity = activity;
mRetrofit = retrofit; mRetrofit = retrofit;
mOauthRetrofit = oauthRetrofit; mOauthRetrofit = oauthRetrofit;
@ -127,6 +128,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
mLocale = locale; mLocale = locale;
mSingleCommentId = singleCommentId; mSingleCommentId = singleCommentId;
mIsSingleCommentThreadMode = isSingleCommentThreadMode; mIsSingleCommentThreadMode = isSingleCommentThreadMode;
mNeedBlurNSFW = needBlurNSFW;
mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback; mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback;
isInitiallyLoading = true; isInitiallyLoading = true;
isInitiallyLoadingFailed = false; 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))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2)))
.into(holder.mImageView); .into(holder.mImageView);
} else { } else {
@ -977,6 +979,10 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
} }
} }
void setBlurNSFW(boolean needBlurNSFW) {
mNeedBlurNSFW = needBlurNSFW;
}
@Override @Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) { if (holder instanceof CommentViewHolder) {

View File

@ -258,6 +258,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
int filter = getArguments().getInt(EXTRA_FILTER); int filter = getArguments().getInt(EXTRA_FILTER);
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);
boolean needBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true);
PostViewModel.Factory factory; PostViewModel.Factory factory;
@ -266,7 +267,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
String query = getArguments().getString(EXTRA_QUERY); String query = getArguments().getString(EXTRA_QUERY);
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
@Override @Override
public void retryLoadingMore() { public void retryLoadingMore() {
mPostViewModel.retryLoadingMore(); mPostViewModel.retryLoadingMore();
@ -298,7 +299,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all")); boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all"));
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, displaySubredditName, new PostRecyclerViewAdapter.Callback() { accessToken, postType, displaySubredditName, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
@Override @Override
public void retryLoadingMore() { public void retryLoadingMore() {
mPostViewModel.retryLoadingMore(); mPostViewModel.retryLoadingMore();
@ -334,7 +335,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
@Override @Override
public void retryLoadingMore() { public void retryLoadingMore() {
mPostViewModel.retryLoadingMore(); mPostViewModel.retryLoadingMore();
@ -363,7 +364,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
} else { } else {
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
@Override @Override
public void retryLoadingMore() { public void retryLoadingMore() {
mPostViewModel.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 @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();

View File

@ -40,7 +40,6 @@ import com.libRG.CustomTextView;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import CustomView.AspectRatioGifImageView; import CustomView.AspectRatioGifImageView;
import SubredditDatabase.SubredditDao;
import User.UserDao; import User.UserDao;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -59,11 +58,11 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
private String mAccessToken; private String mAccessToken;
private RequestManager mGlide; private RequestManager mGlide;
private RedditDataRoomDatabase mRedditDataRoomDatabase; private RedditDataRoomDatabase mRedditDataRoomDatabase;
private SubredditDao mSubredditDao;
private UserDao mUserDao; private UserDao mUserDao;
private boolean canStartActivity = true; private boolean canStartActivity = true;
private int mPostType; private int mPostType;
private boolean mDisplaySubredditName; private boolean mDisplaySubredditName;
private boolean mNeedBlurNSFW;
private static final int VIEW_TYPE_DATA = 0; private static final int VIEW_TYPE_DATA = 0;
private static final int VIEW_TYPE_ERROR = 1; 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, PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, int postType, String accessToken, int postType,
boolean displaySubredditName, Callback callback) { boolean displaySubredditName, boolean needBlurNSFW, Callback callback) {
super(DIFF_CALLBACK); super(DIFF_CALLBACK);
if(context != null) { if(context != null) {
mContext = context; mContext = context;
@ -89,9 +88,9 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
mAccessToken = accessToken; mAccessToken = accessToken;
mPostType = postType; mPostType = postType;
mDisplaySubredditName = displaySubredditName; mDisplaySubredditName = displaySubredditName;
mNeedBlurNSFW = needBlurNSFW;
mGlide = Glide.with(mContext.getApplicationContext()); mGlide = Glide.with(mContext.getApplicationContext());
mRedditDataRoomDatabase = redditDataRoomDatabase; mRedditDataRoomDatabase = redditDataRoomDatabase;
mSubredditDao = redditDataRoomDatabase.subredditDao();
mUserDao = redditDataRoomDatabase.userDao(); mUserDao = redditDataRoomDatabase.userDao();
mCallback = callback; 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))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2)))
.into(((DataViewHolder) holder).imageView); .into(((DataViewHolder) holder).imageView);
} else { } else {
@ -682,6 +681,10 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
return super.getItemCount(); return super.getItemCount();
} }
void setBlurNSFW(boolean needBlurNSFW) {
mNeedBlurNSFW = needBlurNSFW;
}
private boolean hasExtraRow() { private boolean hasExtraRow() {
return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS; return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS;
} }

View File

@ -9,6 +9,7 @@ public class SharedPreferencesUtils {
public static final String NOTIFICATION_INTERVAL_KEY = "notificaiton_interval"; public static final String NOTIFICATION_INTERVAL_KEY = "notificaiton_interval";
public static final String LAZY_MODE_INTERVAL_KEY = "lazy_mode_interval"; public static final String LAZY_MODE_INTERVAL_KEY = "lazy_mode_interval";
public static final String NSFW_KEY = "nsfw"; 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 THEME_KEY = "theme";
public static final String ICON_FOREGROUND_KEY = "icon_foreground"; public static final String ICON_FOREGROUND_KEY = "icon_foreground";
public static final String ICON_BACKGROUND_KEY = "icon_background"; public static final String ICON_BACKGROUND_KEY = "icon_background";

View File

@ -79,6 +79,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
private int orientation; private int orientation;
private int postListPosition = -1; private int postListPosition = -1;
private String mSingleCommentId; private String mSingleCommentId;
private boolean mNeedBlurNsfw;
@State @State
boolean mNullAccessToken = false; boolean mNullAccessToken = false;
@ -216,6 +217,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
toolbar.setTitle(""); toolbar.setTitle("");
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
mNeedBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true);
mGlide = Glide.with(this); mGlide = Glide.with(this);
mLocale = getResources().getConfiguration().locale; mLocale = getResources().getConfiguration().locale;
@ -359,7 +362,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost,
mLocale, mSingleCommentId, isSingleCommentThreadMode, mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override @Override
public void updatePost(Post post) { public void updatePost(Post post) {
@ -471,7 +474,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this,
mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide,
mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId,
isSingleCommentThreadMode, isSingleCommentThreadMode, mNeedBlurNsfw,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override @Override
public void updatePost(Post post) { public void updatePost(Post post) {
@ -901,6 +904,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 @Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) { public void onAccountSwitchEvent(SwitchAccountEvent event) {
if(!getClass().getName().equals(event.excludeActivityClassName)) { if(!getClass().getName().equals(event.excludeActivityClassName)) {

View File

@ -273,7 +273,8 @@
<string name="settings_theme_system_default_summary">Device default</string> <string name="settings_theme_system_default_summary">Device default</string>
<string name="settings_lazy_mode_interval_title">Lazy Mode Interval</string> <string name="settings_lazy_mode_interval_title">Lazy Mode Interval</string>
<string name="settings_enable_nsfw_title">Enable NSFW</string> <string name="settings_enable_nsfw_title">Enable NSFW</string>
<string name="settings_layout_no_limits_title">Display under navigation bar and status bar</string> <string name="settings_blur_nsfw_title">Blur NSFW images</string>
<string name="settings_layout_no_limits_title">Display Under Navigation bar and Status Bar</string>
<string name="settings_about_master_title">About</string> <string name="settings_about_master_title">About</string>
<string name="settings_acknowledgement_master_title">Acknowledgement</string> <string name="settings_acknowledgement_master_title">Acknowledgement</string>
<string name="settings_credits_master_title">Credits</string> <string name="settings_credits_master_title">Credits</string>

View File

@ -29,6 +29,12 @@
app:key="nsfw" app:key="nsfw"
app:title="@string/settings_enable_nsfw_title" /> app:title="@string/settings_enable_nsfw_title" />
<SwitchPreference
app:defaultValue="true"
app:key="blur_nsfw"
app:title="@string/settings_blur_nsfw_title"
app:isPreferenceVisible="false" />
<Preference <Preference
app:title="@string/settings_about_master_title" app:title="@string/settings_about_master_title"
app:fragment="Settings.AboutPreferenceFragment" /> app:fragment="Settings.AboutPreferenceFragment" />