From 062d54d19e1a4666220fb950c53c348c4ed710c0 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Fri, 28 Sep 2018 17:15:01 +0200 Subject: [PATCH] small improvements for viewholders (#860) --- .../tusky/adapter/AccountViewHolder.java | 6 +- .../tusky/adapter/NotificationsAdapter.java | 13 +-- .../tusky/adapter/StatusBaseViewHolder.java | 103 ++++++++---------- .../tusky/adapter/StatusViewHolder.java | 3 +- 4 files changed, 50 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/AccountViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/AccountViewHolder.java index c156d6ae..49bae8c9 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/AccountViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/AccountViewHolder.java @@ -14,7 +14,6 @@ import com.keylesspalace.tusky.util.CustomEmojiHelper; import com.squareup.picasso.Picasso; class AccountViewHolder extends RecyclerView.ViewHolder { - private View container; private TextView username; private TextView displayName; private ImageView avatar; @@ -22,7 +21,6 @@ class AccountViewHolder extends RecyclerView.ViewHolder { AccountViewHolder(View itemView) { super(itemView); - container = itemView.findViewById(R.id.account_container); username = itemView.findViewById(R.id.account_username); displayName = itemView.findViewById(R.id.account_display_name); avatar = itemView.findViewById(R.id.account_avatar); @@ -43,10 +41,10 @@ class AccountViewHolder extends RecyclerView.ViewHolder { } void setupActionListener(final AccountActionListener listener) { - container.setOnClickListener(v -> listener.onViewAccount(accountId)); + itemView.setOnClickListener(v -> listener.onViewAccount(accountId)); } void setupLinkListener(final LinkListener listener) { - container.setOnClickListener(v -> listener.onViewAccount(accountId)); + itemView.setOnClickListener(v -> listener.onViewAccount(accountId)); } } \ No newline at end of file diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java index eeb6b25c..4abce3f1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java @@ -281,8 +281,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { CharSequence emojifiedMessage = CustomEmojiHelper.emojifyString(wholeMessage, account.getEmojis(), message); message.setText(emojifiedMessage); - format = context.getString(R.string.status_username_format); - String username = String.format(format, account.getUsername()); + String username = context.getString(R.string.status_username_format, account.getUsername()); usernameView.setText(username); CharSequence emojifiedDisplayName = CustomEmojiHelper.emojifyString(wrappedDisplayName, account.getEmojis(), usernameView); @@ -313,7 +312,6 @@ public class NotificationsAdapter extends RecyclerView.Adapter { private final TextView username; private final TextView timestampInfo; private final TextView statusContent; - private final ViewGroup container; private final ImageView statusAvatar; private final ImageView notificationAvatar; private final TextView contentWarningDescriptionTextView; @@ -337,7 +335,6 @@ public class NotificationsAdapter extends RecyclerView.Adapter { username = itemView.findViewById(R.id.status_username); timestampInfo = itemView.findViewById(R.id.status_timestamp_info); statusContent = itemView.findViewById(R.id.notification_content); - container = itemView.findViewById(R.id.notification_container); statusAvatar = itemView.findViewById(R.id.notification_status_avatar); notificationAvatar = itemView.findViewById(R.id.notification_notification_avatar); contentWarningDescriptionTextView = itemView.findViewById(R.id.notification_content_warning_description); @@ -348,7 +345,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { statusAvatar.setColorFilter(darkerFilter, PorterDuff.Mode.MULTIPLY); notificationAvatar.setColorFilter(darkerFilter, PorterDuff.Mode.MULTIPLY); - container.setOnClickListener(this); + itemView.setOnClickListener(this); message.setOnClickListener(this); statusContent.setOnClickListener(this); contentWarningButton.setOnCheckedChangeListener(this); @@ -556,11 +553,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { if (getAdapterPosition() != RecyclerView.NO_POSITION) { notificationActionListener.onExpandedChange(isChecked, getAdapterPosition()); } - if (isChecked) { - statusContent.setVisibility(View.VISIBLE); - } else { - statusContent.setVisibility(View.GONE); - } + statusContent.setVisibility(isChecked ? View.VISIBLE : View.GONE); } } } 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 ec58c0a0..4fdc3b2d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -44,7 +44,6 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { private static final InputFilter[] COLLAPSE_INPUT_FILTER = new InputFilter[] { SmartLengthInputFilter.INSTANCE }; private static final InputFilter[] NO_INPUT_FILTER = new InputFilter[0]; - private View container; private TextView displayName; private TextView username; private ImageButton replyButton; @@ -53,14 +52,8 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { private ImageButton moreButton; private boolean favourited; private boolean reblogged; - private ImageView mediaPreview0; - private ImageView mediaPreview1; - private ImageView mediaPreview2; - private ImageView mediaPreview3; - private ImageView mediaOverlay0; - private ImageView mediaOverlay1; - private ImageView mediaOverlay2; - private ImageView mediaOverlay3; + private ImageView[] mediaPreviews; + private ImageView[] mediaOverlays; private TextView sensitiveMediaWarning; private View sensitiveMediaShow; private TextView mediaLabel; @@ -78,7 +71,6 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { StatusBaseViewHolder(View itemView, boolean useAbsoluteTime) { super(itemView); - container = itemView.findViewById(R.id.status_container); displayName = itemView.findViewById(R.id.status_display_name); username = itemView.findViewById(R.id.status_username); timestampInfo = itemView.findViewById(R.id.status_timestamp_info); @@ -90,14 +82,18 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { moreButton = itemView.findViewById(R.id.status_more); reblogged = false; favourited = false; - mediaPreview0 = itemView.findViewById(R.id.status_media_preview_0); - mediaPreview1 = itemView.findViewById(R.id.status_media_preview_1); - mediaPreview2 = itemView.findViewById(R.id.status_media_preview_2); - mediaPreview3 = itemView.findViewById(R.id.status_media_preview_3); - mediaOverlay0 = itemView.findViewById(R.id.status_media_overlay_0); - mediaOverlay1 = itemView.findViewById(R.id.status_media_overlay_1); - mediaOverlay2 = itemView.findViewById(R.id.status_media_overlay_2); - mediaOverlay3 = itemView.findViewById(R.id.status_media_overlay_3); + mediaPreviews = new ImageView[] { + itemView.findViewById(R.id.status_media_preview_0), + itemView.findViewById(R.id.status_media_preview_1), + itemView.findViewById(R.id.status_media_preview_2), + itemView.findViewById(R.id.status_media_preview_3) + }; + mediaOverlays =new ImageView[] { + itemView.findViewById(R.id.status_media_overlay_0), + itemView.findViewById(R.id.status_media_overlay_1), + itemView.findViewById(R.id.status_media_overlay_2), + itemView.findViewById(R.id.status_media_overlay_3) + }; sensitiveMediaWarning = itemView.findViewById(R.id.status_sensitive_media_warning); sensitiveMediaShow = itemView.findViewById(R.id.status_sensitive_media_button); mediaLabel = itemView.findViewById(R.id.status_media_label); @@ -180,11 +176,11 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { protected void showContent(boolean show) { if (show) { - container.setVisibility(View.VISIBLE); - container.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; + itemView.setVisibility(View.VISIBLE); + itemView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; } else { - container.setVisibility(View.INVISIBLE); - container.getLayoutParams().height = Utils.convertDpToPx(container.getContext(), 24); + itemView.setVisibility(View.INVISIBLE); + itemView.getLayoutParams().height = Utils.convertDpToPx(itemView.getContext(), 24); } } @@ -241,13 +237,8 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { private void setMediaPreviews(final List attachments, boolean sensitive, final StatusActionListener listener, boolean showingContent) { - final ImageView[] previews = { - mediaPreview0, mediaPreview1, mediaPreview2, mediaPreview3 - }; - final ImageView[] overlays = { - mediaOverlay0, mediaOverlay1, mediaOverlay2, mediaOverlay3 - }; - Context context = mediaPreview0.getContext(); + + Context context = itemView.getContext(); int mediaPreviewUnloadedId = ThemeUtils.getDrawableId(itemView.getContext(), R.attr.media_preview_unloaded_drawable, @@ -260,46 +251,46 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { String description = attachments.get(i).getDescription(); if (TextUtils.isEmpty(description)) { - previews[i].setContentDescription(context.getString(R.string.action_view_media)); + mediaPreviews[i].setContentDescription(context.getString(R.string.action_view_media)); } else { - previews[i].setContentDescription(description); + mediaPreviews[i].setContentDescription(description); } - previews[i].setVisibility(View.VISIBLE); + mediaPreviews[i].setVisibility(View.VISIBLE); if (TextUtils.isEmpty(previewUrl)) { Picasso.with(context) .load(mediaPreviewUnloadedId) - .into(previews[i]); + .into(mediaPreviews[i]); } else { Picasso.with(context) .load(previewUrl) .placeholder(mediaPreviewUnloadedId) - .into(previews[i]); + .into(mediaPreviews[i]); } final Attachment.Type type = attachments.get(i).getType(); if (type == Attachment.Type.VIDEO | type == Attachment.Type.GIFV) { - overlays[i].setVisibility(View.VISIBLE); + mediaOverlays[i].setVisibility(View.VISIBLE); } else { - overlays[i].setVisibility(View.GONE); + mediaOverlays[i].setVisibility(View.GONE); } final int urlIndex = i; - previews[i].setOnClickListener(v -> { + mediaPreviews[i].setOnClickListener(v -> { if (getAdapterPosition() != RecyclerView.NO_POSITION) { listener.onViewMedia(getAdapterPosition(), urlIndex, v); } }); if (n <= 2) { - previews[0].getLayoutParams().height = getMediaPreviewHeight(context) * 2; - previews[1].getLayoutParams().height = getMediaPreviewHeight(context) * 2; + mediaPreviews[0].getLayoutParams().height = getMediaPreviewHeight(context) * 2; + mediaPreviews[1].getLayoutParams().height = getMediaPreviewHeight(context) * 2; } else { - previews[0].getLayoutParams().height = getMediaPreviewHeight(context); - previews[1].getLayoutParams().height = getMediaPreviewHeight(context); - previews[2].getLayoutParams().height = getMediaPreviewHeight(context); - previews[3].getLayoutParams().height = getMediaPreviewHeight(context); + mediaPreviews[0].getLayoutParams().height = getMediaPreviewHeight(context); + mediaPreviews[1].getLayoutParams().height = getMediaPreviewHeight(context); + mediaPreviews[2].getLayoutParams().height = getMediaPreviewHeight(context); + mediaPreviews[3].getLayoutParams().height = getMediaPreviewHeight(context); } } @@ -336,7 +327,7 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { // Hide any of the placeholder previews beyond the ones set. for (int i = n; i < Status.MAX_MEDIA_ATTACHMENTS; i++) { - previews[i].setVisibility(View.GONE); + mediaPreviews[i].setVisibility(View.GONE); } } @@ -408,17 +399,11 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { if (getAdapterPosition() != RecyclerView.NO_POSITION) { listener.onExpandedChange(isChecked, getAdapterPosition()); } - if (isChecked) { - content.setVisibility(View.VISIBLE); - } else { - content.setVisibility(View.GONE); - } + content.setVisibility(isChecked ? View.VISIBLE : View.GONE); + }); - if (expanded) { - content.setVisibility(View.VISIBLE); - } else { - content.setVisibility(View.GONE); - } + content.setVisibility(expanded ? View.VISIBLE : View.GONE); + } private void hideSpoilerText() { @@ -490,7 +475,7 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { } }; content.setOnClickListener(viewThreadListener); - container.setOnClickListener(viewThreadListener); + itemView.setOnClickListener(viewThreadListener); } void setupWithStatus(StatusViewData.Concrete status, final StatusActionListener listener, @@ -515,10 +500,10 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { } else { setMediaLabel(attachments, sensitive, listener); // Hide all unused views. - mediaPreview0.setVisibility(View.GONE); - mediaPreview1.setVisibility(View.GONE); - mediaPreview2.setVisibility(View.GONE); - mediaPreview3.setVisibility(View.GONE); + mediaPreviews[0].setVisibility(View.GONE); + mediaPreviews[1].setVisibility(View.GONE); + mediaPreviews[2].setVisibility(View.GONE); + mediaPreviews[3].setVisibility(View.GONE); hideSensitiveMediaWarning(); } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java index d4250e1f..5e9d00f0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java @@ -89,8 +89,7 @@ public class StatusViewHolder extends StatusBaseViewHolder { private void setRebloggedByDisplayName(String name) { Context context = rebloggedBar.getContext(); - String format = context.getString(R.string.status_boosted_format); - String boostedText = String.format(format, name); + String boostedText = context.getString(R.string.status_boosted_format, name); rebloggedBar.setText(boostedText); rebloggedBar.setVisibility(View.VISIBLE); }