Support spoilers in messages

This commit is contained in:
Valere 2019-10-28 18:33:15 +01:00 committed by Benoit Marty
parent 84d6c8ec16
commit e976055253
3 changed files with 61 additions and 5 deletions

View File

@ -7,6 +7,7 @@ Features ✨:
Improvements 🙌: Improvements 🙌:
- Search reaction by name or keyword in emoji picker - Search reaction by name or keyword in emoji picker
- Handle code tags (#567) - Handle code tags (#567)
- Support spoiler messages
Other changes: Other changes:
- Markdown set to off by default (#412) - Markdown set to off by default (#412)

View File

@ -31,6 +31,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import me.saket.bettermovementmethod.BetterLinkMovementMethod import me.saket.bettermovementmethod.BetterLinkMovementMethod
import java.net.URL
@EpoxyModelClass(layout = R.layout.item_timeline_event_base) @EpoxyModelClass(layout = R.layout.item_timeline_event_base)
abstract class MessageTextItem : AbsMessageItem<MessageTextItem.Holder>() { abstract class MessageTextItem : AbsMessageItem<MessageTextItem.Holder>() {
@ -49,17 +50,27 @@ abstract class MessageTextItem : AbsMessageItem<MessageTextItem.Holder>() {
private val mvmtMethod = BetterLinkMovementMethod.newInstance().also { private val mvmtMethod = BetterLinkMovementMethod.newInstance().also {
it.setOnLinkClickListener { _, url -> it.setOnLinkClickListener { _, url ->
// Return false to let android manage the click on the link, or true if the link is handled by the application // Return false to let android manage the click on the link, or true if the link is handled by the application
try {
(URL(url))
urlClickCallback?.onUrlClicked(url) == true urlClickCallback?.onUrlClicked(url) == true
} catch (t: Throwable) {
false
}
} }
// We need also to fix the case when long click on link will trigger long click on cell // We need also to fix the case when long click on link will trigger long click on cell
it.setOnLinkLongClickListener { tv, url -> it.setOnLinkLongClickListener { tv, url ->
// Long clicks are handled by parent, return true to block android to do something with url // Long clicks are handled by parent, return true to block android to do something with url
try {
(URL(url))
if (urlClickCallback?.onUrlLongClicked(url) == true) { if (urlClickCallback?.onUrlLongClicked(url) == true) {
tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0)) tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0))
true true
} else { } else {
false false
} }
} catch (t: Throwable) {
false
}
} }
} }

View File

@ -0,0 +1,44 @@
/*
* 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.riotx.features.html
import android.content.Context
import android.graphics.Color
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import im.vector.riotx.R
import im.vector.riotx.features.themes.ThemeUtils
class SpoilerSpan(val bgColor: Int, val context: Context) : ClickableSpan() {
override fun onClick(widget: View) {
isHidden = !isHidden
widget.invalidate()
}
var isHidden = true
override fun updateDrawState(tp: TextPaint) {
tp.bgColor = bgColor
if (isHidden) {
tp.color = Color.TRANSPARENT
} else {
tp.color = ThemeUtils.getColor(context, R.attr.riotx_text_primary)
}
}
}