Implement basic action mode menu

This commit is contained in:
nielsandriesse 2021-06-04 15:10:58 +10:00
parent 36ba931402
commit 3a4ab06df4
4 changed files with 70 additions and 3 deletions

View File

@ -0,0 +1,27 @@
package org.thoughtcrime.securesms.conversation.v2
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import network.loki.messenger.R
class ConversationActionModeCallback : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
val inflater = mode.menuInflater
inflater.inflate(R.menu.conversation_item_action_menu, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu): Boolean {
return false
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return true
}
override fun onDestroyActionMode(mode: ActionMode) {
}
}

View File

@ -3,6 +3,8 @@ package org.thoughtcrime.securesms.conversation.v2
import android.database.Cursor import android.database.Cursor
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.ActionMode
import android.view.Menu
import androidx.loader.app.LoaderManager import androidx.loader.app.LoaderManager
import androidx.loader.content.Loader import androidx.loader.content.Loader
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
@ -19,7 +21,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity() {
private val adapter by lazy { private val adapter by lazy {
val cursor = DatabaseFactory.getMmsSmsDatabase(this).getConversation(threadID) val cursor = DatabaseFactory.getMmsSmsDatabase(this).getConversation(threadID)
val adapter = ConversationAdapter(this, cursor) val adapter = ConversationAdapter(this, cursor) { handleLongPress(it) }
adapter.setHasStableIds(true) adapter.setHasStableIds(true)
adapter adapter
} }
@ -89,5 +91,14 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity() {
private fun reply(messagePosition: Int) { private fun reply(messagePosition: Int) {
Log.d("Loki", "Reply to message at position: $messagePosition.") Log.d("Loki", "Reply to message at position: $messagePosition.")
} }
private fun handleLongPress(messagePosition: Int) {
val actionModeCallback = ConversationActionModeCallback()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
startActionMode(actionModeCallback, ActionMode.TYPE_PRIMARY)
} else {
startActionMode(actionModeCallback)
}
}
// endregion // endregion
} }

View File

@ -11,7 +11,8 @@ import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MessageRecord
import java.lang.IllegalStateException import java.lang.IllegalStateException
class ConversationAdapter(context: Context, cursor: Cursor) : CursorRecyclerViewAdapter<ViewHolder>(context, cursor) { class ConversationAdapter(context: Context, cursor: Cursor, private val onItemLongPress: (Int) -> Unit)
: CursorRecyclerViewAdapter<ViewHolder>(context, cursor) {
private val messageDB = DatabaseFactory.getMmsSmsDatabase(context) private val messageDB = DatabaseFactory.getMmsSmsDatabase(context)
sealed class ViewType(val rawValue: Int) { sealed class ViewType(val rawValue: Int) {
@ -55,7 +56,14 @@ class ConversationAdapter(context: Context, cursor: Cursor) : CursorRecyclerView
override fun onBindItemViewHolder(viewHolder: ViewHolder, cursor: Cursor) { override fun onBindItemViewHolder(viewHolder: ViewHolder, cursor: Cursor) {
val message = getMessage(cursor)!! val message = getMessage(cursor)!!
when (viewHolder) { when (viewHolder) {
is VisibleMessageViewHolder -> viewHolder.view.bind(message) is VisibleMessageViewHolder -> {
val view = viewHolder.view
view.bind(message)
view.setOnLongClickListener {
onItemLongPress(viewHolder.adapterPosition)
true
}
}
is ControlMessageViewHolder -> viewHolder.view.bind(message) is ControlMessageViewHolder -> viewHolder.view.bind(message)
} }
} }

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@drawable/ic_baseline_delete_24"
android:title="Title"
app:showAsAction="ifRoom" />
<item
android:icon="@drawable/ic_baseline_delete_24"
android:title="Title"
app:showAsAction="ifRoom" />
<item
android:icon="@drawable/ic_baseline_delete_24"
android:title="Title"
app:showAsAction="ifRoom" />
</menu>