Skip to content

Commit

Permalink
Blurring spoiler post images is now available. Change spoiler and fla…
Browse files Browse the repository at this point in the history
…ir text background colors. Fixed collapsed comments shown after orientation change.
  • Loading branch information
Docile-Alligator committed Sep 27, 2019
1 parent edf85c2 commit 47832e1
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Binary file modified .idea/caches/gradle_models.ser
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "ml.docilealligator.infinityforreddit"
minSdkVersion 21
targetSdkVersion 29
versionCode 12
versionName "1.1.1"
versionCode 13
versionName "1.1.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/Settings/MainPreferenceFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import ml.docilealligator.infinityforreddit.ChangeNSFWBlurEvent;
import ml.docilealligator.infinityforreddit.ChangeNSFWEvent;
import ml.docilealligator.infinityforreddit.ChangeSpoilerBlurEvent;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SharedPreferencesUtils;
Expand Down Expand Up @@ -44,6 +45,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY);
SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY);
SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_KEY);
ListPreference themePreference = findPreference(SharedPreferencesUtils.THEME_KEY);

if(nsfwSwitch != null) {
Expand Down Expand Up @@ -71,6 +73,13 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
});
}

if(blurSpoilerSwitch != null) {
blurSpoilerSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new ChangeSpoilerBlurEvent((Boolean) newValue));
return true;
});
}

boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;

if(themePreference != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ml.docilealligator.infinityforreddit;

public class ChangeSpoilerBlurEvent {
public boolean needBlurSpoiler;
public ChangeSpoilerBlurEvent(boolean needBlurSpoiler) {
this.needBlurSpoiler = needBlurSpoiler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import retrofit2.Retrofit;

import static ml.docilealligator.infinityforreddit.CommentActivity.WRITE_COMMENT_REQUEST_CODE;

class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_POST_DETAIL = 0;
private static final int VIEW_TYPE_FIRST_LOADING = 1;
Expand All @@ -78,6 +80,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
private String mSingleCommentId;
private boolean mIsSingleCommentThreadMode;
private boolean mNeedBlurNSFW;
private boolean mNeedBlurSpoiler;
private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback;
private boolean isInitiallyLoading;
private boolean isInitiallyLoadingFailed;
Expand All @@ -92,9 +95,10 @@ interface CommentRecyclerViewAdapterCallback {

CommentAndPostRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit,
RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide,
String accessToken, String accountName, Post post,
Locale locale, String singleCommentId, boolean isSingleCommentThreadMode,
boolean needBlurNSFW, CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) {
String accessToken, String accountName, Post post, Locale locale,
String singleCommentId, boolean isSingleCommentThreadMode,
boolean needBlurNSFW, boolean needBlurSpoiler,
CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) {
mActivity = activity;
mRetrofit = retrofit;
mOauthRetrofit = oauthRetrofit;
Expand Down Expand Up @@ -128,6 +132,7 @@ public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder
mSingleCommentId = singleCommentId;
mIsSingleCommentThreadMode = isSingleCommentThreadMode;
mNeedBlurNSFW = needBlurNSFW;
mNeedBlurSpoiler = needBlurSpoiler;
mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback;
isInitiallyLoading = true;
isInitiallyLoadingFailed = false;
Expand Down Expand Up @@ -353,15 +358,11 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi

if(mPost.isSpoiler()) {
((PostDetailViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).spoilerTextView
.setBackgroundColor(mActivity.getResources().getColor(R.color.backgroundColorPrimaryDark));
}

if(mPost.getFlair() != null) {
((PostDetailViewHolder) holder).flairTextView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).flairTextView.setText(mPost.getFlair());
((PostDetailViewHolder) holder).flairTextView
.setBackgroundColor(mActivity.getResources().getColor(R.color.backgroundColorPrimaryDark));
}

if(mPost.isNSFW()) {
Expand Down Expand Up @@ -493,7 +494,7 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false);
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0);
mActivity.startActivity(intent);
mActivity.startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE);
});

((PostDetailViewHolder) holder).commentsCountTextView.setText(Integer.toString(mPost.getnComments()));
Expand Down Expand Up @@ -942,7 +943,7 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
}
});

