Search in sources :

Example 1 with ScrollAwareFloatingActionButtonBehavior

use of com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior in project Reader by TheKeeperOfPie.

the class FragmentComments method onCreateView.

@SuppressWarnings("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    layoutRoot = (CustomFrameLayout) inflater.inflate(R.layout.fragment_comments, container, false);
    ButterKnife.bind(this, layoutRoot);
    youTubeListener = new YouTubeListener() {

        @Override
        public void loadYouTubeVideo(Link link, final String id, final int timeInMillis) {
            loadYoutubeVideo(id, timeInMillis);
        }

        @Override
        public boolean hideYouTube() {
            if (layoutYouTube.isShown()) {
                if (youTubePlayer != null) {
                    youTubePlayer.pause();
                }
                toggleYouTubeVisibility(View.GONE);
                return false;
            }
            return true;
        }
    };
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    toolbar.setBackgroundColor(themer.getColorPrimary());
    setUpToolbar();
    behaviorFloatingActionButton = new ScrollAwareFloatingActionButtonBehavior(getActivity(), null, new ScrollAwareFloatingActionButtonBehavior.OnVisibilityChangeListener() {

        @Override
        public void onStartHideFromScroll() {
            hideLayoutActions(0);
        }

        @Override
        public void onEndHideFromScroll() {
            buttonExpandActions.setImageResource(R.drawable.ic_unfold_more_white_24dp);
            buttonExpandActions.setColorFilter(themer.getColorFilterAccent());
        }
    });
    buttonExpandActions.setOnClickListener(v -> toggleLayoutActions());
    ((CoordinatorLayout.LayoutParams) buttonExpandActions.getLayoutParams()).setBehavior(behaviorFloatingActionButton);
    buttonJumpTop.setOnClickListener(v -> fragmentCurrent.scrollToPositionWithOffset(0, 0));
    buttonJumpTop.setOnLongClickListener(v -> {
        Toast.makeText(getActivity(), getString(R.string.content_description_button_jump_top), Toast.LENGTH_SHORT).show();
        return false;
    });
    buttonCommentPrevious.setOnClickListener(v -> fragmentCurrent.previousComment());
    buttonCommentPrevious.setOnLongClickListener(v -> {
        Toast.makeText(getActivity(), getString(R.string.content_description_button_comment_previous), Toast.LENGTH_SHORT).show();
        return false;
    });
    buttonCommentNext.setOnClickListener(v -> fragmentCurrent.nextComment());
    buttonCommentNext.setOnLongClickListener(v -> {
        Toast.makeText(getActivity(), getString(R.string.content_description_button_comment_next), Toast.LENGTH_SHORT).show();
        return false;
    });
    // Margin is included within shadow margin on pre-Lollipop, so remove all regular margin
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ((CoordinatorLayout.LayoutParams) buttonExpandActions.getLayoutParams()).setMargins(0, 0, 0, 0);
        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
        LinearLayout.LayoutParams layoutParamsJumpTop = (LinearLayout.LayoutParams) buttonJumpTop.getLayoutParams();
        layoutParamsJumpTop.setMargins(0, 0, 0, 0);
        buttonJumpTop.setLayoutParams(layoutParamsJumpTop);
        LinearLayout.LayoutParams layoutParamsPrevious = (LinearLayout.LayoutParams) buttonCommentPrevious.getLayoutParams();
        layoutParamsPrevious.setMargins(0, 0, 0, 0);
        buttonCommentPrevious.setLayoutParams(layoutParamsPrevious);
        LinearLayout.LayoutParams layoutParamsNext = (LinearLayout.LayoutParams) buttonCommentNext.getLayoutParams();
        layoutParamsNext.setMargins(0, 0, 0, 0);
        buttonCommentNext.setLayoutParams(layoutParamsNext);
        ViewGroup.MarginLayoutParams layoutParamsActions = (ViewGroup.MarginLayoutParams) layoutActions.getLayoutParams();
        layoutParamsActions.setMarginStart(margin);
        layoutParamsActions.setMarginEnd(margin);
        layoutActions.setLayoutParams(layoutParamsActions);
    }
    buttonExpandActions.setColorFilter(themer.getColorFilterAccent());
    buttonJumpTop.setColorFilter(themer.getColorFilterAccent());
    buttonCommentPrevious.setColorFilter(themer.getColorFilterAccent());
    buttonCommentNext.setColorFilter(themer.getColorFilterAccent());
    isStartOnLeft = getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
    final float screenWidth = getResources().getDisplayMetrics().widthPixels;
    swipeEndDistance = screenWidth * 0.4f;
    gestureDetector = new GestureDetectorCompat(getActivity(), new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (isFinished) {
                return true;
            }
            swipeDifferenceX = e2.getX() - e1.getX();
            if (isStartOnLeft) {
                if (e1.getX() > screenWidth * 0.2f || swipeDifferenceX < 0) {
                    return super.onScroll(e1, e2, distanceX, distanceY);
                }
            } else {
                if (e1.getX() < screenWidth * 0.8f || swipeDifferenceX > 0) {
                    return super.onScroll(e1, e2, distanceX, distanceY);
                }
            }
            if (!hasSwipedEnd) {
                FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
                if (fragment != null) {
                    fragment.setVisibilityOfThing(View.VISIBLE, linkTop);
                    fragment.onHiddenChanged(false);
                }
                hasSwipedEnd = true;
            }
            float ratio = 1f - swipeDifferenceX / screenWidth;
            buttonExpandActions.setAlpha(ratio);
            buttonExpandActions.setScaleX(ratio);
            buttonExpandActions.setScaleY(ratio);
            layoutAppBar.setTranslationX(swipeDifferenceX);
            layoutComments.setTranslationX(swipeDifferenceX);
            viewBackground.setTranslationX(swipeDifferenceX);
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    });
    if (preferences.getBoolean(AppSettings.SWIPE_EXIT_COMMENTS, true)) {
        layoutRoot.setDispatchTouchListener(this);
    }
    if (savedInstanceState == null) {
        if (getArguments().getInt(ARG_YOUTUBE_TIME, -1) >= 0) {
            loadYoutubeVideo(getArguments().getString(ARG_YOUTUBE_ID), getArguments().getInt(ARG_YOUTUBE_TIME));
        }
    } else {
        String youtubeId = savedInstanceState.getString(ARG_YOUTUBE_ID, null);
        if (!TextUtils.isEmpty(youtubeId)) {
            loadYoutubeVideo(youtubeId, savedInstanceState.getInt(ARG_YOUTUBE_TIME, 0));
        }
    }
    fragmentCallback = new FragmentCommentsInner.Callback() {

        @Override
        public void loadYouTubeVideo(String id, int timeInMillis) {
            FragmentComments.this.loadYoutubeVideo(id, timeInMillis);
        }

        @Override
        public void releaseYouTube() {
            FragmentComments.this.releaseYouTube();
        }

        @Override
        public void setPostExpanded(boolean expanded) {
            FragmentComments.this.setPostExpanded(expanded);
        }

        @Override
        public void setIsCommentThread(boolean isCommentThread) {
            FragmentComments.this.setIsCommentThread(isCommentThread);
        }

        @Override
        public void setSort(Sort sort) {
            FragmentComments.this.setSort(sort);
        }

        @Override
        public void setTitle(CharSequence title) {
            FragmentComments.this.setTitle(title);
        }

        @Override
        public void clearDecoration() {
            behaviorFloatingActionButton.animateOut(buttonExpandActions);
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public int getAppBarHeight() {
            return layoutAppBar.getHeight();
        }

        @Override
        public boolean isCurrentFragment(FragmentCommentsInner fragmentCommentsInner) {
            return positionCurrent == fragmentCommentsInner.getPosition();
        }

        @Override
        public void loadYouTubeVideo(Link link, String id, int timeInMillis) {
            youTubeListener.loadYouTubeVideo(link, id, timeInMillis);
        }

        @Override
        public boolean hideYouTube() {
            return youTubeListener.hideYouTube();
        }
    };
    setUpPager();
    if (!getArguments().getBoolean(ARG_INITIALIZED, false)) {
        viewBackground.setVisibility(View.INVISIBLE);
        pagerComments.setVisibility(View.INVISIBLE);
        layoutAppBar.setVisibility(View.GONE);
        layoutComments.postOnAnimation(() -> animateEnter(layoutRoot));
    } else {
        setAnimationFinished(true);
    }
    return layoutRoot;
}
Also used : ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) FragmentBase(com.winsonchiu.reader.FragmentBase) Sort(com.winsonchiu.reader.data.reddit.Sort) AppBarLayout(android.support.design.widget.AppBarLayout) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) ViewGroup(android.view.ViewGroup) GestureDetectorCompat(android.support.v4.view.GestureDetectorCompat) YouTubeListener(com.winsonchiu.reader.utils.YouTubeListener) MotionEvent(android.view.MotionEvent) AdapterLink(com.winsonchiu.reader.links.AdapterLink) Link(com.winsonchiu.reader.data.reddit.Link) LinearLayout(android.widget.LinearLayout)

