From ab6199b568bcc6521120658a82d8673b4fec3141 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 23 May 2019 17:30:36 +0400 Subject: [PATCH] Avatars now shown in ChatActivity toolbar --- .../presenters/DialogListPresenter.java | 4 +++ .../influence/views/ChatActivity.kt | 25 +++++++++++++++++++ .../influence/views/LoginActivity.java | 1 - app/src/main/res/layout/activity_chat.xml | 1 - 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/DialogListPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/DialogListPresenter.java index cdc1f48..7f064dd 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/DialogListPresenter.java +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/DialogListPresenter.java @@ -36,6 +36,7 @@ import org.jxmpp.stringprep.XmppStringprepException; import java.util.ArrayList; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import io.github.chronosx88.influence.R; import io.github.chronosx88.influence.contracts.CoreContracts; @@ -51,6 +52,7 @@ import java8.util.stream.StreamSupport; import java9.util.concurrent.CompletableFuture; public class DialogListPresenter implements CoreContracts.IDialogListPresenterContract { + private ConcurrentHashMap avatarsMap = new ConcurrentHashMap<>(); private CoreContracts.IChatListViewContract view; private CoreContracts.IDialogListLogicContract logic; private DialogsListAdapter dialogListAdapter = new DialogsListAdapter<>((imageView, url, payload) -> { @@ -75,6 +77,7 @@ public class DialogListPresenter implements CoreContracts.IDialogListPresenterCo if(avatarBytes != null) { Bitmap avatar = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length); imageView.setImageBitmap(avatar); + avatarsMap.put(url, avatarBytes); } }); }); @@ -120,6 +123,7 @@ public class DialogListPresenter implements CoreContracts.IDialogListPresenterCo Intent intent = new Intent(AppHelper.getContext(), ChatActivity.class); intent.putExtra("chatID", chatID); intent.putExtra("chatName", LocalDBWrapper.getChatByChatID(chatID).chatName); + intent.putExtra("chatAvatar", avatarsMap.get(chatID)); view.startActivity(intent); } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.kt b/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.kt index 9437564..0713f3b 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.kt +++ b/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.kt @@ -1,11 +1,16 @@ package io.github.chronosx88.influence.views +import android.content.Intent +import android.graphics.BitmapFactory import android.os.Bundle import android.view.MenuItem +import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.recyclerview.widget.LinearLayoutManager +import com.amulyakhare.textdrawable.TextDrawable +import com.amulyakhare.textdrawable.util.ColorGenerator import com.stfalcon.chatkit.commons.ImageLoader import com.stfalcon.chatkit.messages.MessageInput import com.stfalcon.chatkit.messages.MessagesList @@ -16,11 +21,13 @@ import io.github.chronosx88.influence.helpers.AppHelper import io.github.chronosx88.influence.models.GenericMessage import io.github.chronosx88.influence.models.roomEntities.MessageEntity import io.github.chronosx88.influence.presenters.ChatPresenter +import kotlinx.android.synthetic.main.activity_chat.view.* class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { private var messageList: MessagesList? = null private var messageInput: MessageInput? = null private var chatNameTextView: TextView? = null + private var chatAvatar: ImageView? = null private var presenter: ChatPresenter? = null override fun onCreate(savedInstanceState: Bundle?) { @@ -37,12 +44,14 @@ class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { messageList = findViewById(R.id.messages_list) messageList!!.layoutManager = LinearLayoutManager(this) chatNameTextView = findViewById(R.id.appbar_username) + chatAvatar = findViewById(R.id.profile_image_chat_activity) messageInput = findViewById(R.id.message_input) messageInput!!.setInputListener { presenter!!.sendMessage(it.toString()) } chatNameTextView!!.text = intent.getStringExtra("chatName") presenter = ChatPresenter(this, intent.getStringExtra("chatID")) + loadAvatarFromIntent(intent) presenter!!.loadLocalMessages() } @@ -64,4 +73,20 @@ class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { override fun setAdapter(adapter: MessagesListAdapter) { messageList!!.setAdapter(adapter) } + + private fun loadAvatarFromIntent(intent: Intent) { + val avatarBytes: ByteArray? = intent.getByteArrayExtra("chatAvatar") + if(avatarBytes != null) { + val avatar = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.size) + chatAvatar!!.setImageBitmap(avatar) + } else { + val chatName = intent.getStringExtra("chatName") + chatAvatar!!.setImageDrawable(TextDrawable.builder() + .beginConfig() + .width(32) + .height(32) + .endConfig() + .buildRound(Character.toString(chatName[0]), ColorGenerator.MATERIAL.getColor(Character.toString(chatName[0])))) + } + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java index 8faf1f3..bdce831 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java @@ -41,7 +41,6 @@ import io.github.chronosx88.influence.R; import io.github.chronosx88.influence.XMPPConnectionService; import io.github.chronosx88.influence.contracts.CoreContracts; import io.github.chronosx88.influence.helpers.AppHelper; -import io.github.chronosx88.influence.helpers.HashUtils; import io.github.chronosx88.influence.models.appEvents.AuthenticationStatusEvent; public class LoginActivity extends AppCompatActivity implements CoreContracts.ILoginViewContract { diff --git a/app/src/main/res/layout/activity_chat.xml b/app/src/main/res/layout/activity_chat.xml index fe247cd..5e5aeb5 100644 --- a/app/src/main/res/layout/activity_chat.xml +++ b/app/src/main/res/layout/activity_chat.xml @@ -21,7 +21,6 @@