Skip to content

Commit

Permalink
Seems to fix the gtasks remote id issues, also consolidated some API …
Browse files Browse the repository at this point in the history
…calls for fewer invocations
  • Loading branch information
Tim Su committed Apr 20, 2011
1 parent 10bb077 commit 5097d3f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
4 changes: 2 additions & 2 deletions api/src/com/todoroo/astrid/sync/SyncProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import android.content.Context;
import android.widget.Toast;

import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.NotificationManager;
import com.todoroo.astrid.api.R;
import com.todoroo.astrid.data.Task;
Expand Down Expand Up @@ -303,7 +303,7 @@ protected void sendLocallyCreated(SyncData<TYPE> data,

/* If there exists an incoming remote task with the same name and no
* mapping, we don't want to create this on the remote server,
* because user could have synchronized like this before. Instead,
* because user could have synchronized this before. Instead,
* we create a mapping and do an update.
*/
if (remoteNewTaskNameMap.containsKey(taskTitle)) {
Expand Down
4 changes: 2 additions & 2 deletions astrid/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.timsu.astrid"
android:versionName="3.7.2"
android:versionCode="174">
android:versionName="3.7.3-rc1"
android:versionCode="175">

<!-- widgets, alarms, and services will break if Astrid is installed on SD card -->
<!-- android:installLocation="internalOnly"> -->
Expand Down
Binary file modified astrid/libs/todoroo-g.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@
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.actions.Action;
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.ListActions.TaskBuilder;
import com.todoroo.gtasks.actions.ListActions.TaskCreator;
import com.todoroo.gtasks.actions.ListActions.TaskModifier;
import com.todoroo.gtasks.actions.ListCreationAction;

Expand All @@ -78,6 +79,8 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
private static final Actions a = new Actions();
private static final ListActions l = new ListActions();

ArrayList<GtasksTaskContainer> createdWithoutId;

static {
AstridDependencyInjector.initialize();
}
Expand Down Expand Up @@ -201,6 +204,8 @@ protected void initiateManual(final Activity activity) {
protected void performSync() {
StatisticsService.reportEvent("gtasks-started");
gtasksPreferenceService.recordSyncStart();
System.err.println("- -------- SYNC START!!!!");
createdWithoutId = new ArrayList<GtasksTaskContainer>();

try {
GoogleTaskView taskView = taskService.getTaskView();
Expand All @@ -211,9 +216,7 @@ protected void performSync() {
gtasksTaskListUpdater.createParentSiblingMaps();

// read non-deleted tasks for each list
ArrayList<GtasksTaskContainer> remoteTasks = readAllRemoteTasks(false);

SyncData<GtasksTaskContainer> syncData = populateSyncData(remoteTasks);
SyncData<GtasksTaskContainer> syncData = populateSyncData();
try {
synchronizeTasks(syncData);
} finally {
Expand All @@ -228,6 +231,7 @@ protected void performSync() {

Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH);
ContextManager.getContext().sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
System.err.println("- ------ SYNC FINITO!!!!");
} catch (IllegalStateException e) {
// occurs when application was closed
} catch (Exception e) {
Expand All @@ -242,6 +246,7 @@ private void getActiveList(GoogleTaskView taskView) throws JSONException,
listId = taskView.getActiveTaskList().getInfo().getId();
else if(taskView.getAllLists().length == 0) {
ListCreationAction createList = a.createList(0, ContextManager.getString(R.string.app_name));
System.err.println("ACTION: createList(4)");
taskService.executeActions(createList);
listId = createList.getNewId();
} else {
Expand All @@ -262,6 +267,41 @@ protected void readRemotelyUpdated(SyncData<GtasksTaskContainer> data)
throw new GoogleTasksException(e);
}

// match remote tasks to locally created tasks
HashMap<String, GtasksTaskContainer> locals = new HashMap<String, GtasksTaskContainer>();
for(GtasksTaskContainer task : createdWithoutId) {
locals.put(task.task.getValue(Task.TITLE), task);
}
ArrayList<Action> moveActions = new ArrayList<Action>();
for(GtasksTaskContainer remote : data.remoteUpdated) {
if(remote.task.getId() < 1) {
GtasksTaskContainer local = locals.get(remote.task.getValue(Task.TITLE));
if(local != null) {
System.err.println("FOUND LOCAL - " + remote.task.getValue(Task.TITLE));
remote.task.setId(local.task.getId());

String remoteList = remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
String localList = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
if(!remoteList.equals(localList)) {
try {
System.err.println("MOVE: " + remoteList + " to " + localList);
moveActions.add(a.moveTask(remote.gtaskMetadata.getValue(GtasksMetadata.ID),
remoteList, localList, local.parentId));
} catch (JSONException e) {
Log.e("gtasks-sync", "Error Moving Local Task", e);
}
}
}
}
}
if(moveActions.size() > 0) {
try {
taskService.executeActions(moveActions.toArray(new Action[moveActions.size()]));
} catch (JSONException e) {
Log.e("gtasks-sync", "Error Running Local Action", e);
}
}

super.readRemotelyUpdated(data);
}

Expand All @@ -286,7 +326,12 @@ protected void readRemotelyUpdated(SyncData<GtasksTaskContainer> data)
* Populate SyncData data structure
* @throws JSONException
*/
private SyncData<GtasksTaskContainer> populateSyncData(ArrayList<GtasksTaskContainer> remoteTasks) throws JSONException {
private SyncData<GtasksTaskContainer> populateSyncData() throws JSONException,
GoogleLoginException, IOException {

// fetch remote tasks
ArrayList<GtasksTaskContainer> remoteTasks = readAllRemoteTasks(false);

// fetch locally created tasks
TodorooCursor<Task> localCreated = gtasksMetadataService.getLocallyCreated(PROPERTIES);

Expand All @@ -306,6 +351,7 @@ private ArrayList<GtasksTaskContainer> readAllRemoteTasks(boolean includeDeleted
for(StoreObject dashboard : gtasksListService.getLists()) {
String listId = dashboard.getValue(GtasksList.REMOTE_ID);

System.err.println("ACTION: getTasks, " + listId);
GetTasksAction action = new GetTasksAction(listId, includeDeleted);
taskService.executeActions(action);
List<GoogleTaskTask> list = action.getGoogleTasks();
Expand Down Expand Up @@ -369,20 +415,18 @@ protected GtasksTaskContainer create(GtasksTaskContainer local) throws IOExcepti
if(local.gtaskMetadata.containsNonNullValue(GtasksMetadata.LIST_ID))
list = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
gtasksTaskListUpdater.updateParentAndSibling(local);
local.gtaskMetadata.setValue(GtasksMetadata.ID, null);
local.gtaskMetadata.setValue(GtasksMetadata.LIST_ID, list);

ConvenientTaskCreator createdTask;
createdWithoutId.add(local);
try {
createdTask = taskService.createTask(list, local.task.getValue(Task.TITLE));
TaskCreator createdTask = l.createTask(local.task.getValue(Task.TITLE));
createdTask.parentId(local.parentId);
updateTaskHelper(local, null, createdTask);
} catch (JSONException e) {
throw new RuntimeException(e);
throw new GoogleTasksException(e);
}

String remoteId = updateTaskHelper(local, null, createdTask);
gtasksTaskListUpdater.addRemoteTaskMapping(local.task.getId(), remoteId);
local.gtaskMetadata.setValue(GtasksMetadata.ID, remoteId);
local.gtaskMetadata.setValue(GtasksMetadata.LIST_ID, list);

return local;
}

Expand All @@ -397,6 +441,8 @@ private String updateTaskHelper(GtasksTaskContainer local,
// moving between lists
if(remote != null && !idList.equals(remote.gtaskMetadata.getValue(
GtasksMetadata.LIST_ID))) {
System.err.println("ACTION: moveTask(5), " + idTask + ", " + idList + " to " +
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID));
taskService.executeActions(a.moveTask(idTask, idList,
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), null));
}
Expand All @@ -414,18 +460,23 @@ private String updateTaskHelper(GtasksTaskContainer local,
String id = idList;

// write task (and perform move action if requested)
ListAction action;
if(builder instanceof TaskModifier) {
System.err.println("ACTION: task edit (6), " + idTask);
action = ((TaskModifier) builder).done();
} else if(builder instanceof TaskCreator) {
System.err.println("ACTION: task create (7), " + local.task.getValue(Task.TITLE));
action = ((TaskCreator) builder).done();
} else
throw new GoogleTasksException("Unknown builder " + builder.getClass());

if(idTask != null && local.parentId != null && (remote == null || local.parentId != remote.parentId ||
local.priorSiblingId != remote.priorSiblingId)) {
System.err.println("ACTION: move(1) - " + idTask + ", " + local.parentId + ", " + local.priorSiblingId);
ListAction moveAction = l.move(idTask, local.parentId, local.priorSiblingId);
ListAction action = ((TaskModifier) builder).done();
if(remote == null || local.parentId != remote.parentId || local.priorSiblingId != remote.priorSiblingId)
taskService.executeListActions(idList, action, moveAction);
else if(action.toJson(idList).getJSONObject("entity_delta").length() > 0)
taskService.executeListActions(idList, action);
} else {
id = ((ConvenientTaskCreator)builder).go();
ListAction moveAction = l.move(id, local.parentId, local.priorSiblingId);
taskService.executeListActions(idList, moveAction);
}
taskService.executeListActions(idList, action, moveAction);
} else if(action.toJson(idList).getJSONObject("entity_delta").length() > 0)
taskService.executeListActions(idList, action);

return id;
} catch (JSONException e) {
Expand Down Expand Up @@ -476,6 +527,7 @@ protected void push(GtasksTaskContainer local, GtasksTaskContainer remote) throw
gtasksTaskListUpdater.updateParentAndSibling(local);

String id = local.gtaskMetadata.getValue(GtasksMetadata.ID);
System.err.println("ACTION: modifyTask(3) - " + id);
TaskModifier modifyTask = l.modifyTask(id);
if(shouldTransmit(local, Task.TITLE, remote))
modifyTask.name(local.task.getValue(Task.TITLE));
Expand Down
4 changes: 2 additions & 2 deletions astrid/res/values/strings-gtasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<string name="gtasks_GLA_title">Log In to Google Tasks</string>

<!-- Instructions: Gtasks login -->
<string name="gtasks_GLA_body">Please log in to Google Tasks Sync (Beta!). Google Apps
for Domain is currently unsupported, we\'re working on that!</string>
<string name="gtasks_GLA_body">Please log in to Google Tasks Sync (Beta!). Non-migrated Google Apps
accounts are currently unsupported.</string>

<!-- Instructions: Gtasks further help -->
<string name="gtasks_GLA_further_help">To view your tasks with indentation
Expand Down

0 comments on commit 5097d3f

Please sign in to comment.