From c4632f5d42a84e58f4bf309f7a8d2a7b3608f5cd Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Tue, 5 Apr 2022 20:00:16 +0200 Subject: [PATCH] load avatars of reaction actors, optimize layout Signed-off-by: Andy Scherzinger --- .../talk/adapters/ReactionsAdapter.kt | 6 +- .../talk/adapters/ReactionsViewHolder.kt | 57 ++++++++++++++++++- .../EnumReactionActorTypeConverter.kt | 50 ++++++++++++++++ .../models/json/reactions/ReactionVoter.kt | 9 ++- .../talk/ui/dialog/ShowReactionsDialog.kt | 14 ++++- .../res/layout/dialog_message_reactions.xml | 2 - app/src/main/res/layout/reaction_item.xml | 8 ++- 7 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/models/json/converters/EnumReactionActorTypeConverter.kt diff --git a/app/src/main/java/com/nextcloud/talk/adapters/ReactionsAdapter.kt b/app/src/main/java/com/nextcloud/talk/adapters/ReactionsAdapter.kt index c16cef953..2b0460bbf 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/ReactionsAdapter.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/ReactionsAdapter.kt @@ -24,15 +24,17 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.nextcloud.talk.databinding.ReactionItemBinding +import com.nextcloud.talk.models.database.UserEntity class ReactionsAdapter( - private val clickListener: ReactionItemClickListener + private val clickListener: ReactionItemClickListener, + private val userEntity: UserEntity? ) : RecyclerView.Adapter() { internal var list: MutableList = ArrayList() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReactionsViewHolder { val itemBinding = ReactionItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) - return ReactionsViewHolder(itemBinding) + return ReactionsViewHolder(itemBinding, userEntity?.baseUrl) } override fun onBindViewHolder(holder: ReactionsViewHolder, position: Int) { diff --git a/app/src/main/java/com/nextcloud/talk/adapters/ReactionsViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/ReactionsViewHolder.kt index 78d6bb020..dbe0d37a3 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/ReactionsViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/ReactionsViewHolder.kt @@ -20,14 +20,69 @@ package com.nextcloud.talk.adapters +import android.text.TextUtils import androidx.recyclerview.widget.RecyclerView +import com.facebook.drawee.backends.pipeline.Fresco +import com.facebook.drawee.interfaces.DraweeController +import com.nextcloud.talk.R +import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication import com.nextcloud.talk.databinding.ReactionItemBinding +import com.nextcloud.talk.models.json.reactions.ReactionVoter +import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DisplayUtils -class ReactionsViewHolder(private val binding: ReactionItemBinding) : RecyclerView.ViewHolder(binding.root) { +class ReactionsViewHolder( + private val binding: ReactionItemBinding, + private val baseUrl: String? +) : RecyclerView.ViewHolder(binding.root) { fun bind(reactionItem: ReactionItem, clickListener: ReactionItemClickListener) { binding.root.setOnClickListener { clickListener.onClick(reactionItem) } binding.reaction.text = reactionItem.reaction binding.name.text = reactionItem.reactionVoter.actorDisplayName + + if (baseUrl != null && baseUrl.isNotEmpty()) { + loadAvatar(reactionItem) + } + } + + private fun loadAvatar(reactionItem: ReactionItem) { + if (reactionItem.reactionVoter.actorType == ReactionVoter.ReactionActorType.GUESTS) { + var displayName = sharedApplication?.resources?.getString(R.string.nc_guest) + if (!TextUtils.isEmpty(reactionItem.reactionVoter.actorDisplayName)) { + displayName = reactionItem.reactionVoter.actorDisplayName!! + } + val draweeController: DraweeController = Fresco.newDraweeControllerBuilder() + .setOldController(binding.avatar.controller) + .setAutoPlayAnimations(true) + .setImageRequest( + DisplayUtils.getImageRequestForUrl( + ApiUtils.getUrlForGuestAvatar( + baseUrl, + displayName, + false + ), + null + ) + ) + .build() + binding.avatar.controller = draweeController + } else if (reactionItem.reactionVoter.actorType == ReactionVoter.ReactionActorType.USERS) { + val draweeController: DraweeController = Fresco.newDraweeControllerBuilder() + .setOldController(binding.avatar.controller) + .setAutoPlayAnimations(true) + .setImageRequest( + DisplayUtils.getImageRequestForUrl( + ApiUtils.getUrlForAvatar( + baseUrl, + reactionItem.reactionVoter.actorId, + false + ), + null + ) + ) + .build() + binding.avatar.controller = draweeController + } } } diff --git a/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumReactionActorTypeConverter.kt b/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumReactionActorTypeConverter.kt new file mode 100644 index 000000000..5747a3b30 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumReactionActorTypeConverter.kt @@ -0,0 +1,50 @@ +/* + * Nextcloud Talk application + * + * @author Andy Scherzinger + * Copyright (C) 2022 Andy Scherzinger + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.models.json.converters + +import com.bluelinelabs.logansquare.typeconverters.StringBasedTypeConverter +import com.nextcloud.talk.models.json.reactions.ReactionVoter.ReactionActorType.DUMMY +import com.nextcloud.talk.models.json.reactions.ReactionVoter.ReactionActorType.GUESTS +import com.nextcloud.talk.models.json.reactions.ReactionVoter.ReactionActorType.USERS +import com.nextcloud.talk.models.json.reactions.ReactionVoter + +class EnumReactionActorTypeConverter : StringBasedTypeConverter() { + override fun getFromString(string: String): ReactionVoter.ReactionActorType { + return when (string) { + "guests" -> GUESTS + "users" -> USERS + else -> DUMMY + } + } + + override fun convertToString(`object`: ReactionVoter.ReactionActorType?): String { + + if (`object` == null) { + return "" + } + + return when (`object`) { + GUESTS -> "guests" + USERS -> "users" + else -> "" + } + } +} diff --git a/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt index 5014db144..57357729d 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt @@ -23,13 +23,14 @@ package com.nextcloud.talk.models.json.reactions import android.os.Parcelable import com.bluelinelabs.logansquare.annotation.JsonField import com.bluelinelabs.logansquare.annotation.JsonObject +import com.nextcloud.talk.models.json.converters.EnumReactionActorTypeConverter import kotlinx.android.parcel.Parcelize @Parcelize @JsonObject data class ReactionVoter( - @JsonField(name = ["actorType"]) - var actorType: String?, + @JsonField(name = ["actorType"], typeConverter = EnumReactionActorTypeConverter::class) + var actorType: ReactionActorType?, @JsonField(name = ["actorId"]) var actorId: String?, @JsonField(name = ["actorDisplayName"]) @@ -39,4 +40,8 @@ data class ReactionVoter( ) : Parcelable { // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' constructor() : this(null, null, null, 0) + + enum class ReactionActorType { + DUMMY, GUESTS, USERS + } } diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt index e7c217696..c08e18e7e 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt @@ -76,7 +76,7 @@ class ShowReactionsDialog( binding = DialogMessageReactionsBinding.inflate(layoutInflater) setContentView(binding.root) window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) - adapter = ReactionsAdapter(this) + adapter = ReactionsAdapter(this, userEntity) binding.reactionsList.adapter = adapter binding.reactionsList.layoutManager = LinearLayoutManager(context) initEmojiReactions() @@ -106,7 +106,17 @@ class ShowReactionsDialog( } // TODO: Add proper implementation - adapter?.list?.add(ReactionItem(ReactionVoter(null, null, "Marcel", 0), emoji)) + adapter?.list?.add( + ReactionItem( + ReactionVoter( + ReactionVoter.ReactionActorType.USERS, + "marcel", + "Marcel", + 0 + ), + emoji + ) + ) } } adapter?.notifyDataSetChanged() diff --git a/app/src/main/res/layout/dialog_message_reactions.xml b/app/src/main/res/layout/dialog_message_reactions.xml index 5fcd98883..87d4d616e 100644 --- a/app/src/main/res/layout/dialog_message_reactions.xml +++ b/app/src/main/res/layout/dialog_message_reactions.xml @@ -25,8 +25,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" - android:paddingStart="@dimen/standard_padding" - android:paddingEnd="@dimen/standard_padding" android:paddingBottom="@dimen/standard_half_padding"> + android:layout_height="wrap_content">