From c2b6066c7ec68a9f1d7e72a24d5bfb22c5461e3c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 26 Feb 2021 22:15:39 +0800 Subject: New option: Show avatar on the right in the navigation drawer. --- .../infinityforreddit/activities/MainActivity.java | 26 +++++++++++++++++++++- .../NavigationDrawerRecyclerViewAdapter.java | 16 +++++++++++++ ...AvatarOnTheRightInTheNavigationDrawerEvent.java | 9 ++++++++ .../settings/InterfacePreferenceFragment.java | 9 ++++++++ .../utils/SharedPreferencesUtils.java | 1 + 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/ChangeShowAvatarOnTheRightInTheNavigationDrawerEvent.java (limited to 'app/src/main/java/ml') 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 83b8a2c9..18955498 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -95,6 +95,7 @@ import ml.docilealligator.infinityforreddit.events.ChangeDisableSwipingBetweenTa import ml.docilealligator.infinityforreddit.events.ChangeLockBottomAppBarEvent; import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.events.ChangeRequireAuthToAccountSectionEvent; +import ml.docilealligator.infinityforreddit.events.ChangeShowAvatarOnTheRightInTheNavigationDrawerEvent; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; @@ -1162,7 +1163,30 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Subscribe public void onChangeRequireAuthToAccountSectionEvent(ChangeRequireAuthToAccountSectionEvent changeRequireAuthToAccountSectionEvent) { - adapter.setRequireAuthToAccountSection(changeRequireAuthToAccountSectionEvent.requireAuthToAccountSection); + if (adapter != null) { + adapter.setRequireAuthToAccountSection(changeRequireAuthToAccountSectionEvent.requireAuthToAccountSection); + } + } + + @Subscribe + public void onChangeShowAvatarOnTheRightInTheNavigationDrawerEvent(ChangeShowAvatarOnTheRightInTheNavigationDrawerEvent event) { + if (adapter != null) { + adapter.setShowAvatarOnTheRightInTheNavigationDrawer(event.showAvatarOnTheRightInTheNavigationDrawer); + int previousPosition = -1; + if (navDrawerRecyclerView.getLayoutManager() != null) { + previousPosition = ((LinearLayoutManager) navDrawerRecyclerView.getLayoutManager()).findFirstVisibleItemPosition(); + } + + RecyclerView.LayoutManager layoutManager = navDrawerRecyclerView.getLayoutManager(); + navDrawerRecyclerView.setAdapter(null); + navDrawerRecyclerView.setLayoutManager(null); + navDrawerRecyclerView.setAdapter(adapter); + navDrawerRecyclerView.setLayoutManager(layoutManager); + + if (previousPosition > 0) { + navDrawerRecyclerView.scrollToPosition(previousPosition); + } + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java index 744e6ad8..d6de3458 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java @@ -7,6 +7,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.NonNull; @@ -64,6 +65,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter= Build.VERSION_CODES.O) { @@ -36,6 +38,13 @@ public class InterfacePreferenceFragment extends PreferenceFragmentCompat { }); } + if (showAvatarOnTheRightInTheNavigationDrawer != null) { + showAvatarOnTheRightInTheNavigationDrawer.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeShowAvatarOnTheRightInTheNavigationDrawerEvent((Boolean) newValue)); + return true; + }); + } + if (voteButtonsOnTheRightSwitch != null) { voteButtonsOnTheRightSwitch.setOnPreferenceChangeListener((preference, newValue) -> { EventBus.getDefault().post(new ChangeVoteButtonsPositionEvent((Boolean) newValue)); 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 aa2d7c3f..be8489a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -38,6 +38,7 @@ public class SharedPreferencesUtils { public static final String DISABLE_IMMERSIVE_INTERFACE_IN_LANDSCAPE_MODE = "disable_immersive_interface_in_landscape_mode"; public static final String BOTTOM_APP_BAR_KEY = "bottom_app_bar"; public static final String VOTE_BUTTONS_ON_THE_RIGHT_KEY = "vote_buttons_on_the_right"; + public static final String SHOW_AVATAR_ON_THE_RIGHT_IN_THE_NAVIGATION_DRAWER = "show_avatar_on_the_right_in_the_navigation_drawer"; public static final String SORT_TYPE_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.sort_type"; public static final String SORT_TYPE_BEST_POST = "sort_type_best_post"; -- cgit v1.2.3