Implemented ability to clear chat

This commit is contained in:
ChronosX88 2019-05-25 13:02:03 +04:00
parent 5c42a2304f
commit bd732fa825
8 changed files with 60 additions and 3 deletions

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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))
}
}
}
}

View File

@ -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
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 ChronosX88
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_clear_chat"
app:showAsAction="never"
android:title="@string/clear_chat">
</item>
</menu>

View File

@ -26,4 +26,5 @@
<string name="invalid_jid_error">Неверный JabberID!</string>
<string name="offline">Не в сети</string>
<string name="online">В сети</string>
<string name="clear_chat">Очистить чат</string>
</resources>

View File

@ -25,4 +25,5 @@
<string name="invalid_jid_error">Invalid JabberID!</string>
<string name="offline">Offline</string>
<string name="online">Online</string>
<string name="clear_chat">Clear chat</string>
</resources>