diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java index dc7cbd2a9..86e6de628 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevControlSet.java @@ -2,27 +2,35 @@ package com.todoroo.astrid.producteev; import java.util.ArrayList; +import org.json.JSONObject; + import android.app.Activity; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; +import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; +import android.widget.AdapterView.OnItemSelectedListener; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.astrid.activity.TaskEditActivity.TaskEditControlSet; import com.todoroo.astrid.model.Metadata; import com.todoroo.astrid.model.StoreObject; import com.todoroo.astrid.model.Task; import com.todoroo.astrid.producteev.sync.ProducteevDashboard; import com.todoroo.astrid.producteev.sync.ProducteevDataService; +import com.todoroo.astrid.producteev.sync.ProducteevSyncProvider; import com.todoroo.astrid.producteev.sync.ProducteevTask; import com.todoroo.astrid.producteev.sync.ProducteevUser; import com.todoroo.astrid.service.MetadataService; @@ -38,6 +46,7 @@ public class ProducteevControlSet implements TaskEditControlSet { // --- instance variables private final Activity activity; + private final DialogUtilities dialogUtilites; private final View view; private Task myTask; @@ -54,6 +63,8 @@ public class ProducteevControlSet implements TaskEditControlSet { DependencyInjectionService.getInstance().inject(this); this.activity = activity; + this.dialogUtilites = new DialogUtilities(); + view = LayoutInflater.from(activity).inflate(R.layout.producteev_control, parent, true); this.responsibleSelector = (Spinner) activity.findViewById(R.id.producteev_TEA_task_assign); @@ -67,9 +78,65 @@ public class ProducteevControlSet implements TaskEditControlSet { @Override public void onItemSelected(AdapterView spinnerParent, View spinnerView, int position, long id) { - Spinner dashSelector = (Spinner) spinnerParent; + final Spinner dashSelector = (Spinner) spinnerParent; ProducteevDashboard dashboard = (ProducteevDashboard) dashSelector.getSelectedItem(); - refreshResponsibleSpinner(dashboard.getUsers()); + if (dashboard.getId() == ProducteevUtilities.DASHBOARD_CREATE) { + // let the user create a new dashboard + final EditText editor = new EditText(ProducteevControlSet.this.activity); + OnClickListener okListener = new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Activity context = ProducteevControlSet.this.activity; + String newDashboardName = editor.getText().toString(); + if (newDashboardName == null || newDashboardName.length() == 0) { + dialog.cancel(); + } else { + // create the real dashboard, select it in the spinner and refresh responsiblespinner + ProgressDialog progressDialog = null; + try { + progressDialog = dialogUtilites.progressDialog(context, + context.getString(R.string.DLG_wait)); + JSONObject newDashJSON = ProducteevSyncProvider.getInvoker().dashboardsCreate(newDashboardName).getJSONObject("dashboard"); + StoreObject local = ProducteevDataService.getInstance().updateDashboards(newDashJSON, true); + if (local != null) { + ProducteevDashboard newDashboard = new ProducteevDashboard(local); + ArrayAdapter adapter = (ArrayAdapter) dashSelector.getAdapter(); + adapter.insert(newDashboard, adapter.getCount()-1); + dashSelector.setSelection(adapter.getCount()-2); + refreshResponsibleSpinner(newDashboard.getUsers()); + dialogUtilites.dismissDialog(context, progressDialog); + } + } catch (Exception e) { + dialogUtilites.dismissDialog(context, progressDialog); + dialogUtilites.okDialog(context, + context.getString(R.string.DLG_error, e.getMessage()), + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + e.printStackTrace(); + dashSelector.setSelection(0); + } + } + + } + }; + OnClickListener cancelListener = new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }; + dialogUtilites.viewDialog(ProducteevControlSet.this.activity, + ProducteevControlSet.this.activity.getString(R.string.producteev_create_dashboard_name), + editor, + okListener, + cancelListener); + } else { + refreshResponsibleSpinner(dashboard.getUsers()); + } } @Override @@ -140,8 +207,8 @@ public class ProducteevControlSet implements TaskEditControlSet { ProducteevDashboard ownerDashboard = null; int dashboardSpinnerIndex = -1; - //dashboard to not sync as first spinner-entry - for (int i=0;i dashAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, dashboards); @@ -157,7 +228,8 @@ public class ProducteevControlSet implements TaskEditControlSet { dashboardSelector.setAdapter(dashAdapter); dashboardSelector.setSelection(dashboardSpinnerIndex+1); - if (ownerDashboard == null || ownerDashboard.getId() == ProducteevUtilities.DASHBOARD_NO_SYNC) { + if (ownerDashboard == null || ownerDashboard.getId() == ProducteevUtilities.DASHBOARD_NO_SYNC + || ownerDashboard.getId() == ProducteevUtilities.DASHBOARD_CREATE) { responsibleSelector.setEnabled(false); responsibleSelector.setAdapter(null); view.findViewById(R.id.producteev_TEA_task_assign_label).setVisibility(View.GONE); diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevUtilities.java b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevUtilities.java index 391e96ef7..f73a39823 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevUtilities.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevUtilities.java @@ -18,6 +18,9 @@ public class ProducteevUtilities extends SyncProviderUtilities { public static final ProducteevUtilities INSTANCE = new ProducteevUtilities(); + /** setting for dashboard to getting created */ + public static final int DASHBOARD_CREATE = -2; + /** setting for dashboard to not synchronize */ public static final int DASHBOARD_NO_SYNC = -1; 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 d7d8899bd..1448456b9 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java @@ -389,6 +389,18 @@ public class ProducteevInvoker { "id_colleague", idColleague); } + /** + * create a dasbhoard + * + * @param name + * @return the new created dashboard as JSONObject + */ + public JSONObject dashboardsCreate(String name) throws ApiServiceException, IOException { + return callAuthenticated("dashboards/create.json", + "token", token, + "title", name); + } + /** * return the list of users who can access a specific dashboard * @@ -396,7 +408,7 @@ public class ProducteevInvoker { * @param dashboard array-information about the dashboard, if this ... */ public JSONArray dashboardsAccess(long idDashboard, String dashboard) throws ApiServiceException, IOException { - return getResponse(callAuthenticated("dashboards/access", + return getResponse(callAuthenticated("dashboards/access.json", "token", token, "id_dashboard", idDashboard, "dashboard", dashboard),"dashboard"); diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDashboard.java b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDashboard.java index 2deefe8cf..5c763d3c3 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDashboard.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDashboard.java @@ -38,7 +38,7 @@ public class ProducteevDashboard { private ArrayList users = null; public ProducteevDashboard (StoreObject dashboardData) { - this(dashboardData.getValue(REMOTE_ID),dashboardData.getValue(NAME),dashboardData.getValue(USERS)); + this(dashboardData.getValue(REMOTE_ID),dashboardData.getValue(NAME),(dashboardData.containsValue(USERS)?dashboardData.getValue(USERS):null)); } /** diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDataService.java b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDataService.java index 086da27c6..429873d50 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDataService.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevDataService.java @@ -243,41 +243,49 @@ public final class ProducteevDataService { readDashboards(); for(int i = 0; i < changedDashboards.length(); i++) { JSONObject remote = changedDashboards.getJSONObject(i).getJSONObject("dashboard"); - long id = remote.getLong("id_dashboard"); - StoreObject local = null; - for(StoreObject dashboard : dashboards) { - if(dashboard.getValue(ProducteevDashboard.REMOTE_ID).equals(id)) { - local = dashboard; - break; - } - } + updateDashboards(remote, false); + } - if(remote.getInt("deleted") != 0) { - if(local != null) - storeObjectDao.delete(local.getId()); - continue; - } + // clear dashboard cache + dashboards = null; + } - if(local == null) - local = new StoreObject(); - local.setValue(StoreObject.TYPE, ProducteevDashboard.TYPE); - local.setValue(ProducteevDashboard.REMOTE_ID, id); - local.setValue(ProducteevDashboard.NAME, remote.getString("title")); - - StringBuilder users = new StringBuilder(); - JSONArray accessList = remote.getJSONArray("accesslist"); - for(int j = 0; j < accessList.length(); j++) { - JSONObject user = accessList.getJSONObject(j).getJSONObject("user"); - users.append(user.getLong("id_user")).append(','); - String name = user.optString("firstname", "") + ' ' + - user.optString("lastname", ""); - users.append(name.trim()).append(';'); + public StoreObject updateDashboards(JSONObject remote, boolean reinitCache) throws JSONException { + if (reinitCache) + readDashboards(); + long id = remote.getLong("id_dashboard"); + StoreObject local = null; + for(StoreObject dashboard : dashboards) { + if(dashboard.getValue(ProducteevDashboard.REMOTE_ID).equals(id)) { + local = dashboard; + break; } - local.setValue(ProducteevDashboard.USERS, users.toString()); - storeObjectDao.persist(local); } - // clear dashboard cache - dashboards = null; + if(remote.getInt("deleted") != 0) { + if(local != null) + storeObjectDao.delete(local.getId()); + } + + if(local == null) + local = new StoreObject(); + local.setValue(StoreObject.TYPE, ProducteevDashboard.TYPE); + local.setValue(ProducteevDashboard.REMOTE_ID, id); + local.setValue(ProducteevDashboard.NAME, remote.getString("title")); + + StringBuilder users = new StringBuilder(); + JSONArray accessList = remote.getJSONArray("accesslist"); + for(int j = 0; j < accessList.length(); j++) { + JSONObject user = accessList.getJSONObject(j).getJSONObject("user"); + users.append(user.getLong("id_user")).append(','); + String name = user.optString("firstname", "") + ' ' + + user.optString("lastname", ""); + users.append(name.trim()).append(';'); + } + local.setValue(ProducteevDashboard.USERS, users.toString()); + storeObjectDao.persist(local); + if (reinitCache) + dashboards = null; + return local; } } \ No newline at end of file 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 29dbdcd12..a1558ba1f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java @@ -255,6 +255,7 @@ public class ProducteevSyncProvider extends SyncProvider Do Not Synchronize + + Add new Workspace... + + + Name for new Workspace + + + new + Default Workspace