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;
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);
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);
if(nsfwSwitch != null) {
nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue));
if(blurNSFWSwitch != null) {
blurNSFWSwitch.setVisible((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);
}
if(blurNSFWSwitch != null) {
boolean nsfwEnabled = sharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);
if(nsfwEnabled) {
blurNSFWSwitch.setVisible(true);
} else {
blurNSFWSwitch.setVisible(false);
}
return true;
});
blurNSFWSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new ChangeNSFWBlurEvent((Boolean) newValue));
return true;
});
}
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);
}
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;
});
}
}
}
}

View File

@ -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);
}

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;
public class ChangeNSFWEvent {
boolean nsfw;
public boolean nsfw;
public ChangeNSFWEvent(boolean nsfw) {
this.nsfw = nsfw;
}

View File

@ -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) {

View File

@ -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();

View File

@ -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;
}

View File

@ -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";

View File

@ -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) {
@ -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
public void onAccountSwitchEvent(SwitchAccountEvent event) {
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_lazy_mode_interval_title">Lazy Mode Interval</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_acknowledgement_master_title">Acknowledgement</string>
<string name="settings_credits_master_title">Credits</string>

View File

@ -29,6 +29,12 @@
app:key="nsfw"
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
app:title="@string/settings_about_master_title"
app:fragment="Settings.AboutPreferenceFragment" />