From 4ecfd1f230d581de81057f2112c6a7ad8d4162b1 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Mon, 7 Jun 2021 11:37:20 +1000 Subject: [PATCH] Re-implement conversation item context menu --- .../v2/ConversationActionModeCallback.kt | 53 ++++++++++++++++++- .../conversation/v2/ConversationActivityV2.kt | 9 +--- .../conversation/v2/ConversationAdapter.kt | 1 + .../conversation/v2/ConversationMenuHelper.kt | 7 +++ .../res/layout/activity_conversation_v2.xml | 1 - .../menu/conversation_item_action_menu.xml | 21 -------- .../menu/menu_conversation_item_action.xml | 50 +++++++++++++++++ .../main/res/menu/menu_conversation_muted.xml | 9 ++++ .../res/menu/menu_conversation_unmuted.xml | 9 ++++ 9 files changed, 128 insertions(+), 32 deletions(-) delete mode 100644 app/src/main/res/menu/conversation_item_action_menu.xml create mode 100644 app/src/main/res/menu/menu_conversation_item_action.xml create mode 100644 app/src/main/res/menu/menu_conversation_muted.xml create mode 100644 app/src/main/res/menu/menu_conversation_unmuted.xml diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActionModeCallback.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActionModeCallback.kt index c8b8d60911..a58a88cd36 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActionModeCallback.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActionModeCallback.kt @@ -1,15 +1,64 @@ package org.thoughtcrime.securesms.conversation.v2 +import android.content.Context import android.view.ActionMode import android.view.Menu import android.view.MenuItem import network.loki.messenger.R +import org.session.libsession.messaging.open_groups.OpenGroupAPIV2 +import org.session.libsession.utilities.TextSecurePreferences +import org.thoughtcrime.securesms.database.DatabaseFactory +import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord +import java.util.* -class ConversationActionModeCallback : ActionMode.Callback { +class ConversationActionModeCallback(private val adapter: ConversationAdapter, private val threadID: Long, + private val context: Context) : ActionMode.Callback { override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { + // Prepare val inflater = mode.menuInflater - inflater.inflate(R.menu.conversation_item_action_menu, menu) + inflater.inflate(R.menu.menu_conversation_item_action, menu) + val selectedItems = adapter.selectedItems + val containsControlMessage = selectedItems.any { it.isUpdate } + val hasText = selectedItems.any { it.body.isNotEmpty() } + if (selectedItems.isEmpty()) { mode.finish(); return false } + val firstMessage = selectedItems.iterator().next() + val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getOpenGroupChat(threadID) + val userPublicKey = TextSecurePreferences.getLocalNumber(context)!! + fun userCanDeleteSelectedItems(): Boolean { + if (openGroup == null) { return true } + val allSentByCurrentUser = selectedItems.all { it.isOutgoing } + if (allSentByCurrentUser) { return true } + return OpenGroupAPIV2.isUserModerator(userPublicKey, openGroup.room, openGroup.server) + } + fun userCanBanSelectedUsers(): Boolean { + if (openGroup == null) { return false } + val anySentByCurrentUser = selectedItems.any { it.isOutgoing } + if (anySentByCurrentUser) { return false } // Users can't ban themselves + val selectedUsers = selectedItems.map { it.recipient.address.toString() }.toSet() + if (selectedUsers.size > 1) { return false } + return OpenGroupAPIV2.isUserModerator(userPublicKey, openGroup.room, openGroup.server) + } + // Message info + menu.findItem(R.id.menu_context_details).isVisible = (selectedItems.size == 1) + // Delete message + menu.findItem(R.id.menu_context_delete_message).isVisible = userCanDeleteSelectedItems() + // Ban user + menu.findItem(R.id.menu_context_ban_user).isVisible = userCanBanSelectedUsers() + // Copy message text + menu.findItem(R.id.menu_context_copy).isVisible = !containsControlMessage && hasText + // Copy Session ID + menu.findItem(R.id.menu_context_copy_public_key).isVisible = + (openGroup != null && selectedItems.size == 1 && firstMessage.recipient.address.toString() != userPublicKey) + // Resend + menu.findItem(R.id.menu_context_resend).isVisible = (selectedItems.size == 1 && firstMessage.isFailed) + // Save media + menu.findItem(R.id.menu_context_save_attachment).isVisible = (selectedItems.size == 1 + && firstMessage.isMms && (firstMessage as MediaMmsMessageRecord).containsMediaSlide()) + // Reply + menu.findItem(R.id.menu_context_reply).isVisible = + (selectedItems.size == 1 && !firstMessage.isPending && !firstMessage.isFailed) + // Return return true } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index a345cceb4c..1f3cc38599 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -1,16 +1,11 @@ package org.thoughtcrime.securesms.conversation.v2 import android.database.Cursor -import android.graphics.PorterDuff -import android.graphics.PorterDuffColorFilter import android.os.Bundle import android.util.Log import android.view.ActionMode import android.view.Menu import android.view.MenuItem -import android.widget.ImageView -import android.widget.TextView -import androidx.annotation.ColorInt import androidx.loader.app.LoaderManager import androidx.loader.content.Loader import androidx.recyclerview.widget.ItemTouchHelper @@ -18,10 +13,8 @@ import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_conversation_v2.* import kotlinx.android.synthetic.main.activity_conversation_v2_action_bar.* import network.loki.messenger.R -import org.session.libsession.utilities.ExpirationUtil import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.loki.utilities.getColorWithID import org.thoughtcrime.securesms.mms.GlideApp class ConversationActivityV2 : PassphraseRequiredActionBarActivity() { @@ -106,7 +99,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity() { } private fun handleLongPress(messagePosition: Int) { - val actionModeCallback = ConversationActionModeCallback() + val actionModeCallback = ConversationActionModeCallback(adapter, threadID, this) if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { startActionMode(actionModeCallback, ActionMode.TYPE_PRIMARY) } else { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt index e0cf382fba..5361a0e8fa 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt @@ -14,6 +14,7 @@ import java.lang.IllegalStateException class ConversationAdapter(context: Context, cursor: Cursor, private val onItemLongPress: (Int) -> Unit) : CursorRecyclerViewAdapter(context, cursor) { private val messageDB = DatabaseFactory.getMmsSmsDatabase(context) + var selectedItems = setOf() sealed class ViewType(val rawValue: Int) { object Visible : ViewType(0) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationMenuHelper.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationMenuHelper.kt index 6cfb369b3b..a918faf3d4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationMenuHelper.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationMenuHelper.kt @@ -55,6 +55,13 @@ object ConversationMenuHelper { if (isOpenGroup) { inflater.inflate(R.menu.menu_conversation_open_group, menu) } + // Muting + if (thread.isMuted) { + inflater.inflate(R.menu.menu_conversation_muted, menu) + } else { + inflater.inflate(R.menu.menu_conversation_unmuted, menu) + } + // TODO: Implement search // Return return true } diff --git a/app/src/main/res/layout/activity_conversation_v2.xml b/app/src/main/res/layout/activity_conversation_v2.xml index d12adf80f8..13dc9d7e4c 100644 --- a/app/src/main/res/layout/activity_conversation_v2.xml +++ b/app/src/main/res/layout/activity_conversation_v2.xml @@ -1,7 +1,6 @@ - - - - - - - - - diff --git a/app/src/main/res/menu/menu_conversation_item_action.xml b/app/src/main/res/menu/menu_conversation_item_action.xml new file mode 100644 index 0000000000..109ca2ccb6 --- /dev/null +++ b/app/src/main/res/menu/menu_conversation_item_action.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/menu_conversation_muted.xml b/app/src/main/res/menu/menu_conversation_muted.xml new file mode 100644 index 0000000000..55b181e312 --- /dev/null +++ b/app/src/main/res/menu/menu_conversation_muted.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_conversation_unmuted.xml b/app/src/main/res/menu/menu_conversation_unmuted.xml new file mode 100644 index 0000000000..85b9d16e80 --- /dev/null +++ b/app/src/main/res/menu/menu_conversation_unmuted.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file