Add message date break extension function to reduce duplication

This commit is contained in:
ceokot 2021-12-06 06:34:32 +02:00
parent a966623db2
commit ba51102b6d
4 changed files with 17 additions and 16 deletions

View File

@ -1,5 +0,0 @@
package org.thoughtcrime.securesms.conversation.v2
object ConversationConstants {
const val MAX_TIME_BETWEEN_BREAKS = 5 * 60 * 1000L // 5 minutes
}

View File

@ -6,14 +6,10 @@ import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.view_control_message.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.conversation.v2.ConversationConstants.MAX_TIME_BETWEEN_BREAKS
import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.util.DateUtils
import java.util.Locale
class ControlMessageView : LinearLayout {
@ -30,9 +26,7 @@ class ControlMessageView : LinearLayout {
// region Updating
fun bind(message: MessageRecord, previous: MessageRecord?) {
val showDateBreak = (previous == null || message.timestamp - previous.timestamp > MAX_TIME_BETWEEN_BREAKS)
dateBreakTextView.isVisible = showDateBreak
dateBreakTextView.text = if (showDateBreak) DateUtils.getDisplayFormattedTimeSpanString(context, Locale.getDefault(), message.timestamp) else ""
dateBreakTextView.showDateBreak(message, previous)
iconImageView.visibility = View.GONE
if (message.isExpirationTimerUpdate) {
iconImageView.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_timer, context.theme))

View File

@ -0,0 +1,15 @@
package org.thoughtcrime.securesms.conversation.v2.messages
import android.widget.TextView
import androidx.core.view.isVisible
import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.util.DateUtils
import java.util.Locale
private const val maxTimeBetweenBreaks = 5 * 60 * 1000L // 5 minutes
fun TextView.showDateBreak(message: MessageRecord, previous: MessageRecord?) {
val showDateBreak = (previous == null || message.timestamp - previous.timestamp > maxTimeBetweenBreaks)
isVisible = showDateBreak
text = if (showDateBreak) DateUtils.getDisplayFormattedTimeSpanString(context, Locale.getDefault(), message.timestamp) else ""
}

View File

@ -25,7 +25,6 @@ import org.session.libsession.messaging.open_groups.OpenGroupAPIV2
import org.session.libsession.utilities.ViewUtil
import org.session.libsignal.utilities.ThreadUtils
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.conversation.v2.ConversationConstants.MAX_TIME_BETWEEN_BREAKS
import org.thoughtcrime.securesms.database.*
import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.home.UserDetailsBottomSheet
@ -120,9 +119,7 @@ class VisibleMessageView : LinearLayout {
senderNameTextView.visibility = View.GONE
}
// Date break
val showDateBreak = (previous == null || message.timestamp - previous.timestamp > MAX_TIME_BETWEEN_BREAKS)
dateBreakTextView.isVisible = showDateBreak
dateBreakTextView.text = if (showDateBreak) DateUtils.getDisplayFormattedTimeSpanString(context, Locale.getDefault(), message.timestamp) else ""
dateBreakTextView.showDateBreak(message, previous)
// Timestamp
messageTimestampTextView.text = DateUtils.getDisplayFormattedTimeSpanString(context, Locale.getDefault(), message.timestamp)
// Margins