2021-05-31 06:06:02 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2
|
|
|
|
|
2021-06-04 01:58:04 +02:00
|
|
|
import android.database.Cursor
|
2021-05-31 06:06:02 +02:00
|
|
|
import android.os.Bundle
|
2021-06-04 07:10:58 +02:00
|
|
|
import android.view.ActionMode
|
|
|
|
import android.view.Menu
|
2021-06-07 01:48:01 +02:00
|
|
|
import android.view.MenuItem
|
2021-06-04 01:58:04 +02:00
|
|
|
import androidx.loader.app.LoaderManager
|
|
|
|
import androidx.loader.content.Loader
|
2021-05-31 06:06:02 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2021-05-31 06:29:11 +02:00
|
|
|
import kotlinx.android.synthetic.main.activity_conversation_v2.*
|
2021-06-04 06:55:53 +02:00
|
|
|
import kotlinx.android.synthetic.main.activity_conversation_v2_action_bar.*
|
2021-06-15 06:55:57 +02:00
|
|
|
import kotlinx.android.synthetic.main.view_input_bar.view.*
|
2021-05-31 06:06:02 +02:00
|
|
|
import network.loki.messenger.R
|
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
2021-06-07 06:04:55 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.menus.ConversationActionModeCallback
|
|
|
|
import org.thoughtcrime.securesms.conversation.v2.menus.ConversationMenuHelper
|
2021-05-31 06:06:02 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2021-06-07 06:04:55 +02:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord
|
2021-06-01 08:17:14 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp
|
2021-05-31 06:06:02 +02:00
|
|
|
|
|
|
|
class ConversationActivityV2 : PassphraseRequiredActionBarActivity() {
|
|
|
|
private var threadID: Long = -1
|
2021-06-07 06:04:55 +02:00
|
|
|
private var actionMode: ActionMode? = null
|
2021-05-31 06:06:02 +02:00
|
|
|
|
2021-06-08 07:29:02 +02:00
|
|
|
// TODO: Selected message background color
|
|
|
|
// TODO: Overflow menu background + text color
|
|
|
|
|
2021-06-04 01:58:04 +02:00
|
|
|
private val adapter by lazy {
|
|
|
|
val cursor = DatabaseFactory.getMmsSmsDatabase(this).getConversation(threadID)
|
2021-06-07 06:04:55 +02:00
|
|
|
val adapter = ConversationAdapter(
|
|
|
|
this,
|
|
|
|
cursor,
|
|
|
|
onItemPress = { message, position ->
|
|
|
|
handlePress(message, position)
|
|
|
|
},
|
2021-06-09 03:37:50 +02:00
|
|
|
onItemSwipeToReply = { message, position ->
|
|
|
|
handleSwipeToReply(message, position)
|
|
|
|
},
|
2021-06-07 06:04:55 +02:00
|
|
|
onItemLongPress = { message, position ->
|
|
|
|
handleLongPress(message, position)
|
|
|
|
}
|
|
|
|
)
|
2021-06-04 01:58:04 +02:00
|
|
|
adapter.setHasStableIds(true)
|
|
|
|
adapter
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:17:14 +02:00
|
|
|
private val thread by lazy {
|
|
|
|
DatabaseFactory.getThreadDatabase(this).getRecipientForThreadId(threadID)!!
|
|
|
|
}
|
|
|
|
|
|
|
|
private val glide by lazy { GlideApp.with(this) }
|
|
|
|
|
2021-05-31 06:06:02 +02:00
|
|
|
// region Settings
|
|
|
|
companion object {
|
|
|
|
const val THREAD_ID = "thread_id"
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
|
|
|
|
super.onCreate(savedInstanceState, isReady)
|
|
|
|
setContentView(R.layout.activity_conversation_v2)
|
|
|
|
threadID = intent.getLongExtra(THREAD_ID, -1)
|
|
|
|
setUpRecyclerView()
|
2021-06-15 06:55:57 +02:00
|
|
|
setUpToolBar()
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpRecyclerView() {
|
2021-05-31 06:29:11 +02:00
|
|
|
conversationRecyclerView.adapter = adapter
|
2021-06-07 08:36:05 +02:00
|
|
|
val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true)
|
2021-06-02 05:03:22 +02:00
|
|
|
conversationRecyclerView.layoutManager = layoutManager
|
2021-06-07 01:48:01 +02:00
|
|
|
// Workaround for the fact that CursorRecyclerViewAdapter doesn't auto-update automatically (even though it says it will)
|
2021-06-04 01:58:04 +02:00
|
|
|
LoaderManager.getInstance(this).restartLoader(0, null, object : LoaderManager.LoaderCallbacks<Cursor> {
|
|
|
|
|
|
|
|
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<Cursor> {
|
|
|
|
return ConversationLoader(threadID, this@ConversationActivityV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor?) {
|
|
|
|
adapter.changeCursor(cursor)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoaderReset(cursor: Loader<Cursor>) {
|
|
|
|
adapter.changeCursor(null)
|
|
|
|
}
|
|
|
|
})
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
2021-06-01 08:17:14 +02:00
|
|
|
|
2021-06-15 06:55:57 +02:00
|
|
|
private fun setUpToolBar() {
|
2021-06-04 06:55:53 +02:00
|
|
|
val actionBar = supportActionBar!!
|
|
|
|
actionBar.setCustomView(R.layout.activity_conversation_v2_action_bar)
|
|
|
|
actionBar.setDisplayShowCustomEnabled(true)
|
2021-06-01 08:17:14 +02:00
|
|
|
conversationTitleView.text = thread.toShortString()
|
2021-06-02 05:28:02 +02:00
|
|
|
profilePictureView.glide = glide
|
|
|
|
profilePictureView.update(thread, threadID)
|
2021-06-01 08:17:14 +02:00
|
|
|
}
|
2021-06-07 01:48:01 +02:00
|
|
|
|
|
|
|
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
|
2021-06-07 06:04:55 +02:00
|
|
|
ConversationMenuHelper.onPrepareOptionsMenu(menu, menuInflater, thread, this) { onOptionsItemSelected(it) }
|
|
|
|
super.onPrepareOptionsMenu(menu)
|
|
|
|
return true
|
2021-06-07 01:48:01 +02:00
|
|
|
}
|
2021-06-01 08:17:14 +02:00
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Interaction
|
2021-06-07 01:48:01 +02:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
2021-06-01 08:17:14 +02:00
|
|
|
// TODO: Implement
|
2021-06-07 01:48:01 +02:00
|
|
|
return super.onOptionsItemSelected(item)
|
2021-06-01 08:17:14 +02:00
|
|
|
}
|
2021-06-04 05:15:43 +02:00
|
|
|
|
2021-06-09 03:37:50 +02:00
|
|
|
// `position` is the adapter position; not the visual position
|
2021-06-07 06:04:55 +02:00
|
|
|
private fun handlePress(message: MessageRecord, position: Int) {
|
|
|
|
val actionMode = this.actionMode
|
|
|
|
if (actionMode != null) {
|
|
|
|
adapter.toggleSelection(message, position)
|
|
|
|
val actionModeCallback = ConversationActionModeCallback(adapter, threadID, this)
|
|
|
|
actionModeCallback.updateActionModeMenu(actionMode.menu)
|
|
|
|
if (adapter.selectedItems.isEmpty()) {
|
|
|
|
actionMode.finish()
|
|
|
|
this.actionMode = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 03:37:50 +02:00
|
|
|
// `position` is the adapter position; not the visual position
|
|
|
|
private fun handleSwipeToReply(message: MessageRecord, position: Int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// `position` is the adapter position; not the visual position
|
2021-06-07 06:04:55 +02:00
|
|
|
private fun handleLongPress(message: MessageRecord, position: Int) {
|
|
|
|
val actionMode = this.actionMode
|
2021-06-07 03:37:20 +02:00
|
|
|
val actionModeCallback = ConversationActionModeCallback(adapter, threadID, this)
|
2021-06-07 06:04:55 +02:00
|
|
|
if (actionMode == null) { // Nothing should be selected if this is the case
|
|
|
|
adapter.toggleSelection(message, position)
|
|
|
|
this.actionMode = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
|
|
startActionMode(actionModeCallback, ActionMode.TYPE_PRIMARY)
|
|
|
|
} else {
|
|
|
|
startActionMode(actionModeCallback)
|
|
|
|
}
|
2021-06-04 07:10:58 +02:00
|
|
|
} else {
|
2021-06-07 06:04:55 +02:00
|
|
|
adapter.toggleSelection(message, position)
|
|
|
|
actionModeCallback.updateActionModeMenu(actionMode.menu)
|
|
|
|
if (adapter.selectedItems.isEmpty()) {
|
|
|
|
actionMode.finish()
|
|
|
|
this.actionMode = null
|
|
|
|
}
|
2021-06-04 07:10:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 06:06:02 +02:00
|
|
|
// endregion
|
|
|
|
}
|