Example 2 with ScrollAwareFloatingActionButtonBehavior

use of com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior in project Reader by TheKeeperOfPie.

the class FragmentInbox method onCreateView.

@SuppressWarnings("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_inbox, container, false);
    listener = new ControllerInbox.Listener() {

        @Override
        public void setPage(Page page) {
            spinnerPage.setSelection(adapterInboxPage.getPages().indexOf(page));
        }

        @Override
        public RecyclerView.Adapter getAdapter() {
            return adapterInbox;
        }

        @Override
        public void setToolbarTitle(CharSequence title) {
            toolbar.setTitle(title);
        }

        @Override
        public void setRefreshing(boolean refreshing) {
            swipeRefreshInbox.setRefreshing(refreshing);
        }

        @Override
        public void post(Runnable runnable) {
            recyclerInbox.post(runnable);
        }
    };
    layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
    layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
    int styleColorBackground = AppSettings.THEME_DARK.equals(mListener.getThemeBackground()) ? R.style.MenuDark : R.style.MenuLight;
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(new ThemeWrapper(activity, UtilsColor.getThemeForColor(getResources(), themer.getColorPrimary(), mListener)), styleColorBackground);
    toolbar = (Toolbar) activity.getLayoutInflater().cloneInContext(contextThemeWrapper).inflate(R.layout.toolbar, layoutAppBar, false);
    layoutAppBar.addView(toolbar);
    ((AppBarLayout.LayoutParams) toolbar.getLayoutParams()).setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
    toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mListener.openDrawer();
        }
    });
    setUpOptionsMenu();
    floatingActionButtonNewMessage = (FloatingActionButton) view.findViewById(R.id.fab_new_message);
    floatingActionButtonNewMessage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentNewMessage fragmentNewMessage = FragmentNewMessage.newInstance();
            getFragmentManager().beginTransaction().hide(FragmentInbox.this).add(R.id.frame_fragment, fragmentNewMessage, FragmentNewMessage.TAG).addToBackStack(null).commit();
        }
    });
    floatingActionButtonNewMessage.setColorFilter(themer.getColorFilterAccent());
    behaviorFloatingActionButton = new ScrollAwareFloatingActionButtonBehavior(activity, null, new ScrollAwareFloatingActionButtonBehavior.OnVisibilityChangeListener() {

        @Override
        public void onStartHideFromScroll() {
        }

        @Override
        public void onEndHideFromScroll() {
        }
    });
    ((CoordinatorLayout.LayoutParams) floatingActionButtonNewMessage.getLayoutParams()).setBehavior(behaviorFloatingActionButton);
    adapterInboxPage = new AdapterInboxPage(activity);
    spinnerPage = new AppCompatSpinner(contextThemeWrapper);
    toolbar.addView(spinnerPage);
    ((Toolbar.LayoutParams) spinnerPage.getLayoutParams()).setMarginEnd((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()));
    spinnerPage.setAdapter(adapterInboxPage);
    spinnerPage.setSelection(adapterInboxPage.getPages().indexOf(controllerInbox.getPage()));
    spinnerPage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            controllerInbox.setPage(adapterInboxPage.getItem(position));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    swipeRefreshInbox = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_inbox);
    swipeRefreshInbox.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            controllerInbox.reload();
        }
    });
    linearLayoutManager = new LinearLayoutManager(activity);
    recyclerInbox = (RecyclerView) view.findViewById(R.id.recycler_inbox);
    recyclerInbox.setHasFixedSize(true);
    recyclerInbox.setItemAnimator(new DefaultItemAnimator());
    recyclerInbox.getItemAnimator().setRemoveDuration(UtilsAnimation.EXPAND_ACTION_DURATION);
    recyclerInbox.setLayoutManager(linearLayoutManager);
    recyclerInbox.addItemDecoration(new ItemDecorationDivider(activity, ItemDecorationDivider.VERTICAL_LIST));
    AdapterListener adapterListener = new AdapterListener() {

        @Override
        public void scrollAndCenter(int position, int height) {
            linearLayoutManager.scrollToPositionWithOffset(position, 0);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void clearDecoration() {
            behaviorFloatingActionButton.animateOut(floatingActionButtonNewMessage);
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void requestMore() {
            controllerInbox.loadMore();
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerInbox.requestDisallowInterceptTouchEvent(disallow);
            swipeRefreshInbox.requestDisallowInterceptTouchEvent(disallow);
        }

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
        }
    };
    AdapterCommentList.ViewHolderComment.Listener listenerComments = new AdapterCommentList.ViewHolderComment.Listener() {

        @Override
        public void onToggleComment(Comment comment) {
        }

        @Override
        public void onShowReplyEditor(Comment comment) {
        }

        @Override
        public void onEditComment(Comment comment, String text) {
        }

        @Override
        public void onSendComment(Comment comment, String text) {
        }

        @Override
        public void onMarkRead(Comment comment) {
        }

        @Override
        public void onLoadNestedComments(Comment comment) {
        }

        @Override
        public void onJumpToParent(Comment comment) {
        }

        @Override
        public void onViewProfile(Comment comment) {
        }

        @Override
        public void onCopyText(Comment comment) {
        }

        @Override
        public void onDeleteComment(Comment comment) {
        }

        @Override
        public void onReport(Comment comment) {
        }

        @Override
        public void onVoteComment(Comment comment, AdapterCommentList.ViewHolderComment viewHolderComment, Likes vote) {
        }

        @Override
        public void onSave(Comment comment) {
        }
    };
    if (adapterInbox == null) {
        adapterInbox = new AdapterInbox(controllerInbox, controllerUser, adapterListener, listenerComments, mListener.getEventListenerBase());
    }
    recyclerInbox.setAdapter(adapterInbox);
    return view;
}
Also used : ItemDecorationDivider(com.winsonchiu.reader.utils.ItemDecorationDivider) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) Page(com.winsonchiu.reader.data.Page) FragmentNewMessage(com.winsonchiu.reader.FragmentNewMessage) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) ContextThemeWrapper(android.view.ContextThemeWrapper) ThemeWrapper(com.winsonchiu.reader.theme.ThemeWrapper) AdapterCommentList(com.winsonchiu.reader.comments.AdapterCommentList) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) AppBarLayout(android.support.design.widget.AppBarLayout) Comment(com.winsonchiu.reader.data.reddit.Comment) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) AppCompatSpinner(android.support.v7.widget.AppCompatSpinner) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) ContextThemeWrapper(android.view.ContextThemeWrapper) AdapterView(android.widget.AdapterView) Likes(com.winsonchiu.reader.data.reddit.Likes)

