v2
This commit is contained in:
parent
72118e1113
commit
48b8033788
@ -3,9 +3,10 @@
|
|||||||
<head>
|
<head>
|
||||||
<script src="https://cdn.tailwindcss.com"></script>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
<link rel="stylesheet" href="style/output.css" />
|
<link rel="stylesheet" href="style/output.css" />
|
||||||
|
<script src="js/main.js"></script>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
</head>
|
</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="container">
|
||||||
<div class="settings">
|
<div class="settings">
|
||||||
<img src="file:///android_asset/images/cog.png" alt="cog" />
|
<img src="file:///android_asset/images/cog.png" alt="cog" />
|
||||||
@ -84,6 +85,6 @@
|
|||||||
/></a>
|
/></a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script type="module" src="/js/main.js"></script>
|
<script type="module" src="js/main.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,113 +1,69 @@
|
|||||||
package com.example.satworth
|
package com.example.satworth
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.res.AssetManager
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.webkit.WebView
|
import android.webkit.WebChromeClient
|
||||||
|
import android.webkit.WebResourceRequest
|
||||||
import android.webkit.WebSettings
|
import android.webkit.WebSettings
|
||||||
|
import android.webkit.WebView
|
||||||
|
import android.webkit.WebViewClient
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var webView: WebView
|
private lateinit var webView: WebView
|
||||||
private lateinit var currentThemeCSSCode: String
|
|
||||||
|
|
||||||
@SuppressLint("SetJavaScriptEnabled")
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
// Get the WebView reference from the layout
|
|
||||||
webView = findViewById(R.id.webView)
|
webView = findViewById(R.id.webView)
|
||||||
|
setupWebView()
|
||||||
// Enable JavaScript support
|
loadWebView()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to load WebView with the specified CSS theme code
|
@SuppressLint("SetJavaScriptEnabled")
|
||||||
private fun loadWebViewWithTheme(cssCode: String) {
|
private fun setupWebView() {
|
||||||
val assetManager = assets
|
webView.settings.apply {
|
||||||
|
javaScriptEnabled = true
|
||||||
// Load the HTML content
|
loadWithOverviewMode = true
|
||||||
val htmlContent = try {
|
useWideViewPort = true
|
||||||
assetManager.open("index.html").bufferedReader().use { it.readText() }
|
domStorageEnabled = true
|
||||||
} catch (e: IOException) {
|
cacheMode = WebSettings.LOAD_NO_CACHE
|
||||||
e.printStackTrace()
|
|
||||||
"<html><body><h1>Error loading HTML</h1></body></html>"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load all styles from the 'styles' directory
|
webView.webViewClient = object : WebViewClient() {
|
||||||
val styleFiles = listOf(
|
override fun shouldOverrideUrlLoading(
|
||||||
"style/output.css",
|
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 {
|
try {
|
||||||
assetManager.open(fileName).bufferedReader().use { it.readText() }
|
assetManager.loadUrl("file:///android_asset/js/main.js")
|
||||||
|
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
"/* Error loading style: $fileName */"
|
// Return placeholder for error cases
|
||||||
|
"/* Error loading scripts */"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
private fun loadWebView() {
|
||||||
|
loadScripts()
|
||||||
|
webView.loadUrl("file:///android_asset/index.html")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 */"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user