mirror of
https://github.com/element-hq/element-android
synced 2024-11-25 02:45:37 +03:00
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:
commit
f2320f9571
4 changed files with 14 additions and 21 deletions
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue