Ensure color is retrieved from current theme, even when theme change

This commit is contained in:
Benoit Marty 2019-11-04 18:12:24 +01:00
parent cd1a964067
commit bee8c2d159
4 changed files with 14 additions and 21 deletions

View file

@ -16,15 +16,15 @@
package im.vector.riotx.core.resources
import android.content.Context
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import im.vector.riotx.features.themes.ThemeUtils
import javax.inject.Inject
class ColorProvider @Inject constructor(private val context: AppCompatActivity) {
class ColorProvider @Inject constructor(private val context: Context) {
fun getColor(@ColorRes colorRes: Int): Int {
return ContextCompat.getColor(context, colorRes)
@ -33,7 +33,6 @@ class ColorProvider @Inject constructor(private val context: AppCompatActivity)
/**
* Translates color attributes to colors
*
* @param c Context
* @param colorAttribute Color Attribute
* @return Requested Color
*/

View file

@ -19,6 +19,7 @@ package im.vector.riotx.features.html
import android.content.Context
import im.vector.riotx.core.di.ActiveSessionHolder
import im.vector.riotx.core.glide.GlideApp
import im.vector.riotx.core.resources.ColorProvider
import im.vector.riotx.features.home.AvatarRenderer
import io.noties.markwon.Markwon
import io.noties.markwon.html.HtmlPlugin
@ -49,6 +50,7 @@ class EventHtmlRenderer @Inject constructor(context: Context,
}
class MatrixHtmlPluginConfigure @Inject constructor(private val context: Context,
private val colorProvider: ColorProvider,
private val avatarRenderer: AvatarRenderer,
private val session: ActiveSessionHolder) : HtmlPlugin.HtmlConfigure {
@ -58,7 +60,6 @@ class MatrixHtmlPluginConfigure @Inject constructor(private val context: Context
.addHandler(FontTagHandler())
.addHandler(MxLinkTagHandler(GlideApp.with(context), context, avatarRenderer, session))
.addHandler(MxReplyTagHandler())
// FIXME (P3) SpanHandler is not recreated when theme is change and it depends on theme colors
.addHandler(SpanHandler(context))
.addHandler(SpanHandler(colorProvider))
}
}

View file

@ -15,30 +15,23 @@
*/
package im.vector.riotx.features.html
import android.content.Context
import im.vector.riotx.R
import im.vector.riotx.features.themes.ThemeUtils
import im.vector.riotx.core.resources.ColorProvider
import io.noties.markwon.MarkwonVisitor
import io.noties.markwon.SpannableBuilder
import io.noties.markwon.html.HtmlTag
import io.noties.markwon.html.MarkwonHtmlRenderer
import io.noties.markwon.html.TagHandler
class SpanHandler(context: Context) : TagHandler() {
class SpanHandler(private val colorProvider: ColorProvider) : TagHandler() {
override fun supportedTags() = listOf("span")
private val spoilerBgColorHidden: Int = ThemeUtils.getColor(context, R.attr.vctr_spoiler_background_color)
private val spoilerBgColorRevealed: Int = ThemeUtils.getColor(context, R.attr.vctr_markdown_block_background_color)
private val textColor: Int = ThemeUtils.getColor(context, R.attr.riotx_text_primary)
override fun handle(visitor: MarkwonVisitor, renderer: MarkwonHtmlRenderer, tag: HtmlTag) {
val mxSpoiler = tag.attributes()["data-mx-spoiler"]
if (mxSpoiler != null) {
SpannableBuilder.setSpans(
visitor.builder(),
SpoilerSpan(spoilerBgColorHidden, spoilerBgColorRevealed, textColor),
SpoilerSpan(colorProvider),
tag.start(),
tag.end()
)

View file

@ -20,10 +20,10 @@ 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.core.resources.ColorProvider
class SpoilerSpan(private val bgColorHidden: Int,
private val bgColorRevealed: Int,
private val textColor: Int) : ClickableSpan() {
class SpoilerSpan(private val colorProvider: ColorProvider) : ClickableSpan() {
override fun onClick(widget: View) {
isHidden = !isHidden
@ -34,11 +34,11 @@ class SpoilerSpan(private val bgColorHidden: Int,
override fun updateDrawState(tp: TextPaint) {
if (isHidden) {
tp.bgColor = bgColorHidden
tp.bgColor = colorProvider.getColorFromAttribute(R.attr.vctr_spoiler_background_color)
tp.color = Color.TRANSPARENT
} else {
tp.bgColor = bgColorRevealed
tp.color = textColor
tp.bgColor = colorProvider.getColorFromAttribute(R.attr.vctr_markdown_block_background_color)
tp.color = colorProvider.getColorFromAttribute(R.attr.riotx_text_primary)
}
}
}