2019-01-25 13:04:59 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-03-05 17:31:03 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.detail.timeline.item
|
2018-10-31 12:03:36 +00:00
|
|
|
|
2019-05-10 16:12:35 +00:00
|
|
|
import android.text.Spannable
|
2019-05-08 13:49:32 +00:00
|
|
|
import android.view.View
|
2018-10-31 12:03:36 +00:00
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.TextView
|
2019-03-01 20:44:26 +00:00
|
|
|
import androidx.appcompat.widget.AppCompatTextView
|
|
|
|
import androidx.core.text.PrecomputedTextCompat
|
2019-05-08 08:33:14 +00:00
|
|
|
import androidx.core.text.toSpannable
|
2019-03-01 20:44:26 +00:00
|
|
|
import androidx.core.widget.TextViewCompat
|
2019-02-19 10:57:17 +00:00
|
|
|
import com.airbnb.epoxy.EpoxyAttribute
|
|
|
|
import com.airbnb.epoxy.EpoxyModelClass
|
2018-12-19 18:08:30 +00:00
|
|
|
import im.vector.matrix.android.api.permalinks.MatrixLinkify
|
2018-10-31 12:03:36 +00:00
|
|
|
import im.vector.riotredesign.R
|
2019-03-01 20:44:26 +00:00
|
|
|
import im.vector.riotredesign.features.html.PillImageSpan
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
2018-10-31 12:03:36 +00:00
|
|
|
|
2019-05-10 16:12:35 +00:00
|
|
|
@EpoxyModelClass(layout = R.layout.item_timeline_event_base)
|
2019-02-19 10:57:17 +00:00
|
|
|
abstract class MessageTextItem : AbsMessageItem<MessageTextItem.Holder>() {
|
2018-10-31 12:03:36 +00:00
|
|
|
|
2019-05-08 13:49:32 +00:00
|
|
|
@EpoxyAttribute
|
|
|
|
var message: CharSequence? = null
|
|
|
|
@EpoxyAttribute
|
|
|
|
override lateinit var informationData: MessageInformationData
|
|
|
|
@EpoxyAttribute
|
|
|
|
var clickListener: View.OnClickListener? = null
|
2018-10-31 12:03:36 +00:00
|
|
|
|
2019-02-19 10:57:17 +00:00
|
|
|
override fun bind(holder: Holder) {
|
|
|
|
super.bind(holder)
|
|
|
|
MatrixLinkify.addLinkMovementMethod(holder.messageView)
|
2019-03-01 20:44:26 +00:00
|
|
|
val textFuture = PrecomputedTextCompat.getTextFuture(message ?: "",
|
2019-05-08 13:49:32 +00:00
|
|
|
TextViewCompat.getTextMetricsParams(holder.messageView),
|
|
|
|
null)
|
2019-03-01 20:44:26 +00:00
|
|
|
holder.messageView.setTextFuture(textFuture)
|
2019-04-11 17:19:52 +00:00
|
|
|
holder.messageView.renderSendState()
|
2019-05-08 13:49:32 +00:00
|
|
|
holder.messageView.setOnClickListener (clickListener)
|
2019-05-07 12:02:15 +00:00
|
|
|
holder.messageView.setOnLongClickListener(longClickListener)
|
2019-03-01 20:44:26 +00:00
|
|
|
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 ->
|
2019-03-01 20:44:26 +00:00
|
|
|
spannable.getSpans(0, spannable.length, PillImageSpan::class.java)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pillImageSpans?.forEach { processBlock(it) }
|
|
|
|
}
|
2018-10-31 12:03:36 +00:00
|
|
|
}
|
2019-02-19 10:57:17 +00:00
|
|
|
|
2019-05-10 16:12:35 +00:00
|
|
|
override fun getStubType(): Int = R.id.messageContentTextStub
|
|
|
|
|
2019-02-19 10:57:17 +00:00
|
|
|
class Holder : AbsMessageItem.Holder() {
|
2019-03-01 20:44:26 +00:00
|
|
|
val messageView by bind<AppCompatTextView>(R.id.messageTextView)
|
2019-05-10 16:12:35 +00:00
|
|
|
override fun getStubId(): Int = R.id.messageContentTextStub
|
2019-02-19 10:57:17 +00:00
|
|
|
|
2019-05-10 16:12:35 +00:00
|
|
|
}
|
2019-02-19 10:57:17 +00:00
|
|
|
|
2018-10-31 12:03:36 +00:00
|
|
|
}
|