BayernMessenger/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/item/MessageTextItem.kt

86 lines
3.2 KiB
Kotlin
Raw Normal View History

/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotredesign.features.home.room.detail.timeline.item
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.text.PrecomputedTextCompat
2019-05-08 08:33:14 +00:00
import androidx.core.text.toSpannable
import androidx.core.widget.TextViewCompat
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.riotredesign.R
import im.vector.riotredesign.features.html.PillImageSpan
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.saket.bettermovementmethod.BetterLinkMovementMethod
@EpoxyModelClass(layout = R.layout.item_timeline_event_base)
abstract class MessageTextItem : AbsMessageItem<MessageTextItem.Holder>() {
2019-05-08 13:49:32 +00:00
@EpoxyAttribute
var message: CharSequence? = null
@EpoxyAttribute
override lateinit var informationData: MessageInformationData
val mvmtMethod = BetterLinkMovementMethod.newInstance().also {
it.setOnLinkClickListener { textView, url ->
//Return false to let android manage the click on the link
false
}
it.setOnLinkLongClickListener { textView, url ->
//Long clicks are handled by parent, return false to block android to do something with url
true
}
}
override fun bind(holder: Holder) {
super.bind(holder)
holder.messageView.movementMethod = mvmtMethod
val textFuture = PrecomputedTextCompat.getTextFuture(message ?: "",
2019-05-08 13:49:32 +00:00
TextViewCompat.getTextMetricsParams(holder.messageView),
null)
holder.messageView.setTextFuture(textFuture)
holder.messageView.renderSendState()
holder.messageView.setOnClickListener(cellClickListener)
2019-05-07 12:02:15 +00:00
holder.messageView.setOnLongClickListener(longClickListener)
findPillsAndProcess { it.bind(holder.messageView) }
}
private fun findPillsAndProcess(processBlock: (span: PillImageSpan) -> Unit) {
GlobalScope.launch(Dispatchers.Main) {
val pillImageSpans: Array<PillImageSpan>? = withContext(Dispatchers.IO) {
2019-05-08 08:33:14 +00:00
message?.toSpannable()?.let { spannable ->
spannable.getSpans(0, spannable.length, PillImageSpan::class.java)
}
}
pillImageSpans?.forEach { processBlock(it) }
}
}
override fun getStubType(): Int = R.id.messageContentTextStub
class Holder : AbsMessageItem.Holder() {
val messageView by bind<AppCompatTextView>(R.id.messageTextView)
override fun getStubId(): Int = R.id.messageContentTextStub
}
}