Delete GtasksTaskContainer

pull/848/head^2
Alex Baker 5 years ago
parent 543af73d77
commit cc097843bc

@ -1,48 +0,0 @@
/*
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.gtasks.sync;
import static org.tasks.gtasks.GoogleTaskSynchronizer.mergeDates;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
import java.util.ArrayList;
import org.tasks.data.GoogleTask;
public class GtasksTaskContainer {
public final Task task;
public final ArrayList<GoogleTask> metadata;
public final GoogleTask gtaskMetadata;
public GtasksTaskContainer(
com.google.api.services.tasks.model.Task remoteTask,
Task localTask,
String listId,
GoogleTask metadata) {
task = localTask;
this.metadata = new ArrayList<>();
this.gtaskMetadata = metadata;
task.setTitle(remoteTask.getTitle());
task.setCreationDate(DateUtilities.now());
task.setCompletionDate(
GtasksApiUtilities.gtasksCompletedTimeToUnixTime(remoteTask.getCompleted()));
long dueDate = GtasksApiUtilities.gtasksDueTimeToUnixTime(remoteTask.getDue());
mergeDates(Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dueDate), task);
task.setNotes(remoteTask.getNotes());
gtaskMetadata.setRemoteId(remoteTask.getId());
gtaskMetadata.setListId(listId);
}
public void prepareForSaving() {
metadata.add(gtaskMetadata);
}
}

@ -24,7 +24,6 @@ import com.todoroo.astrid.gtasks.GtasksListService;
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities; import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
import com.todoroo.astrid.gtasks.api.GtasksInvoker; import com.todoroo.astrid.gtasks.api.GtasksInvoker;
import com.todoroo.astrid.gtasks.api.HttpNotFoundException; import com.todoroo.astrid.gtasks.api.HttpNotFoundException;
import com.todoroo.astrid.gtasks.sync.GtasksTaskContainer;
import com.todoroo.astrid.service.TaskCreator; import com.todoroo.astrid.service.TaskCreator;
import com.todoroo.astrid.service.TaskDeleter; import com.todoroo.astrid.service.TaskDeleter;
import com.todoroo.astrid.utility.Constants; import com.todoroo.astrid.utility.Constants;
@ -390,6 +389,7 @@ public class GoogleTaskSynchronizer {
} }
} }
} }
// TODO: don't updateGtask if it was only moved
gtasksInvoker.updateGtask(listId, remoteModel); gtasksInvoker.updateGtask(listId, remoteModel);
} catch (HttpNotFoundException e) { } catch (HttpNotFoundException e) {
googleTaskDao.delete(gtasksMetadata); googleTaskDao.delete(gtasksMetadata);
@ -454,62 +454,42 @@ public class GoogleTaskSynchronizer {
if (task == null) { if (task == null) {
task = taskCreator.createWithValues(""); task = taskCreator.createWithValues("");
} }
GtasksTaskContainer container = new GtasksTaskContainer(gtask, task, listId, googleTask);
container.gtaskMetadata.setRemoteOrder(Long.parseLong(gtask.getPosition())); task.setTitle(gtask.getTitle());
container.gtaskMetadata.setRemoteParent(gtask.getParent()); task.setCreationDate(DateUtilities.now());
container.gtaskMetadata.setParent( task.setCompletionDate(
GtasksApiUtilities.gtasksCompletedTimeToUnixTime(gtask.getCompleted()));
long dueDate = GtasksApiUtilities.gtasksDueTimeToUnixTime(gtask.getDue());
mergeDates(Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dueDate), task);
task.setNotes(gtask.getNotes());
googleTask.setRemoteId(gtask.getId());
googleTask.setListId(listId);
googleTask.setRemoteOrder(Long.parseLong(gtask.getPosition()));
googleTask.setRemoteParent(gtask.getParent());
googleTask.setParent(
Strings.isNullOrEmpty(gtask.getParent()) ? 0 : googleTaskDao.getTask(gtask.getParent())); Strings.isNullOrEmpty(gtask.getParent()) ? 0 : googleTaskDao.getTask(gtask.getParent()));
container.gtaskMetadata.setLastSync(DateUtilities.now() + 1000L); googleTask.setLastSync(DateUtilities.now() + 1000L);
write(container); write(task, googleTask);
} }
list.setLastSync(lastSyncDate); list.setLastSync(lastSyncDate);
googleTaskListDao.insertOrReplace(list); googleTaskListDao.insertOrReplace(list);
} }
private void write(GtasksTaskContainer task) { private void write(Task task, GoogleTask googleTask) {
if (!TextUtils.isEmpty(task.task.getTitle())) { if (!TextUtils.isEmpty(task.getTitle())) {
task.task.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true); task.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true);
task.task.putTransitory(TaskDao.TRANS_SUPPRESS_REFRESH, true); task.putTransitory(TaskDao.TRANS_SUPPRESS_REFRESH, true);
task.prepareForSaving(); if (task.isNew()) {
if (task.task.isNew()) { taskDao.createNew(task);
taskDao.createNew(task.task);
} }
taskDao.save(task.task); taskDao.save(task);
synchronizeMetadata(task.task.getId(), task.metadata); googleTask.setTask(task.getId());
} if (googleTask.getId() == 0) {
} googleTaskDao.insert(googleTask);
/**
* Synchronize metadata for given task id. Deletes rows in database that are not identical to
* those in the metadata list, creates rows that have no match.
*
* @param taskId id of task to perform synchronization on
* @param metadata list of new metadata items to save
*/
private void synchronizeMetadata(long taskId, ArrayList<GoogleTask> metadata) {
for (GoogleTask metadatum : metadata) {
metadatum.setTask(taskId);
metadatum.setId(0);
}
for (GoogleTask item : googleTaskDao.getAllByTaskId(taskId)) {
long id = item.getId();
// clear item id when matching with incoming values
item.setId(0);
if (metadata.contains(item)) {
metadata.remove(item);
} else { } else {
// not matched. cut it googleTaskDao.update(googleTask);
item.setId(id);
googleTaskDao.delete(item);
} }
} }
// everything that remains shall be written
for (GoogleTask values : metadata) {
googleTaskDao.insert(values);
}
} }
} }

Loading…
Cancel
Save