From fe8b0c97986302adaac56716645f2b8acb3cd398 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 24 Jun 2021 16:23:37 +1000 Subject: [PATCH] Create link preview draft view --- .../conversation/v2/ConversationActivityV2.kt | 6 +- .../v2/components/LinkPreviewDraftView.kt | 38 ++++++++++ .../conversation/v2/input_bar/InputBar.kt | 25 +++++-- .../v2/messages/LinkPreviewView.kt | 8 --- .../res/layout/view_link_preview_draft.xml | 72 +++++++++++++++++++ 5 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/LinkPreviewDraftView.kt create mode 100644 app/src/main/res/layout/view_link_preview_draft.xml diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index eed0122158..5412c0896d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -224,11 +224,9 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe linkPreviewViewModel.linkPreviewState.observe(this, { previewState: LinkPreviewState? -> if (previewState == null) return@observe if (previewState.isLoading) { - Log.d("Test", "Loading link preview.") - // inputPanel.setLinkPreviewLoading() + inputBar.draftLinkPreview() } else { - Log.d("Test", "Setting link preview: " + previewState.linkPreview.isPresent) - // inputPanel.setLinkPreview(glideRequests, previewState.linkPreview) + inputBar.updateLinkPreviewDraft(glide, previewState.linkPreview.get()) } }) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/LinkPreviewDraftView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/LinkPreviewDraftView.kt new file mode 100644 index 0000000000..23158acf0f --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/LinkPreviewDraftView.kt @@ -0,0 +1,38 @@ +package org.thoughtcrime.securesms.conversation.v2.components + +import android.content.Context +import android.util.AttributeSet +import android.view.LayoutInflater +import android.widget.LinearLayout +import androidx.core.view.isVisible +import kotlinx.android.synthetic.main.view_link_preview_draft.view.* +import network.loki.messenger.R +import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview +import org.thoughtcrime.securesms.mms.GlideRequests +import org.thoughtcrime.securesms.mms.ImageSlide + +class LinkPreviewDraftView : LinearLayout { + + // 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() } + + private fun initialize() { + LayoutInflater.from(context).inflate(R.layout.view_link_preview_draft, this) + linkPreviewDraftContainer.isVisible = false + thumbnailImageView.clipToOutline = true + } + // endregion + + // region Updating + fun update(glide: GlideRequests, linkPreview: LinkPreview) { + linkPreviewDraftContainer.isVisible = true + linkPreviewDraftLoader.isVisible = false + if (linkPreview.getThumbnail().isPresent) { + thumbnailImageView.setImageResource(glide, ImageSlide(context, linkPreview.getThumbnail().get()), false, false) + } + linkPreviewDraftTitleTextView.text = linkPreview.title + } + // endregion +} \ No newline at end of file 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 e5bab6373e..65965a2177 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 @@ -2,24 +2,23 @@ package org.thoughtcrime.securesms.conversation.v2.input_bar import android.content.Context import android.content.res.Resources -import android.text.Editable import android.util.AttributeSet -import android.util.Log import android.view.LayoutInflater import android.view.MotionEvent -import android.widget.LinearLayout import android.widget.RelativeLayout import androidx.core.view.isVisible import kotlinx.android.synthetic.main.view_input_bar.view.* import kotlinx.android.synthetic.main.view_quote.view.* import network.loki.messenger.R +import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview +import org.thoughtcrime.securesms.conversation.v2.components.LinkPreviewDraftView import org.thoughtcrime.securesms.conversation.v2.messages.QuoteView import org.thoughtcrime.securesms.conversation.v2.messages.QuoteViewDelegate import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.loki.utilities.toDp import org.thoughtcrime.securesms.loki.utilities.toPx -import org.thoughtcrime.securesms.mms.SlideDeck +import org.thoughtcrime.securesms.mms.GlideRequests import kotlin.math.max import kotlin.math.roundToInt @@ -27,6 +26,7 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate { private val screenWidth = Resources.getSystem().displayMetrics.widthPixels private val vMargin by lazy { toDp(4, resources) } private val minHeight by lazy { toPx(56, resources) } + private var linkPreviewDraftView: LinkPreviewDraftView? = null var delegate: InputBarDelegate? = null var additionalContentHeight = 0 @@ -96,6 +96,7 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate { } fun draftQuote(message: MessageRecord) { + linkPreviewDraftView = null inputBarAdditionalContentContainer.removeAllViews() val quoteView = QuoteView(context, QuoteView.Mode.Draft) quoteView.delegate = this @@ -121,6 +122,22 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate { additionalContentHeight = 0 setHeight(newHeight) } + + fun draftLinkPreview() { + val linkPreviewDraftHeight = toPx(88, resources) + inputBarAdditionalContentContainer.removeAllViews() + val linkPreviewDraftView = LinkPreviewDraftView(context) + this.linkPreviewDraftView = linkPreviewDraftView + inputBarAdditionalContentContainer.addView(linkPreviewDraftView) + val newHeight = max(inputBarEditText.height + 2 * vMargin, minHeight) + linkPreviewDraftHeight + additionalContentHeight = linkPreviewDraftHeight + setHeight(newHeight) + } + + fun updateLinkPreviewDraft(glide: GlideRequests, linkPreview: LinkPreview) { + val linkPreviewDraftView = this.linkPreviewDraftView ?: return + linkPreviewDraftView.update(glide, linkPreview) + } // endregion } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt index daf99b6bc1..6219077dad 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt @@ -1,20 +1,12 @@ package org.thoughtcrime.securesms.conversation.v2.messages import android.content.Context -import android.graphics.Canvas -import android.graphics.Outline -import android.graphics.Path -import android.graphics.RectF import android.graphics.drawable.Drawable -import android.os.Build import android.util.AttributeSet -import android.util.Log import android.view.LayoutInflater -import android.view.View import android.view.ViewOutlineProvider import android.widget.LinearLayout import androidx.core.content.res.ResourcesCompat -import androidx.core.view.isVisible import kotlinx.android.synthetic.main.view_link_preview.view.* import network.loki.messenger.R import org.thoughtcrime.securesms.database.model.MmsMessageRecord diff --git a/app/src/main/res/layout/view_link_preview_draft.xml b/app/src/main/res/layout/view_link_preview_draft.xml new file mode 100644 index 0000000000..1e056d7c57 --- /dev/null +++ b/app/src/main/res/layout/view_link_preview_draft.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file