Rename confidence sort to best (#1177)

* Rename CONFIDENCE comments sort type to BEST and remove old BEST type

The Reddit API supports only CONFIDENCE sort type for comments but displays it
as BEST.
I renamed CONFIDENCE name to Best and added a migration step for loading
correct sort type.

* Clean up sortType usages in ViewPostDetailFragment

Removed unnecessary null checks, object creations and most case conversions
This commit is contained in:
Sergei Kozelko 2022-11-03 08:58:08 +07:00 committed by GitHub
parent 78496e080f
commit 72c66e7e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 49 deletions

View File

@ -33,7 +33,7 @@ public class SortType {
RELEVANCE("relevance", "Relevance"), RELEVANCE("relevance", "Relevance"),
COMMENTS("comments", "Comments"), COMMENTS("comments", "Comments"),
ACTIVITY("activity", "Activity"), ACTIVITY("activity", "Activity"),
CONFIDENCE("confidence", "Confidence"), CONFIDENCE("confidence", "Best"),
OLD("old", "Old"), OLD("old", "Old"),
QA("qa", "QA"), QA("qa", "QA"),
LIVE("live", "Live"); LIVE("live", "Live");

View File

@ -31,8 +31,6 @@ public class PostCommentSortTypeBottomSheetFragment extends LandscapeExpandedRou
public static final String EXTRA_CURRENT_SORT_TYPE = "ECST"; public static final String EXTRA_CURRENT_SORT_TYPE = "ECST";
@BindView(R.id.best_type_text_view_post_comment_sort_type_bottom_sheet_fragment) @BindView(R.id.best_type_text_view_post_comment_sort_type_bottom_sheet_fragment)
TextView bestTypeTextView;
@BindView(R.id.confidence_type_text_view_post_comment_sort_type_bottom_sheet_fragment)
TextView confidenceTypeTextView; TextView confidenceTypeTextView;
@BindView(R.id.top_type_text_view_post_comment_sort_type_bottom_sheet_fragment) @BindView(R.id.top_type_text_view_post_comment_sort_type_bottom_sheet_fragment)
TextView topTypeTextView; TextView topTypeTextView;
@ -71,9 +69,7 @@ public class PostCommentSortTypeBottomSheetFragment extends LandscapeExpandedRou
ButterKnife.bind(this, rootView); ButterKnife.bind(this, rootView);
String currentSortType = getArguments().getString(EXTRA_CURRENT_SORT_TYPE); String currentSortType = getArguments().getString(EXTRA_CURRENT_SORT_TYPE);
if (currentSortType.equals(SortType.Type.BEST.value)) { if (currentSortType.equals(SortType.Type.BEST.value) || currentSortType.equals(SortType.Type.CONFIDENCE.value)) {
bestTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(bestTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null);
} else if (currentSortType.equals(SortType.Type.CONFIDENCE.value)) {
confidenceTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(confidenceTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); confidenceTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(confidenceTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null);
} else if (currentSortType.equals(SortType.Type.TOP.value)) { } else if (currentSortType.equals(SortType.Type.TOP.value)) {
topTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(topTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); topTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(topTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null);
@ -96,11 +92,6 @@ public class PostCommentSortTypeBottomSheetFragment extends LandscapeExpandedRou
rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
} }
bestTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.BEST));
dismiss();
});
confidenceTypeTextView.setOnClickListener(view -> { confidenceTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.CONFIDENCE)); ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.CONFIDENCE));
dismiss(); dismiss();

View File

