This commit is contained in:
Chris kerr 2024-03-26 19:39:50 -04:00
commit 1f1ac760ca
3 changed files with 82 additions and 10 deletions

View File

@ -2,10 +2,14 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<<<<<<< HEAD
<!--<link rel="stylesheet" type="text/css" href="themes/*">
<link rel="stylesheet" type="text/css" href="styles/*">-->
<link rel="stylesheet" type="text/css" href="style/output.css">
=======
>>>>>>> 444601f0cb9165b77f4c460bdf72f1cb567eb544
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="themes/Dark.css">
</head>
<body class="bg-black">
@ -14,10 +18,10 @@
<div class="theme-dropdown">
<label for="theme-select"> </label>
<select id="theme-select">
<option value="Dark">Dark</option>
<option value="Lava">Lava</option>
<option value="Midnight">Midnight</option>
<option value="Solar">Solar</option>
<option value="Dark.css">Dark</option>
<option value="Lava.css">Lava</option>
<option value="Midnight.css">Midnight</option>
<option value="Solar.css">Solar</option>
</select>
</div>
<div class="currency-dropdown">
@ -82,4 +86,5 @@
</div>
</body>
</html>

View File

@ -0,0 +1,20 @@
// Function to change the theme based on the selected option
function changeTheme() {
console.log("changeTheme() called"); // Add this line
var themeSelect = document.getElementById("theme-select");
var selectedTheme = themeSelect.value;
// Construct the theme CSS file path based on the selected theme
var themeCSSFile = "themes/" + selectedTheme + ".css";
// Set the theme CSS file as the stylesheet link's href
var styleSheet = document.querySelector('link[href^="themes/"]');
styleSheet.href = themeCSSFile;
}
// Add an event listener to the theme dropdown to change the theme on selection change
document.getElementById("theme-select").addEventListener("change", changeTheme);
// Initial theme setup when the page loads
console.log("Initial theme setup"); // Add this line
changeTheme(); // This line was missing in your previous code

View File

@ -10,6 +10,7 @@ import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
private lateinit var currentThemeCSSCode: String
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
@ -26,17 +27,42 @@ class MainActivity : AppCompatActivity() {
// Clear the WebView cache (optional)
webView.clearCache(true)
// Load the HTML content from the "app.html" file
// Initialize with the default theme
currentThemeCSSCode = loadThemeCSS("themes/Dark.css")
loadWebViewWithTheme(currentThemeCSSCode)
}
// 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 {
// Load the HTML content from the "app.html" file
assets.open("app.html").bufferedReader().use { it.readText() }
assetManager.open("app.html").bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
// If loading HTML fails, set some default content
"<html><body><h1>Error loading HTML</h1></body></html>"
}
val assetManager = assets
// Load all styles from the 'styles' directory
val styleFiles = listOf(
"styles/settings.css",
"styles/bitcoinPrice.css",
"styles/exchange.css",
"styles/fiatToSats.css",
"styles/satsToFiat.css"
)
val styleContents = styleFiles.map { fileName ->
try {
assetManager.open(fileName).bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
"/* Error loading style: $fileName */"
}
}
// Load the JavaScript code
val scriptFiles = listOf(
"scripts/selectTheme.js",
"scripts/viewBitcoinPrice.js",
@ -54,6 +80,7 @@ class MainActivity : AppCompatActivity() {
}
}
<<<<<<< HEAD
val cssFiles = listOf(
"style/output.css"
//"themes/Dark.css",
@ -76,13 +103,18 @@ class MainActivity : AppCompatActivity() {
}
}
=======
>>>>>>> 444601f0cb9165b77f4c460bdf72f1cb567eb544
val javaScriptCode = scriptContents.joinToString("\n")
// Combine the HTML, CSS, and JavaScript code
val finalHtmlContent = """
<html>
<head>
<style type="text/css">$cssCode</style>
<style type="text/css">
${styleContents.joinToString("\n")}
$cssCode
</style>
</head>
<body>
$htmlContent
@ -94,6 +126,21 @@ class MainActivity : AppCompatActivity() {
// 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 */"
}
}
}