saved preference working, not updating currency on change

This commit is contained in:
Chris kerr 2024-06-12 11:45:49 -04:00
parent f7505e8ac5
commit 17839630fd
2 changed files with 48 additions and 16 deletions

View File

@ -13,10 +13,23 @@ function updateBitcoinPrice(selectedCurrency) {
function handleThemeChange() {
var selectedTheme = document.getElementById("theme-select").value;
document.documentElement.setAttribute("data-theme", selectedTheme);
savePreference("theme", selectedTheme);
}
function handleCurrencyChange() {
var selectedCurrency = document.getElementById("currency-select").value;
savePreference("currency", selectedCurrency);
}
function savePreference(key, value) {
if (typeof Android !== 'undefined' && Android !== null) {
Android.savePreference(key, value);
}
}
// Event listener for theme select change
document.getElementById("theme-select").addEventListener("change", handleThemeChange);
document.getElementById("currency-select").addEventListener("change", handleCurrencyChange);
//function refreshBitcoinPrice(selectedCurrency) {
@ -65,22 +78,19 @@ function updateFiatToSatsConversion(selectedCurrency) {
}
document.addEventListener('DOMContentLoaded', () => {
const satsInputElement = document.getElementById('sats-input');
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
// Initially fetch Bitcoin price in USD
updateBitcoinPrice(selectedCurrency);
updateFiatToSatsConversion(selectedCurrency)
const savedTheme = (typeof Android !== 'undefined' && Android !== null) ? Android.getPreference("theme") : null;
if (savedTheme) {
document.getElementById("theme-select").value = savedTheme;
document.documentElement.setAttribute("data-theme", savedTheme);
}
satsInputElement.addEventListener('input', updateSatsToDollarsConversion);
currencySelect.addEventListener('change', () => {
const selectedCurrency = currencySelect.value;
updateBitcoinPrice(selectedCurrency);
updateFiatToSatsConversion(selectedCurrency);
updateSatsToDollarsConversion(selectedCurrency);
updateFiatToSatsConversion(selectedCurrency)
});
const savedCurrency = (typeof Android !== 'undefined' && Android !== null) ? Android.getPreference("currency") : null;
if (savedCurrency) {
document.getElementById("currency-select").value = savedCurrency;
updateBitcoinPrice(savedCurrency);
updateFiatToSatsConversion(savedCurrency);
updateSatsToDollarsConversion(savedCurrency);
}
});

View File

@ -6,6 +6,8 @@ import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebSettings
import java.io.IOException
import android.content.Context
import android.webkit.JavascriptInterface
class MainActivity : AppCompatActivity() {
@ -23,6 +25,9 @@ class MainActivity : AppCompatActivity() {
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
// Add JavaScript interface
webView.addJavascriptInterface(WebAppInterface(this), "Android")
// Clear the WebView cache (optional)
webView.clearCache(true)
@ -84,3 +89,20 @@ class MainActivity : AppCompatActivity() {
webView.loadDataWithBaseURL(null, finalHtmlContent, "text/html", "UTF-8", null)
}
}
class WebAppInterface(private val context: Context) {
private val sharedPreferences = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE)
@JavascriptInterface
fun savePreference(key: String, value: String) {
with(sharedPreferences.edit()) {
putString(key, value)
apply()
}
}
@JavascriptInterface
fun getPreference(key: String): String? {
return sharedPreferences.getString(key, null)
}
}