2021-05-31 06:06:02 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2.messages
|
|
|
|
|
|
|
|
import android.content.Context
|
2021-06-23 03:32:05 +02:00
|
|
|
import android.content.res.Resources
|
2021-06-09 07:12:48 +02:00
|
|
|
import android.graphics.Canvas
|
|
|
|
import android.graphics.Rect
|
2021-06-09 04:04:50 +02:00
|
|
|
import android.graphics.drawable.ColorDrawable
|
2021-06-09 02:57:40 +02:00
|
|
|
import android.os.Build
|
2021-07-14 06:39:20 +02:00
|
|
|
import android.os.Bundle
|
2021-06-09 03:18:15 +02:00
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
2021-05-31 06:06:02 +02:00
|
|
|
import android.util.AttributeSet
|
2021-06-09 02:57:40 +02:00
|
|
|
import android.view.*
|
2021-05-31 06:06:02 +02:00
|
|
|
import android.widget.LinearLayout
|
2021-06-30 07:40:15 +02:00
|
|
|
import android.widget.RelativeLayout
|
2021-07-14 06:39:20 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-06-09 07:12:48 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-06-30 07:40:15 +02:00
|
|
|
import androidx.core.content.res.ResourcesCompat
|
2021-06-07 08:06:37 +02:00
|
|
|
import androidx.core.view.isVisible
|
2021-10-04 09:51:19 +02:00
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
2021-06-01 01:48:02 +02:00
|
|
|
import kotlinx.android.synthetic.main.view_visible_message.view.*
|
2021-05-31 06:29:11 +02:00
|
|
|
import network.loki.messenger.R
|
2021-06-01 05:01:03 +02:00
|
|
|
import org.session.libsession.messaging.contacts.Contact.ContactContext
|
2021-06-22 08:42:53 +02:00
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupAPIV2
|
2021-06-07 07:37:21 +02:00
|
|
|
import org.session.libsession.utilities.ViewUtil
|
2021-06-30 07:40:15 +02:00
|
|
|
import org.session.libsignal.utilities.ThreadUtils
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.thoughtcrime.securesms.database.*
|
2021-05-31 06:06:02 +02:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord
|
2021-07-14 06:39:20 +02:00
|
|
|
import org.thoughtcrime.securesms.home.UserDetailsBottomSheet
|
2021-06-21 07:26:09 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests
|
2021-07-09 05:18:48 +02:00
|
|
|
import org.thoughtcrime.securesms.util.*
|
2021-06-07 08:06:37 +02:00
|
|
|
import java.util.*
|
2021-10-04 09:51:19 +02:00
|
|
|
import javax.inject.Inject
|
2021-06-09 02:57:40 +02:00
|
|
|
import kotlin.math.abs
|
2021-06-09 07:12:48 +02:00
|
|
|
import kotlin.math.min
|
2021-06-07 07:37:21 +02:00
|
|
|
import kotlin.math.roundToInt
|
2021-06-09 02:57:40 +02:00
|
|
|
import kotlin.math.sqrt
|
2021-10-04 09:51:19 +02:00
|
|
|
@AndroidEntryPoint
|
2021-06-01 01:48:02 +02:00
|
|
|
class VisibleMessageView : LinearLayout {
|
2021-10-04 09:51:19 +02:00
|
|
|
|
|
|
|
@Inject lateinit var threadDb: ThreadDatabase
|
|
|
|
@Inject lateinit var contactDb: SessionContactDatabase
|
|
|
|
@Inject lateinit var lokiThreadDb: LokiThreadDatabase
|
|
|
|
@Inject lateinit var mmsSmsDb: MmsSmsDatabase
|
|
|
|
@Inject lateinit var smsDb: SmsDatabase
|
|
|
|
@Inject lateinit var mmsDb: MmsDatabase
|
|
|
|
|
2021-06-23 03:32:05 +02:00
|
|
|
private val screenWidth = Resources.getSystem().displayMetrics.widthPixels
|
2021-06-10 02:39:15 +02:00
|
|
|
private val swipeToReplyIcon = ContextCompat.getDrawable(context, R.drawable.ic_baseline_reply_24)!!.mutate()
|
2021-06-09 07:12:48 +02:00
|
|
|
private val swipeToReplyIconRect = Rect()
|
2021-06-09 02:57:40 +02:00
|
|
|
private var dx = 0.0f
|
|
|
|
private var previousTranslationX = 0.0f
|
2021-06-09 03:18:15 +02:00
|
|
|
private val gestureHandler = Handler(Looper.getMainLooper())
|
2021-06-28 07:41:23 +02:00
|
|
|
private var pressCallback: Runnable? = null
|
2021-06-09 03:18:15 +02:00
|
|
|
private var longPressCallback: Runnable? = null
|
2021-06-09 03:37:50 +02:00
|
|
|
private var onDownTimestamp = 0L
|
2021-06-28 07:41:23 +02:00
|
|
|
private var onDoubleTap: (() -> Unit)? = null
|
2021-07-14 01:37:18 +02:00
|
|
|
var indexInAdapter: Int = -1
|
2021-06-09 04:04:50 +02:00
|
|
|
var snIsSelected = false
|
|
|
|
set(value) { field = value; handleIsSelectedChanged()}
|
2021-06-30 06:29:32 +02:00
|
|
|
var onPress: ((event: MotionEvent) -> Unit)? = null
|
2021-06-09 03:37:50 +02:00
|
|
|
var onSwipeToReply: (() -> Unit)? = null
|
|
|
|
var onLongPress: (() -> Unit)? = null
|
2021-06-30 02:30:10 +02:00
|
|
|
var contentViewDelegate: VisibleMessageContentViewDelegate? = null
|
2021-06-09 02:57:40 +02:00
|
|
|
|
|
|
|
companion object {
|
2021-07-14 03:07:46 +02:00
|
|
|
const val swipeToReplyThreshold = 64.0f // dp
|
2021-06-09 03:18:15 +02:00
|
|
|
const val longPressMovementTreshold = 10.0f // dp
|
2021-06-09 03:37:50 +02:00
|
|
|
const val longPressDurationThreshold = 250L // ms
|
2021-06-28 07:41:23 +02:00
|
|
|
const val maxDoubleTapInterval = 200L
|
2021-06-09 02:57:40 +02:00
|
|
|
}
|
2021-05-31 06:06:02 +02:00
|
|
|
|
|
|
|
// region Lifecycle
|
2021-06-18 07:54:24 +02:00
|
|
|
constructor(context: Context) : super(context) { initialize() }
|
|
|
|
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() }
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialize() }
|
2021-05-31 06:06:02 +02:00
|
|
|
|
2021-06-09 02:57:40 +02:00
|
|
|
private fun initialize() {
|
2021-06-01 01:48:02 +02:00
|
|
|
LayoutInflater.from(context).inflate(R.layout.view_visible_message, this)
|
2021-06-01 06:28:14 +02:00
|
|
|
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
2021-06-09 02:57:40 +02:00
|
|
|
isHapticFeedbackEnabled = true
|
2021-06-09 07:12:48 +02:00
|
|
|
setWillNotDraw(false)
|
2021-07-01 03:18:51 +02:00
|
|
|
expirationTimerViewContainer.disableClipping()
|
|
|
|
messageContentContainer.disableClipping()
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
2021-06-29 03:49:45 +02:00
|
|
|
fun bind(message: MessageRecord, previous: MessageRecord?, next: MessageRecord?, glide: GlideRequests, searchQuery: String?) {
|
2021-06-01 05:01:03 +02:00
|
|
|
val sender = message.individualRecipient
|
|
|
|
val senderSessionID = sender.address.serialize()
|
|
|
|
val threadID = message.threadId
|
2021-10-04 09:51:19 +02:00
|
|
|
val thread = threadDb.getRecipientForThreadId(threadID) ?: return
|
|
|
|
val contact = contactDb.getContactWithSessionID(senderSessionID)
|
2021-06-23 05:57:13 +02:00
|
|
|
val isGroupThread = thread.isGroupRecipient
|
2021-06-22 08:42:53 +02:00
|
|
|
val isStartOfMessageCluster = isStartOfMessageCluster(message, previous, isGroupThread)
|
|
|
|
val isEndOfMessageCluster = isEndOfMessageCluster(message, next, isGroupThread)
|
2021-06-01 06:28:14 +02:00
|
|
|
// Show profile picture and sender name if this is a group thread AND
|
|
|
|
// the message is incoming
|
|
|
|
if (isGroupThread && !message.isOutgoing) {
|
2021-06-22 08:42:53 +02:00
|
|
|
profilePictureContainer.visibility = if (isEndOfMessageCluster) View.VISIBLE else View.INVISIBLE
|
2021-06-01 05:01:03 +02:00
|
|
|
profilePictureView.publicKey = senderSessionID
|
2021-06-22 08:42:53 +02:00
|
|
|
profilePictureView.glide = glide
|
2021-07-26 05:44:04 +02:00
|
|
|
profilePictureView.update(message.individualRecipient, threadID)
|
2021-07-14 06:39:20 +02:00
|
|
|
profilePictureView.setOnClickListener { showUserDetails(message.recipient.address.toString()) }
|
2021-06-22 08:42:53 +02:00
|
|
|
if (thread.isOpenGroupRecipient) {
|
2021-10-04 09:51:19 +02:00
|
|
|
val openGroup = lokiThreadDb.getOpenGroupChat(threadID) ?: return
|
2021-06-22 08:42:53 +02:00
|
|
|
val isModerator = OpenGroupAPIV2.isUserModerator(senderSessionID, openGroup.room, openGroup.server)
|
|
|
|
moderatorIconImageView.visibility = if (isModerator) View.VISIBLE else View.INVISIBLE
|
|
|
|
} else {
|
|
|
|
moderatorIconImageView.visibility = View.INVISIBLE
|
|
|
|
}
|
|
|
|
senderNameTextView.isVisible = isStartOfMessageCluster
|
|
|
|
val context = if (thread.isOpenGroupRecipient) ContactContext.OPEN_GROUP else ContactContext.REGULAR
|
2021-07-09 07:13:43 +02:00
|
|
|
senderNameTextView.text = contact?.displayName(context) ?: senderSessionID
|
2021-06-01 05:01:03 +02:00
|
|
|
} else {
|
|
|
|
profilePictureContainer.visibility = View.GONE
|
|
|
|
senderNameTextView.visibility = View.GONE
|
|
|
|
}
|
2021-06-01 06:38:52 +02:00
|
|
|
// Date break
|
2021-12-06 05:34:32 +01:00
|
|
|
dateBreakTextView.showDateBreak(message, previous)
|
2021-06-08 06:06:16 +02:00
|
|
|
// Timestamp
|
2021-07-08 05:37:08 +02:00
|
|
|
messageTimestampTextView.text = DateUtils.getDisplayFormattedTimeSpanString(context, Locale.getDefault(), message.timestamp)
|
2021-06-01 06:28:14 +02:00
|
|
|
// Margins
|
2021-07-01 03:35:33 +02:00
|
|
|
val startPadding: Int
|
2021-06-01 07:43:37 +02:00
|
|
|
if (isGroupThread) {
|
2021-07-01 03:35:33 +02:00
|
|
|
startPadding = if (message.isOutgoing) resources.getDimension(R.dimen.very_large_spacing).toInt() else 0
|
2021-06-01 07:43:37 +02:00
|
|
|
} else {
|
2021-07-01 03:35:33 +02:00
|
|
|
startPadding = if (message.isOutgoing) resources.getDimension(R.dimen.very_large_spacing).toInt()
|
2021-06-01 07:43:37 +02:00
|
|
|
else resources.getDimension(R.dimen.medium_spacing).toInt()
|
|
|
|
}
|
2021-07-01 03:35:33 +02:00
|
|
|
val endPadding = if (message.isOutgoing) resources.getDimension(R.dimen.medium_spacing).toInt()
|
2021-06-01 07:43:37 +02:00
|
|
|
else resources.getDimension(R.dimen.very_large_spacing).toInt()
|
2021-07-01 03:35:33 +02:00
|
|
|
messageContentContainer.setPaddingRelative(startPadding, 0, endPadding, 0)
|
2021-06-07 07:37:21 +02:00
|
|
|
// Set inter-message spacing
|
2021-06-07 07:48:22 +02:00
|
|
|
setMessageSpacing(isStartOfMessageCluster, isEndOfMessageCluster)
|
2021-06-01 06:28:14 +02:00
|
|
|
// Gravity
|
2021-07-01 03:35:33 +02:00
|
|
|
val gravity = if (message.isOutgoing) Gravity.END else Gravity.START
|
2021-06-01 06:38:52 +02:00
|
|
|
mainContainer.gravity = gravity or Gravity.BOTTOM
|
2021-06-22 07:41:14 +02:00
|
|
|
// Message status indicator
|
2021-07-01 03:06:11 +02:00
|
|
|
val (iconID, iconColor) = getMessageStatusImage(message)
|
2021-06-22 07:41:14 +02:00
|
|
|
if (iconID != null) {
|
2021-07-01 03:06:11 +02:00
|
|
|
val drawable = ContextCompat.getDrawable(context, iconID)?.mutate()
|
|
|
|
if (iconColor != null) {
|
|
|
|
drawable?.setTint(iconColor)
|
|
|
|
}
|
|
|
|
messageStatusImageView.setImageDrawable(drawable)
|
2021-06-22 07:41:14 +02:00
|
|
|
}
|
|
|
|
if (message.isOutgoing) {
|
2021-10-04 09:51:19 +02:00
|
|
|
val lastMessageID = mmsSmsDb.getLastMessageID(message.threadId)
|
2021-06-22 07:41:14 +02:00
|
|
|
messageStatusImageView.isVisible = !message.isSent || message.id == lastMessageID
|
|
|
|
} else {
|
|
|
|
messageStatusImageView.isVisible = false
|
|
|
|
}
|
2021-06-30 07:40:15 +02:00
|
|
|
// Expiration timer
|
|
|
|
updateExpirationTimer(message)
|
2021-06-23 05:57:13 +02:00
|
|
|
// Calculate max message bubble width
|
2021-07-01 03:35:33 +02:00
|
|
|
var maxWidth = screenWidth - startPadding - endPadding
|
2021-06-23 05:39:24 +02:00
|
|
|
if (profilePictureContainer.visibility != View.GONE) { maxWidth -= profilePictureContainer.width }
|
2021-06-23 05:57:13 +02:00
|
|
|
// Populate content view
|
2021-07-14 01:37:18 +02:00
|
|
|
messageContentView.indexInAdapter = indexInAdapter
|
2021-07-09 07:13:43 +02:00
|
|
|
messageContentView.bind(message, isStartOfMessageCluster, isEndOfMessageCluster, glide, maxWidth, thread, searchQuery, isGroupThread || (contact?.isTrusted ?: false))
|
2021-06-30 02:30:10 +02:00
|
|
|
messageContentView.delegate = contentViewDelegate
|
2021-06-28 07:41:23 +02:00
|
|
|
onDoubleTap = { messageContentView.onContentDoubleTap?.invoke() }
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 07:48:22 +02:00
|
|
|
private fun setMessageSpacing(isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) {
|
|
|
|
val topPadding = if (isStartOfMessageCluster) R.dimen.conversation_vertical_message_spacing_default else R.dimen.conversation_vertical_message_spacing_collapse
|
2021-06-07 07:37:21 +02:00
|
|
|
ViewUtil.setPaddingTop(this, resources.getDimension(topPadding).roundToInt())
|
2021-06-07 07:48:22 +02:00
|
|
|
val bottomPadding = if (isEndOfMessageCluster) R.dimen.conversation_vertical_message_spacing_default else R.dimen.conversation_vertical_message_spacing_collapse
|
2021-06-07 07:37:21 +02:00
|
|
|
ViewUtil.setPaddingBottom(this, resources.getDimension(bottomPadding).roundToInt())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun isStartOfMessageCluster(current: MessageRecord, previous: MessageRecord?, isGroupThread: Boolean): Boolean {
|
|
|
|
return if (isGroupThread) {
|
2021-07-14 06:27:21 +02:00
|
|
|
previous == null || previous.isUpdate || !DateUtils.isSameHour(current.timestamp, previous.timestamp)
|
2021-06-07 07:37:21 +02:00
|
|
|
|| current.recipient.address != previous.recipient.address
|
|
|
|
} else {
|
2021-07-14 06:27:21 +02:00
|
|
|
previous == null || previous.isUpdate || !DateUtils.isSameHour(current.timestamp, previous.timestamp)
|
2021-06-07 07:37:21 +02:00
|
|
|
|| current.isOutgoing != previous.isOutgoing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun isEndOfMessageCluster(current: MessageRecord, next: MessageRecord?, isGroupThread: Boolean): Boolean {
|
|
|
|
return if (isGroupThread) {
|
2021-07-08 05:37:08 +02:00
|
|
|
next == null || next.isUpdate || !DateUtils.isSameHour(current.timestamp, next.timestamp)
|
2021-06-07 07:37:21 +02:00
|
|
|
|| current.recipient.address != next.recipient.address
|
|
|
|
} else {
|
2021-07-08 05:37:08 +02:00
|
|
|
next == null || next.isUpdate || !DateUtils.isSameHour(current.timestamp, next.timestamp)
|
2021-06-07 07:37:21 +02:00
|
|
|
|| current.isOutgoing != next.isOutgoing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:06:11 +02:00
|
|
|
private fun getMessageStatusImage(message: MessageRecord): Pair<Int?,Int?> {
|
|
|
|
return when {
|
|
|
|
!message.isOutgoing -> null to null
|
|
|
|
message.isFailed -> R.drawable.ic_error to resources.getColor(R.color.destructive, context.theme)
|
|
|
|
message.isPending -> R.drawable.ic_circle_dot_dot_dot to null
|
|
|
|
message.isRead -> R.drawable.ic_filled_circle_check to null
|
|
|
|
else -> R.drawable.ic_circle_check to null
|
2021-06-22 07:41:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 07:40:15 +02:00
|
|
|
private fun updateExpirationTimer(message: MessageRecord) {
|
|
|
|
val expirationTimerViewLayoutParams = expirationTimerView.layoutParams as RelativeLayout.LayoutParams
|
2021-07-01 06:19:12 +02:00
|
|
|
val ruleToAdd = if (message.isOutgoing) RelativeLayout.ALIGN_START else RelativeLayout.ALIGN_END
|
|
|
|
val ruleToRemove = if (message.isOutgoing) RelativeLayout.ALIGN_END else RelativeLayout.ALIGN_START
|
2021-06-30 07:40:15 +02:00
|
|
|
expirationTimerViewLayoutParams.removeRule(ruleToRemove)
|
2021-07-01 06:19:12 +02:00
|
|
|
expirationTimerViewLayoutParams.addRule(ruleToAdd, R.id.messageContentView)
|
2021-07-01 03:18:51 +02:00
|
|
|
val expirationTimerViewSize = toPx(12, resources)
|
2021-07-01 06:19:12 +02:00
|
|
|
val smallSpacing = resources.getDimension(R.dimen.small_spacing).roundToInt()
|
|
|
|
expirationTimerViewLayoutParams.marginStart = if (message.isOutgoing) -(smallSpacing + expirationTimerViewSize) else 0
|
|
|
|
expirationTimerViewLayoutParams.marginEnd = if (message.isOutgoing) 0 else -(smallSpacing + expirationTimerViewSize)
|
2021-06-30 07:40:15 +02:00
|
|
|
expirationTimerView.layoutParams = expirationTimerViewLayoutParams
|
|
|
|
if (message.expiresIn > 0 && !message.isPending) {
|
|
|
|
expirationTimerView.setColorFilter(ResourcesCompat.getColor(resources, R.color.text, context.theme))
|
|
|
|
expirationTimerView.isVisible = true
|
|
|
|
expirationTimerView.setPercentComplete(0.0f)
|
|
|
|
if (message.expireStarted > 0) {
|
|
|
|
expirationTimerView.setExpirationTime(message.expireStarted, message.expiresIn)
|
|
|
|
expirationTimerView.startAnimation()
|
|
|
|
if (message.expireStarted + message.expiresIn <= System.currentTimeMillis()) {
|
|
|
|
ApplicationContext.getInstance(context).expiringMessageManager.checkSchedule()
|
|
|
|
}
|
2021-09-13 05:45:55 +02:00
|
|
|
} else if (!message.isMediaPending) {
|
|
|
|
expirationTimerView.setPercentComplete(0.0f)
|
|
|
|
expirationTimerView.stopAnimation()
|
2021-06-30 07:40:15 +02:00
|
|
|
ThreadUtils.queue {
|
|
|
|
val expirationManager = ApplicationContext.getInstance(context).expiringMessageManager
|
|
|
|
val id = message.getId()
|
|
|
|
val mms = message.isMms
|
2021-10-04 09:51:19 +02:00
|
|
|
if (mms) mmsDb.markExpireStarted(id) else smsDb.markExpireStarted(id)
|
2021-06-30 07:40:15 +02:00
|
|
|
expirationManager.scheduleDeletion(id, mms, message.expiresIn)
|
|
|
|
}
|
2021-09-13 05:45:55 +02:00
|
|
|
} else {
|
|
|
|
expirationTimerView.stopAnimation()
|
|
|
|
expirationTimerView.setPercentComplete(0.0f)
|
2021-06-30 07:40:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expirationTimerView.isVisible = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:04:50 +02:00
|
|
|
private fun handleIsSelectedChanged() {
|
|
|
|
background = if (snIsSelected) {
|
2021-06-10 03:37:24 +02:00
|
|
|
ColorDrawable(context.resources.getColorWithID(R.color.message_selected, context.theme))
|
2021-06-09 04:04:50 +02:00
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 07:12:48 +02:00
|
|
|
override fun onDraw(canvas: Canvas) {
|
2021-07-01 03:18:51 +02:00
|
|
|
if (translationX < 0 && !expirationTimerView.isVisible) {
|
2021-06-09 07:12:48 +02:00
|
|
|
val spacing = context.resources.getDimensionPixelSize(R.dimen.small_spacing)
|
|
|
|
val threshold = VisibleMessageView.swipeToReplyThreshold
|
|
|
|
val iconSize = toPx(24, context.resources)
|
2021-06-28 07:44:11 +02:00
|
|
|
val bottomVOffset = paddingBottom + messageStatusImageView.height + (messageContentView.height - iconSize) / 2
|
2021-07-01 03:18:51 +02:00
|
|
|
swipeToReplyIconRect.left = messageContentContainer.right - messageContentContainer.paddingEnd + spacing
|
2021-06-09 07:12:48 +02:00
|
|
|
swipeToReplyIconRect.top = height - bottomVOffset - iconSize
|
2021-07-01 03:18:51 +02:00
|
|
|
swipeToReplyIconRect.right = messageContentContainer.right - messageContentContainer.paddingEnd + iconSize + spacing
|
2021-06-09 07:12:48 +02:00
|
|
|
swipeToReplyIconRect.bottom = height - bottomVOffset
|
|
|
|
swipeToReplyIcon.bounds = swipeToReplyIconRect
|
|
|
|
swipeToReplyIcon.alpha = (255.0f * (min(abs(translationX), threshold) / threshold)).roundToInt()
|
|
|
|
} else {
|
|
|
|
swipeToReplyIcon.alpha = 0
|
|
|
|
}
|
2021-06-10 02:04:50 +02:00
|
|
|
swipeToReplyIcon.draw(canvas)
|
2021-06-09 07:12:48 +02:00
|
|
|
super.onDraw(canvas)
|
|
|
|
}
|
|
|
|
|
2021-05-31 06:06:02 +02:00
|
|
|
fun recycle() {
|
2021-06-01 05:01:03 +02:00
|
|
|
profilePictureView.recycle()
|
2021-06-04 01:58:04 +02:00
|
|
|
messageContentView.recycle()
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
// endregion
|
2021-06-09 02:57:40 +02:00
|
|
|
|
|
|
|
// region Interaction
|
|
|
|
override fun onTouchEvent(event: MotionEvent): Boolean {
|
2021-08-12 07:01:48 +02:00
|
|
|
if (onPress == null || onSwipeToReply == null || onLongPress == null) { return false }
|
2021-06-09 02:57:40 +02:00
|
|
|
when (event.action) {
|
|
|
|
MotionEvent.ACTION_DOWN -> onDown(event)
|
|
|
|
MotionEvent.ACTION_MOVE -> onMove(event)
|
2021-06-16 02:54:33 +02:00
|
|
|
MotionEvent.ACTION_CANCEL -> onCancel(event)
|
|
|
|
MotionEvent.ACTION_UP -> onUp(event)
|
2021-06-09 02:57:40 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onDown(event: MotionEvent) {
|
|
|
|
dx = x - event.rawX
|
2021-06-09 03:37:50 +02:00
|
|
|
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
|
|
|
|
val newLongPressCallback = Runnable { onLongPress() }
|
|
|
|
this.longPressCallback = newLongPressCallback
|
|
|
|
gestureHandler.postDelayed(newLongPressCallback, VisibleMessageView.longPressDurationThreshold)
|
|
|
|
onDownTimestamp = Date().time
|
2021-06-09 02:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun onMove(event: MotionEvent) {
|
|
|
|
val translationX = toDp(event.rawX + dx, context.resources)
|
2021-06-09 04:04:50 +02:00
|
|
|
if (abs(translationX) < VisibleMessageView.longPressMovementTreshold || snIsSelected) {
|
2021-06-09 03:18:15 +02:00
|
|
|
return
|
|
|
|
} else {
|
2021-06-09 03:37:50 +02:00
|
|
|
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
|
2021-06-09 03:18:15 +02:00
|
|
|
}
|
2021-06-30 02:30:10 +02:00
|
|
|
if (translationX > 0) { return } // Only allow swipes to the left
|
2021-06-09 02:57:40 +02:00
|
|
|
// The idea here is to asymptotically approach a maximum drag distance
|
|
|
|
val damping = 50.0f
|
|
|
|
val sign = -1.0f
|
|
|
|
val x = (damping * (sqrt(abs(translationX)) / sqrt(damping))) * sign
|
|
|
|
this.translationX = x
|
2021-06-10 02:54:26 +02:00
|
|
|
this.dateBreakTextView.translationX = -x // Bit of a hack to keep the date break text view from moving
|
2021-06-09 07:12:48 +02:00
|
|
|
postInvalidate() // Ensure onDraw(canvas:) is called
|
2021-06-09 02:57:40 +02:00
|
|
|
if (abs(x) > VisibleMessageView.swipeToReplyThreshold && abs(previousTranslationX) < VisibleMessageView.swipeToReplyThreshold) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
|
|
|
|
} else {
|
|
|
|
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previousTranslationX = x
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:54:33 +02:00
|
|
|
private fun onCancel(event: MotionEvent) {
|
2021-06-21 01:40:23 +02:00
|
|
|
if (abs(translationX) > VisibleMessageView.swipeToReplyThreshold) {
|
|
|
|
onSwipeToReply?.invoke()
|
|
|
|
}
|
2021-06-16 02:54:33 +02:00
|
|
|
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
|
2021-06-16 06:50:41 +02:00
|
|
|
resetPosition()
|
2021-06-16 02:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun onUp(event: MotionEvent) {
|
2021-06-09 02:57:40 +02:00
|
|
|
if (abs(translationX) > VisibleMessageView.swipeToReplyThreshold) {
|
2021-06-09 03:37:50 +02:00
|
|
|
onSwipeToReply?.invoke()
|
|
|
|
} else if ((Date().time - onDownTimestamp) < VisibleMessageView.longPressDurationThreshold) {
|
|
|
|
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
|
2021-06-28 07:41:23 +02:00
|
|
|
val pressCallback = this.pressCallback
|
|
|
|
if (pressCallback != null) {
|
|
|
|
// If we're here and pressCallback isn't null, it means that we tapped again within
|
|
|
|
// maxDoubleTapInterval ms and we should count this as a double tap
|
|
|
|
gestureHandler.removeCallbacks(pressCallback)
|
|
|
|
this.pressCallback = null
|
|
|
|
onDoubleTap?.invoke()
|
|
|
|
} else {
|
2021-06-30 06:29:32 +02:00
|
|
|
val newPressCallback = Runnable { onPress(event) }
|
2021-06-28 07:41:23 +02:00
|
|
|
this.pressCallback = newPressCallback
|
|
|
|
gestureHandler.postDelayed(newPressCallback, VisibleMessageView.maxDoubleTapInterval)
|
|
|
|
}
|
2021-06-09 02:57:40 +02:00
|
|
|
}
|
2021-06-16 06:50:41 +02:00
|
|
|
resetPosition()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun resetPosition() {
|
2021-06-09 02:57:40 +02:00
|
|
|
animate()
|
|
|
|
.translationX(0.0f)
|
|
|
|
.setDuration(150)
|
2021-06-10 02:54:26 +02:00
|
|
|
.setUpdateListener {
|
|
|
|
postInvalidate() // Ensure onDraw(canvas:) is called
|
|
|
|
}
|
2021-06-09 02:57:40 +02:00
|
|
|
.start()
|
2021-06-10 02:54:26 +02:00
|
|
|
// Bit of a hack to keep the date break text view from moving
|
|
|
|
dateBreakTextView.animate()
|
2021-06-16 06:50:41 +02:00
|
|
|
.translationX(0.0f)
|
|
|
|
.setDuration(150)
|
|
|
|
.start()
|
2021-06-09 02:57:40 +02:00
|
|
|
}
|
2021-06-09 03:18:15 +02:00
|
|
|
|
|
|
|
private fun onLongPress() {
|
|
|
|
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
|
2021-06-09 03:37:50 +02:00
|
|
|
onLongPress?.invoke()
|
2021-06-09 03:18:15 +02:00
|
|
|
}
|
2021-06-21 06:48:27 +02:00
|
|
|
|
2021-06-30 06:29:32 +02:00
|
|
|
fun onContentClick(event: MotionEvent) {
|
|
|
|
messageContentView.onContentClick?.invoke(event)
|
2021-06-21 06:48:27 +02:00
|
|
|
}
|
2021-06-29 02:05:34 +02:00
|
|
|
|
2021-06-30 06:29:32 +02:00
|
|
|
private fun onPress(event: MotionEvent) {
|
|
|
|
onPress?.invoke(event)
|
2021-06-28 07:41:23 +02:00
|
|
|
pressCallback = null
|
|
|
|
}
|
2021-07-14 06:39:20 +02:00
|
|
|
|
|
|
|
private fun showUserDetails(publicKey: String) {
|
|
|
|
val userDetailsBottomSheet = UserDetailsBottomSheet()
|
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString("publicKey", publicKey)
|
|
|
|
userDetailsBottomSheet.arguments = bundle
|
|
|
|
val activity = context as AppCompatActivity
|
|
|
|
userDetailsBottomSheet.show(activity.supportFragmentManager, userDetailsBottomSheet.tag)
|
|
|
|
}
|
2021-06-09 02:57:40 +02:00
|
|
|
// endregion
|
2021-06-09 03:37:50 +02:00
|
|
|
}
|