Reconstruct task list metadata task order using id maps on different user login

pull/14/head
Sam Bosley 12 years ago
parent 91f1df7ee9
commit 0715c82dc3

@ -105,6 +105,10 @@ import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.service.SyncV2Service; import com.todoroo.astrid.service.SyncV2Service;
import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.subtasks.AstridOrderedListUpdater;
import com.todoroo.astrid.subtasks.AstridOrderedListUpdater.Node;
import com.todoroo.astrid.subtasks.SubtasksHelper;
import com.todoroo.astrid.subtasks.SubtasksHelper.TreeRemapHelper;
import com.todoroo.astrid.tags.TaskToTagMetadata; import com.todoroo.astrid.tags.TaskToTagMetadata;
/** /**
@ -690,7 +694,7 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener
} }
private void generateNewUuids() { private void generateNewUuids() {
HashMap<String, String> uuidTaskMap = new HashMap<String, String>(); final HashMap<String, String> uuidTaskMap = new HashMap<String, String>();
HashMap<String, String> uuidTagMap = new HashMap<String, String>(); HashMap<String, String> uuidTagMap = new HashMap<String, String>();
HashMap<String, String> uuidUserActivityMap = new HashMap<String, String>(); HashMap<String, String> uuidUserActivityMap = new HashMap<String, String>();
HashMap<String, String> uuidTaskListMetadataMap = new HashMap<String, String>(); HashMap<String, String> uuidTaskListMetadataMap = new HashMap<String, String>();
@ -756,15 +760,26 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener
userActivityDao.update(UserActivity.UUID.eq(oldUuid), ua); userActivityDao.update(UserActivity.UUID.eq(oldUuid), ua);
} }
entries = uuidTaskListMetadataMap.entrySet(); TodorooCursor<TaskListMetadata> tlmCursor = taskListMetadataDao.query(Query.select(TaskListMetadata.ID, TaskListMetadata.UUID, TaskListMetadata.TASK_IDS, TaskListMetadata.CHILD_TAG_IDS));
for (Entry<String, String> e : entries) { try {
for (tlmCursor.moveToFirst(); !tlmCursor.isAfterLast(); tlmCursor.moveToNext()) {
tlm.clear(); tlm.clear();
tlm.readFromCursor(tlmCursor);
String oldUuid = e.getKey(); String taskIds = tlm.getValue(TaskListMetadata.TASK_IDS);
String newUuid = e.getValue(); if (!TaskListMetadata.taskIdsIsEmpty(taskIds)) {
Node root = AstridOrderedListUpdater.buildTreeModel(taskIds, null);
tlm.setValue(TaskListMetadata.UUID, newUuid); SubtasksHelper.remapTree(root, uuidTaskMap, new TreeRemapHelper<String>() {
taskListMetadataDao.update(TaskListMetadata.UUID.eq(oldUuid), tlm); public String getKeyFromUuid(String uuid) {
return uuidTaskMap.get(uuid);
}
});
taskIds = AstridOrderedListUpdater.serializeTree(root);
tlm.setValue(TaskListMetadata.TASK_IDS, taskIds);
}
}
} finally {
tlmCursor.close();
} }
} }

@ -139,27 +139,39 @@ public class SubtasksHelper {
return AstridOrderedListUpdater.serializeTree(tree); return AstridOrderedListUpdater.serializeTree(tree);
} }
private static void remapLocalTreeToRemote(Node root, HashMap<Long, String> idMap) { public static interface TreeRemapHelper<T> {
public T getKeyFromUuid(String uuid);
}
public static <T> void remapTree(Node root, HashMap<T, String> idMap, TreeRemapHelper<T> helper) {
ArrayList<Node> children = root.children; ArrayList<Node> children = root.children;
for (int i = 0; i < children.size(); i++) { for (int i = 0; i < children.size(); i++) {
Node child = children.get(i); Node child = children.get(i);
T key = helper.getKeyFromUuid(child.uuid);
long localId = -1L; String uuid = idMap.get(key);
try {
localId = Long.parseLong(child.uuid);
} catch (NumberFormatException e) {/**/}
String uuid = idMap.get(localId);
if (!RemoteModel.isValidUuid(uuid)) { if (!RemoteModel.isValidUuid(uuid)) {
children.remove(i); children.remove(i);
children.addAll(i, child.children); children.addAll(i, child.children);
i--; i--;
} else { } else {
child.uuid = uuid; child.uuid = uuid;
remapLocalTreeToRemote(child, idMap); remapTree(child, idMap, helper);
} }
} }
} }
private static void remapLocalTreeToRemote(Node root, HashMap<Long, String> idMap) {
remapTree(root, idMap, new TreeRemapHelper<Long>() {
public Long getKeyFromUuid(String uuid) {
Long localId = -1L;
try {
localId = Long.parseLong(uuid);
} catch (NumberFormatException e) {/**/}
return localId;
}
});
}
private static <A, B> HashMap<A, B> getIdMap(A[] keys, Property<A> keyProperty, Property<B> valueProperty) { private static <A, B> HashMap<A, B> getIdMap(A[] keys, Property<A> keyProperty, Property<B> valueProperty) {
HashMap<A, B> map = new HashMap<A, B>(); HashMap<A, B> map = new HashMap<A, B>();
TodorooCursor<Task> tasks = PluginServices.getTaskService().query(Query.select(keyProperty, valueProperty).where(keyProperty.in(keys))); TodorooCursor<Task> tasks = PluginServices.getTaskService().query(Query.select(keyProperty, valueProperty).where(keyProperty.in(keys)));

Loading…
Cancel
Save