mirror of https://github.com/tasks/tasks
Apply existing categories on upgrade
parent
31ce07641d
commit
3f71329f61
@ -0,0 +1,63 @@
|
||||
package org.tasks.data;
|
||||
|
||||
import static com.natpryce.makeiteasy.MakeItEasy.with;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.tasks.makers.TagDataMaker.newTagData;
|
||||
import static org.tasks.makers.TagMaker.TAGDATA;
|
||||
import static org.tasks.makers.TagMaker.TASK;
|
||||
import static org.tasks.makers.TagMaker.newTag;
|
||||
import static org.tasks.makers.TaskMaker.ID;
|
||||
import static org.tasks.makers.TaskMaker.newTask;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import com.todoroo.astrid.dao.TaskDao;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
import javax.inject.Inject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class CaldavDaoTests {
|
||||
@Inject TaskDao taskDao;
|
||||
@Inject TagDao tagDao;
|
||||
@Inject TagDataDao tagDataDao;
|
||||
@Inject CaldavDao caldavDao;
|
||||
|
||||
@Test
|
||||
public void getCaldavTasksWithTags() {
|
||||
Task task = newTask(with(ID, 1L));
|
||||
taskDao.createNew(task);
|
||||
TagData one = newTagData();
|
||||
TagData two = newTagData();
|
||||
tagDataDao.createNew(one);
|
||||
tagDataDao.createNew(two);
|
||||
tagDao.insert(newTag(with(TASK, task), with(TAGDATA, one)));
|
||||
tagDao.insert(newTag(with(TASK, task), with(TAGDATA, two)));
|
||||
caldavDao.insert(new CaldavTask(task.getId(), "calendar"));
|
||||
|
||||
assertEquals(singletonList(task.getId()), caldavDao.getTasksWithTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreNonCaldavTaskWithTags() {
|
||||
Task task = newTask(with(ID, 1L));
|
||||
taskDao.createNew(task);
|
||||
TagData tag = newTagData();
|
||||
tagDataDao.createNew(tag);
|
||||
tagDao.insert(newTag(with(TASK, task), with(TAGDATA, tag)));
|
||||
|
||||
assertTrue(caldavDao.getTasksWithTags().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreCaldavTaskWithoutTags() {
|
||||
Task task = newTask(with(ID, 1L));
|
||||
taskDao.createNew(task);
|
||||
tagDataDao.createNew(newTagData());
|
||||
caldavDao.insert(new CaldavTask(task.getId(), "calendar"));
|
||||
|
||||
assertTrue(caldavDao.getTasksWithTags().isEmpty());
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.tasks.caldav;
|
||||
|
||||
import static com.google.common.collect.Lists.transform;
|
||||
import static com.google.common.collect.Sets.difference;
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import java.io.StringReader;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.tasks.data.TagData;
|
||||
import org.tasks.data.TagDataDao;
|
||||
|
||||
public class CaldavUtils {
|
||||
public static @Nullable at.bitfire.ical4android.Task fromVtodo(String vtodo) {
|
||||
List<at.bitfire.ical4android.Task> tasks =
|
||||
at.bitfire.ical4android.Task.Companion.fromReader(new StringReader(vtodo));
|
||||
return tasks.size() == 1 ? tasks.get(0) : null;
|
||||
}
|
||||
|
||||
public static List<TagData> getTags(TagDataDao tagDataDao, List<String> categories) {
|
||||
if (categories.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<TagData> selectedTags = tagDataDao.getTags(categories);
|
||||
Set<String> toCreate =
|
||||
difference(newHashSet(categories), newHashSet(transform(selectedTags, TagData::getName)));
|
||||
for (String name : toCreate) {
|
||||
TagData tag = new TagData(name);
|
||||
tagDataDao.createNew(tag);
|
||||
selectedTags.add(tag);
|
||||
}
|
||||
return selectedTags;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.tasks.data;
|
||||
|
||||
import androidx.room.Embedded;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
|
||||
public class CaldavTaskContainer {
|
||||
@Embedded public Task task;
|
||||
@Embedded public CaldavTask caldavTask;
|
||||
}
|
@ -1,40 +1,67 @@
|
||||
package org.tasks.data;
|
||||
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.collect.Lists.newArrayList;
|
||||
import static com.google.common.collect.Sets.difference;
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Transaction;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Dao
|
||||
public interface TagDao {
|
||||
public abstract class TagDao {
|
||||
|
||||
@Query("UPDATE tags SET name = :name WHERE tag_uid = :tagUid")
|
||||
void rename(String tagUid, String name);
|
||||
public abstract void rename(String tagUid, String name);
|
||||
|
||||
@Query("DELETE FROM tags WHERE tag_uid = :tagUid")
|
||||
void deleteTag(String tagUid);
|
||||
public abstract void deleteTag(String tagUid);
|
||||
|
||||
@Insert
|
||||
void insert(Tag tag);
|
||||
public abstract void insert(Tag tag);
|
||||
|
||||
@Insert
|
||||
void insert(Iterable<Tag> tags);
|
||||
public abstract void insert(Iterable<Tag> tags);
|
||||
|
||||
@Query("DELETE FROM tags WHERE task = :taskId AND tag_uid in (:tagUids)")
|
||||
void deleteTags(long taskId, List<String> tagUids);
|
||||
public abstract void deleteTags(long taskId, List<String> tagUids);
|
||||
|
||||
@Query("SELECT name FROM tags WHERE task = :taskId ORDER BY UPPER(name) ASC")
|
||||
List<String> getTagNames(long taskId);
|
||||
public abstract List<String> getTagNames(long taskId);
|
||||
|
||||
@Query("SELECT * FROM tags WHERE tag_uid = :tagUid")
|
||||
List<Tag> getByTagUid(String tagUid);
|
||||
public abstract List<Tag> getByTagUid(String tagUid);
|
||||
|
||||
@Query("SELECT * FROM tags WHERE task = :taskId")
|
||||
List<Tag> getTagsForTask(long taskId);
|
||||
public abstract List<Tag> getTagsForTask(long taskId);
|
||||
|
||||
@Query("SELECT * FROM tags WHERE task = :taskId AND tag_uid = :tagUid")
|
||||
Tag getTagByTaskAndTagUid(long taskId, String tagUid);
|
||||
public abstract Tag getTagByTaskAndTagUid(long taskId, String tagUid);
|
||||
|
||||
@Query("DELETE FROM tags WHERE _id = :id")
|
||||
void deleteById(long id);
|
||||
public abstract void deleteById(long id);
|
||||
|
||||
@Transaction
|
||||
public boolean applyTags(Task task, TagDataDao tagDataDao,List<TagData> current) {
|
||||
long taskId = task.getId();
|
||||
Set<TagData> existing = newHashSet(tagDataDao.getTagDataForTask(taskId));
|
||||
Set<TagData> selected = newHashSet(current);
|
||||
Set<TagData> added = difference(selected, existing);
|
||||
Set<TagData> removed = difference(existing, selected);
|
||||
deleteTags(taskId, newArrayList(transform(removed, TagData::getRemoteId)));
|
||||
insert(task, added);
|
||||
return !(removed.isEmpty() && added.isEmpty());
|
||||
}
|
||||
|
||||
public void insert(Task task, Collection<TagData> tags) {
|
||||
if (!tags.isEmpty()) {
|
||||
insert(transform(tags, td -> new Tag(task, td)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package org.tasks.db;
|
||||
|
||||
import static com.google.common.collect.Lists.partition;
|
||||
|
||||
import java.util.List;
|
||||
import org.tasks.Callback;
|
||||
|
||||
public class DbUtils {
|
||||
|
||||
private static final int MAX_SQLITE_ARGS = 999;
|
||||
|
||||
public static <T> void batch(List<T> items, Callback<List<T>> callback) {
|
||||
batch(items, MAX_SQLITE_ARGS, callback);
|
||||
}
|
||||
|
||||
public static <T> void batch(List<T> items, int size, Callback<List<T>> callback) {
|
||||
if (items.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (items.size() <= size) {
|
||||
callback.call(items);
|
||||
} else {
|
||||
for (List<T> sublist : partition(items, size)) {
|
||||
callback.call(sublist);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue