From bd732fa82559757e04f184c393d80b71c22cd5c5 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 25 May 2019 13:02:03 +0400 Subject: [PATCH] Implemented ability to clear chat --- .../influence/contracts/CoreContracts.kt | 2 ++ .../influence/helpers/LocalDBWrapper.java | 4 +++ .../influence/models/daos/MessageDao.java | 3 +++ .../influence/presenters/ChatPresenter.kt | 11 ++++++++ .../influence/views/ChatActivity.kt | 16 +++++++++--- app/src/main/res/menu/chat_activity_menu.xml | 25 +++++++++++++++++++ app/src/main/res/values-ru-rRU/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/menu/chat_activity_menu.xml diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt b/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt index 12c28ba..e533ad6 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt @@ -18,6 +18,7 @@ package io.github.chronosx88.influence.contracts import android.content.Context import android.content.Intent +import android.view.MenuItem import com.stfalcon.chatkit.dialogs.DialogsListAdapter import com.stfalcon.chatkit.messages.MessagesListAdapter @@ -86,6 +87,7 @@ interface CoreContracts { fun sendMessage(text: String): Boolean fun loadLocalMessages() fun onDestroy() + fun onOptionsItemSelected(item: MenuItem) } interface IChatViewContract { 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 2564eee..4706104 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 @@ -88,4 +88,8 @@ public class LocalDBWrapper { public static void updateChatUnreadMessagesCount(String chatID, int unreadMessagesCount) { dbInstance.chatDao().updateUnreadMessagesCount(chatID, unreadMessagesCount); } + + public static void clearChat(String chatID) { + dbInstance.messageDao().clearMessagesByChatID(chatID); + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java b/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java index 04978d7..b83e8cd 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java @@ -48,6 +48,9 @@ public interface MessageDao { @Query("DELETE FROM messages") void clearMessages(); + @Query("DELETE FROM messages WHERE jid = :chatID") + void clearMessagesByChatID(String chatID); + @Query("SELECT messageID FROM messages WHERE jid = :chatID GROUP BY :chatID HAVING MAX(messageID)") long getLastMessageByChatID(String chatID); } 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 7a5839c..c063883 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 @@ -16,6 +16,7 @@ package io.github.chronosx88.influence.presenters +import android.view.MenuItem import com.google.gson.Gson import com.stfalcon.chatkit.messages.MessageHolders import com.stfalcon.chatkit.messages.MessagesListAdapter @@ -112,4 +113,14 @@ class ChatPresenter(private val view: CoreContracts.IChatViewContract, private v }) } } + + override fun onOptionsItemSelected(item: MenuItem) { + when (item.itemId) { + R.id.menu_clear_chat -> { + LocalDBWrapper.clearChat(chatID) + chatAdapter.clear() + EventBus.getDefault().post(LastMessageEvent(chatID, null)) + } + } + } } 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 46cb4f9..44030a7 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 @@ -19,6 +19,7 @@ package io.github.chronosx88.influence.views import android.content.Intent import android.graphics.BitmapFactory import android.os.Bundle +import android.view.Menu import android.view.MenuItem import android.widget.ImageView import android.widget.TextView @@ -34,6 +35,7 @@ import com.stfalcon.chatkit.messages.MessagesListAdapter import io.github.chronosx88.influence.R import io.github.chronosx88.influence.contracts.CoreContracts import io.github.chronosx88.influence.helpers.AppHelper +import io.github.chronosx88.influence.helpers.LocalDBWrapper import io.github.chronosx88.influence.models.GenericMessage import io.github.chronosx88.influence.models.roomEntities.MessageEntity import io.github.chronosx88.influence.presenters.ChatPresenter @@ -59,12 +61,12 @@ class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { supportActionBar!!.setTitle("") supportActionBar!!.setDisplayHomeAsUpEnabled(true) supportActionBar!!.setHomeButtonEnabled(true) - messageList = findViewById(R.id.messages_list) + messageList = find(R.id.messages_list) messageList!!.layoutManager = LinearLayoutManager(this) chatNameTextView = find(R.id.appbar_username) userStatus = find(R.id.user_status_text) - chatAvatar = findViewById(R.id.profile_image_chat_activity) - messageInput = findViewById(R.id.message_input) + chatAvatar = find(R.id.profile_image_chat_activity) + messageInput = find(R.id.message_input) messageInput!!.setInputListener { presenter!!.sendMessage(it.toString()) } @@ -81,6 +83,7 @@ class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { return true } } + presenter!!.onOptionsItemSelected(item) return super.onOptionsItemSelected(item) } @@ -114,4 +117,11 @@ class ChatActivity : AppCompatActivity(), CoreContracts.IChatViewContract { override fun setUserStatus(status: String) { userStatus!!.text = status } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.chat_activity_menu, menu) + return true + } + + } diff --git a/app/src/main/res/menu/chat_activity_menu.xml b/app/src/main/res/menu/chat_activity_menu.xml new file mode 100644 index 0000000..328efff --- /dev/null +++ b/app/src/main/res/menu/chat_activity_menu.xml @@ -0,0 +1,25 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-ru-rRU/strings.xml b/app/src/main/res/values-ru-rRU/strings.xml index 720dfb4..742251b 100644 --- a/app/src/main/res/values-ru-rRU/strings.xml +++ b/app/src/main/res/values-ru-rRU/strings.xml @@ -26,4 +26,5 @@ Неверный JabberID! Не в сети В сети + Очистить чат \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 352faed..c31d346 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,4 +25,5 @@ Invalid JabberID! Offline Online + Clear chat