Search in sources :

Example 1 with FragmentBase

use of com.winsonchiu.reader.FragmentBase in project Reader by TheKeeperOfPie.

the class FragmentComments method onTouch.

@Override
public boolean onTouch(View v, MotionEvent event) {
    boolean value = gestureDetector.onTouchEvent(event);
    if (isFinished || MotionEventCompat.getActionMasked(event) != MotionEvent.ACTION_UP || !hasSwipedEnd) {
        return false;
    }
    if (isStartOnLeft && swipeDifferenceX > swipeEndDistance) {
        slideExit();
        return false;
    } else if (!isStartOnLeft && swipeDifferenceX < -swipeEndDistance) {
        slideExit();
        return false;
    } else {
        hasSwipedEnd = false;
        if (!value) {
            behaviorFloatingActionButton.animateIn(buttonExpandActions);
            layoutAppBar.animate().translationX(0);
            viewBackground.animate().translationX(0);
            layoutComments.animate().translationX(0).setListener(new Animator.AnimatorListener() {

                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if (!isFinished) {
                        FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
                        if (fragment != null) {
                            fragment.setVisibilityOfThing(View.INVISIBLE, linkTop);
                            fragment.onHiddenChanged(true);
                        }
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
        }
    }
    return false;
}
Also used : ViewPropertyAnimatorListener(android.support.v4.view.ViewPropertyAnimatorListener) Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) FragmentBase(com.winsonchiu.reader.FragmentBase)

Example 2 with FragmentBase

use of com.winsonchiu.reader.FragmentBase in project Reader by TheKeeperOfPie.

the class FragmentComments method slideExit.

private void slideExit() {
    isFinished = true;
    viewBackground.setVisibility(View.VISIBLE);
    FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
    if (fragment != null) {
        fragment.setVisibilityOfThing(View.VISIBLE, linkTop);
        fragment.onHiddenChanged(false);
    }
    float screenWidth = getResources().getDisplayMetrics().widthPixels;
    float translationX = isStartOnLeft ? screenWidth : -screenWidth;
    behaviorFloatingActionButton.animateOut(buttonExpandActions);
    layoutAppBar.animate().translationX(translationX);
    viewBackground.animate().translationX(translationX);
    layoutComments.animate().translationX(translationX).setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
            if (fragment != null) {
                fragment.onShown();
                fragment.onHiddenChanged(false);
                getFragmentManager().beginTransaction().show(fragment).commit();
            }
            getActivity().onBackPressed();
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });
}
Also used : Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) FragmentBase(com.winsonchiu.reader.FragmentBase)

Example 3 with FragmentBase

use of com.winsonchiu.reader.FragmentBase 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) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) AppBarLayout(android.support.design.widget.AppBarLayout) 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 4 with FragmentBase

use of com.winsonchiu.reader.FragmentBase in project Reader by TheKeeperOfPie.

the class FragmentComments method animateExit.

private void animateExit() {
    isFinished = true;
    FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
    if (fragment != null) {
        fragment.onHiddenChanged(false);
        getFragmentManager().beginTransaction().show(fragment).commit();
        fragment.onShown();
    }
    viewBackground.setVisibility(View.VISIBLE);
    fragmentCurrent.collapseViewHolderLink(getArguments().getBoolean(ARG_ACTIONS_EXPANDED));
    fragmentCurrent.fadeComments(() -> {
        setAnimationFinished(false);
        if (fragmentCurrent.isPostExpanded()) {
            fragmentCurrent.expandPost(false);
        }
        pagerComments.postOnAnimation(() -> {
            if (!isAdded()) {
                return;
            }
            int[] locationSwipeRefresh = new int[2];
            layoutComments.getLocationOnScreen(locationSwipeRefresh);
            final float targetY = startY - locationSwipeRefresh[1];
            buttonExpandActions.hide();
            buttonJumpTop.hide();
            buttonCommentNext.hide();
            buttonCommentPrevious.hide();
            ValueAnimator animatorExit = ValueAnimator.ofFloat(0, 1);
            animatorExit.setDuration(DURATION_EXIT);
            animatorExit.setInterpolator(fastOutSlowInInterpolator);
            animatorExit.addUpdateListener(animation -> {
                float interpolatedValue = animation.getAnimatedFraction();
                layoutComments.setPadding((int) (startX * interpolatedValue), 0, (int) (startMarginRight * interpolatedValue), 0);
                layoutComments.setTranslationY(targetY * interpolatedValue);
                layoutAppBar.setTranslationY(-toolbarHeight * interpolatedValue);
            });
            animatorExit.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationStart(Animator animation) {
                    FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
                    if (fragment != null) {
                        fragment.onHiddenChanged(false);
                        fragment.setVisibilityOfThing(View.INVISIBLE, linkTop);
                        getFragmentManager().beginTransaction().show(fragment).commit();
                    }
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(fragmentParentTag);
                    if (fragment != null) {
                        fragment.onHiddenChanged(false);
                        getFragmentManager().beginTransaction().show(fragment).commit();
                        fragment.onShown();
                    }
                    getActivity().onBackPressed();
                }
            });
            animatorExit.start();
            if (layoutYouTube.isShown()) {
                layoutYouTube.animate().translationYBy(-(layoutYouTube.getHeight() + toolbar.getHeight()));
            }
        });
    });
}
Also used : Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) FragmentBase(com.winsonchiu.reader.FragmentBase) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) ValueAnimator(android.animation.ValueAnimator)

Aggregations

FragmentBase (com.winsonchiu.reader.FragmentBase)4 Animator (android.animation.Animator)3 ValueAnimator (android.animation.ValueAnimator)3 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 AppBarLayout (android.support.design.widget.AppBarLayout)1 GestureDetectorCompat (android.support.v4.view.GestureDetectorCompat)1 ViewPropertyAnimatorListener (android.support.v4.view.ViewPropertyAnimatorListener)1 MotionEvent (android.view.MotionEvent)1 ViewGroup (android.view.ViewGroup)1 LinearLayout (android.widget.LinearLayout)1 Link (com.winsonchiu.reader.data.reddit.Link)1 Sort (com.winsonchiu.reader.data.reddit.Sort)1 AdapterLink (com.winsonchiu.reader.links.AdapterLink)1 ScrollAwareFloatingActionButtonBehavior (com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior)1 YouTubeListener (com.winsonchiu.reader.utils.YouTubeListener)1