This commit is contained in:
0ceanSlim 2024-03-27 18:05:35 -04:00
parent 72118e1113
commit 48b8033788
2 changed files with 42 additions and 85 deletions

View File

@ -3,9 +3,10 @@
<head>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="style/output.css" />
<script src="js/main.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body class="bg-secondary grid-cols-1 text-center text-text">
<body class="bg-primary grid-cols-1 text-center text-text">
<div class="container">
<div class="settings">
<img src="file:///android_asset/images/cog.png" alt="cog" />
@ -84,6 +85,6 @@
/></a>
</div>
</div>
<script type="module" src="/js/main.js"></script>
<script type="module" src="js/main.js"></script>
</body>
</html>

View File

@ -1,113 +1,69 @@
package com.example.satworth
import android.annotation.SuppressLint
import android.content.res.AssetManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
private lateinit var currentThemeCSSCode: String
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the WebView reference from the layout
webView = findViewById(R.id.webView)
// Enable JavaScript support
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
// Clear the WebView cache (optional)
webView.clearCache(true)
// Initialize with the default theme
currentThemeCSSCode = loadThemeCSS("style/output.css")
loadWebViewWithTheme(currentThemeCSSCode)
setupWebView()
loadWebView()
}
// Function to load WebView with the specified CSS theme code
private fun loadWebViewWithTheme(cssCode: String) {
val assetManager = assets
// Load the HTML content
val htmlContent = try {
assetManager.open("index.html").bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
"<html><body><h1>Error loading HTML</h1></body></html>"
@SuppressLint("SetJavaScriptEnabled")
private fun setupWebView() {
webView.settings.apply {
javaScriptEnabled = true
loadWithOverviewMode = true
useWideViewPort = true
domStorageEnabled = true
cacheMode = WebSettings.LOAD_NO_CACHE
}
// Load all styles from the 'styles' directory
val styleFiles = listOf(
"style/output.css",
)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
}
val styleContents = styleFiles.map { fileName ->
webView.webChromeClient = object : WebChromeClient() {
// You can override other WebChromeClient methods if needed
}
}
private fun loadScripts(assetManager: AssetManager) {
try {
assetManager.open(fileName).bufferedReader().use { it.readText() }
assetManager.loadUrl("file:///android_asset/js/main.js")
} catch (e: IOException) {
e.printStackTrace()
"/* Error loading style: $fileName */"
}
// Return placeholder for error cases
"/* Error loading scripts */"
}
// Load the JavaScript code
val scriptFiles = listOf(
"js/main.js",
)
val scriptContents = scriptFiles.map { fileName ->
try {
assetManager.open(fileName).bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
"/* Error loading script: $fileName */"
}
}
val javaScriptCode = scriptContents.joinToString("\n")
// Combine the HTML, CSS, and JavaScript code
val finalHtmlContent = """
<html>
<head>
<style type="text/css">
${styleContents.joinToString("\n")}
$cssCode
</style>
</head>
<body>
$htmlContent
<script>$javaScriptCode</script>
</body>
</html>
""".trimIndent()
// Define the base URL for resolving relative paths (e.g., image source)
val baseUrl = "file:///android_asset/"
// Load the WebView with the HTML content and selected theme
webView.loadDataWithBaseURL(null, finalHtmlContent, "text/html", "UTF-8", null)
// Update the current theme CSS code
currentThemeCSSCode = cssCode
}
// Function to load CSS content from a file
private fun loadThemeCSS(cssFile: String): String {
val assetManager = assets
return try {
assetManager.open(cssFile).bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
"/* Error loading $cssFile */"
}
private fun loadWebView() {
loadScripts()
webView.loadUrl("file:///android_asset/index.html")
}
}