[Messages] Replace hardcoded message colors with brightened/darkened versions instead of white/black.

This commit is contained in:
Kuba Szczodrzyński 2020-03-12 18:46:16 +01:00
parent 6ec2bc6f21
commit c568cd3f2e
4 changed files with 20 additions and 14 deletions

View File

@ -198,7 +198,7 @@ class MessageFragment : Fragment(), CoroutineScope {
}
private fun showMessage() {
b.body.text = MessagesUtils.htmlToSpannable(message.body ?: "")
b.body.text = MessagesUtils.htmlToSpannable(activity, message.body ?: "")
b.date.text = getString(R.string.messages_date_time_format, Date.fromMillis(message.addedDate).formattedStringShort, Time.fromMillis(message.addedDate).stringHM)
val messageInfo = MessagesUtils.getMessageInfo(app, message, 40, 20, 14, 10)

View File

@ -41,7 +41,7 @@ class MessagesAdapter(private val app: App, private val onItemClickListener: OnI
b.messageAttachmentImage.visibility = if (message.hasAttachments()) View.VISIBLE else View.GONE
val text = message.body?.substring(0, message.body!!.length.coerceAtMost(200)) ?: ""
b.messageBody.text = MessagesUtils.htmlToSpannable(text)
b.messageBody.text = MessagesUtils.htmlToSpannable(b.root.context, text)
if (message.type == Message.TYPE_SENT || message.type == Message.TYPE_DRAFT || message.seen) {
b.messageSender.setTextAppearance(b.messageSender.context, R.style.NavView_TextView_Small)

View File

@ -380,7 +380,7 @@ class MessagesComposeFragment : Fragment(), CoroutineScope {
span.replace(0, 0, "\n\n")
subject = "Fwd: ${msg.subject}"
}
body = MessagesUtils.htmlToSpannable(msg.body ?: "Nie udało się wczytać oryginalnej wiadomości.")//Html.fromHtml(msg.body?.replace("<br\\s?/?>".toRegex(), "\n") ?: "Nie udało się wczytać oryginalnej wiadomości.")
body = MessagesUtils.htmlToSpannable(activity,msg.body ?: "Nie udało się wczytać oryginalnej wiadomości.")//Html.fromHtml(msg.body?.replace("<br\\s?/?>".toRegex(), "\n") ?: "Nie udało się wczytać oryginalnej wiadomości.")
}
b.recipients.addTextWithChips(chipList)

View File

@ -1,19 +1,17 @@
package pl.szczodrzynski.edziennik.ui.modules.messages
import android.content.Context
import android.graphics.*
import android.os.Build
import android.text.Html
import android.text.Spanned
import androidx.core.graphics.ColorUtils
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.db.entity.Message
import pl.szczodrzynski.edziennik.data.db.full.MessageFull
import pl.szczodrzynski.edziennik.fixName
import pl.szczodrzynski.edziennik.getNameInitials
import pl.szczodrzynski.edziennik.utils.Colors
import pl.szczodrzynski.edziennik.utils.Themes
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.navlib.blendColors
import kotlin.math.roundToInt
object MessagesUtils {
@ -180,7 +178,7 @@ object MessagesUtils {
class MessageInfo(var profileImage: Bitmap?, var profileName: String?)
@JvmStatic
fun htmlToSpannable(html: String): Spanned {
fun htmlToSpannable(context: Context, html: String): Spanned {
val hexPattern = "(#[a-fA-F0-9]{6})"
val colorRegex = "(?:color=\"$hexPattern\")|(?:style=\"color: ?${hexPattern})"
.toRegex(RegexOption.IGNORE_CASE)
@ -189,17 +187,25 @@ object MessagesUtils {
.replace("\\[META:[A-z0-9]+;[0-9-]+]".toRegex(), "")
.replace("background-color: ?$hexPattern;".toRegex(), "")
val colorBackground = android.R.attr.colorBackground.resolveAttr(context)
val textColorPrimary = android.R.attr.textColorPrimary.resolveAttr(context) and 0xffffff
colorRegex.findAll(text).forEach { result ->
val group = result.groups.drop(1).firstOrNull { it != null } ?: return@forEach
val color = Color.parseColor(group.value)
val luminance = ColorUtils.calculateLuminance(color)
var newColor = 0xff000000.toInt() or color
if (Themes.isDark && luminance <= 0.5) {
text = text.replaceRange(group.range, "#FFFFFF")
} else if (!Themes.isDark && luminance > 0.5) {
text = text.replaceRange(group.range, "#000000")
var blendAmount = 1
var numIterations = 0
while (numIterations < 100 && ColorUtils.calculateContrast(colorBackground, newColor) < 4.5f) {
blendAmount += 2
newColor = blendColors(color, blendAmount shl 24 or textColorPrimary)
numIterations++
}
text = text.replaceRange(group.range, "#" + (newColor and 0xffffff).toString(16))
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {