Search in sources :

Example 1 with MainActivity

use of com.keylesspalace.tusky.MainActivity in project Tusky by Vavassor.

the class NotificationsFragment method onActivityCreated.

@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()) {
                    // hides the button if we're scrolling down
                    composeButton.hide();
                } else if (dy < 0 && !composeButton.isShown()) {
                    // shows it if we are scrolling up
                    composeButton.show();
                }
            } 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);
}
Also used : NotificationsAdapter(com.keylesspalace.tusky.adapter.NotificationsAdapter) EndlessOnScrollListener(com.keylesspalace.tusky.view.EndlessOnScrollListener) SharedPreferences(android.content.SharedPreferences) FloatingActionButton(android.support.design.widget.FloatingActionButton) RecyclerView(android.support.v7.widget.RecyclerView) MainActivity(com.keylesspalace.tusky.MainActivity) Notification(com.keylesspalace.tusky.entity.Notification)

Example 2 with MainActivity

use of com.keylesspalace.tusky.MainActivity in project Tusky by Vavassor.

the class TimelineFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (jumpToTopAllowed()) {
        TabLayout layout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
        onTabSelectedListener = new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                jumpToTop();
            }
        };
        layout.addOnTabSelectedListener(onTabSelectedListener);
    }
    /* This is delayed until onActivityCreated solely because MainActivity.composeButton isn't
         * guaranteed to be set until then. */
    if (composeButtonPresent()) {
        /* Use a modified scroll listener that both loads more statuses as it goes, and hides
             * the follow 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()) {
                        // hides the button if we're scrolling down
                        composeButton.hide();
                    } else if (dy < 0 && !composeButton.isShown()) {
                        // shows it if we are scrolling up
                        composeButton.show();
                    }
                } else if (!composeButton.isShown()) {
                    composeButton.show();
                }
            }

            @Override
            public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
                TimelineFragment.this.onLoadMore(view);
            }
        };
    } else {
        // Just use the basic scroll listener to load more statuses.
        scrollListener = new EndlessOnScrollListener(layoutManager) {

            @Override
            public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
                TimelineFragment.this.onLoadMore(view);
            }
        };
    }
    recyclerView.addOnScrollListener(scrollListener);
    preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}
Also used : EndlessOnScrollListener(com.keylesspalace.tusky.view.EndlessOnScrollListener) SharedPreferences(android.content.SharedPreferences) TabLayout(android.support.design.widget.TabLayout) FloatingActionButton(android.support.design.widget.FloatingActionButton) RecyclerView(android.support.v7.widget.RecyclerView) MainActivity(com.keylesspalace.tusky.MainActivity)

Aggregations

SharedPreferences (android.content.SharedPreferences)2 FloatingActionButton (android.support.design.widget.FloatingActionButton)2 RecyclerView (android.support.v7.widget.RecyclerView)2 MainActivity (com.keylesspalace.tusky.MainActivity)2 EndlessOnScrollListener (com.keylesspalace.tusky.view.EndlessOnScrollListener)2 TabLayout (android.support.design.widget.TabLayout)1 NotificationsAdapter (com.keylesspalace.tusky.adapter.NotificationsAdapter)1 Notification (com.keylesspalace.tusky.entity.Notification)1