saved preference working, not updating currency on change
This commit is contained in:
parent
f7505e8ac5
commit
17839630fd
@ -13,10 +13,23 @@ function updateBitcoinPrice(selectedCurrency) {
|
|||||||
function handleThemeChange() {
|
function handleThemeChange() {
|
||||||
var selectedTheme = document.getElementById("theme-select").value;
|
var selectedTheme = document.getElementById("theme-select").value;
|
||||||
document.documentElement.setAttribute("data-theme", selectedTheme);
|
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
|
// Event listener for theme select change
|
||||||
document.getElementById("theme-select").addEventListener("change", handleThemeChange);
|
document.getElementById("theme-select").addEventListener("change", handleThemeChange);
|
||||||
|
document.getElementById("currency-select").addEventListener("change", handleCurrencyChange);
|
||||||
|
|
||||||
|
|
||||||
//function refreshBitcoinPrice(selectedCurrency) {
|
//function refreshBitcoinPrice(selectedCurrency) {
|
||||||
@ -65,22 +78,19 @@ function updateFiatToSatsConversion(selectedCurrency) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const satsInputElement = document.getElementById('sats-input');
|
const savedTheme = (typeof Android !== 'undefined' && Android !== null) ? Android.getPreference("theme") : null;
|
||||||
const currencySelect = document.getElementById('currency-select');
|
if (savedTheme) {
|
||||||
const selectedCurrency = currencySelect.value;
|
document.getElementById("theme-select").value = savedTheme;
|
||||||
// Initially fetch Bitcoin price in USD
|
document.documentElement.setAttribute("data-theme", savedTheme);
|
||||||
updateBitcoinPrice(selectedCurrency);
|
}
|
||||||
updateFiatToSatsConversion(selectedCurrency)
|
|
||||||
|
|
||||||
satsInputElement.addEventListener('input', updateSatsToDollarsConversion);
|
const savedCurrency = (typeof Android !== 'undefined' && Android !== null) ? Android.getPreference("currency") : null;
|
||||||
|
if (savedCurrency) {
|
||||||
currencySelect.addEventListener('change', () => {
|
document.getElementById("currency-select").value = savedCurrency;
|
||||||
const selectedCurrency = currencySelect.value;
|
updateBitcoinPrice(savedCurrency);
|
||||||
updateBitcoinPrice(selectedCurrency);
|
updateFiatToSatsConversion(savedCurrency);
|
||||||
updateFiatToSatsConversion(selectedCurrency);
|
updateSatsToDollarsConversion(savedCurrency);
|
||||||
updateSatsToDollarsConversion(selectedCurrency);
|
}
|
||||||
updateFiatToSatsConversion(selectedCurrency)
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ import android.os.Bundle
|
|||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
import android.webkit.WebSettings
|
import android.webkit.WebSettings
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import android.content.Context
|
||||||
|
import android.webkit.JavascriptInterface
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@ -23,6 +25,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val webSettings: WebSettings = webView.settings
|
val webSettings: WebSettings = webView.settings
|
||||||
webSettings.javaScriptEnabled = true
|
webSettings.javaScriptEnabled = true
|
||||||
|
|
||||||
|
// Add JavaScript interface
|
||||||
|
webView.addJavascriptInterface(WebAppInterface(this), "Android")
|
||||||
|
|
||||||
// Clear the WebView cache (optional)
|
// Clear the WebView cache (optional)
|
||||||
webView.clearCache(true)
|
webView.clearCache(true)
|
||||||
|
|
||||||
@ -84,3 +89,20 @@ class MainActivity : AppCompatActivity() {
|
|||||||
webView.loadDataWithBaseURL(null, finalHtmlContent, "text/html", "UTF-8", null)
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user