From ba2fbc2d0e5db4c03b043aafc753c3acea6638a0 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 24 May 2019 20:51:05 +0400 Subject: [PATCH] Made unread messages counting --- .../influence/helpers/LocalDBWrapper.java | 4 ++++ .../influence/helpers/NetworkHandler.java | 11 +++++---- .../chronosx88/influence/logic/ChatLogic.java | 2 -- .../influence/models/GenericDialog.java | 4 ++++ .../influence/models/daos/ChatDao.java | 3 +++ .../influence/presenters/ChatPresenter.kt | 3 ++- .../presenters/DialogListPresenter.java | 23 +++++++++++-------- .../influence/views/ChatActivity.kt | 1 + 8 files changed, 34 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java index 0e5255b..2564eee 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java @@ -84,4 +84,8 @@ public class LocalDBWrapper { long messageID = dbInstance.messageDao().getLastMessageByChatID(chatID); return getMessageByID(messageID); } + + public static void updateChatUnreadMessagesCount(String chatID, int unreadMessagesCount) { + dbInstance.chatDao().updateUnreadMessagesCount(chatID, unreadMessagesCount); + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java index 7aae227..14bd2e2 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java @@ -33,12 +33,15 @@ public class NetworkHandler implements IncomingChatMessageListener { @Override public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) { + String chatID = chat.getXmppAddressOfChatPartner().asUnescapedString(); if(LocalDBWrapper.getChatByChatID(from.asEntityBareJidString()) == null) { - LocalDBWrapper.createChatEntry(chat.getXmppAddressOfChatPartner().asUnescapedString(), chat.getXmppAddressOfChatPartner().asBareJid().asUnescapedString()); + LocalDBWrapper.createChatEntry(chatID, chat.getXmppAddressOfChatPartner().asBareJid().asUnescapedString()); } - long messageID = LocalDBWrapper.createMessageEntry(chat.getXmppAddressOfChatPartner().asUnescapedString(), from.asUnescapedString(), TrueTime.now().getTime(), message.getBody(), true, false); + long messageID = LocalDBWrapper.createMessageEntry(chatID, from.asUnescapedString(), TrueTime.now().getTime(), message.getBody(), true, false); + int newUnreadMessagesCount = LocalDBWrapper.getChatByChatID(chatID).unreadMessagesCount + 1; + LocalDBWrapper.updateChatUnreadMessagesCount(chatID, newUnreadMessagesCount); - EventBus.getDefault().post(new NewMessageEvent(chat.getXmppAddressOfChatPartner().toString(), messageID)); - EventBus.getDefault().post(new LastMessageEvent(chat.getXmppAddressOfChatPartner().toString(), new GenericMessage(LocalDBWrapper.getMessageByID(messageID)))); + EventBus.getDefault().post(new NewMessageEvent(chatID, messageID)); + EventBus.getDefault().post(new LastMessageEvent(chatID, new GenericMessage(LocalDBWrapper.getMessageByID(messageID)))); } } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java index cf0aad7..e625e3e 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java @@ -33,12 +33,10 @@ import io.github.chronosx88.influence.models.roomEntities.MessageEntity; public class ChatLogic implements CoreContracts.IChatLogicContract { private String chatID; private ChatEntity chatEntity; - //private KeyPairManager keyPairManager; public ChatLogic(ChatEntity chatEntity) { this.chatEntity = chatEntity; this.chatID = chatEntity.jid; - //this.keyPairManager = new KeyPairManager(); } @Override diff --git a/app/src/main/java/io/github/chronosx88/influence/models/GenericDialog.java b/app/src/main/java/io/github/chronosx88/influence/models/GenericDialog.java index a5b3f03..a83be7a 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/GenericDialog.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/GenericDialog.java @@ -75,4 +75,8 @@ public class GenericDialog implements IDialog { public int getUnreadCount() { return unreadMessagesCount; } + + public void setUnreadMessagesCount(int unreadMessagesCount) { + this.unreadMessagesCount = unreadMessagesCount; + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java b/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java index a23745d..16a3c5d 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java @@ -44,4 +44,7 @@ public interface ChatDao { @Query("DELETE FROM chats") void clearChats(); + + @Query("UPDATE chats SET unreadMessagesCount = :unreadMessagesCount WHERE jid = :chatID") + void updateUnreadMessagesCount(String chatID, int unreadMessagesCount); } diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt index 7314929..a661c2d 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt @@ -74,7 +74,7 @@ class ChatPresenter(private val view: CoreContracts.IChatViewContract, private v } override fun onDestroy() { - // + EventBus.getDefault().unregister(this) } @Subscribe(threadMode = ThreadMode.MAIN) @@ -82,6 +82,7 @@ class ChatPresenter(private val view: CoreContracts.IChatViewContract, private v if(event.chatID.equals(chatEntity!!.jid)) { val messageID = event.messageID chatAdapter.addToStart(GenericMessage(LocalDBWrapper.getMessageByID(messageID)), true) + LocalDBWrapper.updateChatUnreadMessagesCount(chatEntity.jid, 0) } } } 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 36b6e9d..5081a6c 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 @@ -17,27 +17,17 @@ package io.github.chronosx88.influence.presenters; import android.content.Intent; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import androidx.appcompat.app.AlertDialog; -import com.amulyakhare.textdrawable.TextDrawable; -import com.amulyakhare.textdrawable.util.ColorGenerator; import com.stfalcon.chatkit.dialogs.DialogsListAdapter; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; -import org.jivesoftware.smack.roster.RosterEntry; -import org.jxmpp.jid.EntityBareJid; -import org.jxmpp.jid.impl.JidCreate; -import org.jxmpp.stringprep.XmppStringprepException; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import io.github.chronosx88.influence.R; @@ -115,6 +105,8 @@ public class DialogListPresenter implements CoreContracts.IDialogListPresenterCo intent.putExtra("chatName", LocalDBWrapper.getChatByChatID(chatID).chatName); intent.putExtra("chatAvatar", AppHelper.avatarsCache.get(chatID)); view.startActivity(intent); + setUnreadMessagesCount(chatID, 0); + LocalDBWrapper.updateChatUnreadMessagesCount(chatID, 0); } @Subscribe @@ -161,6 +153,17 @@ public class DialogListPresenter implements CoreContracts.IDialogListPresenterCo @Subscribe(threadMode = ThreadMode.MAIN) public void onLastMessage(LastMessageEvent event) { dialogListAdapter.updateDialogWithMessage(event.chatID, event.message); + GenericDialog dialog = new GenericDialog(LocalDBWrapper.getChatByChatID(event.chatID)); + dialog.setLastMessage(event.message); + dialogListAdapter.updateItemById(dialog); dialogListAdapter.sort(dialogComparator); } + + private void setUnreadMessagesCount(String chatID, int unreadMessagesCount) { + GenericDialog dialog = dialogListAdapter.getItemById(chatID); + if(dialog != null) { + dialog.setUnreadMessagesCount(unreadMessagesCount); + dialogListAdapter.updateItemById(dialog); + } + } } 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 97af647..d2d96f8 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 @@ -84,6 +84,7 @@ class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { override fun onDestroy() { super.onDestroy() presenter!!.onDestroy() + presenter = null } override fun setAdapter(adapter: MessagesListAdapter) {