@ -546,15 +546,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
mMessageFullname = getArguments().getString(EXTRA_MESSAGE_FULLNAME); mMessageFullname = getArguments().getString(EXTRA_MESSAGE_FULLNAME);
if (!mRespectSubredditRecommendedSortType || isSingleCommentThreadMode) { if (!mRespectSubredditRecommendedSortType || isSingleCommentThreadMode) {
sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase()); SortType.Type sortTypeType = loadSortType();
if (sortType != null) { activity.setTitle(sortTypeType.fullName);
activity.setTitle(new SortType(SortType.Type.valueOf(sortType)).getType().fullName); sortType = sortTypeType.value;
sortType = sortType.toLowerCase();
}
} }
} else { } else {
if (sortType != null) { if (sortType != null) {
activity.setTitle(new SortType(SortType.Type.valueOf(sortType.toUpperCase())).getType().fullName); activity.setTitle(SortType.Type.valueOf(sortType.toUpperCase()).fullName);
} }
} }
@ -815,6 +813,20 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
fetchCommentsRespectRecommendedSort(false, false, sortType.getType().value); fetchCommentsRespectRecommendedSort(false, false, sortType.getType().value);
} }
@NonNull
private SortType.Type loadSortType() {
String sortTypeName = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.CONFIDENCE.name());
if (SortType.Type.BEST.name().equals(sortTypeName)) {
// migrate from BEST to CONFIDENCE
// key guaranteed to exist because got non-default value
mSharedPreferences.edit()
.putString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.CONFIDENCE.name())
.apply();
return SortType.Type.CONFIDENCE;
}
return SortType.Type.valueOf(sortTypeName);
}
public void goToTop() { public void goToTop() {
((LinearLayoutManagerBugFixed) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(0, 0); ((LinearLayoutManagerBugFixed) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(0, 0);
if (mCommentsRecyclerView != null) { if (mCommentsRecyclerView != null) {
@ -1408,30 +1420,25 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
new FetchSubredditData.FetchSubredditDataListener() { new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
if (subredditData.getSuggestedCommentSort() == null || subredditData.getSuggestedCommentSort().equals("null") || subredditData.getSuggestedCommentSort().equals("")) { String suggestedCommentSort = subredditData.getSuggestedCommentSort();
SortType.Type sortTypeType;
if (suggestedCommentSort == null || suggestedCommentSort.equals("null") || suggestedCommentSort.equals("")) {
mRespectSubredditRecommendedSortType = false; mRespectSubredditRecommendedSortType = false;
ViewPostDetailFragment.this.sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase()); sortTypeType = loadSortType();
if (ViewPostDetailFragment.this.sortType != null) {
activity.setTitle(new SortType(SortType.Type.valueOf(ViewPostDetailFragment.this.sortType)).getType().fullName);
ViewPostDetailFragment.this.sortType = ViewPostDetailFragment.this.sortType.toLowerCase();
}
fetchComments(changeRefreshState, checkSortState, ViewPostDetailFragment.this.sortType);
} else { } else {
ViewPostDetailFragment.this.sortType = subredditData.getSuggestedCommentSort(); sortTypeType = SortType.Type.valueOf(suggestedCommentSort.toUpperCase(Locale.US));
String sortTypeTemp = ViewPostDetailFragment.this.sortType.toLowerCase().substring(0, 1).toUpperCase() + ViewPostDetailFragment.this.sortType.substring(1);
activity.setTitle(sortTypeTemp);
fetchComments(changeRefreshState, checkSortState, subredditData.getSuggestedCommentSort());
} }
activity.setTitle(sortTypeType.fullName);
ViewPostDetailFragment.this.sortType = sortTypeType.value;
fetchComments(changeRefreshState, checkSortState, ViewPostDetailFragment.this.sortType);
} }
@Override @Override
public void onFetchSubredditDataFail(boolean isQuarantined) { public void onFetchSubredditDataFail(boolean isQuarantined) {
mRespectSubredditRecommendedSortType = false; mRespectSubredditRecommendedSortType = false;
ViewPostDetailFragment.this.sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase()); SortType.Type sortTypeType = loadSortType();
if (ViewPostDetailFragment.this.sortType != null) { activity.setTitle(sortTypeType.fullName);
activity.setTitle(new SortType(SortType.Type.valueOf(ViewPostDetailFragment.this.sortType)).getType().fullName); ViewPostDetailFragment.this.sortType = sortTypeType.value;
ViewPostDetailFragment.this.sortType = ViewPostDetailFragment.this.sortType.toLowerCase();
}
} }
}); });
} else { } else {

View File

@ -27,22 +27,6 @@
android:textSize="?attr/font_default" android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" /> android:fontFamily="?attr/font_family" />
<TextView
android:id="@+id/confidence_type_text_view_post_comment_sort_type_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:paddingStart="32dp"
android:paddingTop="16dp"
android:paddingEnd="32dp"
android:paddingBottom="16dp"
android:text="@string/sort_confidence"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
<TextView <TextView
android:id="@+id/top_type_text_view_post_comment_sort_type_bottom_sheet_fragment" android:id="@+id/top_type_text_view_post_comment_sort_type_bottom_sheet_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"