From 0af28cce8cb7aaedd56011d81c9ddcec9b5d7ccb Mon Sep 17 00:00:00 2001 From: TwistedUmbrellaX Date: Sat, 6 Apr 2024 17:30:58 -0400 Subject: [PATCH] feat: add per-widget configuration --- .../statistics/ProfileStatsConfigure.kt | 32 ++++++++----------- .../widgets/statistics/ProfileStatsWidget.kt | 8 +++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsConfigure.kt b/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsConfigure.kt index b3ddec46..38509477 100644 --- a/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsConfigure.kt +++ b/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsConfigure.kt @@ -60,7 +60,11 @@ class ProfileStatsConfigure : AppCompatActivity(), binding = StatisticsWidgetConfigureBinding.inflate(layoutInflater) setContentView(binding.root) - val prefs = getSharedPreferences(ProfileStatsWidget.PREFS_NAME, Context.MODE_PRIVATE) + appWidgetId = intent.getIntExtra( + AppWidgetManager.EXTRA_APPWIDGET_ID, + AppWidgetManager.INVALID_APPWIDGET_ID + ) + val prefs = getSharedPreferences(ProfileStatsWidget.getPrefsName(appWidgetId), Context.MODE_PRIVATE) val topBackground = prefs.getInt(ProfileStatsWidget.PREF_BACKGROUND_COLOR, Color.parseColor("#80000000")) (binding.topBackgroundButton as MaterialButton).iconTint = ColorStateList.valueOf(topBackground) binding.topBackgroundButton.setOnClickListener { @@ -192,7 +196,7 @@ class ProfileStatsConfigure : AppCompatActivity(), ) val subTextColor = typedValueOutline.data - getSharedPreferences(ProfileStatsWidget.PREFS_NAME, Context.MODE_PRIVATE).edit().apply { + getSharedPreferences(ProfileStatsWidget.getPrefsName(appWidgetId), Context.MODE_PRIVATE).edit().apply { putInt(ProfileStatsWidget.PREF_BACKGROUND_COLOR, backgroundColor) putInt(ProfileStatsWidget.PREF_BACKGROUND_FADE, backgroundColor) putInt(ProfileStatsWidget.PREF_TITLE_TEXT_COLOR, textColor) @@ -204,12 +208,13 @@ class ProfileStatsConfigure : AppCompatActivity(), override fun onResult(dialogTag: String, which: Int, extras: Bundle): Boolean { if (which == SimpleDialog.OnDialogResultListener.BUTTON_POSITIVE) { if (!isMonetEnabled) { + val prefs = getSharedPreferences( + ProfileStatsWidget.getPrefsName(appWidgetId), + Context.MODE_PRIVATE + ) when (dialogTag) { ProfileStatsWidget.PREF_BACKGROUND_COLOR -> { - getSharedPreferences( - ProfileStatsWidget.PREFS_NAME, - Context.MODE_PRIVATE - ).edit() + prefs.edit() .putInt( ProfileStatsWidget.PREF_BACKGROUND_COLOR, extras.getInt(SimpleColorDialog.COLOR) @@ -220,10 +225,7 @@ class ProfileStatsConfigure : AppCompatActivity(), } ProfileStatsWidget.PREF_BACKGROUND_FADE -> { - getSharedPreferences( - ProfileStatsWidget.PREFS_NAME, - Context.MODE_PRIVATE - ).edit() + prefs.edit() .putInt( ProfileStatsWidget.PREF_BACKGROUND_FADE, extras.getInt(SimpleColorDialog.COLOR) @@ -234,10 +236,7 @@ class ProfileStatsConfigure : AppCompatActivity(), } ProfileStatsWidget.PREF_TITLE_TEXT_COLOR -> { - getSharedPreferences( - ProfileStatsWidget.PREFS_NAME, - Context.MODE_PRIVATE - ).edit() + prefs.edit() .putInt( ProfileStatsWidget.PREF_TITLE_TEXT_COLOR, extras.getInt(SimpleColorDialog.COLOR) @@ -248,10 +247,7 @@ class ProfileStatsConfigure : AppCompatActivity(), } ProfileStatsWidget.PREF_STATS_TEXT_COLOR -> { - getSharedPreferences( - ProfileStatsWidget.PREFS_NAME, - Context.MODE_PRIVATE - ).edit() + prefs.edit() .putInt( ProfileStatsWidget.PREF_STATS_TEXT_COLOR, extras.getInt(SimpleColorDialog.COLOR) diff --git a/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsWidget.kt b/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsWidget.kt index 24ac05c8..c58fcdec 100644 --- a/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsWidget.kt +++ b/app/src/main/java/ani/dantotsu/widgets/statistics/ProfileStatsWidget.kt @@ -9,6 +9,7 @@ import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Color import android.graphics.drawable.GradientDrawable +import android.net.Uri import android.widget.RemoteViews import androidx.core.content.res.ResourcesCompat import ani.dantotsu.MainActivity @@ -89,7 +90,7 @@ class ProfileStatsWidget : AppWidgetProvider() { appWidgetId: Int ) { - val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + val prefs = context.getSharedPreferences(getPrefsName(appWidgetId), Context.MODE_PRIVATE) val backgroundColor = prefs.getInt(PREF_BACKGROUND_COLOR, Color.parseColor("#80000000")) val backgroundFade = prefs.getInt(PREF_BACKGROUND_FADE, Color.parseColor("#00000000")) @@ -133,6 +134,7 @@ class ProfileStatsWidget : AppWidgetProvider() { 1, Intent(context, ProfileStatsConfigure::class.java).apply { putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) + data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME)) }, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) @@ -248,7 +250,9 @@ class ProfileStatsWidget : AppWidgetProvider() { } } - const val PREFS_NAME = "ani.dantotsu.widgets.ResumableWidget" + fun getPrefsName(appWidgetId: Int): String { + return "ani.dantotsu.widgets.Statistics.${appWidgetId}" + } const val PREF_BACKGROUND_COLOR = "background_color" const val PREF_BACKGROUND_FADE = "background_fade" const val PREF_TITLE_TEXT_COLOR = "title_text_color"