From bac30031823bc9c56435f9482e73a8f66d5967d9 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Mon, 16 Dec 2019 19:51:21 +0100 Subject: [PATCH] Make polls respect content warning visibility setting (#1583) * Make polls respect content warning visibility setting * Only perform poll setup when poll is visible --- .../tusky/adapter/StatusBaseViewHolder.java | 82 +++++++++---------- .../conversation/ConversationViewHolder.java | 5 +- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java index 370864b58..5b74ca088 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -178,11 +178,12 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { @Nullable String spoilerText, @Nullable Status.Mention[] mentions, @NonNull List emojis, + @Nullable PollViewData poll, final StatusActionListener listener) { if (TextUtils.isEmpty(spoilerText)) { contentWarningDescription.setVisibility(View.GONE); contentWarningButton.setVisibility(View.GONE); - this.setTextVisible(true, content, mentions, emojis, listener); + this.setTextVisible(true, content, mentions, emojis, poll, listener); } else { CharSequence emojiSpoiler = CustomEmojiHelper.emojifyString(spoilerText, emojis, contentWarningDescription); contentWarningDescription.setText(emojiSpoiler); @@ -194,9 +195,9 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { if (getAdapterPosition() != RecyclerView.NO_POSITION) { listener.onExpandedChange(isChecked, getAdapterPosition()); } - this.setTextVisible(isChecked, content, mentions, emojis, listener); + this.setTextVisible(isChecked, content, mentions, emojis, poll, listener); }); - this.setTextVisible(expanded, content, mentions, emojis, listener); + this.setTextVisible(expanded, content, mentions, emojis, poll, listener); } } @@ -204,10 +205,14 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { Spanned content, Status.Mention[] mentions, List emojis, + @Nullable PollViewData poll, final StatusActionListener listener) { if (expanded) { Spanned emojifiedText = CustomEmojiHelper.emojifyText(content, emojis, this.content); LinkHelper.setClickableText(this.content, emojifiedText, mentions, listener); + if (poll != null) { + setupPoll(poll, emojis, listener); + } } else { LinkHelper.setClickableMentions(this.content, mentions, listener); } @@ -216,6 +221,14 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { } else { this.content.setVisibility(View.VISIBLE); } + setPollVisible(poll != null && expanded); + } + + private void setPollVisible(boolean visible) { + int visibility = visible ? View.VISIBLE : View.GONE; + pollButton.setVisibility(visibility); + pollDescription.setVisibility(visibility); + pollOptions.setVisibility(visibility); } private void setAvatar(String url, @@ -674,12 +687,10 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { setupButtons(listener, status.getSenderId()); setRebloggingEnabled(status.getRebloggingEnabled(), status.getVisibility()); - setSpoilerAndContent(status.isExpanded(), status.getContent(), status.getSpoilerText(), status.getMentions(), status.getStatusEmojis(), listener); + setSpoilerAndContent(status.isExpanded(), status.getContent(), status.getSpoilerText(), status.getMentions(), status.getStatusEmojis(), status.getPoll(), listener); setDescriptionForStatus(status); - setupPoll(status.getPoll(), status.getStatusEmojis(), listener); - // Workaround for RecyclerView 1.0.0 / androidx.core 1.0.0 // RecyclerView tries to set AccessibilityDelegateCompat to null // but ViewCompat code replaces is with the default one. RecyclerView never @@ -834,55 +845,44 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { } } - protected void setupPoll(PollViewData poll, List emojis, StatusActionListener listener) { - if (poll == null) { + private void setupPoll(PollViewData poll, List emojis, StatusActionListener listener) { + long timestamp = System.currentTimeMillis(); - pollOptions.setVisibility(View.GONE); + boolean expired = poll.getExpired() || (poll.getExpiresAt() != null && timestamp > poll.getExpiresAt().getTime()); - pollDescription.setVisibility(View.GONE); + Context context = pollDescription.getContext(); + + pollOptions.setVisibility(View.VISIBLE); + + if (expired || poll.getVoted()) { + // no voting possible + pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), emojis, PollAdapter.RESULT); pollButton.setVisibility(View.GONE); - } else { - long timestamp = System.currentTimeMillis(); + // voting possible + pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), emojis, poll.getMultiple() ? PollAdapter.MULTIPLE : PollAdapter.SINGLE); - boolean expired = poll.getExpired() || (poll.getExpiresAt() != null && timestamp > poll.getExpiresAt().getTime()); + pollButton.setVisibility(View.VISIBLE); - Context context = pollDescription.getContext(); + pollButton.setOnClickListener(v -> { - pollOptions.setVisibility(View.VISIBLE); + int position = getAdapterPosition(); - if (expired || poll.getVoted()) { - // no voting possible - pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), emojis, PollAdapter.RESULT); + if (position != RecyclerView.NO_POSITION) { - pollButton.setVisibility(View.GONE); - } else { - // voting possible - pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), emojis, poll.getMultiple() ? PollAdapter.MULTIPLE : PollAdapter.SINGLE); + List pollResult = pollAdapter.getSelected(); - pollButton.setVisibility(View.VISIBLE); - - pollButton.setOnClickListener(v -> { - - int position = getAdapterPosition(); - - if (position != RecyclerView.NO_POSITION) { - - List pollResult = pollAdapter.getSelected(); - - if (!pollResult.isEmpty()) { - listener.onVoteInPoll(position, pollResult); - } + if (!pollResult.isEmpty()) { + listener.onVoteInPoll(position, pollResult); } + } - }); - } - - pollDescription.setVisibility(View.VISIBLE); - pollDescription.setText(getPollInfoText(timestamp, poll, context)); - + }); } + + pollDescription.setVisibility(View.VISIBLE); + pollDescription.setText(getPollInfoText(timestamp, poll, context)); } private CharSequence getPollInfoText(long timestamp, PollViewData poll, Context context) { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationViewHolder.java index becb537f3..34651c201 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationViewHolder.java @@ -108,14 +108,11 @@ public class ConversationViewHolder extends StatusBaseViewHolder { setupButtons(listener, account.getId()); - setSpoilerAndContent(status.getExpanded(), status.getContent(), status.getSpoilerText(), status.getMentions(), status.getEmojis(), listener); + setSpoilerAndContent(status.getExpanded(), status.getContent(), status.getSpoilerText(), status.getMentions(), status.getEmojis(), PollViewDataKt.toViewData(status.getPoll()), listener); setConversationName(conversation.getAccounts()); setAvatars(conversation.getAccounts()); - - setupPoll(PollViewDataKt.toViewData(status.getPoll()), status.getEmojis(), listener); - } private void setConversationName(List accounts) {