mirror of https://github.com/tasks/tasks
Move shared functionality to iCalendar
parent
bc9b9744fe
commit
57c642ab9c
@ -1,68 +0,0 @@
|
||||
package org.tasks.caldav;
|
||||
|
||||
import static com.google.common.collect.Iterables.removeIf;
|
||||
import static com.google.common.collect.Iterables.tryFind;
|
||||
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 com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Strings;
|
||||
import java.io.StringReader;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import net.fortuna.ical4j.model.Parameter;
|
||||
import net.fortuna.ical4j.model.parameter.RelType;
|
||||
import net.fortuna.ical4j.model.property.RelatedTo;
|
||||
import org.tasks.data.TagData;
|
||||
import org.tasks.data.TagDataDao;
|
||||
|
||||
public class CaldavUtils {
|
||||
private static final Predicate<RelatedTo> IS_PARENT =
|
||||
r -> r.getParameters().isEmpty() || r.getParameter(Parameter.RELTYPE) == RelType.PARENT;
|
||||
|
||||
public static @Nullable at.bitfire.ical4android.Task fromVtodo(String vtodo) {
|
||||
List<at.bitfire.ical4android.Task> tasks =
|
||||
at.bitfire.ical4android.Task.Companion.tasksFromReader(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;
|
||||
}
|
||||
|
||||
public static void setParent(at.bitfire.ical4android.Task remote, @Nullable String value) {
|
||||
LinkedList<RelatedTo> relatedTo = remote.getRelatedTo();
|
||||
if (Strings.isNullOrEmpty(value)) {
|
||||
removeIf(relatedTo, IS_PARENT);
|
||||
} else {
|
||||
Optional<RelatedTo> parent = tryFind(relatedTo, IS_PARENT);
|
||||
if (parent.isPresent()) {
|
||||
parent.get().setValue(value);
|
||||
} else {
|
||||
relatedTo.add(new RelatedTo(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable String getParent(at.bitfire.ical4android.Task remote) {
|
||||
LinkedList<RelatedTo> relatedTo = remote.getRelatedTo();
|
||||
Optional<RelatedTo> parent = tryFind(relatedTo, IS_PARENT);
|
||||
return parent.isPresent() ? parent.get().getValue() : null;
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import at.bitfire.ical4android.Task
|
||||
import at.bitfire.ical4android.Task.Companion.tasksFromReader
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Strings
|
||||
import com.google.common.collect.Iterables
|
||||
import com.google.common.collect.Lists
|
||||
import com.google.common.collect.Sets.difference
|
||||
import com.google.common.collect.Sets.newHashSet
|
||||
import com.todoroo.andlib.utility.DateUtilities
|
||||
import com.todoroo.astrid.dao.TaskDao
|
||||
import com.todoroo.astrid.data.SyncFlags
|
||||
import com.todoroo.astrid.helper.UUIDHelper
|
||||
import com.todoroo.astrid.service.TaskCreator
|
||||
import net.fortuna.ical4j.model.Parameter
|
||||
import net.fortuna.ical4j.model.parameter.RelType
|
||||
import net.fortuna.ical4j.model.property.RelatedTo
|
||||
import org.tasks.data.*
|
||||
import timber.log.Timber
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.StringReader
|
||||
import javax.inject.Inject
|
||||
|
||||
class iCalendar @Inject constructor(
|
||||
private val tagDataDao: TagDataDao,
|
||||
private val taskCreator: TaskCreator,
|
||||
private val tagDao: TagDao,
|
||||
private val taskDao: TaskDao,
|
||||
private val caldavDao: CaldavDao) {
|
||||
|
||||
companion object {
|
||||
private val IS_PARENT = Predicate { r: RelatedTo? ->
|
||||
r!!.parameters.isEmpty || r.getParameter(Parameter.RELTYPE) === RelType.PARENT
|
||||
}
|
||||
|
||||
fun fromVtodo(vtodo: String): Task? {
|
||||
val tasks = tasksFromReader(StringReader(vtodo))
|
||||
return if (tasks.size == 1) tasks[0] else null
|
||||
}
|
||||
|
||||
fun getParent(remote: Task): String? {
|
||||
val relatedTo = remote.relatedTo
|
||||
val parent = Iterables.tryFind(relatedTo, IS_PARENT)
|
||||
return if (parent.isPresent) parent.get().value else null
|
||||
}
|
||||
|
||||
fun setParent(remote: Task, value: String?) {
|
||||
val relatedTo = remote.relatedTo
|
||||
if (Strings.isNullOrEmpty(value)) {
|
||||
Iterables.removeIf(relatedTo, IS_PARENT)
|
||||
} else {
|
||||
val parent = Iterables.tryFind(relatedTo, IS_PARENT)
|
||||
if (parent.isPresent) {
|
||||
parent.get().value = value
|
||||
} else {
|
||||
relatedTo.add(RelatedTo(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getTags(categories: List<String>): List<TagData> {
|
||||
if (categories.isEmpty()) {
|
||||
return emptyList()
|
||||
}
|
||||
val tags = tagDataDao.getTags(categories)
|
||||
val existing = Lists.transform(tags) { obj: TagData? -> obj!!.name }
|
||||
val toCreate = difference(newHashSet(categories), newHashSet(existing))
|
||||
for (name in toCreate) {
|
||||
val tag = TagData(name)
|
||||
tagDataDao.createNew(tag)
|
||||
tags.add(tag)
|
||||
}
|
||||
return tags
|
||||
}
|
||||
|
||||
fun toVtodo(caldavTask: CaldavTask, task: com.todoroo.astrid.data.Task): ByteArray {
|
||||
val remoteModel = CaldavConverter.toCaldav(caldavTask, task)
|
||||
val categories = remoteModel.categories
|
||||
categories.clear()
|
||||
categories.addAll(Lists.transform(tagDataDao.getTagDataForTask(task.getId())) { obj: TagData? -> obj!!.name })
|
||||
if (Strings.isNullOrEmpty(caldavTask.remoteId)) {
|
||||
val caldavUid = UUIDHelper.newUUID()
|
||||
caldavTask.remoteId = caldavUid
|
||||
remoteModel.uid = caldavUid
|
||||
} else {
|
||||
remoteModel.uid = caldavTask.remoteId
|
||||
}
|
||||
|
||||
val os = ByteArrayOutputStream()
|
||||
remoteModel.write(os)
|
||||
return os.toByteArray()
|
||||
}
|
||||
|
||||
fun fromVtodo(
|
||||
calendar: CaldavCalendar,
|
||||
existing: CaldavTask?,
|
||||
remote: Task,
|
||||
vtodo: String,
|
||||
obj: String? = null,
|
||||
eTag: String? = null) {
|
||||
val task: com.todoroo.astrid.data.Task
|
||||
val caldavTask: CaldavTask
|
||||
if (existing == null) {
|
||||
task = taskCreator.createWithValues("")
|
||||
taskDao.createNew(task)
|
||||
caldavTask = CaldavTask(task.getId(), calendar.uuid, remote.uid, obj)
|
||||
} else {
|
||||
task = taskDao.fetch(existing.task)
|
||||
caldavTask = existing
|
||||
}
|
||||
CaldavConverter.apply(task, remote)
|
||||
tagDao.applyTags(task, tagDataDao, getTags(remote.categories))
|
||||
task.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true)
|
||||
task.putTransitory(TaskDao.TRANS_SUPPRESS_REFRESH, true)
|
||||
taskDao.save(task)
|
||||
caldavTask.vtodo = vtodo
|
||||
caldavTask.etag = eTag
|
||||
caldavTask.lastSync = DateUtilities.now() + 1000L
|
||||
caldavTask.remoteParent = getParent(remote)
|
||||
if (caldavTask.id == com.todoroo.astrid.data.Task.NO_ID) {
|
||||
caldavTask.id = caldavDao.insert(caldavTask)
|
||||
Timber.d("NEW %s", caldavTask)
|
||||
} else {
|
||||
caldavDao.update(caldavTask)
|
||||
Timber.d("UPDATE %s", caldavTask)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue