Remove index from pickable interface

pull/935/head
Alex Baker 5 years ago
parent 4030312820
commit 1b3867598b

@ -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() {

@ -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()

@ -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<Int>
) : ListAdapter<ColorPalettePicker.Pickable, IconPickerHolder>(DiffCallback()) {
) : ListAdapter<Pickable, IconPickerHolder>(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<ColorPalettePicker.Pickable>() {
override fun areItemsTheSame(
oldItem: ColorPalettePicker.Pickable,
newItem: ColorPalettePicker.Pickable
): Boolean {
private class DiffCallback : DiffUtil.ItemCallback<Pickable>() {
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
}
}
}

@ -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);
}

@ -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);

@ -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);
}
}

@ -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());

@ -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;
}

Loading…
Cancel
Save