Exemplo n.º 1
0
  @SuppressWarnings("CodeBlock2Expr")
  private void initializeInfoLine() {
    // get the vote from the service
    Observable<Vote> cachedVote = voteService.getVote(feedItem).compose(bindToLifecycle());

    //noinspection ConstantConditions
    infoLineView = ButterKnife.findById(getView(), R.id.infoview);
    if (infoLineView == null) {
      infoLineView = new InfoLineView(getActivity());
      adapter.addAdapter(SingleViewAdapter.ofView(infoLineView));
    }

    boolean isSelfPost =
        userService
            .getName()
            .transform(name -> name.equalsIgnoreCase(feedItem.getUser()))
            .or(false);

    // display the feed item in the view
    infoLineView.setFeedItem(feedItem, isSelfPost, cachedVote);

    infoLineView.setOnDetailClickedListener(this);

    // register the vote listener
    infoLineView.setOnVoteListener(
        vote -> {
          Runnable action =
              () -> {
                showPostVoteAnimation(vote);

                voteService
                    .vote(feedItem, vote)
                    .compose(bindToLifecycle())
                    .subscribe(Actions.empty(), defaultOnError());
              };

          Runnable retry = () -> infoLineView.getVoteView().setVote(vote);
          return doIfAuthorizedHelper.run(action, retry);
        });

    // and a vote listener vor voting tags.
    infoLineView.setTagVoteListener(
        (tag, vote) -> {
          Runnable action =
              () -> {
                voteService
                    .vote(tag, vote)
                    .compose(bindToLifecycle())
                    .subscribe(Actions.empty(), defaultOnError());
              };

          return doIfAuthorizedHelper.run(action, action);
        });

    infoLineView.setOnAddTagClickedListener(
        () -> {
          NewTagDialogFragment dialog = new NewTagDialogFragment();
          dialog.show(getChildFragmentManager(), null);
        });
  }
Exemplo n.º 2
0
  /**
   * Scroll the th given comment
   *
   * @param commentId The comment id to scroll to
   */
  private void scrollToComment(long commentId) {
    Optional<Integer> offset = adapter.getOffset(commentsAdapter);
    if (!offset.isPresent()) return;

    for (int idx = 0; idx < commentsAdapter.getItemCount(); idx++) {
      if (commentsAdapter.getItemId(idx) == commentId) {
        content.scrollToPosition(offset.get() + idx);
        break;
      }
    }

    commentsAdapter.setSelectedCommentId(commentId);
  }
Exemplo n.º 3
0
  private void initializeCommentPostLine() {
    ViewGroup.MarginLayoutParams layoutParams =
        new ViewGroup.MarginLayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    CommentPostLine line = new CommentPostLine(getActivity());
    line.setLayoutParams(layoutParams);
    adapter.addAdapter(SingleViewAdapter.ofView(line));

    line.setCommentDraft(getArguments().getString(ARG_COMMENT_DRAFT, ""));
    line.textChanges().subscribe(text -> getArguments().putString(ARG_COMMENT_DRAFT, text));

    line.comments()
        .subscribe(
            text -> {
              Runnable action =
                  () -> {
                    line.clear();
                    writeComment(text);
                  };

              doIfAuthorizedHelper.run(action, action);
            });
  }
