Search in sources :

Example 6 with ToolbarScrollHideHandler

use of me.ccrama.redditslide.handler.ToolbarScrollHideHandler in project Slide by ccrama.

the class NewsView method resetScroll.

public void resetScroll() {
    if (toolbarScroll == null) {
        toolbarScroll = new ToolbarScrollHideHandler(((BaseActivity) getActivity()).mToolbar, header) {

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!posts.loading && !posts.nomore && !posts.offline && !adapter.isError) {
                    visibleItemCount = rv.getLayoutManager().getChildCount();
                    totalItemCount = rv.getLayoutManager().getItemCount();
                    int[] firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(null);
                    if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                        for (int firstVisibleItem : firstVisibleItems) {
                            pastVisiblesItems = firstVisibleItem;
                            if (SettingValues.scrollSeen && pastVisiblesItems > 0 && SettingValues.storeHistory) {
                                HasSeen.addSeenScrolling(posts.posts.get(pastVisiblesItems - 1).getFullName());
                            }
                        }
                    }
                    if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                        posts.loading = true;
                        posts.loadMore(mSwipeRefreshLayout.getContext(), NewsView.this, false, posts.subreddit);
                    }
                }
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) {
                    diff += dy;
                } else {
                    diff = 0;
                }
                if (fab != null) {
                    if (dy <= 0 && fab.getId() != 0 && SettingValues.fab) {
                        if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING || diff < -fab.getHeight() * 2) {
                            fab.show();
                        }
                    } else {
                        fab.hide();
                    }
                }
            }

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                // switch (newState) {
                // case RecyclerView.SCROLL_STATE_IDLE:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_DRAGGING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_SETTLING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().pause();
                // break;
                // }
                super.onScrollStateChanged(recyclerView, newState);
                // If the toolbar search is open, and the user scrolls in the Main view--close the search UI
                if (getActivity() instanceof MainActivity && (SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_TOOLBAR || SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_BOTH) && ((MainActivity) getContext()).findViewById(R.id.toolbar_search).getVisibility() == View.VISIBLE) {
                    ((MainActivity) getContext()).findViewById(R.id.close_search_toolbar).performClick();
                }
            }
        };
        rv.addOnScrollListener(toolbarScroll);
    } else {
        toolbarScroll.reset = true;
    }
}
Also used : BaseActivity(me.ccrama.redditslide.Activities.BaseActivity) RecyclerView(androidx.recyclerview.widget.RecyclerView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Example 7 with ToolbarScrollHideHandler

use of me.ccrama.redditslide.handler.ToolbarScrollHideHandler in project Slide by ccrama.

the class ReadLaterView method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);
    final RecyclerView rv = v.findViewById(R.id.vertical_content);
    final PreCachingLayoutManager mLayoutManager = new PreCachingLayoutManager(getContext());
    rv.setLayoutManager(mLayoutManager);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    final SwipeRefreshLayout mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("default", getActivity()));
    // If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    // So, we estimate the height of the header in dp
    mSwipeRefreshLayout.setProgressViewOffset(false, Constants.SINGLE_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.SINGLE_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });
    posts = new HistoryPosts("readLater");
    adapter = new ContributionAdapter(getActivity(), posts, rv);
    rv.setAdapter(adapter);
    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    // TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            posts.loadMore(adapter, true);
        // TODO catch errors
        }
    });
    rv.addOnScrollListener(new ToolbarScrollHideHandler(getActivity().findViewById(R.id.toolbar), getActivity().findViewById(R.id.header)) {

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = rv.getLayoutManager().getChildCount();
            totalItemCount = rv.getLayoutManager().getItemCount();
            if (rv.getLayoutManager() instanceof PreCachingLayoutManager) {
                pastVisiblesItems = ((PreCachingLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
            } else {
                int[] firstVisibleItems = null;
                firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(firstVisibleItems);
                if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                    pastVisiblesItems = firstVisibleItems[0];
                }
            }
            if (!posts.loading) {
                if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount && !posts.nomore) {
                    posts.loading = true;
                    posts.loadMore(adapter, false);
                }
            }
        }
    });
    return v;
}
Also used : CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) ContributionAdapter(me.ccrama.redditslide.Adapters.ContributionAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) HistoryPosts(me.ccrama.redditslide.Adapters.HistoryPosts) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Example 8 with ToolbarScrollHideHandler

use of me.ccrama.redditslide.handler.ToolbarScrollHideHandler in project Slide by ccrama.

the class SubmissionsView method resetScroll.