if(mPost.isNSFW() && mNeedBlurNSFW) {
if((mPost.isNSFW() && mNeedBlurNSFW) || (mPost.isSpoiler() && mNeedBlurSpoiler)) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2)))
.into(holder.mImageView);
} else {
Expand Down Expand Up @@ -1093,6 +1094,10 @@ void setSingleComment(String singleCommentId, boolean isSingleCommentThreadMode)
mIsSingleCommentThreadMode = isSingleCommentThreadMode;
}

ArrayList<CommentData> getVisibleComments() {
return mVisibleComments;
}

void initiallyLoading() {
if(mVisibleComments.size() != 0) {
int previousSize = mVisibleComments.size();
Expand Down Expand Up @@ -1162,6 +1167,10 @@ void setBlurNSFW(boolean needBlurNSFW) {
mNeedBlurNSFW = needBlurNSFW;
}

void setBlurSpoiler(boolean needBlurSpoiler) {
mNeedBlurSpoiler = needBlurSpoiler;
}

@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);
boolean needBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true);
boolean needBlurSpoiler = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_SPOILER_KEY, false);

PostViewModel.Factory factory;

Expand All @@ -268,7 +269,8 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
String query = getArguments().getString(EXTRA_QUERY);

mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
accessToken, postType, true, needBlurNsfw, needBlurSpoiler,
new PostRecyclerViewAdapter.Callback() {
@Override
public void retryLoadingMore() {
mPostViewModel.retryLoadingMore();
Expand Down Expand Up @@ -300,7 +302,8 @@ public void typeChipClicked(int filter) {

boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all"));
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, displaySubredditName, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
accessToken, postType, displaySubredditName, needBlurNsfw, needBlurSpoiler,
new PostRecyclerViewAdapter.Callback() {
@Override
public void retryLoadingMore() {
mPostViewModel.retryLoadingMore();
Expand Down Expand Up @@ -336,7 +339,8 @@ public void typeChipClicked(int filter) {
}

mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
accessToken, postType, true, needBlurNsfw, needBlurSpoiler,
new PostRecyclerViewAdapter.Callback() {
@Override
public void retryLoadingMore() {
mPostViewModel.retryLoadingMore();
Expand Down Expand Up @@ -365,7 +369,8 @@ public void typeChipClicked(int filter) {
}
} else {
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, true, needBlurNsfw, new PostRecyclerViewAdapter.Callback() {
accessToken, postType, true, needBlurNsfw, needBlurSpoiler,
new PostRecyclerViewAdapter.Callback() {
@Override
public void retryLoadingMore() {
mPostViewModel.retryLoadingMore();
Expand Down Expand Up @@ -558,7 +563,16 @@ public void onPostUpdateEvent(PostUpdateEventToPostList event) {
@Subscribe
public void onChangeNSFWBlurEvent(ChangeNSFWBlurEvent event) {
mAdapter.setBlurNSFW(event.needBlurNSFW);
refreshAdapter();
}

@Subscribe
public void onChangeSpoilerBlurEvent(ChangeSpoilerBlurEvent event) {
mAdapter.setBlurSpoiler(event.needBlurSpoiler);
refreshAdapter();
}

private void refreshAdapter() {
int previousPosition = -1;
if(mLinearLayoutManager != null) {
previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
private int mPostType;
private boolean mDisplaySubredditName;
private boolean mNeedBlurNSFW;
private boolean mNeedBlurSpoiler;

private static final int VIEW_TYPE_DATA = 0;
private static final int VIEW_TYPE_ERROR = 1;
Expand All @@ -76,9 +77,9 @@ interface Callback {
}

PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit,
RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, int postType,
boolean displaySubredditName, boolean needBlurNSFW, Callback callback) {
RedditDataRoomDatabase redditDataRoomDatabase, String accessToken,
int postType, boolean displaySubredditName, boolean needBlurNSFW,
boolean needBlurSpoiler, Callback callback) {
super(DIFF_CALLBACK);
if(context != null) {
mContext = context;
Expand All @@ -88,6 +89,7 @@ interface Callback {
mPostType = postType;
mDisplaySubredditName = displaySubredditName;
mNeedBlurNSFW = needBlurNSFW;
mNeedBlurSpoiler = needBlurSpoiler;
mGlide = Glide.with(mContext.getApplicationContext());
mRedditDataRoomDatabase = redditDataRoomDatabase;
mUserDao = redditDataRoomDatabase.userDao();
Expand Down Expand Up @@ -710,7 +712,7 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
}
});

if(post.isNSFW() && mNeedBlurNSFW) {
if((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2)))
.into(((DataViewHolder) holder).imageView);
} else {
Expand Down Expand Up @@ -744,6 +746,10 @@ void setBlurNSFW(boolean needBlurNSFW) {
mNeedBlurNSFW = needBlurNSFW;
}

void setBlurSpoiler(boolean needBlurSpoiler) {
mNeedBlurSpoiler = needBlurSpoiler;
}

private boolean hasExtraRow() {
return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SharedPreferencesUtils {
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 BLUR_SPOILER_KEY = "blur_spoiler";
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
private int postListPosition = -1;
private String mSingleCommentId;
private boolean mNeedBlurNsfw;
private boolean mNeedBlurSpoiler;

@State
boolean mNullAccessToken = false;
Expand Down Expand Up @@ -228,6 +229,7 @@ void onStateChanged(AppBarLayout appBarLayout, State state) {
setSupportActionBar(toolbar);

mNeedBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true);
mNeedBlurSpoiler = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_SPOILER_KEY, false);

mGlide = Glide.with(this);
mLocale = getResources().getConfiguration().locale;
Expand Down Expand Up @@ -372,7 +374,7 @@ public void readFailed() {

mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost,
mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw,
mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override
public void updatePost(Post post) {
Expand Down Expand Up @@ -484,7 +486,7 @@ public void onParsePostSuccess(Post post) {
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this,
mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide,
mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId,
isSingleCommentThreadMode, mNeedBlurNsfw,
isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override
public void updatePost(Post post) {
Expand Down Expand Up @@ -927,7 +929,16 @@ public void onPostUpdateEvent(PostUpdateEventToDetailActivity event) {
@Subscribe
public void onChangeNSFWBlurEvent(ChangeNSFWBlurEvent event) {
mAdapter.setBlurNSFW(event.needBlurNSFW);
refreshAdapter();
}

@Subscribe
public void onChangeSpoilerBlurEvent(ChangeSpoilerBlurEvent event) {
mAdapter.setBlurSpoiler(event.needBlurSpoiler);
refreshAdapter();
}

private void refreshAdapter() {
int previousPosition = -1;
if(mLinearLayoutManager != null) {
previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
Expand Down Expand Up @@ -1201,13 +1212,15 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
if(requestCode == WRITE_COMMENT_REQUEST_CODE) {
if(data != null && resultCode == RESULT_OK) {
if(data.hasExtra(EXTRA_COMMENT_DATA_KEY)) {
CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA_KEY);
if(comment.getDepth() == 0) {
CommentData comment = data.getParcelableExtra(EXTRA_COMMENT_DATA_KEY);
if(comment != null && comment.getDepth() == 0) {
mAdapter.addComment(comment);
} else {
String parentFullname = data.getExtras().getString(CommentActivity.EXTRA_PARENT_FULLNAME_KEY);
int parentPosition = data.getExtras().getInt(CommentActivity.EXTRA_PARENT_POSITION_KEY);
mAdapter.addChildComment(comment, parentFullname, parentPosition);
String parentFullname = data.getStringExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY);
int parentPosition = data.getIntExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, -1);
if(parentFullname != null && parentPosition >= 0) {
mAdapter.addChildComment(comment, parentFullname, parentPosition);
}
}
} else {
Toast.makeText(this, R.string.send_comment_failed, Toast.LENGTH_SHORT).show();
Expand All @@ -1219,7 +1232,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
}
} else if(requestCode == EDIT_COMMENT_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
mAdapter.editComment(data.getExtras().getString(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT),
mAdapter.editComment(data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT),
data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION));
}
}
Expand All @@ -1228,6 +1241,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
comments = mAdapter.getVisibleComments();
Bridge.saveInstanceState(this, outState);
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-night/ic_font_size_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M9,4v3h5v12h3L17,7h5L22,4L9,4zM3,12h3v7h3v-7h3L12,9L3,9v3z"/>
</vector>
Loading

0 comments on commit 47832e1

Please sign in to comment.