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.SyncV2Service;
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;
/**
@ -690,7 +694,7 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener
}
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> uuidUserActivityMap = 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);
}
entries = uuidTaskListMetadataMap.entrySet();
for (Entry<String, String> e : entries) {
TodorooCursor<TaskListMetadata> tlmCursor = taskListMetadataDao.query(Query.select(TaskListMetadata.ID, TaskListMetadata.UUID, TaskListMetadata.TASK_IDS, TaskListMetadata.CHILD_TAG_IDS));
try {
for (tlmCursor.moveToFirst(); !tlmCursor.isAfterLast(); tlmCursor.moveToNext()) {
tlm.clear();
tlm.readFromCursor(tlmCursor);
String oldUuid = e.getKey();
String newUuid = e.getValue();
tlm.setValue(TaskListMetadata.UUID, newUuid);
taskListMetadataDao.update(TaskListMetadata.UUID.eq(oldUuid), tlm);
String taskIds = tlm.getValue(TaskListMetadata.TASK_IDS);
if (!TaskListMetadata.taskIdsIsEmpty(taskIds)) {
Node root = AstridOrderedListUpdater.buildTreeModel(taskIds, null);
SubtasksHelper.remapTree(root, uuidTaskMap, new TreeRemapHelper<String>() {
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);
}
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;
for (int i = 0; i < children.size(); i++) {
Node child = children.get(i);
long localId = -1L;
try {
localId = Long.parseLong(child.uuid);
} catch (NumberFormatException e) {/**/}
String uuid = idMap.get(localId);
T key = helper.getKeyFromUuid(child.uuid);
String uuid = idMap.get(key);
if (!RemoteModel.isValidUuid(uuid)) {
children.remove(i);
children.addAll(i, child.children);
i--;
} else {
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) {
HashMap<A, B> map = new HashMap<A, B>();
TodorooCursor<Task> tasks = PluginServices.getTaskService().query(Query.select(keyProperty, valueProperty).where(keyProperty.in(keys)));

Loading…
Cancel
Save