public void resetScroll() {
    if (toolbarScroll == null) {
        toolbarScroll = new ToolbarScrollHideHandler(((BaseActivity) getActivity()).mToolbar, header) {

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!posts.loading && !posts.nomore && !posts.offline && !adapter.isError) {
                    visibleItemCount = rv.getLayoutManager().getChildCount();
                    totalItemCount = rv.getLayoutManager().getItemCount();
                    int[] firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(null);
                    if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                        for (int firstVisibleItem : firstVisibleItems) {
                            pastVisiblesItems = firstVisibleItem;
                            if (SettingValues.scrollSeen && pastVisiblesItems > 0 && SettingValues.storeHistory) {
                                HasSeen.addSeenScrolling(posts.posts.get(pastVisiblesItems - 1).getFullName());
                            }
                        }
                    }
                    if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                        posts.loading = true;
                        posts.loadMore(mSwipeRefreshLayout.getContext(), SubmissionsView.this, false, posts.subreddit);
                    }
                }
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) {
                    diff += dy;
                } else {
                    diff = 0;
                }
                if (fab != null) {
                    if (dy <= 0 && fab.getId() != 0 && SettingValues.fab) {
                        if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING || diff < -fab.getHeight() * 2) {
                            fab.show();
                        }
                    } else {
                        if (!SettingValues.alwaysShowFAB) {
                            fab.hide();
                        }
                    }
                }
            }

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                // switch (newState) {
                // case RecyclerView.SCROLL_STATE_IDLE:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_DRAGGING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_SETTLING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().pause();
                // break;
                // }
                super.onScrollStateChanged(recyclerView, newState);
                // If the toolbar search is open, and the user scrolls in the Main view--close the search UI
                if (getActivity() instanceof MainActivity && (SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_TOOLBAR || SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_BOTH) && ((MainActivity) getContext()).findViewById(R.id.toolbar_search).getVisibility() == View.VISIBLE) {
                    ((MainActivity) getContext()).findViewById(R.id.close_search_toolbar).performClick();
                }
            }
        };
        rv.addOnScrollListener(toolbarScroll);
    } else {
        toolbarScroll.reset = true;
    }
}
Also used : BaseActivity(me.ccrama.redditslide.Activities.BaseActivity) RecyclerView(androidx.recyclerview.widget.RecyclerView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Example 9 with ToolbarScrollHideHandler

use of me.ccrama.redditslide.handler.ToolbarScrollHideHandler in project Slide by ccrama.

the class SubredditListView method doAdapter.

public void doAdapter() {
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });
    posts = new SubredditNames(where, getContext(), SubredditListView.this);
    adapter = new SubredditAdapter(getActivity(), posts, rv, where, this);
    rv.setAdapter(adapter);
    posts.loadMore(mSwipeRefreshLayout.getContext(), true, where);
    mSwipeRefreshLayout.setOnRefreshListener(this::refresh);
    rv.addOnScrollListener(new ToolbarScrollHideHandler(((BaseActivity) getActivity()).mToolbar, getActivity().findViewById(R.id.header)) {

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (!posts.loading && !posts.nomore) {
                visibleItemCount = rv.getLayoutManager().getChildCount();
                totalItemCount = rv.getLayoutManager().getItemCount();
                pastVisiblesItems = ((LinearLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
                if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                    posts.loading = true;
                    LogUtil.v("Loading more");
                    posts.loadMore(mSwipeRefreshLayout.getContext(), false, where);
                }
            }
        }
    });
}
Also used : BaseActivity(me.ccrama.redditslide.Activities.BaseActivity) RecyclerView(androidx.recyclerview.widget.RecyclerView) SubredditAdapter(me.ccrama.redditslide.Adapters.SubredditAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SubredditNames(me.ccrama.redditslide.Adapters.SubredditNames) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Example 10 with ToolbarScrollHideHandler

use of me.ccrama.redditslide.handler.ToolbarScrollHideHandler in project Slide by ccrama.

the class ModPage method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);
    RecyclerView rv = v.findViewById(R.id.vertical_content);
    final PreCachingLayoutManager mLayoutManager = new PreCachingLayoutManager(getActivity());
    rv.setLayoutManager(mLayoutManager);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    final SwipeRefreshLayout mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getActivity()));
    // If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    // So, we estimate the height of the header in dp
    mSwipeRefreshLayout.setProgressViewOffset(false, Constants.TAB_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.TAB_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });
    posts = new ModeratorPosts(id, sub);
    adapter = new ModeratorAdapter(getActivity(), posts, rv);
    rv.setAdapter(adapter);
    rv.addOnScrollListener(new ToolbarScrollHideHandler(((ModQueue) getActivity()).mToolbar, (getActivity()).findViewById(R.id.header)));
    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            posts.loadMore(adapter, id, sub);
        }
    });
    return v;
}
Also used : ModQueue(me.ccrama.redditslide.Activities.ModQueue) ModeratorAdapter(me.ccrama.redditslide.Adapters.ModeratorAdapter) ModeratorPosts(me.ccrama.redditslide.Adapters.ModeratorPosts) RecyclerView(androidx.recyclerview.widget.RecyclerView) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Aggregations

RecyclerView (androidx.recyclerview.widget.RecyclerView)13 ToolbarScrollHideHandler (me.ccrama.redditslide.handler.ToolbarScrollHideHandler)13 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)9 View (android.view.View)8 PreCachingLayoutManager (me.ccrama.redditslide.Views.PreCachingLayoutManager)8 CatchStaggeredGridLayoutManager (me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager)7 ContributionAdapter (me.ccrama.redditslide.Adapters.ContributionAdapter)5 BaseActivity (me.ccrama.redditslide.Activities.BaseActivity)3 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 NonNull (androidx.annotation.NonNull)2 AlertDialog (androidx.appcompat.app.AlertDialog)2 MainActivity (me.ccrama.redditslide.Activities.MainActivity)2 ModQueue (me.ccrama.redditslide.Activities.ModQueue)2 HistoryPosts (me.ccrama.redditslide.Adapters.HistoryPosts)2 Constants (me.ccrama.redditslide.Constants)2 R (me.ccrama.redditslide.R)2 Palette (me.ccrama.redditslide.Visuals.Palette)2 Configuration (android.content.res.Configuration)1 TextPaint (android.text.TextPaint)1