From ba51102b6d8cf8b4494ef4105a77a17700ab6052 Mon Sep 17 00:00:00 2001 From: ceokot Date: Mon, 6 Dec 2021 06:34:32 +0200 Subject: [PATCH] Add message date break extension function to reduce duplication --- .../conversation/v2/ConversationConstants.kt | 5 ----- .../v2/messages/ControlMessageView.kt | 8 +------- .../conversation/v2/messages/MessageUtilities.kt | 15 +++++++++++++++ .../v2/messages/VisibleMessageView.kt | 5 +---- 4 files changed, 17 insertions(+), 16 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationConstants.kt create mode 100644 app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageUtilities.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationConstants.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationConstants.kt deleted file mode 100644 index 9e893ef89f..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationConstants.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.thoughtcrime.securesms.conversation.v2 - -object ConversationConstants { - const val MAX_TIME_BETWEEN_BREAKS = 5 * 60 * 1000L // 5 minutes -} \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt index 76215040d2..2b3a545b03 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt @@ -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)) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageUtilities.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageUtilities.kt new file mode 100644 index 0000000000..2eed1ee614 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageUtilities.kt @@ -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 "" +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt index 64db47c5f5..d9cd334bc8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt @@ -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