mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-12 18:51:21 +01:00
FIX: Android widget was hardcoded to USD
This commit is contained in:
parent
1ec38c915b
commit
ccdf39bfd5
3 changed files with 80 additions and 17 deletions
|
@ -28,8 +28,8 @@ class BitcoinPriceWidget : AppWidgetProvider() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clearCache(context: Context) {
|
private fun clearCache(context: Context) {
|
||||||
val sharedPref = context.getSharedPreferences("widget_prefs", Context.MODE_PRIVATE)
|
val sharedPref = context.getSharedPreferences("group.io.bluewallet.bluewallet", Context.MODE_PRIVATE)
|
||||||
sharedPref.edit().clear().apply()
|
sharedPref.edit().clear().apply() // Clear all preferences in the group
|
||||||
Log.d("BitcoinPriceWidget", "Cache cleared")
|
Log.d("BitcoinPriceWidget", "Cache cleared from group.io.bluewallet.bluewallet")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,6 +8,7 @@ import android.util.Log
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
import androidx.work.*
|
import androidx.work.*
|
||||||
|
import java.text.DecimalFormatSymbols
|
||||||
import java.text.NumberFormat
|
import java.text.NumberFormat
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -33,17 +34,22 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private lateinit var sharedPref: SharedPreferences
|
||||||
|
private lateinit var preferenceChangeListener: SharedPreferences.OnSharedPreferenceChangeListener
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
Log.d(TAG, "Widget update worker running")
|
Log.d(TAG, "Widget update worker running")
|
||||||
|
|
||||||
|
sharedPref = applicationContext.getSharedPreferences("group.io.bluewallet.bluewallet", Context.MODE_PRIVATE)
|
||||||
|
registerPreferenceChangeListener()
|
||||||
|
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(applicationContext)
|
val appWidgetManager = AppWidgetManager.getInstance(applicationContext)
|
||||||
val thisWidget = ComponentName(applicationContext, BitcoinPriceWidget::class.java)
|
val thisWidget = ComponentName(applicationContext, BitcoinPriceWidget::class.java)
|
||||||
val appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
|
val appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
|
||||||
val views = RemoteViews(applicationContext.packageName, R.layout.widget_layout)
|
val views = RemoteViews(applicationContext.packageName, R.layout.widget_layout)
|
||||||
|
|
||||||
val sharedPref = applicationContext.getSharedPreferences("widget_prefs", Context.MODE_PRIVATE)
|
val preferredCurrency = sharedPref.getString("preferredCurrency", null) ?: "USD"
|
||||||
val preferredCurrency = sharedPref.getString("preferredCurrency", "USD")
|
val preferredCurrencyLocale = sharedPref.getString("preferredCurrencyLocale", null) ?: "en-US"
|
||||||
val preferredCurrencyLocale = sharedPref.getString("preferredCurrencyLocale", "en-US")
|
|
||||||
val previousPrice = sharedPref.getString("previous_price", null)
|
val previousPrice = sharedPref.getString("previous_price", null)
|
||||||
|
|
||||||
val currentTime = SimpleDateFormat("hh:mm a", Locale.getDefault()).format(Date())
|
val currentTime = SimpleDateFormat("hh:mm a", Locale.getDefault()).format(Date())
|
||||||
|
@ -51,13 +57,47 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
fetchPrice(preferredCurrency) { fetchedPrice, error ->
|
fetchPrice(preferredCurrency) { fetchedPrice, error ->
|
||||||
handlePriceResult(
|
handlePriceResult(
|
||||||
appWidgetManager, appWidgetIds, views, sharedPref,
|
appWidgetManager, appWidgetIds, views, sharedPref,
|
||||||
fetchedPrice, previousPrice, currentTime, preferredCurrencyLocale, error
|
fetchedPrice, previousPrice, currentTime, preferredCurrency, preferredCurrencyLocale, error
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result.success()
|
return Result.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun registerPreferenceChangeListener() {
|
||||||
|
preferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||||
|
if (key == "preferredCurrency" || key == "preferredCurrencyLocale" || key == "previous_price") {
|
||||||
|
Log.d(TAG, "Preference changed: $key")
|
||||||
|
updateWidgetOnPreferenceChange()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sharedPref.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStopped() {
|
||||||
|
super.onStopped()
|
||||||
|
sharedPref.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateWidgetOnPreferenceChange() {
|
||||||
|
val appWidgetManager = AppWidgetManager.getInstance(applicationContext)
|
||||||
|
val thisWidget = ComponentName(applicationContext, BitcoinPriceWidget::class.java)
|
||||||
|
val appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
|
||||||
|
val views = RemoteViews(applicationContext.packageName, R.layout.widget_layout)
|
||||||
|
|
||||||
|
val preferredCurrency = sharedPref.getString("preferredCurrency", null) ?: "USD"
|
||||||
|
val preferredCurrencyLocale = sharedPref.getString("preferredCurrencyLocale", null) ?: "en-US"
|
||||||
|
val previousPrice = sharedPref.getString("previous_price", null)
|
||||||
|
val currentTime = SimpleDateFormat("hh:mm a", Locale.getDefault()).format(Date())
|
||||||
|
|
||||||
|
fetchPrice(preferredCurrency) { fetchedPrice, error ->
|
||||||
|
handlePriceResult(
|
||||||
|
appWidgetManager, appWidgetIds, views, sharedPref,
|
||||||
|
fetchedPrice, previousPrice, currentTime, preferredCurrency, preferredCurrencyLocale, error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun handlePriceResult(
|
private fun handlePriceResult(
|
||||||
appWidgetManager: AppWidgetManager,
|
appWidgetManager: AppWidgetManager,
|
||||||
appWidgetIds: IntArray,
|
appWidgetIds: IntArray,
|
||||||
|
@ -66,6 +106,7 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
fetchedPrice: String?,
|
fetchedPrice: String?,
|
||||||
previousPrice: String?,
|
previousPrice: String?,
|
||||||
currentTime: String,
|
currentTime: String,
|
||||||
|
preferredCurrency: String?,
|
||||||
preferredCurrencyLocale: String?,
|
preferredCurrencyLocale: String?,
|
||||||
error: String?
|
error: String?
|
||||||
) {
|
) {
|
||||||
|
@ -77,11 +118,11 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
if (!isPriceCached) {
|
if (!isPriceCached) {
|
||||||
showLoadingError(views)
|
showLoadingError(views)
|
||||||
} else {
|
} else {
|
||||||
displayCachedPrice(views, previousPrice, currentTime, preferredCurrencyLocale)
|
displayCachedPrice(views, previousPrice, currentTime, preferredCurrency, preferredCurrencyLocale)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
displayFetchedPrice(
|
displayFetchedPrice(
|
||||||
views, fetchedPrice!!, previousPrice, currentTime, preferredCurrencyLocale
|
views, fetchedPrice!!, previousPrice, currentTime, preferredCurrency, preferredCurrencyLocale
|
||||||
)
|
)
|
||||||
savePrice(sharedPref, fetchedPrice)
|
savePrice(sharedPref, fetchedPrice)
|
||||||
}
|
}
|
||||||
|
@ -103,11 +144,10 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
views: RemoteViews,
|
views: RemoteViews,
|
||||||
previousPrice: String?,
|
previousPrice: String?,
|
||||||
currentTime: String,
|
currentTime: String,
|
||||||
|
preferredCurrency: String?,
|
||||||
preferredCurrencyLocale: String?
|
preferredCurrencyLocale: String?
|
||||||
) {
|
) {
|
||||||
val currencyFormat = NumberFormat.getCurrencyInstance(Locale.forLanguageTag(preferredCurrencyLocale!!)).apply {
|
val currencyFormat = getCurrencyFormat(preferredCurrency, preferredCurrencyLocale)
|
||||||
maximumFractionDigits = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
views.apply {
|
views.apply {
|
||||||
setViewVisibility(R.id.loading_indicator, View.GONE)
|
setViewVisibility(R.id.loading_indicator, View.GONE)
|
||||||
|
@ -125,12 +165,11 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
fetchedPrice: String,
|
fetchedPrice: String,
|
||||||
previousPrice: String?,
|
previousPrice: String?,
|
||||||
currentTime: String,
|
currentTime: String,
|
||||||
|
preferredCurrency: String?,
|
||||||
preferredCurrencyLocale: String?
|
preferredCurrencyLocale: String?
|
||||||
) {
|
) {
|
||||||
val currentPrice = fetchedPrice.toDouble().let { it.toInt() } // Remove cents
|
val currentPrice = fetchedPrice.toDouble().toInt() // Remove cents
|
||||||
val currencyFormat = NumberFormat.getCurrencyInstance(Locale.forLanguageTag(preferredCurrencyLocale!!)).apply {
|
val currencyFormat = getCurrencyFormat(preferredCurrency, preferredCurrencyLocale)
|
||||||
maximumFractionDigits = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
views.apply {
|
views.apply {
|
||||||
setViewVisibility(R.id.loading_indicator, View.GONE)
|
setViewVisibility(R.id.loading_indicator, View.GONE)
|
||||||
|
@ -153,6 +192,30 @@ class WidgetUpdateWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getCurrencyFormat(currencyCode: String?, localeString: String?): NumberFormat {
|
||||||
|
val localeParts = localeString?.split("-") ?: listOf("en", "US")
|
||||||
|
val locale = if (localeParts.size == 2) {
|
||||||
|
Locale(localeParts[0], localeParts[1])
|
||||||
|
} else {
|
||||||
|
Locale.getDefault()
|
||||||
|
}
|
||||||
|
val currencyFormat = NumberFormat.getCurrencyInstance(locale)
|
||||||
|
val currency = try {
|
||||||
|
Currency.getInstance(currencyCode ?: "USD")
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
Currency.getInstance("USD") // Default to USD if an invalid code is provided
|
||||||
|
}
|
||||||
|
currencyFormat.currency = currency
|
||||||
|
currencyFormat.maximumFractionDigits = 0 // No cents
|
||||||
|
|
||||||
|
// Remove the ISO country code and keep only the symbol
|
||||||
|
val decimalFormatSymbols = (currencyFormat as java.text.DecimalFormat).decimalFormatSymbols
|
||||||
|
decimalFormatSymbols.currencySymbol = currency.symbol
|
||||||
|
currencyFormat.decimalFormatSymbols = decimalFormatSymbols
|
||||||
|
|
||||||
|
return currencyFormat
|
||||||
|
}
|
||||||
|
|
||||||
private fun fetchPrice(currency: String?, callback: (String?, String?) -> Unit) {
|
private fun fetchPrice(currency: String?, callback: (String?, String?) -> Unit) {
|
||||||
val price = MarketAPI.fetchPrice(applicationContext, currency ?: "USD")
|
val price = MarketAPI.fetchPrice(applicationContext, currency ?: "USD")
|
||||||
if (price == null) {
|
if (price == null) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:initialLayout="@layout/widget_layout"
|
android:initialLayout="@layout/widget_layout"
|
||||||
android:minWidth="160dp"
|
android:minWidth="160dp"
|
||||||
android:minHeight="80dp"
|
android:minHeight="100dp"
|
||||||
android:updatePeriodMillis="0"
|
android:updatePeriodMillis="0"
|
||||||
android:widgetCategory="home_screen"
|
android:widgetCategory="home_screen"
|
||||||
android:previewImage="@drawable/widget_preview"
|
android:previewImage="@drawable/widget_preview"
|
||||||
|
|
Loading…
Add table
Reference in a new issue