2017-02-09 17:38:41 +01:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.view.holder.message
|
|
|
|
|
2017-02-10 07:17:48 +01:00
|
|
|
import android.support.v4.view.GravityCompat
|
2017-02-11 09:04:11 +01:00
|
|
|
import android.text.SpannableStringBuilder
|
2017-02-11 07:50:17 +01:00
|
|
|
import android.text.format.DateUtils
|
2017-02-09 17:38:41 +01:00
|
|
|
import android.view.View
|
2017-02-10 07:17:48 +01:00
|
|
|
import android.widget.FrameLayout
|
2017-02-11 07:50:17 +01:00
|
|
|
import android.widget.LinearLayout
|
2017-02-09 17:38:41 +01:00
|
|
|
import kotlinx.android.synthetic.main.list_item_message_conversation_text.view.*
|
2017-02-11 09:04:11 +01:00
|
|
|
import org.mariotaku.ktextension.empty
|
2017-02-10 07:17:48 +01:00
|
|
|
import org.mariotaku.ktextension.isNullOrEmpty
|
|
|
|
import org.mariotaku.messagebubbleview.library.MessageBubbleView
|
2017-02-09 17:38:41 +01:00
|
|
|
import org.mariotaku.twidere.R
|
2017-02-10 07:17:48 +01:00
|
|
|
import org.mariotaku.twidere.adapter.MessagesConversationAdapter
|
2017-02-11 09:04:11 +01:00
|
|
|
import org.mariotaku.twidere.extension.model.applyTo
|
2017-02-10 07:17:48 +01:00
|
|
|
import org.mariotaku.twidere.extension.model.timestamp
|
2017-02-09 17:38:41 +01:00
|
|
|
import org.mariotaku.twidere.model.ParcelableMessage
|
2017-02-11 09:04:11 +01:00
|
|
|
import org.mariotaku.twidere.model.SpanItem
|
2017-02-09 17:38:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 2017/2/9.
|
|
|
|
*/
|
|
|
|
|
2017-02-10 07:17:48 +01:00
|
|
|
class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : AbsMessageViewHolder(itemView, adapter) {
|
2017-02-09 17:38:41 +01:00
|
|
|
|
2017-02-11 07:50:17 +01:00
|
|
|
private val date by lazy { itemView.date }
|
2017-02-09 17:38:41 +01:00
|
|
|
private val text by lazy { itemView.text }
|
2017-02-10 07:17:48 +01:00
|
|
|
private val time by lazy { itemView.time }
|
|
|
|
private val mediaPreview by lazy { itemView.mediaPreview }
|
|
|
|
private val messageContent by lazy { itemView.messageContent }
|
2017-02-09 17:38:41 +01:00
|
|
|
|
2017-02-11 07:50:17 +01:00
|
|
|
init {
|
|
|
|
val textSize = adapter.textSize
|
|
|
|
text.textSize = textSize
|
|
|
|
time.textSize = textSize * 0.8f
|
|
|
|
date.textSize = textSize * 0.9f
|
2017-02-11 09:04:11 +01:00
|
|
|
mediaPreview.style = adapter.mediaPreviewStyle
|
2017-02-11 07:50:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun display(message: ParcelableMessage, showDate: Boolean) {
|
|
|
|
super.display(message, showDate)
|
2017-02-10 07:17:48 +01:00
|
|
|
setOutgoingStatus(messageContent, message.is_outgoing)
|
2017-02-11 07:50:17 +01:00
|
|
|
if (showDate) {
|
|
|
|
date.visibility = View.VISIBLE
|
|
|
|
date.text = DateUtils.getRelativeTimeSpanString(message.timestamp, System.currentTimeMillis(),
|
|
|
|
DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_SHOW_DATE)
|
|
|
|
} else {
|
|
|
|
date.visibility = View.GONE
|
|
|
|
}
|
2017-02-11 09:04:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Loop through text and spans to found non-space char count
|
|
|
|
val hideText = run {
|
|
|
|
|
|
|
|
fun String.nonSpaceCount(range: IntRange): Int {
|
|
|
|
if (range.isEmpty()) return 0
|
|
|
|
return range.count { !Character.isSpaceChar(this[it]) }
|
|
|
|
}
|
|
|
|
|
|
|
|
val text = message.text_unescaped
|
|
|
|
|
|
|
|
var nonSpaceCount = 0
|
|
|
|
var curPos = 0
|
|
|
|
message.spans?.forEach { span ->
|
|
|
|
if (message.media?.firstOrNull { media -> span.link == media.url } != null) {
|
|
|
|
// Skip if span is hidden
|
|
|
|
span.type = SpanItem.SpanType.HIDE
|
|
|
|
} else {
|
|
|
|
nonSpaceCount += text.nonSpaceCount(curPos until span.end)
|
|
|
|
}
|
|
|
|
curPos = span.end
|
|
|
|
}
|
|
|
|
nonSpaceCount += text.nonSpaceCount(curPos..text.lastIndex)
|
|
|
|
return@run nonSpaceCount == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text.text = SpannableStringBuilder.valueOf(message.text_unescaped).apply {
|
|
|
|
message.spans?.applyTo(this)
|
|
|
|
adapter.linkify.applyAllLinks(this, message.account_key, layoutPosition.toLong(),
|
|
|
|
false, adapter.linkHighlightingStyle, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
text.visibility = if (hideText || text.empty) {
|
|
|
|
View.GONE
|
|
|
|
} else {
|
|
|
|
View.VISIBLE
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:50:17 +01:00
|
|
|
time.text = DateUtils.formatDateTime(adapter.context, message.timestamp,
|
|
|
|
DateUtils.FORMAT_SHOW_TIME)
|
2017-02-11 09:04:11 +01:00
|
|
|
|
2017-02-10 07:17:48 +01:00
|
|
|
if (message.media.isNullOrEmpty()) {
|
|
|
|
mediaPreview.visibility = View.GONE
|
|
|
|
} else {
|
|
|
|
mediaPreview.visibility = View.VISIBLE
|
|
|
|
mediaPreview.displayMedia(adapter.mediaLoader, message.media, message.account_key,
|
|
|
|
withCredentials = true)
|
|
|
|
}
|
2017-02-09 17:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val layoutResource = R.layout.list_item_message_conversation_text
|
2017-02-10 07:17:48 +01:00
|
|
|
|
|
|
|
fun setOutgoingStatus(view: MessageBubbleView, outgoing: Boolean) {
|
|
|
|
view.setCaretPosition(if (outgoing) MessageBubbleView.BOTTOM_END else MessageBubbleView.BOTTOM_START)
|
|
|
|
setMessageContentGravity(view, outgoing)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setMessageContentGravity(view: View, outgoing: Boolean) {
|
|
|
|
val lp = view.layoutParams
|
|
|
|
when (lp) {
|
|
|
|
is FrameLayout.LayoutParams -> {
|
|
|
|
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
|
|
|
|
}
|
2017-02-11 07:50:17 +01:00
|
|
|
is LinearLayout.LayoutParams -> {
|
|
|
|
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
|
|
|
|
}
|
2017-02-10 07:17:48 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 17:38:41 +01:00
|
|
|
}
|
|
|
|
}
|