From 72c66e7e4e19200f5cdd470fa101a4c826ab950b Mon Sep 17 00:00:00 2001
From: Sergei Kozelko <KozelkoS@yandex.ru>
Date: Thu, 3 Nov 2022 08:58:08 +0700
Subject: 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
---
 .../infinityforreddit/SortType.java                |  2 +-
 .../PostCommentSortTypeBottomSheetFragment.java    | 11 +----
 .../fragments/ViewPostDetailFragment.java          | 51 ++++++++++++----------
 ...ragment_post_comment_sort_type_bottom_sheet.xml | 16 -------
 4 files changed, 31 insertions(+), 49 deletions(-)

(limited to 'app/src/main')

diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java
index 83d45f57..be4b1b89 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java
@@ -33,7 +33,7 @@ public class SortType {
         RELEVANCE("relevance", "Relevance"),
         COMMENTS("comments", "Comments"),
         ACTIVITY("activity", "Activity"),
-        CONFIDENCE("confidence", "Confidence"),
+        CONFIDENCE("confidence", "Best"),
         OLD("old", "Old"),
         QA("qa", "QA"),
         LIVE("live", "Live");
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostCommentSortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostCommentSortTypeBottomSheetFragment.java
index 20871a1f..66dfe02c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostCommentSortTypeBottomSheetFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostCommentSortTypeBottomSheetFragment.java
@@ -31,8 +31,6 @@ public class PostCommentSortTypeBottomSheetFragment extends LandscapeExpandedRou
     public static final String EXTRA_CURRENT_SORT_TYPE = "ECST";
 
     @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;
     @BindView(R.id.top_type_text_view_post_comment_sort_type_bottom_sheet_fragment)
     TextView topTypeTextView;
@@ -71,9 +69,7 @@ public class PostCommentSortTypeBottomSheetFragment extends LandscapeExpandedRou
         ButterKnife.bind(this, rootView);
 
         String currentSortType = getArguments().getString(EXTRA_CURRENT_SORT_TYPE);
-        if (currentSortType.equals(SortType.Type.BEST.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)) {
+        if (currentSortType.equals(SortType.Type.BEST.value) || 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);
         } 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);
@@ -96,11 +92,6 @@ public class PostCommentSortTypeBottomSheetFragment extends LandscapeExpandedRou
             rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
         }
 
-        bestTypeTextView.setOnClickListener(view -> {
-            ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.BEST));
-            dismiss();
-        });
-
         confidenceTypeTextView.setOnClickListener(view -> {
             ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.CONFIDENCE));
             dismiss();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java
index 4475e8f8..baaf1261 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java
@@ -546,15 +546,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
             mMessageFullname = getArguments().getString(EXTRA_MESSAGE_FULLNAME);
 
             if (!mRespectSubredditRecommendedSortType || isSingleCommentThreadMode) {
-                sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase());
-                if (sortType != null) {
-                    activity.setTitle(new SortType(SortType.Type.valueOf(sortType)).getType().fullName);
-                    sortType = sortType.toLowerCase();
-                }
+                SortType.Type sortTypeType = loadSortType();
+                activity.setTitle(sortTypeType.fullName);
+                sortType = sortTypeType.value;
             }
         } else {
             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);
     }
 
+    @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() {
         ((LinearLayoutManagerBugFixed) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(0, 0);
         if (mCommentsRecyclerView != null) {
@@ -1408,30 +1420,25 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
                     new FetchSubredditData.FetchSubredditDataListener() {
                         @Override
                         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;
-                                ViewPostDetailFragment.this.sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase());
-                                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);
+                                sortTypeType = loadSortType();
                             } else {
-                                ViewPostDetailFragment.this.sortType = subredditData.getSuggestedCommentSort();
-                                String sortTypeTemp = ViewPostDetailFragment.this.sortType.toLowerCase().substring(0, 1).toUpperCase() + ViewPostDetailFragment.this.sortType.substring(1);
-                                activity.setTitle(sortTypeTemp);
-                                fetchComments(changeRefreshState, checkSortState, subredditData.getSuggestedCommentSort());
+                                sortTypeType = SortType.Type.valueOf(suggestedCommentSort.toUpperCase(Locale.US));
                             }
+                            activity.setTitle(sortTypeType.fullName);
+                            ViewPostDetailFragment.this.sortType = sortTypeType.value;
+                            fetchComments(changeRefreshState, checkSortState, ViewPostDetailFragment.this.sortType);
                         }
 
                         @Override
                         public void onFetchSubredditDataFail(boolean isQuarantined) {
                             mRespectSubredditRecommendedSortType = false;
-                            ViewPostDetailFragment.this.sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase());
-                            if (ViewPostDetailFragment.this.sortType != null) {
-                                activity.setTitle(new SortType(SortType.Type.valueOf(ViewPostDetailFragment.this.sortType)).getType().fullName);
-                                ViewPostDetailFragment.this.sortType = ViewPostDetailFragment.this.sortType.toLowerCase();
-                            }
+                            SortType.Type sortTypeType = loadSortType();
+                            activity.setTitle(sortTypeType.fullName);
+                            ViewPostDetailFragment.this.sortType = sortTypeType.value;
                         }
                     });
         } else {
diff --git a/app/src/main/res/layout/fragment_post_comment_sort_type_bottom_sheet.xml b/app/src/main/res/layout/fragment_post_comment_sort_type_bottom_sheet.xml
index cade8c2e..d26e7bcb 100644
--- a/app/src/main/res/layout/fragment_post_comment_sort_type_bottom_sheet.xml
+++ b/app/src/main/res/layout/fragment_post_comment_sort_type_bottom_sheet.xml
@@ -27,22 +27,6 @@
             android:textSize="?attr/font_default"
             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
             android:id="@+id/top_type_text_view_post_comment_sort_type_bottom_sheet_fragment"
             android:layout_width="match_parent"
-- 
cgit v1.2.3