From 0c7e5bc16f72a2f3bf1b5c5ec2446f1f74556c14 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 21 Aug 2022 10:58:18 +1000 Subject: Show an indicator for the current sort type in SortTypeBottomSheetFragment. --- .../AccessTokenAuthenticator.java | 7 +++++- .../AnyAccountAccessTokenAuthenticator.java | 7 +++++- .../activities/FilteredPostsActivity.java | 11 ++------ .../infinityforreddit/activities/MainActivity.java | 10 ++++---- .../activities/ViewMultiRedditDetailActivity.java | 9 +++---- .../activities/ViewSubredditDetailActivity.java | 10 ++++---- .../SortTypeBottomSheetFragment.java | 29 +++++++++++++++++++++- .../ic_round_check_circle_day_night_24dp.xml | 5 ++++ .../ic_round_check_circle_day_night_24dp.xml | 5 ++++ 9 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 app/src/main/res/drawable-night/ic_round_check_circle_day_night_24dp.xml create mode 100644 app/src/main/res/drawable/ic_round_check_circle_day_night_24dp.xml (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 1ac3b368..588d4041 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -39,7 +39,12 @@ class AccessTokenAuthenticator implements Authenticator { @Override public Request authenticate(Route route, @NonNull Response response) { if (response.code() == 401) { - String accessToken = response.request().header(APIUtils.AUTHORIZATION_KEY).substring(APIUtils.AUTHORIZATION_BASE.length()); + String accessTokenHeader = response.request().header(APIUtils.AUTHORIZATION_KEY); + if (accessTokenHeader == null) { + return null; + } + + String accessToken = accessTokenHeader.substring(APIUtils.AUTHORIZATION_BASE.length()); synchronized (this) { Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount(); if (account == null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AnyAccountAccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AnyAccountAccessTokenAuthenticator.java index 4cedc8e9..9bd31c2d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AnyAccountAccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AnyAccountAccessTokenAuthenticator.java @@ -42,7 +42,12 @@ public class AnyAccountAccessTokenAuthenticator implements Authenticator { @Override public Request authenticate(Route route, @NonNull Response response) { if (response.code() == 401) { - String accessToken = response.request().header(APIUtils.AUTHORIZATION_KEY).substring(APIUtils.AUTHORIZATION_BASE.length()); + String accessTokenHeader = response.request().header(APIUtils.AUTHORIZATION_KEY); + if (accessTokenHeader == null) { + return null; + } + + String accessToken = accessTokenHeader.substring(APIUtils.AUTHORIZATION_BASE.length()); synchronized (this) { if (mAccount == null) { return null; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index 36438c02..1d59eb8d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -7,7 +7,6 @@ import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; -import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -356,10 +355,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec } else if (itemId == R.id.action_sort_filtered_thing_activity) { switch (postType) { case PostPagingSource.TYPE_FRONT_PAGE: - SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); - Bundle bestBundle = new Bundle(); - bestBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, false); - bestSortTypeBottomSheetFragment.setArguments(bestBundle); + SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment = SortTypeBottomSheetFragment.getNewInstance(false, mFragment.getSortType().getType().fullName); bestSortTypeBottomSheetFragment.show(getSupportFragmentManager(), bestSortTypeBottomSheetFragment.getTag()); break; case PostPagingSource.TYPE_SEARCH: @@ -372,10 +368,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec case PostPagingSource.TYPE_MULTI_REDDIT: case PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT: case PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE: - SortTypeBottomSheetFragment sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); - Bundle popularBundle = new Bundle(); - popularBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true); - sortTypeBottomSheetFragment.setArguments(popularBundle); + SortTypeBottomSheetFragment sortTypeBottomSheetFragment = SortTypeBottomSheetFragment.getNewInstance(true, mFragment.getSortType().getType().fullName); sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); break; case PostPagingSource.TYPE_USER: diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index ebe833a4..1a453f35 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -1065,11 +1065,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private void changeSortType() { int currentPostType = sectionsPagerAdapter.getCurrentPostType(); - Bundle bundle = new Bundle(); - bundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, currentPostType != PostPagingSource.TYPE_FRONT_PAGE); - SortTypeBottomSheetFragment sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); - sortTypeBottomSheetFragment.setArguments(bundle); - sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + PostFragment postFragment = sectionsPagerAdapter.getCurrentFragment(); + if (postFragment != null) { + SortTypeBottomSheetFragment sortTypeBottomSheetFragment = SortTypeBottomSheetFragment.getNewInstance(currentPostType != PostPagingSource.TYPE_FRONT_PAGE, postFragment.getSortType().getType().fullName); + sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index 31c0a19f..e3cf7c63 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -575,11 +575,10 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT } private void showSortTypeBottomSheetFragment() { - SortTypeBottomSheetFragment sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); - Bundle bottomSheetBundle = new Bundle(); - bottomSheetBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true); - sortTypeBottomSheetFragment.setArguments(bottomSheetBundle); - sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + if (mFragment instanceof PostFragment) { + SortTypeBottomSheetFragment sortTypeBottomSheetFragment = SortTypeBottomSheetFragment.getNewInstance(true, ((PostFragment) mFragment).getSortType().getType().fullName); + sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + } } private void showPostLayoutBottomSheetFragment() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index af31b76a..d266145a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -1091,11 +1091,11 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp } private void displaySortTypeBottomSheetFragment() { - SortTypeBottomSheetFragment sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); - Bundle bottomSheetBundle = new Bundle(); - bottomSheetBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true); - sortTypeBottomSheetFragment.setArguments(bottomSheetBundle); - sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + Fragment fragment = fragmentManager.findFragmentByTag("f0"); + if (fragment instanceof PostFragment) { + SortTypeBottomSheetFragment sortTypeBottomSheetFragment = SortTypeBottomSheetFragment.getNewInstance(true, ((PostFragment) fragment).getSortType().getType().fullName); + sortTypeBottomSheetFragment.show(fragmentManager, sortTypeBottomSheetFragment.getTag()); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SortTypeBottomSheetFragment.java index f2d50c38..881b1910 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SortTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SortTypeBottomSheetFragment.java @@ -11,6 +11,7 @@ import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.appcompat.content.res.AppCompatResources; import androidx.fragment.app.Fragment; import butterknife.BindView; @@ -29,6 +30,8 @@ import ml.docilealligator.infinityforreddit.utils.Utils; public class SortTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment { public static final String EXTRA_NO_BEST_TYPE = "ENBT"; + public static final String EXTRA_CURRENT_SORT_TYPE = "ECST"; + @BindView(R.id.best_type_text_view_sort_type_bottom_sheet_fragment) TextView bestTypeTextView; @BindView(R.id.hot_type_text_view_sort_type_bottom_sheet_fragment) @@ -46,6 +49,15 @@ public class SortTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomS // Required empty public constructor } + public static SortTypeBottomSheetFragment getNewInstance(boolean isNoBestType, String currentSortType) { + SortTypeBottomSheetFragment fragment = new SortTypeBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putBoolean(EXTRA_NO_BEST_TYPE, isNoBestType); + bundle.putString(EXTRA_CURRENT_SORT_TYPE, currentSortType); + fragment.setArguments(bundle); + return fragment; + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -57,7 +69,7 @@ public class SortTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomS rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); } - if (getArguments() == null || getArguments().getBoolean(EXTRA_NO_BEST_TYPE)) { + if (getArguments().getBoolean(EXTRA_NO_BEST_TYPE)) { bestTypeTextView.setVisibility(View.GONE); } else { bestTypeTextView.setOnClickListener(view -> { @@ -66,6 +78,21 @@ public class SortTypeBottomSheetFragment extends LandscapeExpandedRoundedBottomS }); } + String currentSortType = getArguments().getString(EXTRA_CURRENT_SORT_TYPE); + if (currentSortType.equals(SortType.Type.BEST.fullName)) { + 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.HOT.fullName)) { + hotTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(hotTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); + } else if (currentSortType.equals(SortType.Type.NEW.fullName)) { + newTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(newTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); + } else if (currentSortType.equals(SortType.Type.RISING.fullName)) { + risingTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(risingTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); + } else if (currentSortType.equals(SortType.Type.TOP.fullName)) { + topTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(topTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); + } else if (currentSortType.equals(SortType.Type.CONTROVERSIAL.fullName)) { + controversialTypeTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(controversialTypeTextView.getCompoundDrawablesRelative()[0], null, AppCompatResources.getDrawable(activity, R.drawable.ic_round_check_circle_day_night_24dp), null); + } + hotTypeTextView.setOnClickListener(view -> { ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.HOT)); dismiss(); diff --git a/app/src/main/res/drawable-night/ic_round_check_circle_day_night_24dp.xml b/app/src/main/res/drawable-night/ic_round_check_circle_day_night_24dp.xml new file mode 100644 index 00000000..d11c68bf --- /dev/null +++ b/app/src/main/res/drawable-night/ic_round_check_circle_day_night_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_round_check_circle_day_night_24dp.xml b/app/src/main/res/drawable/ic_round_check_circle_day_night_24dp.xml new file mode 100644 index 00000000..f484cad7 --- /dev/null +++ b/app/src/main/res/drawable/ic_round_check_circle_day_night_24dp.xml @@ -0,0 +1,5 @@ + + + -- cgit v1.2.3