ChipProvider updates itself

pull/795/head
Alex Baker 6 years ago
parent 43e55d0efc
commit 3d05aa54ae

@ -172,16 +172,20 @@ public class AndroidUtilities {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}
public static void assertMainThread() {
if (BuildConfig.DEBUG && !isMainThread()) {
throw new IllegalStateException("Should be called from main thread");
}
}
public static void assertNotMainThread() {
if (BuildConfig.DEBUG && isMainThread()) {
throw new IllegalStateException();
throw new IllegalStateException("Should not be called from main thread");
}
}
private static boolean isMainThread() {
return atLeastMarshmallow()
? Looper.getMainLooper().isCurrentThread()
: Thread.currentThread() == Looper.getMainLooper().getThread();
return Thread.currentThread() == Looper.getMainLooper().getThread();
}
/**

@ -355,7 +355,9 @@ public final class TagsControlSet extends TaskEditControlFragment {
chipGroup.setVisibility(View.VISIBLE);
chipGroup.removeAllViews();
for (TagData tagData : orderByName.sortedCopy(selectedTags)) {
Chip chip = chipProvider.getChip(tagData);
Chip chip = new Chip(getActivity());
chipProvider.apply(chip, tagData);
chip.setCloseIconVisible(true);
chip.setOnClickListener(view -> onClickRow());
chip.setOnCloseIconClickListener(
view -> {

@ -1,5 +1,6 @@
package org.tasks.data;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
@ -10,6 +11,9 @@ import java.util.List;
@Dao
public interface CaldavDao {
@Query("SELECT * FROM caldav_calendar")
LiveData<List<CaldavCalendar>> subscribeToCalendars();
@Query("SELECT * FROM caldav_calendar WHERE uuid = :uuid LIMIT 1")
CaldavCalendar getCalendarByUuid(String uuid);

@ -1,5 +1,6 @@
package org.tasks.data;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
@ -25,6 +26,9 @@ public abstract class GoogleTaskListDao {
@Query("SELECT * FROM google_task_lists WHERE remote_id = :remoteId LIMIT 1")
public abstract GoogleTaskList getByRemoteId(String remoteId);
@Query("SELECT * FROM google_task_lists")
public abstract LiveData<List<GoogleTaskList>> subscribeToLists();
@Query(
"SELECT * FROM google_task_lists WHERE remote_id = :remoteId AND IFNULL(account, '') = '' LIMIT 1")
public abstract GoogleTaskList findExistingList(String remoteId);

@ -1,5 +1,6 @@
package org.tasks.data;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
@ -11,6 +12,9 @@ import java.util.List;
@Dao
public abstract class TagDataDao {
@Query("SELECT * FROM tagdata")
public abstract LiveData<List<TagData>> subscribeToTags();
@Query("SELECT * FROM tagdata WHERE name = :name COLLATE NOCASE LIMIT 1")
public abstract TagData getTagByName(String name);

@ -7,7 +7,7 @@ import static com.todoroo.andlib.utility.AndroidUtilities.atLeastLollipop;
import static com.todoroo.andlib.utility.DateUtilities.getAbbreviatedRelativeDateWithTime;
import android.annotation.SuppressLint;
import android.content.Context;
import android.app.Activity;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.DisplayMetrics;
@ -38,7 +38,7 @@ import org.tasks.ui.ChipProvider;
class ViewHolder extends RecyclerView.ViewHolder {
private final Context context;
private final Activity context;
private final Preferences preferences;
private final CheckBoxes checkBoxes;
private final int textColorSecondary;
@ -84,7 +84,7 @@ class ViewHolder extends RecyclerView.ViewHolder {
private boolean moving;
ViewHolder(
Context context,
Activity context,
Locale locale,
ViewGroup view,
Preferences preferences,
@ -279,7 +279,7 @@ class ViewHolder extends RecyclerView.ViewHolder {
List<String> tagUuids = tags != null ? newArrayList(tags.split(",")) : Lists.newArrayList();
List<Chip> chips =
chipProvider.getChips(task.getCaldav(), task.getGoogleTaskList(), tagUuids);
chipProvider.getChips(context, task.getCaldav(), task.getGoogleTaskList(), tagUuids);
if (chips.isEmpty()) {
chipGroup.setVisibility(View.GONE);
} else {

@ -5,6 +5,7 @@ import static com.todoroo.andlib.utility.AndroidUtilities.convertDpToPixels;
import static org.tasks.preferences.ResourceResolver.getData;
import static org.tasks.preferences.ResourceResolver.getResourceId;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
@ -65,7 +66,7 @@ public class ViewHolderFactory {
ViewHolder newViewHolder(ViewGroup parent, ViewHolder.ViewHolderCallbacks callbacks) {
return new ViewHolder(
context,
(Activity) context,
locale,
(ViewGroup)
LayoutInflater.from(context).inflate(R.layout.task_adapter_row_simple, parent, false),

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

Loading…
Cancel
Save