|
|
|
@ -2,6 +2,7 @@ package org.tasks.ui;
|
|
|
|
|
|
|
|
|
|
import static com.google.common.collect.Iterables.filter;
|
|
|
|
|
import static com.google.common.collect.Iterables.transform;
|
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread;
|
|
|
|
|
import static org.tasks.preferences.ResourceResolver.getDimen;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
@ -16,33 +17,33 @@ 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 com.todoroo.astrid.tags.TagService;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import org.tasks.LocalBroadcastManager;
|
|
|
|
|
import org.tasks.R;
|
|
|
|
|
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.injection.ForActivity;
|
|
|
|
|
import org.tasks.data.TagDataDao;
|
|
|
|
|
import org.tasks.injection.ApplicationScope;
|
|
|
|
|
import org.tasks.injection.ForApplication;
|
|
|
|
|
import org.tasks.themes.ThemeCache;
|
|
|
|
|
import org.tasks.themes.ThemeColor;
|
|
|
|
|
|
|
|
|
|
@ApplicationScope
|
|
|
|
|
public class ChipProvider {
|
|
|
|
|
|
|
|
|
|
private final Map<String, GtasksFilter> googleTaskLists = new HashMap<>();
|
|
|
|
|
private final Map<String, CaldavFilter> caldavCalendars = new HashMap<>();
|
|
|
|
|
private final Map<String, TagFilter> tagDatas = new HashMap<>();
|
|
|
|
|
private final Context context;
|
|
|
|
|
private final ThemeCache themeCache;
|
|
|
|
|
private final int iconAlpha;
|
|
|
|
|
private final GoogleTaskListDao googleTaskListDao;
|
|
|
|
|
private final CaldavDao caldavDao;
|
|
|
|
|
private final TagService tagService;
|
|
|
|
|
private final LocalBroadcastManager localBroadcastManager;
|
|
|
|
|
private final Ordering<TagFilter> orderByName =
|
|
|
|
|
new Ordering<TagFilter>() {
|
|
|
|
|
@Override
|
|
|
|
@ -53,43 +54,64 @@ public class ChipProvider {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
public ChipProvider(
|
|
|
|
|
@ForActivity Context context,
|
|
|
|
|
@ForApplication Context context,
|
|
|
|
|
ThemeCache themeCache,
|
|
|
|
|
GoogleTaskListDao googleTaskListDao,
|
|
|
|
|
CaldavDao caldavDao,
|
|
|
|
|
TagService tagService) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
TagDataDao tagDataDao,
|
|
|
|
|
LocalBroadcastManager localBroadcastManager) {
|
|
|
|
|
this.themeCache = themeCache;
|
|
|
|
|
this.googleTaskListDao = googleTaskListDao;
|
|
|
|
|
this.caldavDao = caldavDao;
|
|
|
|
|
this.tagService = tagService;
|
|
|
|
|
this.localBroadcastManager = localBroadcastManager;
|
|
|
|
|
iconAlpha = (int) (255 * getDimen(context, R.dimen.alpha_secondary));
|
|
|
|
|
|
|
|
|
|
googleTaskListDao.subscribeToLists().observeForever(this::updateGoogleTaskLists);
|
|
|
|
|
caldavDao.subscribeToCalendars().observeForever(this::updateCaldavCalendars);
|
|
|
|
|
tagDataDao.subscribeToTags().observeForever(this::updateTags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Chip getChip(TagData tagData) {
|
|
|
|
|
Chip chip = new Chip(context);
|
|
|
|
|
chip.setCloseIconVisible(true);
|
|
|
|
|
apply(chip, tagData.getName(), tagData.getColor());
|
|
|
|
|
return chip;
|
|
|
|
|
private void updateGoogleTaskLists(List<GoogleTaskList> updated) {
|
|
|
|
|
googleTaskLists.clear();
|
|
|
|
|
for (GoogleTaskList update : updated) {
|
|
|
|
|
googleTaskLists.put(update.getRemoteId(), new GtasksFilter(update));
|
|
|
|
|
}
|
|
|
|
|
localBroadcastManager.broadcastRefresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateCaldavCalendars(List<CaldavCalendar> updated) {
|
|
|
|
|
caldavCalendars.clear();
|
|
|
|
|
for (CaldavCalendar update : updated) {
|
|
|
|
|
caldavCalendars.put(update.getUuid(), new CaldavFilter(update));
|
|
|
|
|
}
|
|
|
|
|
localBroadcastManager.broadcastRefresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Chip> getChips(String caldav, String googleTask, Iterable<String> tagUuids) {
|
|
|
|
|
private void updateTags(List<TagData> updated) {
|
|
|
|
|
tagDatas.clear();
|
|
|
|
|
for (TagData update : updated) {
|
|
|
|
|
tagDatas.put(update.getRemoteId(), new TagFilter(update));
|
|
|
|
|
}
|
|
|
|
|
localBroadcastManager.broadcastRefresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Chip> getChips(Activity activity, String caldav, String googleTask, Iterable<String> tagUuids) {
|
|
|
|
|
assertMainThread();
|
|
|
|
|
|
|
|
|
|
List<Chip> chips = new ArrayList<>();
|
|
|
|
|
if (!Strings.isNullOrEmpty(googleTask)) {
|
|
|
|
|
GtasksFilter googleTaskFilter = getGoogleTaskList(googleTask);
|
|
|
|
|
GtasksFilter googleTaskFilter = googleTaskLists.get(googleTask);
|
|
|
|
|
if (googleTaskFilter != null) {
|
|
|
|
|
chips.add(newChip(googleTaskFilter));
|
|
|
|
|
chips.add(newChip(activity, googleTaskFilter));
|
|
|
|
|
}
|
|
|
|
|
} else if (!Strings.isNullOrEmpty(caldav)) {
|
|
|
|
|
CaldavFilter caldavFilter = getCaldavCalendar(caldav);
|
|
|
|
|
CaldavFilter caldavFilter = caldavCalendars.get(caldav);
|
|
|
|
|
if (caldavFilter != null) {
|
|
|
|
|
chips.add(newChip(caldavFilter));
|
|
|
|
|
chips.add(newChip(activity, caldavFilter));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Iterable<TagFilter> tagFilters =
|
|
|
|
|
filter(transform(tagUuids, this::getTag), Predicates.notNull());
|
|
|
|
|
filter(transform(tagUuids, tagDatas::get), Predicates.notNull());
|
|
|
|
|
for (TagFilter tagFilter : orderByName.sortedCopy(tagFilters)) {
|
|
|
|
|
chips.add(newChip(tagFilter));
|
|
|
|
|
chips.add(newChip(activity, tagFilter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chips;
|
|
|
|
@ -99,9 +121,12 @@ public class ChipProvider {
|
|
|
|
|
apply(chip, filter.listingTitle, filter.tint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Chip newChip(Filter filter) {
|
|
|
|
|
LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
|
|
|
|
|
Chip chip = (Chip) layoutInflater.inflate(R.layout.chip_task_list, null);
|
|
|
|
|
public void apply(Chip chip, TagData tagData) {
|
|
|
|
|
apply(chip, tagData.getName(), tagData.getColor());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Chip newChip(Activity activity, Filter filter) {
|
|
|
|
|
Chip chip = (Chip) activity.getLayoutInflater().inflate(R.layout.chip_task_list, null);
|
|
|
|
|
chip.setTag(filter);
|
|
|
|
|
apply(chip, filter.listingTitle, filter.tint);
|
|
|
|
|
return chip;
|
|
|
|
@ -121,40 +146,4 @@ public class ChipProvider {
|
|
|
|
|
},
|
|
|
|
|
new int[] {color.getPrimaryColor(), color.getPrimaryColor()}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GtasksFilter getGoogleTaskList(String remoteId) {
|
|
|
|
|
GtasksFilter gtasksFilter = googleTaskLists.get(remoteId);
|
|
|
|
|
if (gtasksFilter == null) {
|
|
|
|
|
GoogleTaskList googleTaskList = googleTaskListDao.getByRemoteId(remoteId);
|
|
|
|
|
if (googleTaskList != null) {
|
|
|
|
|
gtasksFilter = new GtasksFilter(googleTaskList);
|
|
|
|
|
googleTaskLists.put(remoteId, gtasksFilter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return gtasksFilter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CaldavFilter getCaldavCalendar(String uuid) {
|
|
|
|
|
CaldavFilter caldavFilter = caldavCalendars.get(uuid);
|
|
|
|
|
if (caldavFilter == null) {
|
|
|
|
|
CaldavCalendar calendar = caldavDao.getCalendar(uuid);
|
|
|
|
|
if (calendar != null) {
|
|
|
|
|
caldavFilter = new CaldavFilter(calendar);
|
|
|
|
|
caldavCalendars.put(uuid, caldavFilter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return caldavFilter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TagFilter getTag(String uuid) {
|
|
|
|
|
TagFilter tagFilter = tagDatas.get(uuid);
|
|
|
|
|
if (tagFilter == null) {
|
|
|
|
|
TagData tagData = tagService.getTagByUuid(uuid);
|
|
|
|
|
if (tagData != null) {
|
|
|
|
|
tagFilter = new TagFilter(tagData);
|
|
|
|
|
tagDatas.put(uuid, tagFilter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tagFilter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|