Synchronize CalDAV categories

pull/848/head
Alex Baker 5 years ago
parent 1c55f2bc3c
commit 8e8332b0b7

@ -53,6 +53,21 @@ public class TagDataDaoTest extends InjectingTestCase {
assertEquals("Derp", tagDataDao.getTagWithCase("derp"));
}
@Test
public void getTagsByName() {
TagData tagData = newTagData(with(NAME, "Derp"));
tagDataDao.createNew(tagData);
assertEquals(singletonList(tagData), tagDataDao.getTags(singletonList("Derp")));
}
@Test
public void getTagsByNameCaseSensitive() {
tagDataDao.createNew(newTagData(with(NAME, "Derp")));
assertTrue(tagDataDao.getTags(singletonList("derp")).isEmpty());
}
@Test
public void getTagDataForTask() {
Task taskOne = newTask();

@ -38,6 +38,7 @@ import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
@ -55,6 +56,10 @@ import org.tasks.data.CaldavAccount;
import org.tasks.data.CaldavCalendar;
import org.tasks.data.CaldavDao;
import org.tasks.data.CaldavTask;
import org.tasks.data.Tag;
import org.tasks.data.TagDao;
import org.tasks.data.TagData;
import org.tasks.data.TagDataDao;
import org.tasks.injection.ForApplication;
import timber.log.Timber;
@ -67,6 +72,8 @@ public class CaldavSynchronizer {
private final CaldavDao caldavDao;
private final TaskDao taskDao;
private final TagDataDao tagDataDao;
private final TagDao tagDao;
private final LocalBroadcastManager localBroadcastManager;
private final TaskCreator taskCreator;
private final TaskDeleter taskDeleter;
@ -80,6 +87,8 @@ public class CaldavSynchronizer {
@ForApplication Context context,
CaldavDao caldavDao,
TaskDao taskDao,
TagDataDao tagDataDao,
TagDao tagDao,
LocalBroadcastManager localBroadcastManager,
TaskCreator taskCreator,
TaskDeleter taskDeleter,
@ -89,6 +98,8 @@ public class CaldavSynchronizer {
this.context = context;
this.caldavDao = caldavDao;
this.taskDao = taskDao;
this.tagDataDao = tagDataDao;
this.tagDao = tagDao;
this.localBroadcastManager = localBroadcastManager;
this.taskCreator = taskCreator;
this.taskDeleter = taskDeleter;
@ -300,7 +311,9 @@ public class CaldavSynchronizer {
}
at.bitfire.ical4android.Task remoteModel = CaldavConverter.toCaldav(caldavTask, task);
LinkedList<String> categories = remoteModel.getCategories();
categories.clear();
categories.addAll(transform(tagDataDao.getTagDataForTask(task.getId()), TagData::getName));
if (Strings.isNullOrEmpty(caldavTask.getRemoteId())) {
String caldavUid = UUIDHelper.newUUID();
caldavTask.setRemoteId(caldavUid);
@ -353,6 +366,7 @@ public class CaldavSynchronizer {
task = taskDao.fetch(caldavTask.getTask());
}
CaldavConverter.apply(task, remote);
applyCategories(task, remote.getCategories());
task.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true);
taskDao.save(task);
caldavTask.setVtodo(vtodo);
@ -369,4 +383,25 @@ public class CaldavSynchronizer {
Timber.e("Received VCALENDAR with %s VTODOs; ignoring %s", tasks.size(), fileName);
}
}
private void applyCategories(Task task, List<String> categories) {
long taskId = task.getId();
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);
}
Set<TagData> existing = newHashSet(tagDataDao.getTagDataForTask(taskId));
Set<TagData> selected = newHashSet(selectedTags);
Set<TagData> added = difference(selected, existing);
Set<TagData> removed = difference(existing, selected);
tagDao.deleteTags(taskId, newArrayList(Iterables.transform(removed, TagData::getRemoteId)));
for (TagData tagData : added) {
Tag newLink = new Tag(task, tagData);
tagDao.insert(newLink);
}
}
}

@ -46,6 +46,11 @@ public final class TagData implements Parcelable {
@ColumnInfo(name = "td_icon")
private Integer icon = -1;
@Ignore
public TagData(String name) {
this.name = name;
}
public TagData() {}
@Ignore

@ -42,9 +42,13 @@ public abstract class TagDataDao {
@Query("SELECT tagdata.* FROM tagdata "
+ "INNER JOIN tags ON tags.tag_uid = tagdata.remoteId "
+ "WHERE tags.task = :id")
+ "WHERE tags.task = :id "
+ "ORDER BY UPPER(tagdata.name) ASC")
public abstract List<TagData> getTagDataForTask(long id);
@Query("SELECT * FROM tagdata WHERE name IN (:names)")
public abstract List<TagData> getTags(List<String> names);
@Update
public abstract void update(TagData tagData);

Loading…
Cancel
Save