Merge pull request #665 from vector-im/feature/color_theme

Ensure color is retrieved from current theme, even when theme change
This commit is contained in:
Benoit Marty 2019-11-12 12:29:27 +01:00 committed by GitHub
commit f2320f9571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 21 deletions

View file

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

View file

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

View file

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

View file

@ -20,10 +20,10 @@ import android.graphics.Color
import android.text.TextPaint import android.text.TextPaint
import android.text.style.ClickableSpan import android.text.style.ClickableSpan
import android.view.View import android.view.View
import im.vector.riotx.R
import im.vector.riotx.core.resources.ColorProvider
class SpoilerSpan(private val bgColorHidden: Int, class SpoilerSpan(private val colorProvider: ColorProvider) : ClickableSpan() {
private val bgColorRevealed: Int,
private val textColor: Int) : ClickableSpan() {
override fun onClick(widget: View) { override fun onClick(widget: View) {
isHidden = !isHidden isHidden = !isHidden
@ -34,11 +34,11 @@ class SpoilerSpan(private val bgColorHidden: Int,
override fun updateDrawState(tp: TextPaint) { override fun updateDrawState(tp: TextPaint) {
if (isHidden) { if (isHidden) {
tp.bgColor = bgColorHidden tp.bgColor = colorProvider.getColorFromAttribute(R.attr.vctr_spoiler_background_color)
tp.color = Color.TRANSPARENT tp.color = Color.TRANSPARENT
} else { } else {
tp.bgColor = bgColorRevealed tp.bgColor = colorProvider.getColorFromAttribute(R.attr.vctr_markdown_block_background_color)
tp.color = textColor tp.color = colorProvider.getColorFromAttribute(R.attr.riotx_text_primary)
} }
} }
} }