Exemplo n.º 4
0
  private void initializeMediaView() {
    int padding = AndroidUtility.getActionBarContentOffset(getActivity());

    // initialize a new viewer fragment
    MediaUri uri = MediaUri.of(feedItem.getId(), UriHelper.of(getContext()).media(feedItem));
    if (!uri.isLocal() && AndroidUtility.isOnMobile(getActivity())) {
      Settings.ConfirmOnMobile confirmOnMobile = settings.confirmPlayOnMobile(getContext());
      if (confirmOnMobile == Settings.ConfirmOnMobile.ALL) {
        uri = uri.withDelay(true);

      } else if (confirmOnMobile == Settings.ConfirmOnMobile.VIDEO
          && uri.getMediaType() != MediaUri.MediaType.IMAGE) {

        uri = uri.withDelay(true);
      }
    }

    viewer =
        MediaViews.newInstance(
            getActivity(),
            uri,
            () -> {
              //  mark this item seen. We do that in a background thread
              seenService.markAsSeen(feedItem);
            });

    // inform viewer over fragment lifecycle events!
    MediaViews.adaptFragmentLifecycle(lifecycle(), viewer);

    registerTapListener(viewer);

    PreviewInfo previewInfo =
        this.previewInfo != null ? this.previewInfo : getPreviewInfoFromCache();

    if (previewInfo != null) {
      viewer.setPreviewImage(previewInfo, "TransitionTarget-" + feedItem.getId());
      if (Sdk.isAtLeastLollipop()) {
        viewer.postDelayed(this::onTransitionEnds, 350);
      } else {
        viewer.post(this::onTransitionEnds);
      }
    } else {
      viewer.post(this::onTransitionEnds);
    }

    // add views in the correct order
    int idx = playerContainer.indexOfChild(voteAnimationIndicator);
    playerContainer.addView(viewer, idx);

    if (tabletLayout) {
      viewer.setLayoutParams(
          new FrameLayout.LayoutParams(
              ViewGroup.LayoutParams.MATCH_PARENT,
              ViewGroup.LayoutParams.MATCH_PARENT,
              Gravity.CENTER));
    } else {
      viewer.setPadding(0, padding, 0, 0);

      // we add a placeholder to the first element of the recycler view.
      // this placeholder will mirror the size of the viewer.
      PlaceholderView placeholder = new PlaceholderView();
      adapter.addAdapter(SingleViewAdapter.ofView(placeholder));

      RxView.layoutChanges(viewer)
          .subscribe(
              event -> {
                int newHeight = viewer.getMeasuredHeight();
                if (newHeight != placeholder.fixedHeight) {
                  placeholder.fixedHeight = newHeight;

                  if (Sdk.isAtLeastKitKat()) {
                    placeholder.requestLayout();
                  } else {
                    // it looks like a requestLayout is not honored on pre kitkat devices
                    // if already in a layout pass.
                    placeholder.post(placeholder::requestLayout);
                  }

                  if (isVideoFullScreen()) {
                    realignFullScreen();
                  }
                }
              });

      RxView.layoutChanges(placeholder)
          .subscribe(
              event -> {
                // simulate scroll after layouting the placeholder to
                // reflect changes to the viewers clipping.
                simulateScroll();
              });
    }
  }
Exemplo n.º 5
0
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    this.tabletLayout = view.findViewById(R.id.infoview) != null;

    if (!(getActivity() instanceof ToolbarActivity)) {
      throw new IllegalStateException("Fragment must be child of a ToolbarActivity.");
    }

    ToolbarActivity activity = (ToolbarActivity) getActivity();
    activity.getScrollHideToolbarListener().reset();

    int abHeight = AndroidUtility.getActionBarContentOffset(getActivity());
    if (tabletLayout) {
      View tabletLayout = ButterKnife.findById(view, R.id.tabletlayout);
      tabletLayout.setPadding(0, abHeight, 0, 0);

      ((FrameLayout.LayoutParams) voteAnimationIndicator.getLayoutParams()).gravity =
          Gravity.CENTER;

      scrollHandler = new NoopOnScrollListener();
    } else {
      // use height of the toolbar to configure swipe refresh layout.
      swipeRefreshLayout.setProgressViewOffset(false, 0, (int) (1.5 * abHeight));

      scrollHandler = new ScrollHandler(activity);
    }

    content.addOnScrollListener(scrollHandler);

    swipeRefreshLayout.setColorSchemeResources(R.color.primary);
    swipeRefreshLayout.setOnRefreshListener(
        () -> {
          if (!isVideoFullScreen()) {
            rewindOnLoad = true;
            loadPostDetails();
          }
        });

    swipeRefreshLayout.setKeepScreenOn(settings.keepScreenOn());

    adapter = new MergeRecyclerAdapter();
    content.setItemAnimator(null);
    content.setLayoutManager(new LinearLayoutManager(getActivity()));
    content.setAdapter(adapter);

    initializeMediaView();
    initializeInfoLine();
    initializeCommentPostLine();

    commentsAdapter = new CommentsAdapter(userService.getName().or(""));
    commentsAdapter.setCommentActionListener(this);
    commentsAdapter.setPrioritizeOpComments(settings.prioritizeOpComments());
    commentsAdapter.setShowFavCommentButton(userService.isAuthorized());
    adapter.addAdapter(commentsAdapter);

    // restore the postInfo, if possible.
    if (tags != null && comments != null) {
      displayTags(tags);
      displayComments(comments);
    }

    loadPostDetails();

    // show the repost badge if this is a repost
    repostHint.setVisibility(isRepost() ? View.VISIBLE : View.GONE);
  }