From a484d2ab858087c5f2ba91240e39611dedff914e Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Wed, 10 Aug 2022 11:27:27 +0200 Subject: [PATCH] codacy: shorten long method by extracting common logic Signed-off-by: Andy Scherzinger --- .../talk/adapters/items/ContactItem.java | 72 +++++++++---------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/items/ContactItem.java b/app/src/main/java/com/nextcloud/talk/adapters/items/ContactItem.java index bd0cdf410..a28c2f3a3 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/items/ContactItem.java +++ b/app/src/main/java/com/nextcloud/talk/adapters/items/ContactItem.java @@ -159,59 +159,57 @@ public class ContactItem extends AbstractFlexibleItem= Build.VERSION_CODES.O) { - holder.binding.avatarDraweeView.getHierarchy().setPlaceholderImage( - DisplayUtils.getRoundedDrawable( - viewThemeUtils.themePlaceholderAvatar(holder.binding.avatarDraweeView, - R.drawable.ic_avatar_group))); - } else { - holder.binding.avatarDraweeView.setImageResource(R.drawable.ic_circular_group); - } + setGenericAvatar(holder, R.drawable.ic_avatar_group, R.drawable.ic_circular_group); } else if (participant.getCalculatedActorType() == Participant.ActorType.EMAILS) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - holder.binding.avatarDraweeView.getHierarchy().setPlaceholderImage( - DisplayUtils.getRoundedDrawable( - viewThemeUtils.themePlaceholderAvatar(holder.binding.avatarDraweeView, - R.drawable.ic_avatar_mail))); - } else { - holder.binding.avatarDraweeView.setImageResource(R.drawable.ic_circular_mail); - } + setGenericAvatar(holder, R.drawable.ic_avatar_mail, R.drawable.ic_circular_mail); } else if ( participant.getCalculatedActorType() == Participant.ActorType.GUESTS || Participant.ParticipantType.GUEST.equals(participant.getType()) || Participant.ParticipantType.GUEST_MODERATOR.equals(participant.getType())) { - String displayName = NextcloudTalkApplication.Companion.getSharedApplication() - .getResources().getString(R.string.nc_guest); + String displayName; if (!TextUtils.isEmpty(participant.getDisplayName())) { displayName = participant.getDisplayName(); + } else { + displayName = NextcloudTalkApplication.Companion.getSharedApplication() + .getResources().getString(R.string.nc_guest); } - DraweeController draweeController = Fresco.newDraweeControllerBuilder() - .setOldController(holder.binding.avatarDraweeView.getController()) - .setAutoPlayAnimations(true) - .setImageRequest(DisplayUtils.getImageRequestForUrl( - ApiUtils.getUrlForGuestAvatar(user.getBaseUrl(), - displayName, - false))) - .build(); - holder.binding.avatarDraweeView.setController(draweeController); - + setUserStyleAvatar(holder, + ApiUtils.getUrlForGuestAvatar(user.getBaseUrl(), displayName, false)); } else if (participant.getCalculatedActorType() == Participant.ActorType.USERS || PARTICIPANT_SOURCE_USERS.equals(participant.getSource())) { - DraweeController draweeController = Fresco.newDraweeControllerBuilder() - .setOldController(holder.binding.avatarDraweeView.getController()) - .setAutoPlayAnimations(true) - .setImageRequest(DisplayUtils.getImageRequestForUrl( - ApiUtils.getUrlForAvatar(user.getBaseUrl(), - participant.getCalculatedActorId(), - false))) - .build(); - holder.binding.avatarDraweeView.setController(draweeController); + setUserStyleAvatar(holder, + ApiUtils.getUrlForAvatar(user.getBaseUrl(), + participant.getCalculatedActorId(), + false)); + } + } + + private void setUserStyleAvatar(ContactItemViewHolder holder, String avatarUrl) { + DraweeController draweeController = Fresco.newDraweeControllerBuilder() + .setOldController(holder.binding.avatarDraweeView.getController()) + .setAutoPlayAnimations(true) + .setImageRequest(DisplayUtils.getImageRequestForUrl(avatarUrl)) + .build(); + holder.binding.avatarDraweeView.setController(draweeController); + } + + private void setGenericAvatar( + ContactItemViewHolder holder, + int roundPlaceholderDrawable, + int fallbackImageResource) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + holder.binding.avatarDraweeView.getHierarchy().setPlaceholderImage( + DisplayUtils.getRoundedDrawable( + viewThemeUtils.themePlaceholderAvatar(holder.binding.avatarDraweeView, + roundPlaceholderDrawable))); + } else { + holder.binding.avatarDraweeView.setImageResource(fallbackImageResource); } }