basic price working

This commit is contained in:
0ceanSlim 2024-03-28 12:44:15 -04:00
parent 48b8033788
commit 3b142d20a4
3 changed files with 77 additions and 47 deletions

View File

@ -1,9 +1,8 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://cdn.tailwindcss.com"></script>
<!--<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-primary grid-cols-1 text-center text-text">
@ -85,6 +84,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,8 +1,8 @@
import "/style/output.css";
//import "/style/output.css";
import { updateFiatToSatsConversion } from "./viewFiatToSats";
//import { updateFiatToSatsConversion } from "./viewFiatToSats";
//import { updateBitcoinPrice, refreshBitcoinPrice } from "./viewBitcoinPrice";
import { updateSatsToDollarsConversion } from "./calcSatsToFiat";
//import { updateSatsToDollarsConversion } from "./calcSatsToFiat";
function updateBitcoinPrice(selectedCurrency) {
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)

View File

@ -1,69 +1,100 @@
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.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.webkit.WebSettings
import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
@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)
setupWebView()
loadWebView()
}
@SuppressLint("SetJavaScriptEnabled")
private fun setupWebView() {
webView.settings.apply {
javaScriptEnabled = true
loadWithOverviewMode = true
useWideViewPort = true
domStorageEnabled = true
cacheMode = WebSettings.LOAD_NO_CACHE
}
// Enable JavaScript support
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
}
webView.webChromeClient = object : WebChromeClient() {
// You can override other WebChromeClient methods if needed
}
}
private fun loadScripts(assetManager: AssetManager) {
try {
assetManager.loadUrl("file:///android_asset/js/main.js")
// Clear the WebView cache (optional)
webView.clearCache(true)
// Load the HTML content from the "app.html" file
val htmlContent = try {
// Load the HTML content from the "app.html" file
assets.open("index.html").bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
// Return placeholder for error cases
"/* Error loading scripts */"
// If loading HTML fails, set some default content
"<html><body><h1>Error loading HTML</h1></body></html>"
}
val assetManager = assets
val scriptFiles = listOf(
"js/main.js",
//"scripts/selectTheme.js",
//"scripts/viewBitcoinPrice.js",
//"scripts/viewFiatToSats.js",
//"scripts/selectCurrency.js",
//"scripts/calcSatsToFiat.js"
)
val scriptContents = scriptFiles.map { fileName ->
try {
assetManager.open(fileName).bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
"/* Error loading script: $fileName */"
}
private fun loadWebView() {
loadScripts()
webView.loadUrl("file:///android_asset/index.html")
}
val cssFiles = listOf(
"style/output.css"
//"themes/Dark.css",
//"themes/Midnight.css",
//"themes/Lava.css",
//"themes/Solar.css",
//"styles/settings.css",
//"styles/bitcoinPrice.css",
//"styles/exchange.css",
//"styles/fiatToSats.css",
//"styles/satsToFiat.css"
) // List of your CSS files
val cssCode = cssFiles.joinToString("\n") { fileName ->
try {
assetManager.open(fileName).bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
"/* Error loading $fileName */"
}
}
val javaScriptCode = scriptContents.joinToString("\n")
// Combine the HTML, CSS, and JavaScript code
val finalHtmlContent = """
<html>
<head>
<style type="text/css">$cssCode</style>
</head>
<body>
$htmlContent
<script> type="module" $javaScriptCode</script>
</body>
</html>
""".trimIndent()
// Define the base URL for resolving relative paths (e.g., image source)
val baseUrl = "file:///android_asset/"
webView.loadDataWithBaseURL(null, finalHtmlContent, "text/html", "UTF-8", null)
}
}