Skip to content

Commit

Permalink
move set html body to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Mar 26, 2024
1 parent 3cf84bf commit ef6eb16
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import de.westnordost.streetcomplete.util.ktx.getRawTextFile
import de.westnordost.streetcomplete.util.ktx.indicesOf
import de.westnordost.streetcomplete.util.ktx.pxToDp
import de.westnordost.streetcomplete.util.ktx.pxToSp
import de.westnordost.streetcomplete.util.ktx.setHtmlBody
import de.westnordost.streetcomplete.util.ktx.viewLifecycleScope
import de.westnordost.streetcomplete.util.viewBinding
import kotlinx.coroutines.CoroutineScope
Expand All @@ -41,7 +42,7 @@ class ChangelogFragment : TwoPaneDetailFragment(R.layout.fragment_changelog), Ha

viewLifecycleScope.launch {
val changelog = readChangelog(resources)
binding.webView.setHtmlFromString(changelog)
binding.webView.setHtmlBody(changelog)
}
}
}
Expand All @@ -67,7 +68,7 @@ class WhatsNewDialog(context: Context, sinceVersion: String) : AlertDialog(conte
}
val changelog = fullChangelog.substring(0, sinceVersionIndex)

binding.webView.setHtmlFromString(changelog)
binding.webView.setHtmlBody(changelog)
}
}

Expand All @@ -80,58 +81,3 @@ class WhatsNewDialog(context: Context, sinceVersion: String) : AlertDialog(conte
private suspend fun readChangelog(resources: Resources): String = withContext(Dispatchers.IO) {
resources.getRawTextFile(R.raw.changelog)
}

private fun Resources.getHexColor(@ColorRes resId: Int) =
String.format("#%06X", 0xffffff and getColor(resId))

private fun Resources.getDimensionInSp(@DimenRes resId: Int) = pxToSp(getDimension(resId)).roundToInt()

private fun Resources.getDimensionInDp(@DimenRes resId: Int) = pxToDp(getDimension(resId)).roundToInt()

private fun WebView.setHtmlFromString(body: String) {
val textColor = resources.getHexColor(R.color.text)
val linkColor = resources.getHexColor(R.color.accent)
val dividerColor = resources.getHexColor(R.color.divider)

val textSize = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_body_1_material)
val h2Size = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_headline_material)
val h3Size = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_medium_material)
val h4Size = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_subhead_material)

val verticalMargin = resources.getDimensionInDp(R.dimen.activity_vertical_margin)
val horizontalMargin = resources.getDimensionInDp(R.dimen.activity_horizontal_margin)

val html = """
<html>
<head>
<meta name="color-scheme" content="dark light">
<style>
body {
margin: ${verticalMargin}px ${horizontalMargin}px;
color: $textColor;
font-size: ${textSize}px;
}
:link { color: $linkColor; }
h2, h3, h4 { font-family: sans-serif-condensed; }
h2 { font-size: ${h2Size}px; }
h3 { font-size: ${h3Size}px; }
h4 { font-size: ${h4Size}px; }
h2:not(:first-child) {
border-top: 1px solid $dividerColor;
padding-top: 1rem;
}
@media (prefers-color-scheme: dark) {}
</style>
</head>
<body>$body</body>
</html>
""".trimIndent()

loadDataWithBaseURL(null, html, "text/html", "utf-8", null)
setBackgroundColor(Color.TRANSPARENT)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package de.westnordost.streetcomplete.util.ktx

import android.content.res.Resources
import android.graphics.Color
import android.webkit.WebView
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import de.westnordost.streetcomplete.R
import kotlin.math.roundToInt

fun WebView.setHtmlBody(body: String) {
val textColor = resources.getHexColor(R.color.text)
val linkColor = resources.getHexColor(R.color.accent)
val dividerColor = resources.getHexColor(R.color.divider)

val textSize = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_body_1_material)
val h2Size = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_headline_material)
val h3Size = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_medium_material)
val h4Size = resources.getDimensionInSp(androidx.appcompat.R.dimen.abc_text_size_subhead_material)

val verticalMargin = resources.getDimensionInDp(R.dimen.activity_vertical_margin)
val horizontalMargin = resources.getDimensionInDp(R.dimen.activity_horizontal_margin)

val html = """
<html>
<head>
<meta name="color-scheme" content="dark light">
<style>
body {
margin: ${verticalMargin}px ${horizontalMargin}px;
color: $textColor;
font-size: ${textSize}px;
}
:link { color: $linkColor; }
h2, h3, h4 { font-family: sans-serif-condensed; }
h2 { font-size: ${h2Size}px; }
h3 { font-size: ${h3Size}px; }
h4 { font-size: ${h4Size}px; }
h2:not(:first-child) {
border-top: 1px solid $dividerColor;
padding-top: 1rem;
}
@media (prefers-color-scheme: dark) {}
</style>
</head>
<body>$body</body>
</html>
""".trimIndent()

loadDataWithBaseURL(null, html, "text/html", "utf-8", null)
setBackgroundColor(Color.TRANSPARENT)
}

private fun Resources.getHexColor(@ColorRes resId: Int) =
String.format("#%06X", 0xffffff and getColor(resId))

private fun Resources.getDimensionInSp(@DimenRes resId: Int) = pxToSp(getDimension(resId)).roundToInt()

private fun Resources.getDimensionInDp(@DimenRes resId: Int) = pxToDp(getDimension(resId)).roundToInt()

0 comments on commit ef6eb16

Please sign in to comment.