1
0
Fork 0

Move HTML generation to separate class

This commit is contained in:
Alex Kotov 2021-08-14 07:34:58 +05:00
parent eaebd08661
commit 6a38971518
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 31 additions and 28 deletions

View 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;
}
"""
}
}

View file

@ -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;
}
"""
}
} }