Example 3 with ScrollAwareFloatingActionButtonBehavior

use of com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior in project Reader by TheKeeperOfPie.

the class FragmentThreadList method onCreateView.

@SuppressWarnings("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    initialize();
    view = bind(inflater.inflate(R.layout.fragment_thread_list, container, false));
    setUpToolbar();
    buttonExpandActions.setOnClickListener(v -> toggleLayoutActions());
    behaviorButtonExpandActions = new ScrollAwareFloatingActionButtonBehavior(getActivity(), null, new ScrollAwareFloatingActionButtonBehavior.OnVisibilityChangeListener() {

        @Override
        public void onStartHideFromScroll() {
            hideLayoutActions(0);
        }

        @Override
        public void onEndHideFromScroll() {
            buttonExpandActions.setImageResource(R.drawable.ic_unfold_more_white_24dp);
            buttonExpandActions.setColorFilter(themer.getColorFilterAccent());
        }
    });
    ((CoordinatorLayout.LayoutParams) buttonExpandActions.getLayoutParams()).setBehavior(behaviorButtonExpandActions);
    buttonJumpTop.setOnClickListener(v -> scrollToPositionWithOffset(0, 0));
    buttonJumpTop.setOnLongClickListener(v -> {
        Toast.makeText(getActivity(), getString(R.string.content_description_button_jump_top), Toast.LENGTH_SHORT).show();
        return false;
    });
    buttonClearViewed.setOnClickListener(v -> controllerLinks.clearViewed(historian));
    buttonClearViewed.setOnLongClickListener(v -> {
        Toast.makeText(getActivity(), getString(R.string.content_description_button_clear_viewed), Toast.LENGTH_SHORT).show();
        return false;
    });
    // Margin is included within shadow margin on pre-Lollipop, so remove all regular margin
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ((CoordinatorLayout.LayoutParams) buttonExpandActions.getLayoutParams()).setMargins(0, 0, 0, 0);
        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
        LinearLayout.LayoutParams layoutParamsJumpTop = (LinearLayout.LayoutParams) buttonJumpTop.getLayoutParams();
        layoutParamsJumpTop.setMargins(0, 0, 0, 0);
        buttonJumpTop.setLayoutParams(layoutParamsJumpTop);
        LinearLayout.LayoutParams layoutParamsClearViewed = (LinearLayout.LayoutParams) buttonClearViewed.getLayoutParams();
        layoutParamsClearViewed.setMargins(0, 0, 0, 0);
        buttonClearViewed.setLayoutParams(layoutParamsClearViewed);
        RelativeLayout.LayoutParams layoutParamsActions = (RelativeLayout.LayoutParams) layoutActions.getLayoutParams();
        layoutParamsActions.setMarginStart(margin);
        layoutParamsActions.setMarginEnd(margin);
        layoutActions.setLayoutParams(layoutParamsActions);
    }
    buttonExpandActions.setColorFilter(themer.getColorFilterAccent());
    buttonJumpTop.setColorFilter(themer.getColorFilterAccent());
    buttonClearViewed.setColorFilter(themer.getColorFilterAccent());
    swipeRefreshThreadList.setOnRefreshListener(() -> {
        controllerLinks.reload();
    });
    AdapterListener adapterListener = new AdapterListener() {

        @Override
        public void requestMore() {
            controllerLinks.loadMore();
        }

        @Override
        public void scrollAndCenter(int position, int height) {
            UtilsAnimation.scrollToPositionWithCentering(position, recyclerThreadList, height, 0, 0, false);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void clearDecoration() {
            behaviorButtonExpandActions.animateOut(buttonExpandActions);
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerThreadList.requestDisallowInterceptTouchEvent(disallow);
            swipeRefreshThreadList.requestDisallowInterceptTouchEvent(disallow);
            itemTouchHelper.select(null, CustomItemTouchHelper.ACTION_STATE_IDLE);
        }

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
            itemTouchHelper.setDisallow(disallow);
        }
    };
    AdapterLink.ViewHolderLink.Listener listener = new LinksListenerBase(mListener.getEventListenerBase()) {

        @Override
        public void onVote(Link link, AdapterLink.ViewHolderLink viewHolderLink, Likes vote) {
            mListener.getEventListenerBase().onVote(link, vote).subscribe(new ObserverEmpty<Link>() {

                @Override
                public void onError(Throwable e) {
                    controllerLinks.getEventHolder().getErrors().call(LinksError.VOTE);
                }

                @Override
                public void onNext(Link link) {
                    controllerLinks.update(link);
                }
            });
        }

        @Override
        public void onDelete(Link link) {
            new AlertDialog.Builder(getContext()).setTitle(R.string.delete_post).setMessage(link.getTitle()).setPositiveButton(R.string.yes, (dialog, which) -> {
                mListener.getEventListenerBase().onDelete(link).subscribe(new ObserverEmpty<Link>() {

                    @Override
                    public void onError(Throwable e) {
                        controllerLinks.getEventHolder().getErrors().call(LinksError.DELETE);
                    }

                    @Override
                    public void onNext(Link link) {
                        controllerLinks.remove(link);
                    }
                });
            }).setNegativeButton(R.string.no, null).show();
        }

        @Override
        public void onReport(Link link) {
            // TODO: Add link title
            new AlertDialog.Builder(getContext()).setTitle(R.string.report_title).setSingleChoiceItems(Report.getDisplayReasons(getResources()), -1, (dialog, which) -> {
                reportSelected = Report.values()[which];
            }).setPositiveButton(R.string.ok, (dialog, which) -> {
                if (reportSelected == Report.OTHER) {
                    View viewDialog = LayoutInflater.from(getContext()).inflate(R.layout.dialog_text_input, null, false);
                    final EditText editText = (EditText) viewDialog.findViewById(R.id.edit_text);
                    editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(100) });
                    new AlertDialog.Builder(getContext()).setView(viewDialog).setTitle(R.string.item_report).setPositiveButton(R.string.ok, (dialog1, which1) -> {
                        mListener.getEventListenerBase().onReport(link, editText.getText().toString()).subscribe(new ObserverError<String>() {

                            @Override
                            public void onError(Throwable e) {
                                controllerLinks.getEventHolder().getErrors().call(LinksError.REPORT);
                            }
                        });
                    }).setNegativeButton(R.string.cancel, (dialog1, which1) -> {
                        dialog1.dismiss();
                    }).show();
                } else if (reportSelected != null) {
                    mListener.getEventListenerBase().onReport(link, reportSelected.getReason()).subscribe(new ObserverError<String>() {

                        @Override
                        public void onError(Throwable e) {
                            controllerLinks.getEventHolder().getErrors().call(LinksError.REPORT);
                        }
                    });
                }
            }).setNegativeButton(R.string.cancel, null).show();
        }

        @Override
        public void onSave(Link link) {
            if (link.isSaved()) {
                mListener.getEventListenerBase().onUnsave(link).subscribe(new ObserverEmpty<Link>() {

                    @Override
                    public void onError(Throwable e) {
                        controllerLinks.getEventHolder().getErrors().call(LinksError.UNSAVE);
                    }

                    @Override
                    public void onNext(Link link) {
                        controllerLinks.update(link);
                    }
                });
            } else {
                mListener.getEventListenerBase().onSave(link).subscribe(new ObserverEmpty<Link>() {

                    @Override
                    public void onError(Throwable e) {
                        controllerLinks.getEventHolder().getErrors().call(LinksError.SAVE);
                    }

                    @Override
                    public void onNext(Link link) {
                        controllerLinks.update(link);
                    }
                });
            }
        }

        @Override
        public void onMarkNsfw(Link link) {
            if (link.isOver18()) {
                mListener.getEventListenerBase().onUnmarkNsfw(link).subscribe(new ObserverEmpty<Link>() {

                    @Override
                    public void onError(Throwable e) {
                        controllerLinks.getEventHolder().getErrors().call(LinksError.UNMARK_NSFW);
                    }

                    @Override
                    public void onNext(Link link) {
                        controllerLinks.update(link);
                    }
                });
            } else {
                mListener.getEventListenerBase().onMarkNsfw(link).subscribe(new ObserverEmpty<Link>() {

                    @Override
                    public void onError(Throwable e) {
                        controllerLinks.getEventHolder().getErrors().call(LinksError.MARK_NSFW);
                    }

                    @Override
                    public void onNext(Link link) {
                        controllerLinks.update(link);
                    }
                });
            }
        }
    };
    adapterLinkList = new AdapterLinkList(getActivity(), adapterListener, eventListenerHeader, listener);
    adapterLinkGrid = new AdapterLinkGrid(getActivity(), adapterListener, eventListenerHeader, listener);
    adapterLink = AppSettings.MODE_GRID.equals(preferences.getString(AppSettings.INTERFACE_MODE, AppSettings.MODE_GRID)) ? adapterLinkGrid : adapterLinkList;
    itemDecorationDivider = new ItemDecorationDivider(getActivity(), ItemDecorationDivider.VERTICAL_LIST);
    //        recyclerThreadList.setItemAnimator(null);
    resetAdapter(adapterLink);
    recyclerThreadList.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            switch(newState) {
                case RecyclerView.SCROLL_STATE_IDLE:
                case RecyclerView.SCROLL_STATE_DRAGGING:
                    picasso.resumeTag(AdapterLink.TAG_PICASSO);
                    break;
                case RecyclerView.SCROLL_STATE_SETTLING:
                    picasso.pauseTag(AdapterLink.TAG_PICASSO);
                    break;
            }
        }
    });
    itemTouchHelper = new CustomItemTouchHelper(new CustomItemTouchHelper.SimpleCallback(getActivity(), R.drawable.ic_visibility_off_white_24dp, ItemTouchHelper.START | ItemTouchHelper.END, ItemTouchHelper.START | ItemTouchHelper.END) {

        @Override
        public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
            if (layoutManager instanceof StaggeredGridLayoutManager) {
                return 1f / ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
            }
            return 0.5f;
        }

        @Override
        public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            if (viewHolder.getAdapterPosition() == 0) {
                return 0;
            }
            ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
            if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams && !((StaggeredGridLayoutManager.LayoutParams) layoutParams).isFullSpan()) {
                int spanCount = layoutManager instanceof StaggeredGridLayoutManager ? ((StaggeredGridLayoutManager) layoutManager).getSpanCount() : 2;
                int spanIndex = ((StaggeredGridLayoutManager.LayoutParams) layoutParams).getSpanIndex() % spanCount;
                if (spanIndex == 0) {
                    return ItemTouchHelper.END;
                } else if (spanIndex == spanCount - 1) {
                    return ItemTouchHelper.START;
                }
            }
            return super.getSwipeDirs(recyclerView, viewHolder);
        }

        @Override
        public boolean isLongPressDragEnabled() {
            return false;
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
            // Offset by 1 due to subreddit header
            Link link = controllerLinks.hideLink(viewHolder.getAdapterPosition());
            mListener.getEventListenerBase().hide(link);
            if (snackbar != null) {
                snackbar.dismiss();
            }
            SpannableString text = new SpannableString(link.isHidden() ? getString(R.string.link_hidden) : getString(R.string.link_shown));
            text.setSpan(new ForegroundColorSpan(themer.getColorFilterPrimary().getColor()), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            //noinspection ResourceType
            snackbar = Snackbar.make(recyclerThreadList, text, UtilsAnimation.SNACKBAR_DURATION).setActionTextColor(themer.getColorFilterPrimary().getColor()).setAction(R.string.undo, v -> {
                mListener.getEventListenerBase().hide(link);
                controllerLinks.reshowLastHiddenLink();
            });
            snackbar.getView().setBackgroundColor(themer.getColorPrimary());
            snackbar.show();
        }
    });
    itemTouchHelper.attachToRecyclerView(recyclerThreadList);
    recyclerThreadList.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            //                rv.getChildViewHolder(rv.findChildViewUnder(e.getX(), e.getY()));
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        }
    });
    if (layoutManager instanceof LinearLayoutManager) {
        recyclerThreadList.setPadding(0, 0, 0, 0);
    } else {
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        recyclerThreadList.setPadding(padding, 0, padding, 0);
    }
    textSidebar.setMovementMethod(LinkMovementMethod.getInstance());
    buttonSubscribe.setOnClickListener(v -> controllerLinks.subscribe());
    return view;
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) ObserverEmpty(com.winsonchiu.reader.rx.ObserverEmpty) Historian(com.winsonchiu.reader.history.Historian) ViewPropertyAnimatorListener(android.support.v4.view.ViewPropertyAnimatorListener) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) LinkMovementMethod(android.text.method.LinkMovementMethod) UtilsAnimation(com.winsonchiu.reader.utils.UtilsAnimation) BindView(butterknife.BindView) Picasso(com.squareup.picasso.Picasso) Reddit(com.winsonchiu.reader.data.reddit.Reddit) Likes(com.winsonchiu.reader.data.reddit.Likes) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) View(android.view.View) Button(android.widget.Button) ViewCompat(android.support.v4.view.ViewCompat) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Log(android.util.Log) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) ActionLog(com.winsonchiu.reader.rx.ActionLog) FloatingActionButton(android.support.design.widget.FloatingActionButton) CustomItemTouchHelper(com.winsonchiu.reader.utils.CustomItemTouchHelper) UtilsReddit(com.winsonchiu.reader.utils.UtilsReddit) ViewGroup(android.view.ViewGroup) R(com.winsonchiu.reader.R) UtilsColor(com.winsonchiu.reader.utils.UtilsColor) FragmentBase(com.winsonchiu.reader.FragmentBase) FragmentNewPost(com.winsonchiu.reader.FragmentNewPost) TextView(android.widget.TextView) RelativeLayout(android.widget.RelativeLayout) Time(com.winsonchiu.reader.data.reddit.Time) Nullable(android.support.annotation.Nullable) Snackbar(android.support.design.widget.Snackbar) InputFilter(android.text.InputFilter) User(com.winsonchiu.reader.data.reddit.User) Subscription(rx.Subscription) ObserverError(com.winsonchiu.reader.rx.ObserverError) Report(com.winsonchiu.reader.data.reddit.Report) AppBarLayout(android.support.design.widget.AppBarLayout) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) Spanned(android.text.Spanned) FastOutSlowInInterpolator(android.support.v4.view.animation.FastOutSlowInInterpolator) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) AppSettings(com.winsonchiu.reader.AppSettings) MenuItem(android.view.MenuItem) GravityCompat(android.support.v4.view.GravityCompat) Inject(javax.inject.Inject) MotionEvent(android.view.MotionEvent) Toast(android.widget.Toast) Menu(android.view.Menu) Link(com.winsonchiu.reader.data.reddit.Link) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) Build(android.os.Build) DrawerLayout(android.support.v4.widget.DrawerLayout) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) SpannableString(android.text.SpannableString) LayoutInflater(android.view.LayoutInflater) ControllerUser(com.winsonchiu.reader.ControllerUser) Sort(com.winsonchiu.reader.data.reddit.Sort) ItemDecorationDivider(com.winsonchiu.reader.utils.ItemDecorationDivider) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) TextUtils(android.text.TextUtils) Thing(com.winsonchiu.reader.data.reddit.Thing) FragmentSearch(com.winsonchiu.reader.search.FragmentSearch) RecyclerView(android.support.v7.widget.RecyclerView) AlertDialog(android.support.v7.app.AlertDialog) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) Toolbar(android.support.v7.widget.Toolbar) ActivityMain(com.winsonchiu.reader.ActivityMain) FragmentListenerBase(com.winsonchiu.reader.FragmentListenerBase) Activity(android.app.Activity) UtilsTheme(com.winsonchiu.reader.utils.UtilsTheme) EditText(android.widget.EditText) ItemDecorationDivider(com.winsonchiu.reader.utils.ItemDecorationDivider) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) SpannableString(android.text.SpannableString) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) AppBarLayout(android.support.design.widget.AppBarLayout) EditText(android.widget.EditText) InputFilter(android.text.InputFilter) ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannableString(android.text.SpannableString) RelativeLayout(android.widget.RelativeLayout) Link(com.winsonchiu.reader.data.reddit.Link) CustomItemTouchHelper(com.winsonchiu.reader.utils.CustomItemTouchHelper) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) ViewGroup(android.view.ViewGroup) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) MotionEvent(android.view.MotionEvent) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) ObserverError(com.winsonchiu.reader.rx.ObserverError) RecyclerView(android.support.v7.widget.RecyclerView) Likes(com.winsonchiu.reader.data.reddit.Likes) LinearLayout(android.widget.LinearLayout)

Aggregations

AppBarLayout (android.support.design.widget.AppBarLayout)3 ScrollAwareFloatingActionButtonBehavior (com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior)3 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)2 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 RecyclerView (android.support.v7.widget.RecyclerView)2 MotionEvent (android.view.MotionEvent)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 LinearLayout (android.widget.LinearLayout)2 FragmentBase (com.winsonchiu.reader.FragmentBase)2 AdapterListener (com.winsonchiu.reader.adapter.AdapterListener)2 Likes (com.winsonchiu.reader.data.reddit.Likes)2 Link (com.winsonchiu.reader.data.reddit.Link)2 Sort (com.winsonchiu.reader.data.reddit.Sort)2 ItemDecorationDivider (com.winsonchiu.reader.utils.ItemDecorationDivider)2 Activity (android.app.Activity)1 SharedPreferences (android.content.SharedPreferences)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1