From 216c1bdc07a9d84cb2364c069f6be70bbfcaa11b Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Wed, 7 Feb 2024 10:01:18 +0200 Subject: [PATCH] Extract avatar loading function --- .../ui/notifications/adapters/NotesAdapter.kt | 59 ++++++------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt b/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt index 5f66da511dde..e0df36148505 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt @@ -225,61 +225,36 @@ class NotesAdapter( } private fun NoteViewHolder.loadAvatars(note: Note) { - if (note.canShowMultipleAvatars()) { - if(note.iconURLs!!.size == 2) { + if (note.shouldShowMultipleAvatars() && note.iconURLs != null && note.iconURLs!!.size > 1) { + val avatars = note.iconURLs!!.toList() + if (avatars.size == 2) { imageAvatar.visibility = View.INVISIBLE twoAvatarsView.visibility = View.VISIBLE threeAvatarsView.visibility = View.INVISIBLE - val avatarUrl = GravatarUtils.fixGravatarUrl(note.iconURL, avatarSize) - val avatarUrl2 = GravatarUtils.fixGravatarUrl(note.iconURLs!![1], avatarSize) - imageManager?.loadIntoCircle( - twoAvatars1, - ImageType.AVATAR_WITH_BACKGROUND, - avatarUrl2 - ) - imageManager?.loadIntoCircle( - twoAvatars2, - ImageType.AVATAR_WITH_BACKGROUND, - avatarUrl - ) - } else { + loadAvatar(twoAvatars1, avatars[1]) + loadAvatar(twoAvatars2, avatars[0]) + } else { // size > 3 imageAvatar.visibility = View.INVISIBLE twoAvatarsView.visibility = View.INVISIBLE threeAvatarsView.visibility = View.VISIBLE - val avatarUrl = GravatarUtils.fixGravatarUrl(note.iconURL, avatarSize) - val avatarUrl2 = GravatarUtils.fixGravatarUrl(note.iconURLs!![1], avatarSize) - val avatarUrl3 = GravatarUtils.fixGravatarUrl(note.iconURLs!![2], avatarSize) - imageManager?.loadIntoCircle( - threeAvatars1, - ImageType.AVATAR_WITH_BACKGROUND, - avatarUrl3 - ) - imageManager?.loadIntoCircle( - threeAvatars2, - ImageType.AVATAR_WITH_BACKGROUND, - avatarUrl2 - ) - imageManager?.loadIntoCircle( - threeAvatars3, - ImageType.AVATAR_WITH_BACKGROUND, - avatarUrl - ) + loadAvatar(threeAvatars1, avatars[2]) + loadAvatar(threeAvatars2, avatars[1]) + loadAvatar(threeAvatars3, avatars[0]) } - } else { + } else { // single avatar imageAvatar.visibility = View.VISIBLE twoAvatarsView.visibility = View.INVISIBLE threeAvatarsView.visibility = View.INVISIBLE - val avatarUrl = GravatarUtils.fixGravatarUrl(note.iconURL, avatarSize) - imageManager?.loadIntoCircle( - imageAvatar, - ImageType.AVATAR_WITH_BACKGROUND, - avatarUrl - ) + loadAvatar(imageAvatar, note.iconURL) } } - private fun Note.canShowMultipleAvatars() = - (isFollowType || isLikeType || isCommentLikeType) && iconURLs != null && iconURLs!!.size > 1 + private fun loadAvatar(imageView: ImageView, avatarUrl: String) { + val url = GravatarUtils.fixGravatarUrl(avatarUrl, avatarSize) + imageManager?.loadIntoCircle(imageView, ImageType.AVATAR_WITH_BACKGROUND, url) + } + + private fun Note.shouldShowMultipleAvatars() = isFollowType || isLikeType || isCommentLikeType private fun handleMaxLines(subject: TextView, detail: TextView) { subject.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {