Fix for AST-410 - sync w/ google tasks removes due time

pull/14/head
Tim Su 14 years ago
parent 211dc5024b
commit 104382f108

@ -5,6 +5,7 @@ package com.todoroo.astrid.gtasks.sync;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -31,6 +32,7 @@ import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.StoreObject;
import com.todoroo.astrid.data.Task;
@ -53,17 +55,17 @@ import com.todoroo.astrid.utility.Flags;
import com.todoroo.gtasks.GoogleConnectionManager;
import com.todoroo.gtasks.GoogleLoginException;
import com.todoroo.gtasks.GoogleTaskService;
import com.todoroo.gtasks.GoogleTaskService.ConvenientTaskCreator;
import com.todoroo.gtasks.GoogleTaskTask;
import com.todoroo.gtasks.GoogleTaskView;
import com.todoroo.gtasks.GoogleTasksException;
import com.todoroo.gtasks.GoogleTaskService.ConvenientTaskCreator;
import com.todoroo.gtasks.actions.Actions;
import com.todoroo.gtasks.actions.GetTasksAction;
import com.todoroo.gtasks.actions.ListAction;
import com.todoroo.gtasks.actions.ListActions;
import com.todoroo.gtasks.actions.ListCreationAction;
import com.todoroo.gtasks.actions.ListActions.TaskBuilder;
import com.todoroo.gtasks.actions.ListActions.TaskModifier;
import com.todoroo.gtasks.actions.ListCreationAction;
@SuppressWarnings("nls")
public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
@ -446,6 +448,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
long dueDate = remoteTask.getTask_date();
task.setValue(Task.DUE_DATE, task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dueDate));
task.setValue(Task.NOTES, remoteTask.getNotes());
task.setValue(Task.NOTES, remoteTask.getNotes());
Metadata gtasksMetadata = GtasksMetadata.createEmptyMetadata(AbstractModel.NO_ID);
gtasksMetadata.setValue(GtasksMetadata.ID, remoteTask.getId());
@ -495,9 +498,28 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
@Override
protected void write(GtasksTaskContainer task) throws IOException {
// merge astrid dates with google dates
if(task.task.isSaved()) {
Task local = PluginServices.getTaskService().fetchById(task.task.getId(), Task.DUE_DATE);
mergeDates(task.task, local);
}
gtasksMetadataService.saveTaskAndMetadata(task);
}
/** pick up due time from local task */
private void mergeDates(Task remote, Task local) {
if(remote.hasDueDate() && local.hasDueTime()) {
Date newDate = new Date(remote.getValue(Task.DUE_DATE));
Date oldDate = new Date(local.getValue(Task.DUE_DATE));
newDate.setHours(oldDate.getHours());
newDate.setMinutes(oldDate.getMinutes());
newDate.setSeconds(oldDate.getSeconds());
remote.setValue(Task.DUE_DATE, remote.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME,
newDate.getTime()));
}
}
// ----------------------------------------------------------------------
// --------------------------------------------------------- misc helpers
// ----------------------------------------------------------------------

@ -0,0 +1,131 @@
package com.todoroo.astrid.gtasks;
import java.util.ArrayList;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gtasks.sync.GtasksSyncProvider;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.test.DatabaseTestCase;
import com.todoroo.gtasks.GoogleTaskListInfo;
import com.todoroo.gtasks.GoogleTaskService;
import com.todoroo.gtasks.GoogleTaskTask;
import com.todoroo.gtasks.GoogleTaskView;
import com.todoroo.gtasks.actions.ListAction;
import com.todoroo.gtasks.actions.ListActions;
public class GtasksSyncTest extends DatabaseTestCase {
private static final String TEST_USERNAME = "tasktest@todoroo.com";
private static final String TEST_PASSWORD = "tasktest0000";
private static boolean initialized = false;
private static GoogleTaskService testService;
private static GoogleTaskListInfo taskList;
@Autowired TaskService taskService;
// --- tests
public void testBasicTaskCreation() throws Exception {
Task task = givenTask("wasabi");
whenSynchronizing();
thenAssertTaskExistsRemotely(task);
}
public void testTaskWithDueDate() throws Exception {
Task task = givenTask("wasabi");
task.setValue(Task.DUE_DATE, task.createDueDate(Task.URGENCY_SPECIFIC_DAY,
DateUtilities.now()));
whenSynchronizing();
GoogleTaskTask remote = thenAssertTaskExistsRemotely(task);
assertTrue(remote.getTask_date() > DateUtilities.now() - DateUtilities.ONE_DAY);
assertEquals(task.getValue(Task.DUE_DATE), refetchLocal(task).getValue(Task.DUE_DATE));
}
public void testTaskWithDueTime() throws Exception {
Task task = givenTask("wasabi");
task.setValue(Task.DUE_DATE, task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME,
DateUtilities.now()));
whenSynchronizing();
GoogleTaskTask remote = thenAssertTaskExistsRemotely(task);
assertTrue(remote.getTask_date() > DateUtilities.now() - DateUtilities.ONE_DAY);
assertEquals(task.getValue(Task.DUE_DATE), refetchLocal(task).getValue(Task.DUE_DATE));
}
// --- helpers
private Task refetchLocal(Task task) {
return taskService.fetchById(task.getId(), Task.PROPERTIES);
}
private GoogleTaskTask thenAssertTaskExistsRemotely(Task task) throws Exception {
for(GoogleTaskTask remote : testService.getTasks(taskList.getId())) {
if(remote.getName().equals(task.getValue(Task.TITLE)))
return remote;
}
fail("Task didn't exist remotely: " + task);
return null;
}
private void whenSynchronizing() {
new GtasksSyncProvider().synchronize(getContext());
}
private Task givenTask(String title) {
Task task = new Task();
task.setValue(Task.TITLE, title);
taskService.save(task);
return task;
}
// --- setup stuff
// set up task list and clean it out
@Override
protected void setUp() throws Exception {
super.setUp();
if(!initialized)
initializeTestService();
ArrayList<ListAction> actions = new ArrayList<ListAction>();
ListActions l = new ListActions();
for(GoogleTaskTask task : testService.getTasks(taskList.getId())) {
actions.add(l.modifyTask(task.getId()).deleted(true).done());
}
testService.executeListActions(taskList.getId(), actions.toArray(new ListAction[actions.size()]));
}
public static void initializeTestService() throws Exception {
testService = new GoogleTaskService(TEST_USERNAME, TEST_PASSWORD);
GoogleTaskView taskView = testService.getTaskView();
GoogleTaskListInfo[] lists = taskView.getAllLists();
outer: {
for(GoogleTaskListInfo list : lists) {
if("AstridUnitTests".equals(list.getName())) {
taskList = list;
break outer;
}
}
fail("could not find the main list");
}
initialized = true;
Preferences.setString(GtasksPreferenceService.PREF_DEFAULT_LIST, taskList.getId());
Preferences.setString(GtasksPreferenceService.PREF_USER_NAME, TEST_USERNAME);
Preferences.setString(GtasksPreferenceService.PREF_PASSWORD, TEST_PASSWORD);
}
}
Loading…
Cancel
Save