From cded7963f94778b36c1966e4e2d81c3063077c99 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Sat, 18 Dec 2010 14:26:48 -0800 Subject: [PATCH] Changed producteev api to use change_labels call instead of set/unset labels --- .../producteev/api/ProducteevInvoker.java | 30 +++++++++++++++- .../sync/ProducteevSyncProvider.java | 35 ++++++------------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java index 6efa0bbcf..c74e21a8a 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java @@ -12,6 +12,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.text.TextUtils; + @SuppressWarnings("nls") public class ProducteevInvoker { @@ -309,6 +311,25 @@ public class ProducteevInvoker { "id_task", idTask), "labels"); } + /** + * change labels for a task + * + * @param idTask + * @param idLabels + * + * @return array: tasks/view + */ + public JSONObject tasksChangeLabel(long idTask, long... idLabels) throws ApiServiceException, IOException { + Object[] parameters = new Object[2 * (idLabels.length + 2)]; + parameters[0] = "token"; parameters[1] = token; + parameters[2] = "id_task"; parameters[3] = idTask; + for(int i = 0; i < idLabels.length; i++) { + parameters[i * 2 + 4] = "id_label[]"; + parameters[i * 2 + 5] = idLabels[i]; + } + return callAuthenticated("tasks/change_labels.json", parameters); + } + /** * set a labels to a task * @@ -317,6 +338,7 @@ public class ProducteevInvoker { * * @return array: tasks/view */ + @Deprecated public JSONObject tasksSetLabel(long idTask, long idLabel) throws ApiServiceException, IOException { return callAuthenticated("tasks/set_label.json", "token", token, @@ -332,6 +354,7 @@ public class ProducteevInvoker { * * @return array: tasks/view */ + @Deprecated public JSONObject tasksUnsetLabel(long idTask, long idLabel) throws ApiServiceException, IOException { return callAuthenticated("tasks/unset_label.json", "token", token, @@ -506,6 +529,9 @@ public class ProducteevInvoker { return new JSONObject(); } try { + if(TextUtils.isEmpty(response)) + return new JSONObject(); + return new JSONObject(response); } catch (JSONException e) { System.err.println(response); @@ -566,7 +592,9 @@ public class ProducteevInvoker { String encoded = URLEncoder.encode(value, "UTF-8"); requestBuilder.append(key).append('=').append(encoded).append('&'); - sigBuilder.append(key).append(value); + + if(!key.endsWith("[]")) + sigBuilder.append(key).append(value); } sigBuilder.append(apiSecret); diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java index f24974e39..ea00488dd 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java @@ -592,30 +592,17 @@ public class ProducteevSyncProvider extends SyncProvider toAdd = new HashSet(localTags); - toAdd.removeAll(remoteTags); - HashSet toRemove = remoteTags; - toRemove.removeAll(localTags); - - if(toAdd.size() > 0) { - for(String label : toAdd) { - String pdvLabel = idDashboard + label; - if(!labelMap.containsKey(pdvLabel)) { - JSONObject result = invoker.labelsCreate(idDashboard, label).getJSONObject("label"); - long id = putLabelIntoCache(result); - invoker.tasksSetLabel(idTask, id); - } else - invoker.tasksSetLabel(idTask, labelMap.get(pdvLabel)); - } - } - - if(toRemove.size() > 0) { - for(String label : toRemove) { - String pdvLabel = idDashboard + label; - if(!labelMap.containsKey(pdvLabel)) - continue; - invoker.tasksUnsetLabel(idTask, labelMap.get(pdvLabel)); - } + long[] labels = new long[localTags.size()]; + int index = 0; + for(String label : localTags) { + String pdvLabel = idDashboard + label; + final long id; + if(!labelMap.containsKey(pdvLabel)) { + JSONObject result = invoker.labelsCreate(idDashboard, label).getJSONObject("label"); + id = putLabelIntoCache(result); + } else + id = labelMap.get(pdvLabel); + labels[index++] = id; } } }