From a54a3395f8d5c2b61905f4c0ae77c4b7d11b3a16 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 21 Feb 2020 00:27:04 +0800 Subject: Add an option to show rounded number of votes. --- .../Activity/ViewPostDetailActivity.java | 4 ++ .../Adapter/CommentAndPostRecyclerViewAdapter.java | 39 ++++++++++----- .../CommentsListingRecyclerViewAdapter.java | 19 +++++--- .../Adapter/PostRecyclerViewAdapter.java | 37 ++++++++------ .../ChangeShowAbsoluteNumberOfVotesEvent.java | 9 ++++ .../Fragment/CommentsListingFragment.java | 5 +- .../infinityforreddit/Fragment/PostFragment.java | 56 +++++++++++++++------- .../Settings/InterfacePreferenceFragment.java | 9 ++++ .../Utils/SharedPreferencesUtils.java | 1 + .../infinityforreddit/Utils/Utils.java | 13 +++++ 10 files changed, 141 insertions(+), 51 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeShowAbsoluteNumberOfVotesEvent.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 65711954..d8950f2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -177,6 +177,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private boolean mSwipeUpToHideFab; private boolean mExpandChildren; private boolean mShowCommentDivider; + private boolean mShowAbsoluteNumberOfVotes; private LinearLayoutManager mLinearLayoutManager; private CommentAndPostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller mSmoothScroller; @@ -265,6 +266,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mSwipeUpToHideFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); mExpandChildren = !mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_TOP_LEVEL_COMMENTS_FIRST, false); mShowCommentDivider = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_COMMENT_DIVIDER, false); + mShowAbsoluteNumberOfVotes = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; @@ -511,6 +513,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler, mVoteButtonsOnTheRight, mShowElapsedTime, mExpandChildren, mShowCommentDivider, + mShowAbsoluteNumberOfVotes, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -634,6 +637,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler, mVoteButtonsOnTheRight, mShowElapsedTime, mExpandChildren, mShowCommentDivider, + mShowAbsoluteNumberOfVotes, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 82270285..450c3486 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -124,6 +124,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, FilteredThingActivity.class); @@ -742,7 +744,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 0) { @@ -894,7 +897,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 0) { ((PostViewHolder) holder).gildedNumberTextView.setVisibility(View.VISIBLE); @@ -558,7 +561,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 0) { ((PostCompactViewHolder) holder).gildedNumberTextView.setVisibility(View.VISIBLE); @@ -1104,7 +1107,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter mCommentViewModel.retryLoadingMore()); + voteButtonsOnTheRight, mShowElapsedTime, mShowCommentDivider, mShowAbsoluteNumberOfVotes, + () -> mCommentViewModel.retryLoadingMore()); String username = getArguments().getString(EXTRA_USERNAME); String sort = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, SortType.Type.NEW.value); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index e309343c..cf434238 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit.Fragment; -import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -55,6 +54,7 @@ import ml.docilealligator.infinityforreddit.Adapter.PostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.Event.ChangeDefaultPostLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangePostLayoutEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent; @@ -323,6 +323,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { boolean voteButtonsOnTheRight = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY, false); boolean showElapsedTime = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); boolean showDividerInCompactLayout = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT, true); + boolean showAbsoluteNumberOfVotes = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); if (postType == PostDataSource.TYPE_SEARCH) { @@ -336,7 +337,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, postLayout, true, needBlurNsfw, needBlurSpoiler, - voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, new PostRecyclerViewAdapter.Callback() { + voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, showAbsoluteNumberOfVotes, + new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -399,7 +401,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, postLayout, displaySubredditName, needBlurNsfw, needBlurSpoiler, - voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, new PostRecyclerViewAdapter.Callback() { + voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, showAbsoluteNumberOfVotes, + new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -447,7 +450,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, postLayout, true, needBlurNsfw, needBlurSpoiler, - voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, new PostRecyclerViewAdapter.Callback() { + voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, showAbsoluteNumberOfVotes, + new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -493,7 +497,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, postLayout, true, needBlurNsfw, needBlurSpoiler, - voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, new PostRecyclerViewAdapter.Callback() { + voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, showAbsoluteNumberOfVotes, + new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -532,7 +537,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, postLayout, true, needBlurNsfw, needBlurSpoiler, - voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, new PostRecyclerViewAdapter.Callback() { + voteButtonsOnTheRight, showElapsedTime, showDividerInCompactLayout, showAbsoluteNumberOfVotes, + new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -738,26 +744,34 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Subscribe public void onChangeShowElapsedTime(ChangeShowElapsedTimeEvent event) { - mAdapter.setShowElapsedTime(event.showElapsedTime); - refreshAdapter(); + if (mAdapter != null) { + mAdapter.setShowElapsedTime(event.showElapsedTime); + refreshAdapter(); + } } @Subscribe public void onChangeVoteButtonsPositionEvent(ChangeVoteButtonsPositionEvent event) { - mAdapter.setVoteButtonsPosition(event.voteButtonsOnTheRight); - refreshAdapter(); + if (mAdapter != null) { + mAdapter.setVoteButtonsPosition(event.voteButtonsOnTheRight); + refreshAdapter(); + } } @Subscribe public void onChangeNSFWBlurEvent(ChangeNSFWBlurEvent event) { - mAdapter.setBlurNSFW(event.needBlurNSFW); - refreshAdapter(); + if (mAdapter != null) { + mAdapter.setBlurNSFW(event.needBlurNSFW); + refreshAdapter(); + } } @Subscribe public void onChangeSpoilerBlurEvent(ChangeSpoilerBlurEvent event) { - mAdapter.setBlurSpoiler(event.needBlurSpoiler); - refreshAdapter(); + if (mAdapter != null) { + mAdapter.setBlurSpoiler(event.needBlurSpoiler); + refreshAdapter(); + } } @Subscribe @@ -767,8 +781,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Subscribe public void onShowDividerInCompactLayoutPreferenceEvent(ShowDividerInCompactLayoutPreferenceEvent event) { - mAdapter.setShowDividerInCompactLayout(event.showDividerInCompactLayout); - refreshAdapter(); + if (mAdapter != null) { + mAdapter.setShowDividerInCompactLayout(event.showDividerInCompactLayout); + refreshAdapter(); + } } @Subscribe @@ -805,6 +821,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Subscribe + public void onChangeShowAbsoluteNumberOfVotesEvent(ChangeShowAbsoluteNumberOfVotesEvent changeShowAbsoluteNumberOfVotesEvent) { + if (mAdapter != null) { + mAdapter.setShowAbsoluteNumberOfVotes(changeShowAbsoluteNumberOfVotesEvent.showAbsoluteNumberOfVotes); + refreshAdapter(); + } + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java index 4123f276..78634adf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java @@ -17,6 +17,7 @@ import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; import ml.docilealligator.infinityforreddit.Event.ChangeDefaultPostLayoutEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; @@ -48,6 +49,7 @@ public class InterfacePreferenceFragment extends PreferenceFragmentCompat { SwitchPreference showElapsedTimeSwitch = findPreference(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY); ListPreference defaultPostLayoutSwitch = findPreference(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY); SwitchPreference showDividerInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT); + SwitchPreference showAbsoluteNumberOfVotes = findPreference(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES); boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; @@ -134,6 +136,13 @@ public class InterfacePreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + if (showAbsoluteNumberOfVotes != null) { + showAbsoluteNumberOfVotes.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeShowAbsoluteNumberOfVotesEvent((Boolean) newValue)); + return true; + }); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index 85c1f9f9..0ff474bd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -76,4 +76,5 @@ public class SharedPreferencesUtils { public static final String CONFIRM_TO_EXIT = "confirm_to_exit"; public static final String LOCK_BOTTOM_APP_BAR = "lock_bottom_app_bar"; public static final String SHOW_COMMENT_DIVIDER = "show_comment_divider"; + public static final String SHOW_ABSOLUTE_NUMBER_OF_VOTES = "show_absolute_number_of_votes"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java index e39d6e35..552f8515 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java @@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit.Utils; import android.content.Context; +import java.util.Locale; + import ml.docilealligator.infinityforreddit.R; public class Utils { @@ -60,4 +62,15 @@ public class Utils { return context.getString(R.string.elapsed_time_years_ago, diff / YEAR_MILLIS); } } + + public static String getNVotes(boolean showAbsoluteNumberOfVotes, int votes) { + if (showAbsoluteNumberOfVotes) { + return Integer.toString(votes); + } else { + if (Math.abs(votes) < 1000) { + return Integer.toString(votes); + } + return String.format(Locale.US, "%.1f", (float) votes / 1000) + "K"; + } + } } -- cgit v1.2.3