From 7c3feb7136eb77ae38797e455b3b6ddbdf37f61e Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sat, 22 Apr 2017 10:41:49 +0200 Subject: [PATCH] fix bugs with fab showing/hiding on scroll --- .../tusky/NotificationsFragment.java | 76 +++++++++++++++---- .../keylesspalace/tusky/TimelineFragment.java | 16 +++- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java index 76a89c1f6..56afc0fd8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java @@ -19,7 +19,9 @@ import android.content.Context; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; +import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.DividerItemDecoration; @@ -40,15 +42,17 @@ import retrofit2.Response; public class NotificationsFragment extends SFragment implements SwipeRefreshLayout.OnRefreshListener, StatusActionListener, StatusRemoveListener, - NotificationsAdapter.NotificationActionListener { + NotificationsAdapter.NotificationActionListener, SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = "Notifications"; // logging tag private SwipeRefreshLayout swipeRefreshLayout; private LinearLayoutManager layoutManager; + private RecyclerView recyclerView; private EndlessOnScrollListener scrollListener; private NotificationsAdapter adapter; private TabLayout.OnTabSelectedListener onTabSelectedListener; private Call> listCall; + private boolean hideFab; public static NotificationsFragment newInstance() { NotificationsFragment fragment = new NotificationsFragment(); @@ -60,7 +64,7 @@ public class NotificationsFragment extends SFragment implements @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { + @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_timeline, container, false); // Setup the SwipeRefreshLayout. @@ -68,7 +72,7 @@ public class NotificationsFragment extends SFragment implements swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setOnRefreshListener(this); // Setup the RecyclerView. - RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); + recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(context); recyclerView.setLayoutManager(layoutManager); @@ -78,19 +82,7 @@ public class NotificationsFragment extends SFragment implements R.drawable.status_divider_dark); divider.setDrawable(drawable); recyclerView.addItemDecoration(divider); - scrollListener = new EndlessOnScrollListener(layoutManager) { - @Override - public void onLoadMore(int page, int totalItemsCount, RecyclerView view) { - NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter(); - Notification notification = adapter.getItem(adapter.getItemCount() - 2); - if (notification != null) { - sendFetchNotificationsRequest(notification.id, null); - } else { - sendFetchNotificationsRequest(); - } - } - }; - recyclerView.addOnScrollListener(scrollListener); + adapter = new NotificationsAdapter(this, this); recyclerView.setAdapter(adapter); @@ -112,6 +104,51 @@ public class NotificationsFragment extends SFragment implements return rootView; } + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + /* This is delayed until onActivityCreated solely because MainActivity.composeButton isn't + * guaranteed to be set until then. + * Use a modified scroll listener that both loads more notifications as it goes, and hides + * the compose button on down-scroll. */ + MainActivity activity = (MainActivity) getActivity(); + final FloatingActionButton composeButton = activity.composeButton; + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences( + activity); + preferences.registerOnSharedPreferenceChangeListener(this); + hideFab = preferences.getBoolean("fabHide", false); + scrollListener = new EndlessOnScrollListener(layoutManager) { + @Override + public void onScrolled(RecyclerView view, int dx, int dy) { + super.onScrolled(view, dx, dy); + + if (hideFab) { + if (dy > 0 && composeButton.isShown()) { + composeButton.hide(); // hides the button if we're scrolling down + } else if (dy < 0 && !composeButton.isShown()) { + composeButton.show(); // shows it if we are scrolling up + } + } else if (!composeButton.isShown()) { + composeButton.show(); + } + } + + @Override + public void onLoadMore(int page, int totalItemsCount, RecyclerView view) { + NotificationsAdapter adapter = (NotificationsAdapter) view.getAdapter(); + Notification notification = adapter.getItem(adapter.getItemCount() - 2); + if (notification != null) { + sendFetchNotificationsRequest(notification.id, null); + } else { + sendFetchNotificationsRequest(); + } + } + }; + + recyclerView.addOnScrollListener(scrollListener); + } + @Override public void onDestroy() { super.onDestroy(); @@ -245,4 +282,11 @@ public class NotificationsFragment extends SFragment implements public void onViewAccount(String id) { super.viewAccount(id); } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if(key.equals("fabHide")) { + hideFab = sharedPreferences.getBoolean("fabHide", false); + } + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java index 223f6a084..6973524a9 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java @@ -39,7 +39,7 @@ import retrofit2.Call; import retrofit2.Callback; public class TimelineFragment extends SFragment implements - SwipeRefreshLayout.OnRefreshListener, StatusActionListener, StatusRemoveListener { + SwipeRefreshLayout.OnRefreshListener, StatusActionListener, StatusRemoveListener, SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = "Timeline"; // logging tag private Call> listCall; @@ -61,6 +61,7 @@ public class TimelineFragment extends SFragment implements private LinearLayoutManager layoutManager; private EndlessOnScrollListener scrollListener; private TabLayout.OnTabSelectedListener onTabSelectedListener; + private boolean hideFab; public static TimelineFragment newInstance(Kind kind) { TimelineFragment fragment = new TimelineFragment(); @@ -152,17 +153,21 @@ public class TimelineFragment extends SFragment implements final FloatingActionButton composeButton = activity.composeButton; final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences( activity); + preferences.registerOnSharedPreferenceChangeListener(this); + hideFab = preferences.getBoolean("fabHide", false); scrollListener = new EndlessOnScrollListener(layoutManager) { @Override public void onScrolled(RecyclerView view, int dx, int dy) { super.onScrolled(view, dx, dy); - if (preferences.getBoolean("fabHide", false)) { + if (hideFab) { if (dy > 0 && composeButton.isShown()) { composeButton.hide(); // hides the button if we're scrolling down } else if (dy < 0 && !composeButton.isShown()) { composeButton.show(); // shows it if we are scrolling up } + } else if (!composeButton.isShown()) { + composeButton.show(); } } @@ -358,4 +363,11 @@ public class TimelineFragment extends SFragment implements } super.viewAccount(id); } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if(key.equals("fabHide")) { + hideFab = sharedPreferences.getBoolean("fabHide", false); + } + } }