Search in sources :

Example 1 with CustomFrameLayout

use of com.winsonchiu.reader.views.CustomFrameLayout in project Reader by TheKeeperOfPie.

the class FragmentCommentsInner method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    scrollToPaddingTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
    scrollToPaddingBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
    layoutRoot = (CustomFrameLayout) inflater.inflate(R.layout.fragment_comments_inner, container, false);
    CallbackYouTubeDestruction callbackYouTubeDestruction = () -> {
        adapterCommentList.destroyYouTubePlayerFragments();
        adapterLink.destroyYouTubePlayerFragments();
    };
    swipeRefreshCommentList = (SwipeRefreshLayout) layoutRoot.findViewById(R.id.swipe_refresh_comment_list);
    swipeRefreshCommentList.setOnRefreshListener(() -> controllerComments.reloadAllComments());
    linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerCommentList = (RecyclerView) layoutRoot.findViewById(R.id.recycler_comment_list);
    recyclerCommentList.setLayoutManager(linearLayoutManager);
    recyclerCommentList.setHasFixedSize(true);
    recyclerCommentList.setItemAnimator(null);
    AdapterListener adapterListenerComment = new AdapterListener() {

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

        @Override
        public void hideToolbar() {
            callback.hideToolbar();
        }

        @Override
        public void clearDecoration() {
            callback.clearDecoration();
        }

        @Override
        public void requestMore() {
            controllerComments.loadMoreComments();
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerCommentList.requestDisallowInterceptTouchEvent(disallow);
            swipeRefreshCommentList.requestDisallowInterceptTouchEvent(disallow);
        }

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

        @Override
        public void onVote(Link link, AdapterLink.ViewHolderLink viewHolderLink, Likes vote) {
        }

        @Override
        public void onDelete(Link link) {
        }

        @Override
        public void onReport(Link link) {
        }

        @Override
        public void onSave(Link link) {
        }

        @Override
        public void onMarkNsfw(Link link) {
        }
    };
    AdapterCommentList.ViewHolderComment.Listener listenerComment = new AdapterCommentList.ViewHolderComment.Listener() {

        @Override
        public void onToggleComment(Comment comment) {
            controllerComments.toggleComment(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) {
            controllerComments.loadNestedComments(comment);
        }

        @Override
        public void onJumpToParent(Comment comment) {
            controllerComments.jumpToParent(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) {
            // TODO: Add comment text
            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) {
                                controllerComments.getEventHolder().getErrors().call(CommentsError.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) {
                            controllerComments.getEventHolder().getErrors().call(CommentsError.REPORT);
                        }
                    });
                }
            }).setNegativeButton(R.string.cancel, null).show();
        }

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

        @Override
        public void onSave(Comment comment) {
        }
    };
    adapterCommentList = new AdapterCommentList(getActivity(), adapterListenerComment, listenerComment, listenerLink, youTubeListener, callbackYouTubeDestruction, getArguments().getBoolean(ARG_IS_GRID, false), getArguments().getString(ARG_FIRST_LINK_NAME), getArguments().getInt(ARG_COLOR_LINK, 0), getArguments().getBoolean(ARG_ACTIONS_EXPANDED, false));
    observer = new RecyclerView.AdapterDataObserver() {

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            super.onItemRangeChanged(positionStart, itemCount);
            if (positionStart == 0) {
                callback.releaseYouTube();
            }
        }
    };
    adapterCommentList.registerAdapterDataObserver(observer);
    recyclerCommentList.setAdapter(adapterCommentList);
    layoutManagerLink = new LinearLayoutManagerWrapHeight(getActivity(), LinearLayoutManager.VERTICAL, false);
    layoutManagerLink.setOnSizeChangedListener((width, height, oldWidth, oldHeight) -> {
        if (postExpanded && targetExpandPostHeight == 0) {
            targetExpandPostHeight = height + heightExpandHandle;
            postExpanded = false;
            expandPost(true);
        }
    });
    AdapterListener adapterListenerLink = new AdapterListener() {

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

        @Override
        public void hideToolbar() {
            callback.hideToolbar();
        }

        @Override
        public void clearDecoration() {
            callback.clearDecoration();
        }

        @Override
        public void requestMore() {
        }

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

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
        }
    };
    adapterLink = new AdapterLinkHeader(getActivity(), controllerComments, adapterListenerLink, listenerLink, youTubeListener, callbackYouTubeDestruction, getArguments().getBoolean(ARG_IS_GRID, false), getArguments().getString(ARG_FIRST_LINK_NAME), getArguments().getInt(ARG_COLOR_LINK, 0), getArguments().getBoolean(ARG_ACTIONS_EXPANDED, false));
    final GestureDetectorCompat gestureDetectorExpand = new GestureDetectorCompat(getActivity(), new GestureDetector.SimpleOnGestureListener() {

        private float startY;

        private int startHeight;

        @Override
        public boolean onDown(MotionEvent e) {
            if (targetExpandPostHeight == 0) {
                targetExpandPostHeight = layoutManagerLink.getFirstChildHeight() + heightExpandHandle;
            }
            if (e.getY() > imageExpandIndicator.getY() && e.getY() < imageExpandIndicator.getY() + heightExpandHandle) {
                startY = e.getY();
                startHeight = targetExpandPostHeight;
            } else {
                startY = 0;
            }
            return super.onDown(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (startY == 0) {
                return super.onScroll(e1, e2, distanceX, distanceY);
            }
            float distance = e2.getY() - startY;
            targetExpandPostHeight = (int) Math.min(startHeight + distance, layoutRoot.getHeight());
            layoutExpandPostInner.getLayoutParams().height = targetExpandPostHeight;
            layoutExpandPostInner.requestLayout();
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (startY == 0) {
                return super.onFling(e1, e2, velocityX, velocityY);
            }
            if (velocityY < -expandFlingThreshold) {
                startY = 0;
                expandPost(false);
                return true;
            } else if (velocityY > expandFlingThreshold) {
                startY = 0;
                targetExpandPostHeight = layoutRoot.getHeight() - heightExpandHandle;
                postExpanded = false;
                expandPost(true);
                return true;
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    });
    heightExpandHandle = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 33, getResources().getDisplayMetrics());
    expandFlingThreshold = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EXPAND_FLING_THRESHOLD, getResources().getDisplayMetrics());
    layoutExpandPostInner = (ViewGroup) layoutRoot.findViewById(R.id.layout_expand_post_inner);
    imageExpandIndicator = (ImageView) layoutRoot.findViewById(R.id.image_expand_indicator);
    imageExpandIndicator.setColorFilter(themer.getColorFilterIcon());
    layoutRoot.setDispatchTouchListener((v, event) -> gestureDetectorExpand.onTouchEvent(event));
    recyclerLink = (RecyclerView) layoutRoot.findViewById(R.id.recycler_link);
    recyclerLink.setLayoutManager(layoutManagerLink);
    recyclerLink.setItemAnimator(null);
    if (savedInstanceState != null) {
        String youtubeId = savedInstanceState.getString(ARG_YOUTUBE_ID, null);
        if (!TextUtils.isEmpty(youtubeId)) {
            callback.loadYouTubeVideo(youtubeId, savedInstanceState.getInt(ARG_YOUTUBE_TIME, 0));
        }
    }
    if (savedInstanceState != null && savedInstanceState.containsKey("Link")) {
        adapterLink.setAnimationFinished(true);
        adapterCommentList.setAnimationFinished(true);
        controllerComments.setLinkFromCache((Link) savedInstanceState.get("Link"));
    } else if (link != null) {
        controllerComments.setLink(link);
        link = null;
    }
    adapterCommentList.setData(controllerComments.getData());
    if (animationFinished) {
        setAnimationFinished(true);
    }
    return layoutRoot;
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Context(android.content.Context) GestureDetector(android.view.GestureDetector) CallbackYouTubeDestruction(com.winsonchiu.reader.utils.CallbackYouTubeDestruction) Bundle(android.os.Bundle) LinksListenerBase(com.winsonchiu.reader.links.LinksListenerBase) CustomApplication(com.winsonchiu.reader.CustomApplication) AdapterLink(com.winsonchiu.reader.links.AdapterLink) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) ImageView(android.widget.ImageView) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) UtilsList(com.winsonchiu.reader.utils.UtilsList) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Animator(android.animation.Animator) CustomFrameLayout(com.winsonchiu.reader.views.CustomFrameLayout) UtilsAnimation(com.winsonchiu.reader.utils.UtilsAnimation) Likes(com.winsonchiu.reader.data.reddit.Likes) MotionEvent(android.view.MotionEvent) Link(com.winsonchiu.reader.data.reddit.Link) View(android.view.View) Comment(com.winsonchiu.reader.data.reddit.Comment) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) RectF(android.graphics.RectF) LinearLayoutManagerWrapHeight(com.winsonchiu.reader.utils.LinearLayoutManagerWrapHeight) LayoutInflater(android.view.LayoutInflater) YouTubeListener(com.winsonchiu.reader.utils.YouTubeListener) Sort(com.winsonchiu.reader.data.reddit.Sort) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) TextUtils(android.text.TextUtils) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) ComponentActivity(com.winsonchiu.reader.dagger.components.ComponentActivity) ViewGroup(android.view.ViewGroup) R(com.winsonchiu.reader.R) RecyclerView(android.support.v7.widget.RecyclerView) FragmentBase(com.winsonchiu.reader.FragmentBase) AlertDialog(android.support.v7.app.AlertDialog) TypedValue(android.util.TypedValue) ActivityMain(com.winsonchiu.reader.ActivityMain) GestureDetectorCompat(android.support.v4.view.GestureDetectorCompat) FragmentListenerBase(com.winsonchiu.reader.FragmentListenerBase) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent) Nullable(android.support.annotation.Nullable) InputFilter(android.text.InputFilter) Activity(android.app.Activity) Subscription(rx.Subscription) EditText(android.widget.EditText) ObserverError(com.winsonchiu.reader.rx.ObserverError) ValueAnimator(android.animation.ValueAnimator) Report(com.winsonchiu.reader.data.reddit.Report) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) YouTubeListener(com.winsonchiu.reader.utils.YouTubeListener) CallbackYouTubeDestruction(com.winsonchiu.reader.utils.CallbackYouTubeDestruction) LinearLayoutManagerWrapHeight(com.winsonchiu.reader.utils.LinearLayoutManagerWrapHeight) GestureDetector(android.view.GestureDetector) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) EditText(android.widget.EditText) Comment(com.winsonchiu.reader.data.reddit.Comment) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) InputFilter(android.text.InputFilter) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) GestureDetectorCompat(android.support.v4.view.GestureDetectorCompat) MotionEvent(android.view.MotionEvent) LinksListenerBase(com.winsonchiu.reader.links.LinksListenerBase) ObserverError(com.winsonchiu.reader.rx.ObserverError) RecyclerView(android.support.v7.widget.RecyclerView) Likes(com.winsonchiu.reader.data.reddit.Likes) AdapterLink(com.winsonchiu.reader.links.AdapterLink) Link(com.winsonchiu.reader.data.reddit.Link)

Aggregations

Animator (android.animation.Animator)1 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 ValueAnimator (android.animation.ValueAnimator)1 Activity (android.app.Activity)1 Context (android.content.Context)1 RectF (android.graphics.RectF)1 Bundle (android.os.Bundle)1 Nullable (android.support.annotation.Nullable)1 GestureDetectorCompat (android.support.v4.view.GestureDetectorCompat)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 AlertDialog (android.support.v7.app.AlertDialog)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 InputFilter (android.text.InputFilter)1 TextUtils (android.text.TextUtils)1 TypedValue (android.util.TypedValue)1 GestureDetector (android.view.GestureDetector)1 LayoutInflater (android.view.LayoutInflater)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1