diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt index f728c5e8..e3306892 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt @@ -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) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt index 95b2f5d5..8cdbc005 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt @@ -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) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesComposeFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesComposeFragment.kt index 784b95a1..b0181278 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesComposeFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesComposeFragment.kt @@ -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("".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("".toRegex(), "\n") ?: "Nie udało się wczytać oryginalnej wiadomości.") } b.recipients.addTextWithChips(chipList) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt index 448e8ea5..4808d2ef 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt @@ -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) {