Move HTML generation to separate class
This commit is contained in:
parent
eaebd08661
commit
6a38971518
2 changed files with 31 additions and 28 deletions
28
app/src/main/java/com/causa_arcana/HtmlGenerator.kt
Normal file
28
app/src/main/java/com/causa_arcana/HtmlGenerator.kt
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package com.causa_arcana
|
||||||
|
|
||||||
|
class HtmlGenerator(private val contentHtml: String) {
|
||||||
|
fun fullHtml(): String {
|
||||||
|
return """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>${headInnerHtml()}</head>
|
||||||
|
<body>$contentHtml</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun headInnerHtml(): String {
|
||||||
|
return """
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<style>${fullCss()}</style>
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fullCss(): String {
|
||||||
|
return """
|
||||||
|
body {
|
||||||
|
background-color: #FFFF00;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,39 +12,14 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
val mainWebView: WebView = findViewById(R.id.mainWebView)
|
val mainWebView: WebView = findViewById(R.id.mainWebView)
|
||||||
|
|
||||||
val contentHtml = """
|
val htmlGenerator = HtmlGenerator("""
|
||||||
<h1>Hello, World!</h1>
|
<h1>Hello, World!</h1>
|
||||||
<p>This text is rendered with WebView.</p>
|
<p>This text is rendered with WebView.</p>
|
||||||
"""
|
""")
|
||||||
|
|
||||||
val fullHtml = fullHtml(contentHtml)
|
val fullHtml = htmlGenerator.fullHtml()
|
||||||
val encodedFullHtml = Base64.encodeToString(fullHtml.toByteArray(), Base64.NO_PADDING)
|
val encodedFullHtml = Base64.encodeToString(fullHtml.toByteArray(), Base64.NO_PADDING)
|
||||||
|
|
||||||
mainWebView.loadData(encodedFullHtml, "text/html", "base64")
|
mainWebView.loadData(encodedFullHtml, "text/html", "base64")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fullHtml(contentHtml: String): String {
|
|
||||||
return """
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>${headInnerHtml()}</head>
|
|
||||||
<body>$contentHtml</body>
|
|
||||||
</html>
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun headInnerHtml(): String {
|
|
||||||
return """
|
|
||||||
<meta charset="utf-8"/>
|
|
||||||
<style>${fullCss()}</style>
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun fullCss(): String {
|
|
||||||
return """
|
|
||||||
body {
|
|
||||||
background-color: #FFFF00;
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue