From a37e06d38e2279d70e214275d3a14e29bfd6e9bc Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Sun, 22 Apr 2018 19:18:08 -0400 Subject: [PATCH] #1075 - Rewrite message layout and adapter This addresses the following issues - Message layout doesn't use ConstraintLayout - Images don't have individual bubbles - Image bubbles don't have the correct bubble grouping --- domain/src/main/java/model/Message.kt | 3 +- .../java/common/widget/BubbleImageView.kt | 110 +++++++++++ .../main/java/common/widget/MmsPreviewView.kt | 82 -------- .../java/feature/compose/MessagesAdapter.kt | 183 ++++++++++-------- .../src/main/java/injection/AppComponent.kt | 2 - .../main/res/layout/message_list_item_in.xml | 134 +++++-------- .../main/res/layout/message_list_item_out.xml | 122 +++++------- .../main/res/layout/mms_preview_list_item.xml | 13 +- 8 files changed, 327 insertions(+), 322 deletions(-) create mode 100644 presentation/src/main/java/common/widget/BubbleImageView.kt delete mode 100644 presentation/src/main/java/common/widget/MmsPreviewView.kt diff --git a/domain/src/main/java/model/Message.kt b/domain/src/main/java/model/Message.kt index 222df3e3..9c05a223 100644 --- a/domain/src/main/java/model/Message.kt +++ b/domain/src/main/java/model/Message.kt @@ -97,8 +97,7 @@ open class Message : RealmObject() { } /** - * Returns the text that should be displayed in the messagelist view - * The returned text may be an empty string + * Returns the text that should be copied to the clipboard */ fun getText(): String { return when { diff --git a/presentation/src/main/java/common/widget/BubbleImageView.kt b/presentation/src/main/java/common/widget/BubbleImageView.kt new file mode 100644 index 00000000..8ae82656 --- /dev/null +++ b/presentation/src/main/java/common/widget/BubbleImageView.kt @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package common.widget + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Path +import android.graphics.RectF +import android.util.AttributeSet +import android.widget.ImageView +import common.util.extensions.dpToPx + +class BubbleImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ImageView(context, attrs) { + + enum class Style(val topLeft: Boolean, val topRight: Boolean, val bottomRight: Boolean, val bottomLeft: Boolean) { + + ONLY(true, true, true, true), + IN_FIRST(true, true, true, false), + IN_MIDDLE(false, true, true, false), + IN_LAST(false, true, true, true), + OUT_FIRST(true, true, false, true), + OUT_MIDDLE(true, false, false, true), + OUT_LAST(true, false, true, true) + } + + var bubbleStyle = Style.ONLY + set(value) { + field = value + setPath() + invalidate() + } + + private val path = Path() + private val radiusSmall = 4.dpToPx(context).toFloat() + private val radiusLarge = 18.dpToPx(context).toFloat() + + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + super.onSizeChanged(w, h, oldw, oldh) + setPath() + } + + override fun onDraw(canvas: Canvas) { + if (!path.isEmpty) { + canvas.clipPath(path) + } + + super.onDraw(canvas) + } + + private fun setPath() { + path.rewind() + + val width = width.toFloat() + val height = height.toFloat() + + val cornerRectSmall = RectF().apply { set(-radiusSmall, -radiusSmall, radiusSmall, radiusSmall) } + val cornerRectLarge = RectF().apply { set(-radiusLarge, -radiusLarge, radiusLarge, radiusLarge) } + + if (bubbleStyle.topLeft) { + cornerRectLarge.offsetTo(0f, 0f) + path.arcTo(cornerRectLarge, 180f, 90f) + } else { + cornerRectSmall.offsetTo(0f, 0f) + path.arcTo(cornerRectSmall, 180f, 90f) + } + + if (bubbleStyle.topRight) { + cornerRectLarge.offsetTo(width - radiusLarge * 2, 0f) + path.arcTo(cornerRectLarge, 270f, 90f) + } else { + cornerRectSmall.offsetTo(width - radiusSmall * 2, 0f) + path.arcTo(cornerRectSmall, 270f, 90f) + } + + if (bubbleStyle.bottomRight) { + cornerRectLarge.offsetTo(width - radiusLarge * 2, height - radiusLarge * 2) + path.arcTo(cornerRectLarge, 0f, 90f) + } else { + cornerRectSmall.offsetTo(width - radiusSmall * 2, height - radiusSmall * 2) + path.arcTo(cornerRectSmall, 0f, 90f) + } + + if (bubbleStyle.bottomLeft) { + cornerRectLarge.offsetTo(0f, height - radiusLarge * 2) + path.arcTo(cornerRectLarge, 90f, 90f) + } else { + cornerRectSmall.offsetTo(0f, height - radiusSmall * 2) + path.arcTo(cornerRectSmall, 90f, 90f) + } + + path.close() + } + +} \ No newline at end of file diff --git a/presentation/src/main/java/common/widget/MmsPreviewView.kt b/presentation/src/main/java/common/widget/MmsPreviewView.kt deleted file mode 100644 index e3f9a082..00000000 --- a/presentation/src/main/java/common/widget/MmsPreviewView.kt +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2017 Moez Bhatti - * - * This file is part of QKSMS. - * - * QKSMS is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * QKSMS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with QKSMS. If not, see . - */ -package common.widget - -import android.content.Context -import android.util.AttributeSet -import android.view.LayoutInflater -import android.widget.LinearLayout -import com.moez.QKSMS.R -import common.Navigator -import common.util.GlideApp -import common.util.extensions.setVisible -import injection.appComponent -import kotlinx.android.synthetic.main.mms_preview_list_item.view.* -import model.MmsPart -import util.extensions.hasThumbnails -import util.extensions.isImage -import util.extensions.isVideo -import javax.inject.Inject - -class MmsPreviewView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs) { - - @Inject lateinit var navigator: Navigator - - private val layoutInflater = LayoutInflater.from(context) - - var parts: List = ArrayList() - set(value) { - field = value - updateView() - } - - init { - if (!isInEditMode) { - appComponent.inject(this) - } - - orientation = LinearLayout.VERTICAL - } - - private fun updateView() { - val media = parts.filter { it.hasThumbnails() } - setVisible(media.isNotEmpty()) - - if (childCount > 0) { - removeAllViews() - } - - media.forEach { part -> - val view = layoutInflater.inflate(R.layout.mms_preview_list_item, this, false) - view.video.setVisible(part.isVideo()) - - GlideApp.with(context).load(part.getUri()).fitCenter().into(view.thumbnail) - - setOnClickListener { - when { - part.isImage() -> navigator.showImage(part.id) - part.isVideo() -> navigator.showVideo(part.getUri(), part.type) - } - } - - addView(view) - } - } - -} \ No newline at end of file diff --git a/presentation/src/main/java/feature/compose/MessagesAdapter.kt b/presentation/src/main/java/feature/compose/MessagesAdapter.kt index 49005fc0..065c30f4 100644 --- a/presentation/src/main/java/feature/compose/MessagesAdapter.kt +++ b/presentation/src/main/java/feature/compose/MessagesAdapter.kt @@ -19,39 +19,52 @@ package feature.compose import android.content.Context +import android.graphics.Typeface import android.os.Build import android.support.v7.widget.RecyclerView import android.telephony.PhoneNumberUtils +import android.text.Spannable +import android.text.SpannableStringBuilder +import android.text.style.StyleSpan import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.jakewharton.rxbinding2.view.RxView +import com.jakewharton.rxbinding2.view.clicks +import com.jakewharton.rxbinding2.view.longClicks import com.moez.QKSMS.R +import common.Navigator import common.base.QkRealmAdapter import common.base.QkViewHolder import common.util.Colors import common.util.DateFormatter +import common.util.GlideApp import common.util.extensions.dpToPx import common.util.extensions.setBackgroundTint import common.util.extensions.setPadding import common.util.extensions.setVisible +import common.widget.BubbleImageView.Style.* import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.plusAssign import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.Subject import io.realm.RealmResults import kotlinx.android.synthetic.main.message_list_item_in.view.* +import kotlinx.android.synthetic.main.mms_preview_list_item.view.* import model.Conversation import model.Message import model.Recipient import util.extensions.hasThumbnails +import util.extensions.isImage +import util.extensions.isVideo import java.util.concurrent.TimeUnit import javax.inject.Inject class MessagesAdapter @Inject constructor( private val context: Context, private val colors: Colors, - private val dateFormatter: DateFormatter + private val dateFormatter: DateFormatter, + private val navigator: Navigator ) : QkRealmAdapter() { companion object { @@ -84,6 +97,7 @@ class MessagesAdapter @Inject constructor( private val conversation: Conversation? get() = data?.first?.takeIf { it.isValid } + private val layoutInflater = LayoutInflater.from(context) private val contactCache = ContactCache() private val selected = HashMap() private val disposables = CompositeDisposable() @@ -98,20 +112,20 @@ class MessagesAdapter @Inject constructor( */ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { + // Use the parent's context to inflate the layout, otherwise link clicks will crash the app val layoutInflater = LayoutInflater.from(parent.context) val view: View if (viewType == VIEW_TYPE_MESSAGE_OUT) { view = layoutInflater.inflate(R.layout.message_list_item_out, parent, false) disposables += colors.bubble - .subscribe { color -> view.messageBackground.setBackgroundTint(color) } + .subscribe { color -> view.body.setBackgroundTint(color) } } else { view = layoutInflater.inflate(R.layout.message_list_item_in, parent, false) view.avatar.threadId = conversation?.id ?: 0 - view.subject.textColorObservable = textPrimaryOnTheme view.body.textColorObservable = textPrimaryOnTheme disposables += theme - .subscribe { color -> view.messageBackground.setBackgroundTint(color) } + .subscribe { color -> view.body.setBackgroundTint(color) } } return QkViewHolder(view) @@ -124,41 +138,111 @@ class MessagesAdapter @Inject constructor( override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) { val message = getItem(position)!! + val previous = if (position == 0) null else getItem(position - 1) + val next = if (position == itemCount - 1) null else getItem(position + 1) val view = viewHolder.itemView RxView.clicks(view).subscribe { clicks.onNext(message) selected[message.id] = view.status.visibility != View.VISIBLE - notifyItemChanged(position) + bindStatus(viewHolder, position) } RxView.longClicks(view).subscribe { longClicks.onNext(message) } - view.mmsPreview.parts = message.parts + bindStatus(viewHolder, position) - view.subject.text = message.getCleansedSubject() - view.subject.setVisible(view.subject.text.isNotBlank()) - - view.body.text = message.getText() - view.body.setVisible(message.isSms() || view.body.text.isNotEmpty() || message.parts.all { !it.hasThumbnails() }) + // Bind the timestamp + val timeSincePrevious = TimeUnit.MILLISECONDS.toMinutes(message.date - (previous?.date ?: 0)) view.timestamp.text = dateFormatter.getMessageTimestamp(message.date) + view.timestamp.visibility = if (timeSincePrevious < TIMESTAMP_THRESHOLD) View.GONE else View.VISIBLE - bindAvatar(view, position) - bindStatus(view, position) - bindGrouping(view, position) + + // Bind the grouping + val media = message.parts.filter { it.hasThumbnails() } + view.setPadding(bottom = if (canGroup(message, next)) 0 else 16.dpToPx(context)) + + + // Bind the avatar + if (!message.isMe()) { + view.avatar.threadId = conversation?.id ?: 0 + view.avatar.setContact(contactCache[message.address]) + view.avatar.setVisible(!canGroup(message, next), View.INVISIBLE) + } + + + // Bind the body text + view.body.text = when (message.isSms()) { + true -> message.body + false -> { + val subject = message.getCleansedSubject() + val sb = SpannableStringBuilder(message.parts + .mapNotNull { it.text } + .apply { subject + this } + .filter { it.isNotBlank() } + .joinToString("\n") { text -> text }) + + val bss = StyleSpan(Typeface.BOLD) + sb.setSpan(bss, 0, subject.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE) + sb.toString() + } + } + view.body.setVisible(message.isSms() || view.body.text.isNotBlank()) + + val canBodyGroupWithPrevious = canGroup(message, previous) || media.isNotEmpty() + val canBodyGroupWithNext = canGroup(message, next) + view.body.setBackgroundResource(when { + !canBodyGroupWithPrevious && canBodyGroupWithNext -> if (message.isMe()) R.drawable.message_out_first else R.drawable.message_in_first + canBodyGroupWithPrevious && canBodyGroupWithNext -> if (message.isMe()) R.drawable.message_out_middle else R.drawable.message_in_middle + canBodyGroupWithPrevious && !canBodyGroupWithNext -> if (message.isMe()) R.drawable.message_out_last else R.drawable.message_in_last + else -> R.drawable.message_only + }) + + + // Bind the attachments + view.attachments.setVisible(media.isNotEmpty()) + if (view.attachments.childCount > 0) { + view.attachments.removeAllViews() + } + + media.forEachIndexed { index, part -> + val mediaView = layoutInflater.inflate(R.layout.mms_preview_list_item, view.attachments, false) + mediaView.video.setVisible(part.isVideo()) + mediaView.longClicks().subscribe { longClicks.onNext(message) } + mediaView.clicks().subscribe { + when { + part.isImage() -> navigator.showImage(part.id) + part.isVideo() -> navigator.showVideo(part.getUri(), part.type) + } + } + + val canMediaGroupWithPrevious = canGroup(message, previous) || index > 0 + val canMediaGroupWithNext = canGroup(message, next) || index < media.size && view.body.visibility == View.VISIBLE + mediaView.thumbnail.bubbleStyle = when { + !canMediaGroupWithPrevious && canMediaGroupWithNext -> if (message.isMe()) OUT_FIRST else IN_FIRST + canMediaGroupWithPrevious && canMediaGroupWithNext -> if (message.isMe()) OUT_MIDDLE else IN_MIDDLE + canMediaGroupWithPrevious && !canMediaGroupWithNext -> if (message.isMe()) OUT_LAST else IN_LAST + else -> ONLY + } + + GlideApp.with(context).load(part.getUri()).fitCenter().into(mediaView.thumbnail) + view.attachments.addView(mediaView) + } + + + // If we're on API 21, we need to re-tint the background + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) { + view.body.setBackgroundTint(when (message.isMe()) { + true -> colors.bubble + false -> theme + }.blockingFirst()) + } } - private fun bindAvatar(view: View, position: Int) { - val message = getItem(position)!! - if (message.isMe()) return - - view.avatar.threadId = conversation?.id ?: 0 - view.avatar.setContact(contactCache[message.address]) - } - - private fun bindStatus(view: View, position: Int) { + private fun bindStatus(viewHolder: QkViewHolder, position: Int) { val message = getItem(position)!! val next = if (position == itemCount - 1) null else getItem(position + 1) + val view = viewHolder.itemView val age = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - message.date) val timestamp = dateFormatter.getTimestamp(message.date) @@ -182,59 +266,6 @@ class MessagesAdapter @Inject constructor( }) } - private fun bindGrouping(view: View, position: Int) { - val message = getItem(position)!! - val previous = if (position == 0) null else getItem(position - 1) - val next = if (position == itemCount - 1) null else getItem(position + 1) - - val diff = TimeUnit.MILLISECONDS.toMinutes(message.date - (previous?.date ?: 0)) - - val sent = message.isMe() - val canGroupWithPrevious = canGroup(message, previous) - val canGroupWithNext = canGroup(message, next) - - view.timestamp.visibility = if (diff < TIMESTAMP_THRESHOLD) View.GONE else View.VISIBLE - - when { - !canGroupWithPrevious && canGroupWithNext -> { - view.setPadding(bottom = 2.dpToPx(context)) - view.messageBackground.setBackgroundResource(if (sent) R.drawable.message_out_first else R.drawable.message_in_first) - view.avatar?.visibility = View.INVISIBLE - } - canGroupWithPrevious && canGroupWithNext -> { - view.setPadding(bottom = 2.dpToPx(context)) - view.messageBackground.setBackgroundResource(if (sent) R.drawable.message_out_middle else R.drawable.message_in_middle) - view.avatar?.visibility = View.INVISIBLE - } - canGroupWithPrevious && !canGroupWithNext -> { - view.setPadding(bottom = 16.dpToPx(context)) - view.messageBackground.setBackgroundResource(if (sent) R.drawable.message_out_last else R.drawable.message_in_last) - view.avatar?.visibility = View.VISIBLE - } - else -> { - view.setPadding(bottom = 16.dpToPx(context)) - view.messageBackground.setBackgroundResource(R.drawable.message_only) - view.avatar?.visibility = View.VISIBLE - } - } - - // If we're showing any thumbnails, set clipToOutline to true - val hasImages = message.parts.filter { it.hasThumbnails() }.any() - view.messageBackground.clipToOutline = hasImages - - // setClipToOutline doesn't work unless all of the corners have the same radius, so we have - // to use the message_only bubble - if (hasImages) view.messageBackground.setBackgroundResource(R.drawable.message_only) - - // If we're on API 21, we need to re-tint the background - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) { - view.messageBackground.setBackgroundTint(when (message.isMe()) { - true -> colors.bubble - false -> theme - }.blockingFirst()) - } - } - private fun canGroup(message: Message, other: Message?): Boolean { if (other == null) return false val diff = TimeUnit.MILLISECONDS.toMinutes(Math.abs(message.date - other.date)) diff --git a/presentation/src/main/java/injection/AppComponent.kt b/presentation/src/main/java/injection/AppComponent.kt index 99b108e0..8178d274 100644 --- a/presentation/src/main/java/injection/AppComponent.kt +++ b/presentation/src/main/java/injection/AppComponent.kt @@ -22,7 +22,6 @@ import common.QKApplication import common.QkDialog import common.util.ContactImageLoader import common.widget.AvatarView -import common.widget.MmsPreviewView import common.widget.PagerTitleView import common.widget.PreferenceView import common.widget.QkEditText @@ -112,7 +111,6 @@ interface AppComponent { fun inject(view: AvatarView) fun inject(view: DetailedChipView) - fun inject(view: MmsPreviewView) fun inject(view: PagerTitleView) fun inject(view: PreferenceView) fun inject(view: QkEditText) diff --git a/presentation/src/main/res/layout/message_list_item_in.xml b/presentation/src/main/res/layout/message_list_item_in.xml index 19d2d069..7555b800 100644 --- a/presentation/src/main/res/layout/message_list_item_in.xml +++ b/presentation/src/main/res/layout/message_list_item_in.xml @@ -17,111 +17,83 @@ ~ You should have received a copy of the GNU General Public License ~ along with QKSMS. If not, see . --> - + android:background="@drawable/ripple"> + + + + - - - - - - - - - - - - - - + android:paddingStart="12dp" + android:paddingTop="8dp" + app:layout_constrainedWidth="true" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0" + app:layout_constraintStart_toEndOf="@id/avatar" + app:layout_constraintTop_toBottomOf="@id/attachments" + tools:backgroundTint="@color/tools_theme" + tools:text="@tools:sample/lorem" /> - \ No newline at end of file + \ No newline at end of file diff --git a/presentation/src/main/res/layout/message_list_item_out.xml b/presentation/src/main/res/layout/message_list_item_out.xml index 78ce9e35..93a92bf4 100644 --- a/presentation/src/main/res/layout/message_list_item_out.xml +++ b/presentation/src/main/res/layout/message_list_item_out.xml @@ -17,105 +17,77 @@ ~ You should have received a copy of the GNU General Public License ~ along with QKSMS. If not, see . --> - + android:background="@drawable/ripple"> + + - - - - - - - - - - - - + android:paddingStart="12dp" + android:paddingTop="8dp" + app:layout_constrainedWidth="true" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="1" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/attachments" + tools:backgroundTint="@color/white" + tools:text="Hey! How's it going?" /> - \ No newline at end of file + \ No newline at end of file diff --git a/presentation/src/main/res/layout/mms_preview_list_item.xml b/presentation/src/main/res/layout/mms_preview_list_item.xml index 6402ab80..ca69eba7 100644 --- a/presentation/src/main/res/layout/mms_preview_list_item.xml +++ b/presentation/src/main/res/layout/mms_preview_list_item.xml @@ -19,13 +19,18 @@ --> + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:paddingTop="2dp"> -