From ce5f923b256ad8704acbb86fbb0263d51e48f7e0 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 21 Jun 2021 10:53:52 +1000 Subject: [PATCH] Adapt quote view for use in messages --- .../conversation/v2/input_bar/InputBar.kt | 4 +- .../conversation/v2/messages/QuoteView.kt | 60 +++++++++++++++++-- .../v2/messages/VisibleMessageContentView.kt | 9 ++- app/src/main/res/layout/view_quote.xml | 2 + 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt index 4a891b1d92..9f0076da61 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt @@ -85,10 +85,10 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate { fun draftQuote(message: MessageRecord) { inputBarAdditionalContentContainer.removeAllViews() - val quoteView = QuoteView(context) + val quoteView = QuoteView(context, QuoteView.Mode.Draft) quoteView.delegate = this inputBarAdditionalContentContainer.addView(quoteView) - quoteView.bind(message.individualRecipient.address.toString(), message.body, null, message.recipient) + quoteView.bind(message.individualRecipient.address.toString(), message.body, null, message.recipient, true) val quoteViewIntrinsicHeight = quoteView.getIntrinsicHeight() val newHeight = max(inputBarEditText.height + 2 * vMargin, toPx(56, resources)) + quoteViewIntrinsicHeight additionalContentHeight = quoteViewIntrinsicHeight diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/QuoteView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/QuoteView.kt index 35c1ead059..9f1fca0a07 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/QuoteView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/QuoteView.kt @@ -6,6 +6,8 @@ import android.util.AttributeSet import android.view.LayoutInflater import android.widget.LinearLayout import android.widget.RelativeLayout +import androidx.annotation.ColorInt +import androidx.core.content.res.ResourcesCompat import androidx.core.view.isVisible import kotlinx.android.synthetic.main.view_quote.view.* import network.loki.messenger.R @@ -14,12 +16,16 @@ import org.session.libsession.utilities.recipients.Recipient import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.model.MessageRecord +import org.thoughtcrime.securesms.loki.utilities.UiMode +import org.thoughtcrime.securesms.loki.utilities.UiModeUtilities import org.thoughtcrime.securesms.loki.utilities.toPx import org.thoughtcrime.securesms.mms.SlideDeck import kotlin.math.max import kotlin.math.min +import kotlin.math.roundToInt class QuoteView : LinearLayout { + private lateinit var mode: Mode private val screenWidth by lazy { Resources.getSystem().displayMetrics.widthPixels } private val vPadding by lazy { toPx(6, resources) } var delegate: QuoteViewDelegate? = null @@ -27,13 +33,25 @@ class QuoteView : LinearLayout { enum class Mode { Regular, Draft } // region Lifecycle - 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() } + constructor(context: Context) : super(context) { throw IllegalAccessError("Use QuoteView(context:mode:) instead.") } + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { throw IllegalAccessError("Use QuoteView(context:mode:) instead.") } + constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { throw IllegalAccessError("Use QuoteView(context:mode:) instead.") } - private fun initialize() { + constructor(context: Context, mode: Mode) : super(context) { + this.mode = mode LayoutInflater.from(context).inflate(R.layout.view_quote, this) - quoteViewCancelButton.setOnClickListener { delegate?.cancelQuoteDraft() } + when (mode) { + Mode.Draft -> quoteViewCancelButton.setOnClickListener { delegate?.cancelQuoteDraft() } + Mode.Regular -> { + quoteViewCancelButton.isVisible = false + mainQuoteViewContainer.setBackgroundColor(ResourcesCompat.getColor(resources, R.color.transparent, context.theme)) + val hPadding = resources.getDimension(R.dimen.medium_spacing).roundToInt() + mainQuoteViewContainer.setPadding(hPadding, toPx(12, resources), hPadding, toPx(0, resources)) + val quoteViewMainContentContainerLayoutParams = quoteViewMainContentContainer.layoutParams as RelativeLayout.LayoutParams + quoteViewMainContentContainerLayoutParams.marginEnd = resources.getDimension(R.dimen.medium_spacing).roundToInt() + quoteViewMainContentContainer.layoutParams = quoteViewMainContentContainerLayoutParams + } + } } // endregion @@ -60,21 +78,51 @@ class QuoteView : LinearLayout { // endregion // region Updating - fun bind(authorPublicKey: String, body: String, attachments: SlideDeck?, thread: Recipient) { + fun bind(authorPublicKey: String, body: String?, attachments: SlideDeck?, thread: Recipient, isOutgoingMessage: Boolean) { val contactDB = DatabaseFactory.getSessionContactDatabase(context) // Author if (thread.isGroupRecipient) { val author = contactDB.getContactWithSessionID(authorPublicKey) val authorDisplayName = author?.displayName(Contact.contextForRecipient(thread)) ?: authorPublicKey quoteViewAuthorTextView.text = authorDisplayName + quoteViewAuthorTextView.setTextColor(getTextColor(isOutgoingMessage)) } quoteViewAuthorTextView.isVisible = thread.isGroupRecipient // Body quoteViewBodyTextView.text = body + quoteViewBodyTextView.setTextColor(getTextColor(isOutgoingMessage)) // Accent line val accentLineLayoutParams = quoteViewAccentLine.layoutParams as RelativeLayout.LayoutParams accentLineLayoutParams.height = getIntrinsicContentHeight() quoteViewAccentLine.layoutParams = accentLineLayoutParams + quoteViewAccentLine.setBackgroundColor(getLineColor(isOutgoingMessage)) + } + // endregion + + // region Convenience + @ColorInt private fun getLineColor(isOutgoingMessage: Boolean): Int { + val isLightMode = UiModeUtilities.isDayUiMode(context) + if ((mode == Mode.Regular && isLightMode) || (mode == Mode.Draft && isLightMode)) { + return ResourcesCompat.getColor(resources, R.color.black, context.theme) + } else if (mode == Mode.Regular && !isLightMode) { + if (isOutgoingMessage) { + return ResourcesCompat.getColor(resources, R.color.black, context.theme) + } else { + return ResourcesCompat.getColor(resources, R.color.accent, context.theme) + } + } else { // Draft & dark mode + return ResourcesCompat.getColor(resources, R.color.accent, context.theme) + } + } + + @ColorInt private fun getTextColor(isOutgoingMessage: Boolean): Int { + if (mode == Mode.Draft) { return ResourcesCompat.getColor(resources, R.color.text, context.theme) } + val isLightMode = UiModeUtilities.isDayUiMode(context) + if ((isOutgoingMessage && !isLightMode) || (!isOutgoingMessage && isLightMode)) { + return ResourcesCompat.getColor(resources, R.color.black, context.theme) + } else { + return ResourcesCompat.getColor(resources, R.color.white, context.theme) + } } // endregion } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt index cce23aa7c5..559130eb9e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt @@ -14,6 +14,7 @@ import androidx.core.graphics.BlendModeCompat import kotlinx.android.synthetic.main.view_visible_message_content.view.* import network.loki.messenger.R import org.session.libsession.utilities.ThemeUtil +import org.session.libsession.utilities.ViewUtil import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.loki.utilities.UiMode @@ -52,9 +53,13 @@ class VisibleMessageContentView : LinearLayout { linkPreviewView.bind(message) mainContainer.addView(linkPreviewView) } else if (message is MmsMessageRecord && message.quote != null) { - val quoteView = QuoteView(context) - quoteView.bind(message.individualRecipient.address.toString(), "iuasfhiausfh", null, message.recipient) + val quote = message.quote!! + val quoteView = QuoteView(context, QuoteView.Mode.Regular) + quoteView.bind(quote.author.toString(), quote.text, quote.attachment, message.recipient, message.isOutgoing) mainContainer.addView(quoteView) + val bodyTextView = getBodyTextView(message) + ViewUtil.setPaddingTop(bodyTextView, 0) + mainContainer.addView(bodyTextView) } else if (message is MmsMessageRecord && message.slideDeck.audioSlide != null) { val voiceMessageView = VoiceMessageView(context) voiceMessageView.bind(message) diff --git a/app/src/main/res/layout/view_quote.xml b/app/src/main/res/layout/view_quote.xml index 9f173e0755..8bb1001018 100644 --- a/app/src/main/res/layout/view_quote.xml +++ b/app/src/main/res/layout/view_quote.xml @@ -1,5 +1,6 @@