Update BitcoinPriceWidget.kt

This commit is contained in:
Marcos Rodriguez Velez 2025-03-01 10:44:15 -04:00
parent 07b93d521d
commit 9507a48314

View file

@ -12,7 +12,6 @@ class BitcoinPriceWidget : AppWidgetProvider() {
companion object { companion object {
private const val TAG = "BitcoinPriceWidget" private const val TAG = "BitcoinPriceWidget"
private const val SHARED_PREF_NAME = "group.io.bluewallet.bluewallet" private const val SHARED_PREF_NAME = "group.io.bluewallet.bluewallet"
private const val WIDGET_COUNT_KEY = "widget_count"
} }
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
@ -25,16 +24,12 @@ class BitcoinPriceWidget : AppWidgetProvider() {
override fun onEnabled(context: Context) { override fun onEnabled(context: Context) {
super.onEnabled(context) super.onEnabled(context)
val sharedPref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
val widgetCount = sharedPref.getInt(WIDGET_COUNT_KEY, 0)
sharedPref.edit().putInt(WIDGET_COUNT_KEY, widgetCount + 1).apply()
Log.d(TAG, "onEnabled called") Log.d(TAG, "onEnabled called")
WidgetUpdateWorker.scheduleWork(context) WidgetUpdateWorker.scheduleWork(context)
} }
override fun onDisabled(context: Context) { override fun onDisabled(context: Context) {
super.onDisabled(context) super.onDisabled(context)
clearWidgetCount(context)
Log.d(TAG, "onDisabled called") Log.d(TAG, "onDisabled called")
clearCache(context) clearCache(context)
WorkManager.getInstance(context).cancelUniqueWork(WidgetUpdateWorker.WORK_NAME) WorkManager.getInstance(context).cancelUniqueWork(WidgetUpdateWorker.WORK_NAME)
@ -42,19 +37,9 @@ class BitcoinPriceWidget : AppWidgetProvider() {
override fun onDeleted(context: Context, appWidgetIds: IntArray) { override fun onDeleted(context: Context, appWidgetIds: IntArray) {
super.onDeleted(context, appWidgetIds) super.onDeleted(context, appWidgetIds)
val sharedPref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
val widgetCount = sharedPref.getInt(WIDGET_COUNT_KEY, 1)
val newCount = widgetCount - appWidgetIds.size
sharedPref.edit().putInt(WIDGET_COUNT_KEY, if (newCount >= 0) newCount else 0).apply()
Log.d(TAG, "onDeleted called for widgets: ${appWidgetIds.joinToString()}") Log.d(TAG, "onDeleted called for widgets: ${appWidgetIds.joinToString()}")
} }
private fun clearWidgetCount(context: Context) {
val sharedPref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
sharedPref.edit().putInt(WIDGET_COUNT_KEY, 0).apply()
Log.d(TAG, "Widget count reset to 0")
}
private fun clearCache(context: Context) { private fun clearCache(context: Context) {
val sharedPref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE) val sharedPref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
sharedPref.edit().clear().apply() sharedPref.edit().clear().apply()