From 1b3867598b3c74810c7e511016c34c3c2f999920 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 3 Mar 2020 11:56:52 -0600 Subject: [PATCH] Remove index from pickable interface --- .../com/todoroo/astrid/service/Upgrader.java | 34 ++++++++++++++++--- .../org/tasks/dialogs/ColorPalettePicker.kt | 16 ++++----- .../org/tasks/dialogs/ColorPickerAdapter.kt | 19 ++++------- .../java/org/tasks/themes/ThemeAccent.java | 9 ----- .../java/org/tasks/themes/ThemeColor.java | 16 ++------- .../main/java/org/tasks/ui/ChipProvider.java | 2 +- .../java/org/tasks/widget/TasksWidget.java | 2 +- .../org/tasks/widget/WidgetPreferences.java | 7 ++-- 8 files changed, 51 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/service/Upgrader.java b/app/src/main/java/com/todoroo/astrid/service/Upgrader.java index 4548044c4..111335f52 100644 --- a/app/src/main/java/com/todoroo/astrid/service/Upgrader.java +++ b/app/src/main/java/com/todoroo/astrid/service/Upgrader.java @@ -8,6 +8,7 @@ import static org.tasks.db.DbUtils.batch; import android.content.Context; import android.os.Environment; +import androidx.annotation.ColorRes; import androidx.core.content.ContextCompat; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; @@ -42,7 +43,6 @@ import org.tasks.data.UserActivityDao; import org.tasks.injection.ForApplication; import org.tasks.preferences.DefaultFilterProvider; import org.tasks.preferences.Preferences; -import org.tasks.themes.ThemeColor; public class Upgrader { @@ -148,9 +148,35 @@ public class Upgrader { } public static int getAndroidColor(Context context, int index) { - return index >= 0 && index < ThemeColor.COLORS.length - ? ContextCompat.getColor(context, ThemeColor.COLORS[index]) - : 0; + int legacyColor = getLegacyColor(index, 0); + return legacyColor == 0 ? 0 : ContextCompat.getColor(context, legacyColor); + } + + public static @ColorRes int getLegacyColor(int index, int def) { + switch (index) { + case 0: return R.color.blue_grey_500; + case 1: return R.color.grey_900; + case 2: return R.color.red_500; + case 3: return R.color.pink_500; + case 4: return R.color.purple_500; + case 5: return R.color.deep_purple_500; + case 6: return R.color.indigo_500; + case 7: return R.color.blue_500; + case 8: return R.color.light_blue_500; + case 9: return R.color.cyan_500; + case 10: return R.color.teal_500; + case 11: return R.color.green_500; + case 12: return R.color.light_green_500; + case 13: return R.color.lime_500; + case 14: return R.color.yellow_500; + case 15: return R.color.amber_500; + case 16: return R.color.orange_500; + case 17: return R.color.deep_orange_500; + case 18: return R.color.brown_500; + case 19: return R.color.grey_500; + case 20: return R.color.white_100; + default: return def; + } } private void applyCaldavSubtasks() { diff --git a/app/src/main/java/org/tasks/dialogs/ColorPalettePicker.kt b/app/src/main/java/org/tasks/dialogs/ColorPalettePicker.kt index 09b233ce6..16adde05a 100644 --- a/app/src/main/java/org/tasks/dialogs/ColorPalettePicker.kt +++ b/app/src/main/java/org/tasks/dialogs/ColorPalettePicker.kt @@ -60,7 +60,6 @@ class ColorPalettePicker : InjectingDialogFragment() { interface Pickable : Parcelable { val pickerColor: Int val isFree: Boolean - val index: Int } interface ColorPickedCallback { @@ -82,17 +81,17 @@ class ColorPalettePicker : InjectingDialogFragment() { ButterKnife.bind(this, view) palette = arguments!!.getSerializable(EXTRA_PALETTE) as Palette colors = when (palette) { - Palette.COLORS -> ThemeColor.COLORS.mapIndexed { index, color -> - ThemeColor(context, index, ContextCompat.getColor(context!!, color), true) + Palette.COLORS -> ThemeColor.COLORS.map { color -> + ThemeColor(context, ContextCompat.getColor(context!!, color), true) } Palette.ACCENTS -> ThemeAccent.ACCENTS.mapIndexed { index, _ -> ThemeAccent(context, index) } - Palette.LAUNCHERS -> ThemeColor.LAUNCHER_COLORS.mapIndexed { index, color -> - ThemeColor(context, index, ContextCompat.getColor(context!!, color), false) + Palette.LAUNCHERS -> ThemeColor.LAUNCHER_COLORS.map { color -> + ThemeColor(context, ContextCompat.getColor(context!!, color), false) } - Palette.WIDGET -> ThemeColor.COLORS.mapIndexed { index, color -> - ThemeColor(context, index, ContextCompat.getColor(context!!, color), false) + Palette.WIDGET -> ThemeColor.COLORS.map { color -> + ThemeColor(context, ContextCompat.getColor(context!!, color), false) } } @@ -134,8 +133,7 @@ class ColorPalettePicker : InjectingDialogFragment() { private fun onSelected(index: Int) { val result = when (palette) { - Palette.COLORS, Palette.WIDGET -> - (colors.find { it.index == index } as ThemeColor).originalColor + Palette.COLORS, Palette.WIDGET -> (colors[index] as ThemeColor).originalColor else -> index } dialog?.dismiss() diff --git a/app/src/main/java/org/tasks/dialogs/ColorPickerAdapter.kt b/app/src/main/java/org/tasks/dialogs/ColorPickerAdapter.kt index 39aa04d4f..e25b0145e 100644 --- a/app/src/main/java/org/tasks/dialogs/ColorPickerAdapter.kt +++ b/app/src/main/java/org/tasks/dialogs/ColorPickerAdapter.kt @@ -7,12 +7,13 @@ import androidx.recyclerview.widget.ListAdapter import org.tasks.Callback import org.tasks.R import org.tasks.billing.Inventory +import org.tasks.dialogs.ColorPalettePicker.Pickable class ColorPickerAdapter( private val activity: Activity, private val inventory: Inventory, private val onSelected: Callback -) : ListAdapter(DiffCallback()) { +) : ListAdapter(DiffCallback()) { enum class Palette { COLORS, @@ -30,7 +31,7 @@ class ColorPickerAdapter( val pickable = getItem(position) val available = inventory.purchasedThemes() || pickable.isFree holder.bind( - pickable.index, + position, if (available) R.drawable.color_picker else R.drawable.ic_outline_vpn_key_24px, pickable.pickerColor, 1f, @@ -38,19 +39,13 @@ class ColorPickerAdapter( ) } - private class DiffCallback : DiffUtil.ItemCallback() { - override fun areItemsTheSame( - oldItem: ColorPalettePicker.Pickable, - newItem: ColorPalettePicker.Pickable - ): Boolean { + private class DiffCallback : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: Pickable, newItem: Pickable): Boolean { return oldItem == newItem } - override fun areContentsTheSame( - oldItem: ColorPalettePicker.Pickable, - newItem: ColorPalettePicker.Pickable - ): Boolean { - return oldItem.index == newItem.index + override fun areContentsTheSame(oldItem: Pickable, newItem: Pickable): Boolean { + return true } } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/themes/ThemeAccent.java b/app/src/main/java/org/tasks/themes/ThemeAccent.java index f1f819c6a..a54ffe0a6 100644 --- a/app/src/main/java/org/tasks/themes/ThemeAccent.java +++ b/app/src/main/java/org/tasks/themes/ThemeAccent.java @@ -43,19 +43,16 @@ public class ThemeAccent implements Pickable { return new ThemeAccent[size]; } }; - private final int index; private final int style; @Deprecated private final int accentColor; public ThemeAccent(Context context, int index) { - this.index = index; this.style = ACCENTS[index]; Resources.Theme theme = new ContextThemeWrapper(context, ThemeAccent.ACCENTS[index]).getTheme(); this.accentColor = resolveAttribute(theme, R.attr.colorSecondary); } private ThemeAccent(Parcel source) { - index = source.readInt(); style = source.readInt(); accentColor = source.readInt(); } @@ -86,11 +83,6 @@ public class ThemeAccent implements Pickable { } } - @Override - public int getIndex() { - return index; - } - @Deprecated public int getAccentColor() { return accentColor; @@ -103,7 +95,6 @@ public class ThemeAccent implements Pickable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(index); dest.writeInt(style); dest.writeInt(accentColor); } diff --git a/app/src/main/java/org/tasks/themes/ThemeColor.java b/app/src/main/java/org/tasks/themes/ThemeColor.java index 6f78655f0..3fc029f7c 100644 --- a/app/src/main/java/org/tasks/themes/ThemeColor.java +++ b/app/src/main/java/org/tasks/themes/ThemeColor.java @@ -172,7 +172,6 @@ public class ThemeColor implements Pickable { colorMap.put(-1, -16777216); // white & black } - private final int index; private final int original; private final int colorOnPrimary; private final int colorPrimary; @@ -180,16 +179,14 @@ public class ThemeColor implements Pickable { private final boolean isDark; public static ThemeColor getLauncherColor(Context context, int index) { - return new ThemeColor( - context, index, ContextCompat.getColor(context, LAUNCHER_COLORS[index]), false); + return new ThemeColor(context, ContextCompat.getColor(context, LAUNCHER_COLORS[index]), false); } public ThemeColor(Context context, int color) { - this(context, -1, color == 0 ? ContextCompat.getColor(context, R.color.blue_500) : color, true); + this(context, color == 0 ? ContextCompat.getColor(context, R.color.blue_500) : color, true); } - public ThemeColor(Context context, int index, int color, boolean adjustColor) { - this.index = index; + public ThemeColor(Context context, int color, boolean adjustColor) { color |= 0xFF000000; // remove alpha original = color; if (adjustColor && context.getResources().getBoolean(R.bool.is_dark)) { @@ -216,7 +213,6 @@ public class ThemeColor implements Pickable { } private ThemeColor(Parcel source) { - index = source.readInt(); colorOnPrimary = source.readInt(); colorPrimary = source.readInt(); colorPrimaryVariant = source.readInt(); @@ -349,11 +345,6 @@ public class ThemeColor implements Pickable { } } - @Override - public int getIndex() { - return index; - } - public int getOriginalColor() { return original; } @@ -381,7 +372,6 @@ public class ThemeColor implements Pickable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(index); dest.writeInt(colorOnPrimary); dest.writeInt(colorPrimary); dest.writeInt(colorPrimaryVariant); diff --git a/app/src/main/java/org/tasks/ui/ChipProvider.java b/app/src/main/java/org/tasks/ui/ChipProvider.java index 96e359e42..c2ab53752 100644 --- a/app/src/main/java/org/tasks/ui/ChipProvider.java +++ b/app/src/main/java/org/tasks/ui/ChipProvider.java @@ -222,6 +222,6 @@ public class ChipProvider { private static ThemeColor getUntaggedThemeColor(Context context) { return new ThemeColor( - context, -1, ContextCompat.getColor(context, R.color.tag_color_none_background), false); + context, ContextCompat.getColor(context, R.color.tag_color_none_background), false); } } diff --git a/app/src/main/java/org/tasks/widget/TasksWidget.java b/app/src/main/java/org/tasks/widget/TasksWidget.java index 7b77a8034..381cbf23c 100644 --- a/app/src/main/java/org/tasks/widget/TasksWidget.java +++ b/app/src/main/java/org/tasks/widget/TasksWidget.java @@ -73,7 +73,7 @@ public class TasksWidget extends InjectingAppWidgetProvider { Intent rvIntent = new Intent(context, ScrollableWidgetUpdateService.class); rvIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id); rvIntent.setData(Uri.parse(rvIntent.toUri(Intent.URI_INTENT_SCHEME))); - ThemeColor color = new ThemeColor(context, -1, widgetPreferences.getColor(), false); + ThemeColor color = new ThemeColor(context, widgetPreferences.getColor(), false); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.scrollable_widget); if (atLeastJellybeanMR1()) { remoteViews.setInt(R.id.widget, "setLayoutDirection", locale.getDirectionality()); diff --git a/app/src/main/java/org/tasks/widget/WidgetPreferences.java b/app/src/main/java/org/tasks/widget/WidgetPreferences.java index 1c20a3d41..606910d71 100644 --- a/app/src/main/java/org/tasks/widget/WidgetPreferences.java +++ b/app/src/main/java/org/tasks/widget/WidgetPreferences.java @@ -2,9 +2,9 @@ package org.tasks.widget; import android.content.Context; import androidx.core.content.ContextCompat; +import com.todoroo.astrid.service.Upgrader; import org.tasks.R; import org.tasks.preferences.Preferences; -import org.tasks.themes.ThemeColor; public class WidgetPreferences { @@ -52,10 +52,7 @@ public class WidgetPreferences { return color; } int index = preferences.getInt(getKey(R.string.p_widget_color), -1); - if (index < 0 || index > ThemeColor.COLORS.length) { - index = 7; - } - color = ContextCompat.getColor(context, ThemeColor.COLORS[index]); + color = ContextCompat.getColor(context, Upgrader.getLegacyColor(index, R.color.blue_500)); preferences.setInt(getKey(R.string.p_widget_color_v2), color); return color; }