From b8c98aa2f137724714287225b57c121560ff40df Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 11 Mar 2020 08:54:35 -0500 Subject: [PATCH] Add ColorProvider --- .../todoroo/astrid/activity/MainActivity.java | 5 +- .../astrid/activity/TaskListFragment.java | 5 +- .../todoroo/astrid/adapter/FilterAdapter.java | 13 ++- .../astrid/adapter/FilterViewHolder.java | 7 +- .../adapter/NavigationDrawerAdapter.java | 11 ++- app/src/main/java/org/tasks/Tasks.java | 2 - .../activities/BaseListSettingsActivity.java | 5 +- .../org/tasks/dialogs/ColorPalettePicker.kt | 12 +-- .../org/tasks/injection/ActivityModule.java | 5 +- .../preferences/fragments/LookAndFeel.kt | 1 - .../org/tasks/tags/TagPickerActivity.java | 4 +- .../org/tasks/tags/TagRecyclerAdapter.java | 17 +++- .../java/org/tasks/themes/ColorProvider.kt | 88 +++++++++++++++++ .../java/org/tasks/themes/ThemeColor.java | 98 ++----------------- .../main/java/org/tasks/ui/ChipListCache.java | 76 ++++++++++++++ .../main/java/org/tasks/ui/ChipProvider.java | 86 ++++------------ .../org/tasks/ui/EmptyTaskEditFragment.kt | 12 ++- .../java/org/tasks/widget/TasksWidget.java | 2 +- 18 files changed, 258 insertions(+), 191 deletions(-) create mode 100644 app/src/main/java/org/tasks/themes/ColorProvider.kt create mode 100644 app/src/main/java/org/tasks/ui/ChipListCache.java diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.java b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.java index bc2274e06..d260ecd5f 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.java +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.java @@ -13,7 +13,6 @@ import static com.todoroo.andlib.utility.AndroidUtilities.atLeastNougat; import static com.todoroo.astrid.activity.TaskEditFragment.newTaskEditFragment; import static com.todoroo.astrid.activity.TaskListFragment.newTaskListFragment; import static org.tasks.tasklist.ActionUtils.applySupportActionModeColor; -import static org.tasks.themes.ThemeColor.newThemeColor; import static org.tasks.ui.NavigationDrawerFragment.REQUEST_NEW_LIST; import android.annotation.SuppressLint; @@ -54,6 +53,7 @@ import org.tasks.intents.TaskIntents; import org.tasks.preferences.DefaultFilterProvider; import org.tasks.preferences.Preferences; import org.tasks.receivers.RepeatConfirmationReceiver; +import org.tasks.themes.ColorProvider; import org.tasks.themes.Theme; import org.tasks.themes.ThemeColor; import org.tasks.ui.DeadlineControlSet; @@ -92,6 +92,7 @@ public class MainActivity extends InjectingAppCompatActivity @Inject TaskCreator taskCreator; @Inject PlayServices playServices; @Inject Inventory inventory; + @Inject ColorProvider colorProvider; private CompositeDisposable disposables; private NavigationDrawerFragment navigationDrawer; @@ -302,7 +303,7 @@ public class MainActivity extends InjectingAppCompatActivity private ThemeColor getFilterColor() { return filter != null && filter.tint != 0 - ? newThemeColor(this, filter.tint) + ? colorProvider.getThemeColor(filter.tint, true) : theme.getThemeColor(); } diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java index e02902791..d639e91a2 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java @@ -12,7 +12,6 @@ import static com.google.common.collect.Lists.newArrayList; import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread; import static org.tasks.activities.RemoteListPicker.newRemoteListSupportPicker; import static org.tasks.caldav.CaldavCalendarSettingsActivity.EXTRA_CALDAV_CALENDAR; -import static org.tasks.themes.ThemeColor.newThemeColor; import static org.tasks.ui.CheckBoxes.getPriorityColor; import android.app.Activity; @@ -103,6 +102,7 @@ import org.tasks.tasklist.DragAndDropRecyclerAdapter; import org.tasks.tasklist.PagedListRecyclerAdapter; import org.tasks.tasklist.TaskListRecyclerAdapter; import org.tasks.tasklist.ViewHolderFactory; +import org.tasks.themes.ColorProvider; import org.tasks.themes.ThemeColor; import org.tasks.ui.TaskListViewModel; import org.tasks.ui.Toaster; @@ -153,6 +153,7 @@ public final class TaskListFragment extends InjectingFragment @Inject TagDataDao tagDataDao; @Inject CaldavDao caldavDao; @Inject ThemeColor defaultThemeColor; + @Inject ColorProvider colorProvider; @BindView(R.id.swipe_layout) SwipeRefreshLayout swipeRefreshLayout; @@ -263,7 +264,7 @@ public final class TaskListFragment extends InjectingFragment filter = getFilter(); - themeColor = filter.tint != 0 ? newThemeColor(context, filter.tint) : defaultThemeColor; + themeColor = filter.tint != 0 ? colorProvider.getThemeColor(filter.tint, true) : defaultThemeColor; filter.setFilterQueryOverride(null); diff --git a/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java b/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java index 3f5904f63..2ce4b4e7e 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java +++ b/app/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java @@ -25,6 +25,7 @@ import javax.inject.Inject; import org.tasks.billing.Inventory; import org.tasks.filters.NavigationDrawerSubheader; import org.tasks.locale.Locale; +import org.tasks.themes.ColorProvider; import org.tasks.themes.Theme; import org.tasks.themes.ThemeAccent; @@ -37,16 +38,23 @@ public class FilterAdapter extends BaseAdapter { private final ThemeAccent accent; private final Locale locale; private final Inventory inventory; + private final ColorProvider colorProvider; private final LayoutInflater inflater; private Filter selected = null; private List items = new ArrayList<>(); @Inject - public FilterAdapter(Activity activity, Theme theme, Locale locale, Inventory inventory) { + public FilterAdapter( + Activity activity, + Theme theme, + Locale locale, + Inventory inventory, + ColorProvider colorProvider) { this.activity = activity; this.accent = theme.getThemeAccent(); this.locale = locale; this.inventory = inventory; + this.colorProvider = colorProvider; this.inflater = theme.getLayoutInflater(activity); } @@ -101,7 +109,8 @@ public class FilterAdapter extends BaseAdapter { switch (viewType) { case ITEM: viewHolder = - new FilterViewHolder(convertView, accent, false, locale, activity, inventory, null); + new FilterViewHolder( + convertView, accent, false, locale, activity, inventory, colorProvider, null); break; case SEPARATOR: viewHolder = new FilterViewHolder(convertView); diff --git a/app/src/main/java/com/todoroo/astrid/adapter/FilterViewHolder.java b/app/src/main/java/com/todoroo/astrid/adapter/FilterViewHolder.java index ca93f8338..d025516bb 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/FilterViewHolder.java +++ b/app/src/main/java/com/todoroo/astrid/adapter/FilterViewHolder.java @@ -1,7 +1,6 @@ package com.todoroo.astrid.adapter; import static com.todoroo.andlib.utility.AndroidUtilities.preLollipop; -import static org.tasks.themes.ThemeColor.newThemeColor; import android.app.Activity; import android.content.Intent; @@ -29,6 +28,7 @@ import org.tasks.billing.Inventory; import org.tasks.filters.NavigationDrawerSubheader; import org.tasks.locale.Locale; import org.tasks.preferences.SyncPreferences; +import org.tasks.themes.ColorProvider; import org.tasks.themes.CustomIcons; import org.tasks.themes.DrawableUtil; import org.tasks.themes.ThemeAccent; @@ -56,6 +56,7 @@ public class FilterViewHolder extends RecyclerView.ViewHolder { private Activity activity; private View itemView; private Inventory inventory; + private ColorProvider colorProvider; FilterViewHolder( @NonNull View itemView, @@ -64,9 +65,11 @@ public class FilterViewHolder extends RecyclerView.ViewHolder { Locale locale, Activity activity, Inventory inventory, + ColorProvider colorProvider, OnClick onClick) { super(itemView); this.inventory = inventory; + this.colorProvider = colorProvider; ButterKnife.bind(this, itemView); @@ -135,7 +138,7 @@ public class FilterViewHolder extends RecyclerView.ViewHolder { private int getColor(FilterListItem filter) { if (filter.tint != 0) { - ThemeColor color = newThemeColor(activity, filter.tint); + ThemeColor color = colorProvider.getThemeColor(filter.tint, true); if (color.isFree() || inventory.purchasedThemes()) { return color.getPrimaryColor(); } diff --git a/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java b/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java index 78e6e49ae..237bb8432 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java +++ b/app/src/main/java/com/todoroo/astrid/adapter/NavigationDrawerAdapter.java @@ -30,6 +30,7 @@ import javax.inject.Inject; import org.tasks.billing.Inventory; import org.tasks.filters.NavigationDrawerSubheader; import org.tasks.locale.Locale; +import org.tasks.themes.ColorProvider; import org.tasks.themes.Theme; import org.tasks.themes.ThemeAccent; @@ -40,6 +41,7 @@ public class NavigationDrawerAdapter extends ListAdapter ThemeColor.COLORS.map { color -> - ThemeColor(context, ContextCompat.getColor(context!!, color), true) - } + Palette.COLORS -> colorProvider.getThemeColors() Palette.ACCENTS -> ThemeAccent.ACCENTS.mapIndexed { index, _ -> ThemeAccent(context, index) } Palette.LAUNCHERS -> ThemeColor.LAUNCHER_COLORS.map { color -> - ThemeColor(context, ContextCompat.getColor(context!!, color), false) - } - Palette.WIDGET -> ThemeColor.COLORS.map { color -> - ThemeColor(context, ContextCompat.getColor(context!!, color), false) + ThemeColor(context, ContextCompat.getColor(context!!, color)) } + Palette.WIDGET -> colorProvider.getWidgetColors() } val iconPickerAdapter = ColorPickerAdapter( diff --git a/app/src/main/java/org/tasks/injection/ActivityModule.java b/app/src/main/java/org/tasks/injection/ActivityModule.java index ba6aa6e3e..e887f361a 100644 --- a/app/src/main/java/org/tasks/injection/ActivityModule.java +++ b/app/src/main/java/org/tasks/injection/ActivityModule.java @@ -9,6 +9,7 @@ import org.tasks.billing.Inventory; import org.tasks.location.Geocoder; import org.tasks.location.MapboxGeocoder; import org.tasks.preferences.Preferences; +import org.tasks.themes.ColorProvider; import org.tasks.themes.ThemeAccent; import org.tasks.themes.ThemeBase; import org.tasks.themes.ThemeColor; @@ -41,8 +42,8 @@ public class ActivityModule { @Provides @ActivityScope - public ThemeColor getThemeColor(Preferences preferences) { - return ThemeColor.newThemeColor(activity, preferences.getInt(R.string.p_theme_color, 0)); + public ThemeColor getThemeColor(ColorProvider colorProvider, Preferences preferences) { + return colorProvider.getThemeColor(preferences.getInt(R.string.p_theme_color, 0), true); } @Provides diff --git a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt index 6e0a85657..9b37778a1 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt @@ -109,7 +109,6 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange } findPreference(R.string.p_desaturate_colors).setOnPreferenceChangeListener { _, newValue -> - ThemeColor.enableSaturation(newValue as Boolean) if (context?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) { activity?.recreate() } diff --git a/app/src/main/java/org/tasks/tags/TagPickerActivity.java b/app/src/main/java/org/tasks/tags/TagPickerActivity.java index fab4745fb..47ef4a134 100644 --- a/app/src/main/java/org/tasks/tags/TagPickerActivity.java +++ b/app/src/main/java/org/tasks/tags/TagPickerActivity.java @@ -19,6 +19,7 @@ import org.tasks.data.TagData; import org.tasks.injection.ActivityComponent; import org.tasks.injection.ThemedInjectingAppCompatActivity; import org.tasks.tags.CheckBoxTriStates.State; +import org.tasks.themes.ColorProvider; import org.tasks.themes.Theme; import org.tasks.themes.ThemeColor; @@ -39,6 +40,7 @@ public class TagPickerActivity extends ThemedInjectingAppCompatActivity { @Inject Theme theme; @Inject Inventory inventory; + @Inject ColorProvider colorProvider; private TagPickerViewModel viewModel; private ArrayList taskIds; @@ -68,7 +70,7 @@ public class TagPickerActivity extends ThemedInjectingAppCompatActivity { themeColor.apply(toolbar); TagRecyclerAdapter recyclerAdapter = - new TagRecyclerAdapter(this, viewModel, inventory, this::onToggle); + new TagRecyclerAdapter(this, viewModel, inventory, colorProvider, this::onToggle); recyclerView.setAdapter(recyclerAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); diff --git a/app/src/main/java/org/tasks/tags/TagRecyclerAdapter.java b/app/src/main/java/org/tasks/tags/TagRecyclerAdapter.java index 4ef8be86a..6e2b3b78a 100644 --- a/app/src/main/java/org/tasks/tags/TagRecyclerAdapter.java +++ b/app/src/main/java/org/tasks/tags/TagRecyclerAdapter.java @@ -1,7 +1,5 @@ package org.tasks.tags; -import static org.tasks.themes.ThemeColor.newThemeColor; - import android.content.Context; import android.view.LayoutInflater; import android.view.View; @@ -17,7 +15,9 @@ import org.tasks.R; import org.tasks.billing.Inventory; import org.tasks.data.TagData; import org.tasks.tags.CheckBoxTriStates.State; +import org.tasks.themes.ColorProvider; import org.tasks.themes.CustomIcons; +import org.tasks.themes.ThemeColor; public class TagRecyclerAdapter extends RecyclerView.Adapter { @@ -25,16 +25,19 @@ public class TagRecyclerAdapter extends RecyclerView.Adapter callback; TagRecyclerAdapter( Context context, TagPickerViewModel viewModel, Inventory inventory, + ColorProvider colorProvider, Function2 callback) { this.context = context; this.viewModel = viewModel; this.inventory = inventory; + this.colorProvider = colorProvider; this.callback = callback; differ = new AsyncListDiffer<>(this, new TagDiffCallback()); } @@ -58,9 +61,13 @@ public class TagRecyclerAdapter extends RecyclerView.Adapter = hashMapOf( + // 2014 material design palette + -10453621 to -5194043, // blue_grey + -12434878 to -14606047, // grey + RED to -1074534, // red + -1499549 to -749647, // pink + -6543440 to -3238952, // purple + -10011977 to -5005861, // deep purple +// -12627531 to -6313766, // indigo + BLUE to -7288071, // blue + -16537100 to -8268550, // light blue + -16728876 to -8331542, // cyan +// -16738680 to -8336444, // teal + -11751600 to -5908825, // green + -7617718 to -3808859, // light green + -3285959 to -1642852, // lime + -5317 to -2659, // yellow + AMBER to -8062, // amber + -26624 to -13184, // orange + -43230 to -21615, // deep orange +// -8825528 to -4412764, // brown + GREY to -1118482, // grey + WHITE to BLACK, + + // 2019 google calendar + -2818048 to -3397335, // tomato + -765666 to -2136512, // tangerine + -1086464 to -2459092, // pumpkin + -1010944 to -2254804, // mango + -606426 to -2050234, // banana + -1784767 to -2769834, // citron + -4142541 to -4274613, // avocado + -8604862 to -7817131, // pistachio + -16023485 to -14116514, // basil + -16738680 to -14571622, // eucalyptus + -13388167 to -11879802, // sage + -16540699 to -13787178, // peacock + -12417548 to -10974241, // cobalt + -12627531 to -11312199, // blueberry + -8812853 to -8615738, // lavender + -5005861 to -5597744, // wisteria + -6395473 to -5934410, // amethyst + -7461718 to -6668365, // grape + -5434281 to -4967572, // radicchio + -2614432 to -3261327, // cherry blossom + -1672077 to -2654344, // flamingo + -8825528 to -6984611, // cocoa + -10395295 to -7895161, // graphite + -5792882 to -5135210 // birch + ) + } + + private val isDark = context.resources.getBoolean(R.bool.is_dark) + private val desaturate = preferences.getBoolean(R.string.p_desaturate_colors, true) + + fun getThemeColor(@ColorInt color: Int, adjust: Boolean = true) = + ThemeColor(context, color, if (adjust && isDark && desaturate) { + saturated[color] ?: color + } else { + color + }) + + fun getThemeColors(adjust: Boolean = true) = ThemeColor.COLORS.map { c -> + getThemeColor(ContextCompat.getColor(context, c), adjust) + } + + fun getWidgetColors() = getThemeColors(true) +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/themes/ThemeColor.java b/app/src/main/java/org/tasks/themes/ThemeColor.java index a5a45e831..d8d23d8c1 100644 --- a/app/src/main/java/org/tasks/themes/ThemeColor.java +++ b/app/src/main/java/org/tasks/themes/ThemeColor.java @@ -1,6 +1,5 @@ package org.tasks.themes; -import static com.google.common.collect.Maps.newHashMap; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastLollipop; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo; @@ -26,11 +25,8 @@ import androidx.core.graphics.ColorUtils; import androidx.core.os.ParcelCompat; import androidx.drawerlayout.widget.DrawerLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; -import java.util.Collections; -import java.util.Map; import org.tasks.R; import org.tasks.dialogs.ColorPalettePicker.Pickable; -import timber.log.Timber; public class ThemeColor implements Pickable { @@ -165,66 +161,8 @@ public class ThemeColor implements Pickable { } }; + private static final int BLUE = -14575885; private static final int WHITE = -1; - private static final int BLACK = -16777216; - - private static final Map saturated = newHashMap(); - public static Map colorMap = saturated; - - public static void enableSaturation(boolean enabled) { - colorMap = enabled ? saturated : Collections.emptyMap(); - } - - static { - // 2014 material design palette - saturated.put(-10453621, -5194043); // blue_grey - saturated.put(-12434878, -14606047); // grey - saturated.put(-769226, -1074534); // red - saturated.put(-1499549, -749647); // pink - saturated.put(-6543440, -3238952); // purple - saturated.put(-10011977, -5005861); // deep purple - saturated.put(-12627531, -6313766); // indigo - saturated.put(-14575885, -7288071); // blue - saturated.put(-16537100, -8268550); // light blue - saturated.put(-16728876, -8331542); // cyan - saturated.put(-16738680, -8336444); // teal - saturated.put(-11751600, -5908825); // green - saturated.put(-7617718, -3808859); // light green - saturated.put(-3285959, -1642852); // lime - saturated.put(-5317, -2659); // yellow - saturated.put(-16121, -8062); // amber - saturated.put(-26624, -13184); // orange - saturated.put(-43230, -21615); // deep orange - saturated.put(-8825528, -4412764); // brown - saturated.put(-6381922, -1118482); // grey - saturated.put(WHITE, BLACK); - - // 2019 google calendar - saturated.put(-2818048, -3397335); // tomato - saturated.put(-765666, -2136512); // tangerine - saturated.put(-1086464, -2459092); // pumpkin - saturated.put(-1010944, -2254804); // mango - saturated.put(-606426, -2050234); // banana - saturated.put(-1784767, -2769834); // citron - saturated.put(-4142541, -4274613); // avocado - saturated.put(-8604862, -7817131); // pistachio - saturated.put(-16023485, -14116514); // basil - // saturated.put(-16738680, -14571622); // eucalyptus - saturated.put(-13388167, -11879802); // sage - saturated.put(-16540699, -13787178); // peacock - saturated.put(-12417548, -10974241); // cobalt - // saturated.put(-12627531, -11312199); // blueberry - saturated.put(-8812853, -8615738); // lavender - saturated.put(-5005861, -5597744); // wisteria - saturated.put(-6395473, -5934410); // amethyst - saturated.put(-7461718, -6668365); // grape - saturated.put(-5434281, -4967572); // radicchio - saturated.put(-2614432, -3261327); // cherry blossom - saturated.put(-1672077, -2654344); // flamingo - // saturated.put(-8825528, -6984611); // cocoa - saturated.put(-10395295, -7895161); // graphite - saturated.put(-5792882, -5135210); // birch - } private final int original; private final int colorOnPrimary; @@ -233,17 +171,17 @@ public class ThemeColor implements Pickable { private final boolean isDark; public ThemeColor(Context context, int color) { - this(context, color == 0 ? ContextCompat.getColor(context, R.color.blue_500) : color, true); + this(context, color, color); } - public ThemeColor(Context context, int color, boolean adjustColor) { - color |= 0xFF000000; // remove alpha - original = color; - if (adjustColor && context.getResources().getBoolean(R.bool.is_dark)) { - colorPrimary = desaturate(color); + public ThemeColor(Context context, int original, int color) { + this.original = original; + if (color == 0) { + color = BLUE; } else { - colorPrimary = color; + color |= 0xFF000000; // remove alpha } + colorPrimary = color; colorPrimaryVariant = ColorUtil.darken(colorPrimary, 6); double contrast = ColorUtils.calculateContrast(WHITE, colorPrimary); @@ -260,16 +198,7 @@ public class ThemeColor implements Pickable { } public static ThemeColor getLauncherColor(Context context, int index) { - return new ThemeColor(context, ContextCompat.getColor(context, LAUNCHER_COLORS[index]), false); - } - - public static ThemeColor newThemeColor(Context context, int color) { - try { - return new ThemeColor(context, color); - } catch (Exception e) { - Timber.e(e); - return new ThemeColor(context, 0); - } + return new ThemeColor(context, ContextCompat.getColor(context, LAUNCHER_COLORS[index])); } private static void colorMenu(Menu menu, int color) { @@ -297,15 +226,6 @@ public class ThemeColor implements Pickable { return drawable; } - private int desaturate(int color) { - if (colorMap.containsKey(color)) { - //noinspection ConstantConditions - return colorMap.get(color); - } else { - return color; - } - } - public void applyToSystemBars(Activity activity) { setStatusBarColor(activity); diff --git a/app/src/main/java/org/tasks/ui/ChipListCache.java b/app/src/main/java/org/tasks/ui/ChipListCache.java new file mode 100644 index 000000000..ed4a2edde --- /dev/null +++ b/app/src/main/java/org/tasks/ui/ChipListCache.java @@ -0,0 +1,76 @@ +package org.tasks.ui; + +import com.todoroo.astrid.api.CaldavFilter; +import com.todoroo.astrid.api.Filter; +import com.todoroo.astrid.api.GtasksFilter; +import com.todoroo.astrid.api.TagFilter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.inject.Inject; +import org.tasks.LocalBroadcastManager; +import org.tasks.data.CaldavCalendar; +import org.tasks.data.CaldavDao; +import org.tasks.data.GoogleTaskList; +import org.tasks.data.GoogleTaskListDao; +import org.tasks.data.TagData; +import org.tasks.data.TagDataDao; +import org.tasks.injection.ApplicationScope; + +@ApplicationScope +public class ChipListCache { + + private final Map googleTaskLists = new HashMap<>(); + private final Map caldavCalendars = new HashMap<>(); + private final Map tagDatas = new HashMap<>(); + private final LocalBroadcastManager localBroadcastManager; + + @Inject + public ChipListCache( + GoogleTaskListDao googleTaskListDao, + CaldavDao caldavDao, + TagDataDao tagDataDao, + LocalBroadcastManager localBroadcastManager) { + this.localBroadcastManager = localBroadcastManager; + + googleTaskListDao.subscribeToLists().observeForever(this::updateGoogleTaskLists); + caldavDao.subscribeToCalendars().observeForever(this::updateCaldavCalendars); + tagDataDao.subscribeToTags().observeForever(this::updateTags); + } + + private void updateGoogleTaskLists(List updated) { + googleTaskLists.clear(); + for (GoogleTaskList update : updated) { + googleTaskLists.put(update.getRemoteId(), new GtasksFilter(update)); + } + localBroadcastManager.broadcastRefresh(); + } + + private void updateCaldavCalendars(List updated) { + caldavCalendars.clear(); + for (CaldavCalendar update : updated) { + caldavCalendars.put(update.getUuid(), new CaldavFilter(update)); + } + localBroadcastManager.broadcastRefresh(); + } + + private void updateTags(List updated) { + tagDatas.clear(); + for (TagData update : updated) { + tagDatas.put(update.getRemoteId(), new TagFilter(update)); + } + localBroadcastManager.broadcastRefresh(); + } + + Filter getGoogleTaskList(String googleTaskList) { + return googleTaskLists.get(googleTaskList); + } + + Filter getCaldavList(String caldav) { + return caldavCalendars.get(caldav); + } + + TagFilter getTag(String tag) { + return tagDatas.get(tag); + } +} diff --git a/app/src/main/java/org/tasks/ui/ChipProvider.java b/app/src/main/java/org/tasks/ui/ChipProvider.java index d412d73ee..e9f4b2d5f 100644 --- a/app/src/main/java/org/tasks/ui/ChipProvider.java +++ b/app/src/main/java/org/tasks/ui/ChipProvider.java @@ -1,12 +1,10 @@ package org.tasks.ui; -import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Collections2.filter; +import static com.google.common.collect.Collections2.transform; import static com.google.common.collect.Iterables.removeIf; -import static com.google.common.collect.Iterables.transform; -import static com.google.common.collect.Lists.transform; import static com.google.common.collect.Sets.newHashSet; import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread; -import static org.tasks.themes.ThemeColor.newThemeColor; import android.app.Activity; import android.content.Context; @@ -24,36 +22,25 @@ import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.GtasksFilter; import com.todoroo.astrid.api.TagFilter; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; import javax.inject.Inject; -import org.tasks.LocalBroadcastManager; import org.tasks.R; import org.tasks.billing.Inventory; -import org.tasks.data.CaldavCalendar; -import org.tasks.data.CaldavDao; -import org.tasks.data.GoogleTaskList; -import org.tasks.data.GoogleTaskListDao; import org.tasks.data.TagData; -import org.tasks.data.TagDataDao; import org.tasks.data.TaskContainer; -import org.tasks.injection.ApplicationScope; -import org.tasks.injection.ForApplication; +import org.tasks.injection.ForActivity; import org.tasks.preferences.Preferences; +import org.tasks.themes.ColorProvider; import org.tasks.themes.CustomIcons; import org.tasks.themes.ThemeColor; -@ApplicationScope public class ChipProvider { - private final Map googleTaskLists = new HashMap<>(); - private final Map caldavCalendars = new HashMap<>(); - private final Map tagDatas = new HashMap<>(); private final Inventory inventory; private final int iconAlpha; - private final LocalBroadcastManager localBroadcastManager; + private final ChipListCache lists; + private final ColorProvider colorProvider; private final Ordering orderByName = new Ordering() { @Override @@ -67,21 +54,17 @@ public class ChipProvider { @Inject public ChipProvider( - @ForApplication Context context, + @ForActivity Context context, Inventory inventory, - GoogleTaskListDao googleTaskListDao, - CaldavDao caldavDao, - TagDataDao tagDataDao, - LocalBroadcastManager localBroadcastManager, - Preferences preferences) { + ChipListCache lists, + Preferences preferences, + ColorProvider colorProvider) { this.inventory = inventory; - this.localBroadcastManager = localBroadcastManager; iconAlpha = (int) (255 * ResourcesCompat.getFloat(context.getResources(), R.dimen.alpha_secondary)); + this.lists = lists; + this.colorProvider = colorProvider; - googleTaskListDao.subscribeToLists().observeForever(this::updateGoogleTaskLists); - caldavDao.subscribeToCalendars().observeForever(this::updateCaldavCalendars); - tagDataDao.subscribeToTags().observeForever(this::updateTags); setStyle(preferences.getIntegerFromString(R.string.p_chip_style, 0)); setAppearance(preferences.getIntegerFromString(R.string.p_chip_appearance, 0)); } @@ -95,30 +78,6 @@ public class ChipProvider { showIcon = appearance != 1; } - private void updateGoogleTaskLists(List updated) { - googleTaskLists.clear(); - for (GoogleTaskList update : updated) { - googleTaskLists.put(update.getRemoteId(), new GtasksFilter(update)); - } - localBroadcastManager.broadcastRefresh(); - } - - private void updateCaldavCalendars(List updated) { - caldavCalendars.clear(); - for (CaldavCalendar update : updated) { - caldavCalendars.put(update.getUuid(), new CaldavFilter(update)); - } - localBroadcastManager.broadcastRefresh(); - } - - private void updateTags(List updated) { - tagDatas.clear(); - for (TagData update : updated) { - tagDatas.put(update.getRemoteId(), new TagFilter(update)); - } - localBroadcastManager.broadcastRefresh(); - } - public List getChips( Activity activity, Filter filter, @@ -131,7 +90,6 @@ public class ChipProvider { if (!hideSubtaskChip && task.hasChildren()) { Chip chip = newChip(activity, task); apply( - activity, chip, task.isCollapsed() ? R.drawable.ic_keyboard_arrow_up_black_24dp @@ -146,8 +104,7 @@ public class ChipProvider { } if (task.hasLocation()) { Chip chip = newChip(activity, task.getLocation()); - apply( - activity, chip, R.drawable.ic_outline_place_24px, task.getLocation().getDisplayName(), 0, showText, showIcon); + apply(chip, R.drawable.ic_outline_place_24px, task.getLocation().getDisplayName(), 0, showText, showIcon); chips.add(chip); } if (!isSubtask) { @@ -155,12 +112,12 @@ public class ChipProvider { chips.add( newChip( activity, - googleTaskLists.get(task.getGoogleTaskList()), + lists.getGoogleTaskList(task.getGoogleTaskList()), R.drawable.ic_outline_cloud_24px)); } else if (!Strings.isNullOrEmpty(task.getCaldav()) && !(filter instanceof CaldavFilter)) { chips.add( newChip( - activity, caldavCalendars.get(task.getCaldav()), R.drawable.ic_outline_cloud_24px)); + activity, lists.getCaldavList(task.getCaldav()), R.drawable.ic_outline_cloud_24px)); } } String tagString = task.getTagsString(); @@ -171,7 +128,7 @@ public class ChipProvider { } chips.addAll( transform( - orderByName.sortedCopy(filter(transform(tags, tagDatas::get), Predicates.notNull())), + orderByName.sortedCopy(filter(transform(tags, lists::getTag), Predicates.notNull())), tag -> newChip(activity, tag, R.drawable.ic_outline_label_24px))); } @@ -181,7 +138,6 @@ public class ChipProvider { public void apply(Chip chip, Filter filter) { apply( - chip.getContext(), chip, getIcon(filter.icon, R.drawable.ic_outline_cloud_24px), filter.listingTitle, @@ -192,7 +148,6 @@ public class ChipProvider { public void apply(Chip chip, @NonNull TagData tagData) { apply( - chip.getContext(), chip, getIcon(tagData.getIcon(), R.drawable.ic_outline_label_24px), tagData.getName(), @@ -210,7 +165,7 @@ public class ChipProvider { return null; } Chip chip = newChip(activity, filter); - apply(activity, chip, getIcon(filter.icon, defIcon), filter.listingTitle, filter.tint, showText, showIcon); + apply(chip, getIcon(filter.icon, defIcon), filter.listingTitle, filter.tint, showText, showIcon); return chip; } @@ -235,7 +190,6 @@ public class ChipProvider { } private void apply( - Context context, Chip chip, @Nullable @DrawableRes Integer icon, String name, @@ -251,7 +205,7 @@ public class ChipProvider { chip.setTextStartPadding(0f); chip.setChipEndPadding(0f); } - ThemeColor themeColor = getColor(context, theme); + ThemeColor themeColor = getColor(theme); if (themeColor != null) { int primaryColor = themeColor.getPrimaryColor(); ColorStateList primaryColorSL = @@ -282,9 +236,9 @@ public class ChipProvider { return icon != null ? icon : def; } - private @Nullable ThemeColor getColor(Context context, int theme) { + private @Nullable ThemeColor getColor(int theme) { if (theme != 0) { - ThemeColor color = newThemeColor(context, theme); + ThemeColor color = colorProvider.getThemeColor(theme, true); if (color.isFree() || inventory.purchasedThemes()) { return color; } diff --git a/app/src/main/java/org/tasks/ui/EmptyTaskEditFragment.kt b/app/src/main/java/org/tasks/ui/EmptyTaskEditFragment.kt index 2142936d0..5b73926a1 100644 --- a/app/src/main/java/org/tasks/ui/EmptyTaskEditFragment.kt +++ b/app/src/main/java/org/tasks/ui/EmptyTaskEditFragment.kt @@ -8,13 +8,14 @@ import com.todoroo.astrid.api.Filter import org.tasks.databinding.FragmentTaskEditEmptyBinding import org.tasks.injection.FragmentComponent import org.tasks.injection.InjectingFragment +import org.tasks.themes.ColorProvider import org.tasks.themes.ThemeColor -import org.tasks.themes.ThemeColor.newThemeColor import javax.inject.Inject class EmptyTaskEditFragment : InjectingFragment() { @Inject lateinit var themeColor: ThemeColor + @Inject lateinit var colorProvider: ColorProvider companion object { const val EXTRA_FILTER = "extra_filter" @@ -35,10 +36,11 @@ class EmptyTaskEditFragment : InjectingFragment() { val tint = arguments?.getParcelable(EXTRA_FILTER)?.tint - val color = newThemeColor( - context!!, - if (tint == null || tint == 0) themeColor.primaryColor else tint - ) + val color = colorProvider.getThemeColor(if (tint == null || tint == 0) { + themeColor.primaryColor + } else { + tint + }) color.apply(binding.toolbar.toolbar) diff --git a/app/src/main/java/org/tasks/widget/TasksWidget.java b/app/src/main/java/org/tasks/widget/TasksWidget.java index 381cbf23c..76314e0da 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, widgetPreferences.getColor(), false); + ThemeColor color = new ThemeColor(context, widgetPreferences.getColor()); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.scrollable_widget); if (atLeastJellybeanMR1()) { remoteViews.setInt(R.id.widget, "setLayoutDirection", locale.getDirectionality());