diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevFilterExposer.java b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevFilterExposer.java index 99ba4d183..6b442e407 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevFilterExposer.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevFilterExposer.java @@ -3,10 +3,14 @@ */ package com.todoroo.astrid.producteev; +import java.util.TreeMap; +import java.util.Map.Entry; + import android.content.BroadcastReceiver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.util.Pair; import com.timsu.astrid.R; import com.todoroo.andlib.sql.Criterion; @@ -52,6 +56,25 @@ public class ProducteevFilterExposer extends BroadcastReceiver { return filter; } + private Filter filterFromUser(Context context, String user, Pair ids) { + String title = context.getString(R.string.producteev_FEx_responsible_title, user); + ContentValues values = new ContentValues(); + values.put(Metadata.KEY.name, ProducteevTask.METADATA_KEY); + values.put(ProducteevTask.DASHBOARD_ID.name, ids.first); + values.put(ProducteevTask.ID.name, 0); + values.put(ProducteevTask.CREATOR_ID.name, 0); + values.put(ProducteevTask.RESPONSIBLE_ID.name, ids.second); + Filter filter = new Filter(user, title, new QueryTemplate().join( + ProducteevDataService.METADATA_JOIN).where(Criterion.and( + MetadataCriteria.withKey(ProducteevTask.METADATA_KEY), + TaskCriteria.isActive(), + TaskCriteria.isVisible(), + ProducteevTask.RESPONSIBLE_ID.eq(ids.second))), + values); + + return filter; + } + @Override public void onReceive(Context context, Intent intent) { // if we aren't logged in, don't expose features @@ -60,26 +83,60 @@ public class ProducteevFilterExposer extends BroadcastReceiver { StoreObject[] dashboards = ProducteevDataService.getInstance().getDashboards(); - // If user does not have any tags, don't show this section at all + // If user does not have any dashboards, don't show this section at all if(dashboards.length == 0) return; + FilterListHeader producteevHeader = new FilterListHeader(context.getString(R.string.producteev_FEx_header)); + + // load dashboards Filter[] dashboardFilters = new Filter[dashboards.length]; for(int i = 0; i < dashboards.length; i++) dashboardFilters[i] = filterFromList(context, new ProducteevDashboard(dashboards[i])); - - FilterListHeader producteevHeader = new FilterListHeader(context.getString(R.string.producteev_FEx_header)); FilterCategory producteevDashboards = new FilterCategory(context.getString(R.string.producteev_FEx_dashboard), dashboardFilters); + // load responsible people + TreeMap> people = loadResponsiblePeople(dashboards); + Filter[] peopleFilters = new Filter[people.size()]; + int index = 0; + for(Entry> person : people.entrySet()) + peopleFilters[index++] = filterFromUser(context, person.getKey(), person.getValue()); + FilterCategory producteevUsers = new FilterCategory(context.getString(R.string.producteev_FEx_responsible), + peopleFilters); + // transmit filter list - FilterListItem[] list = new FilterListItem[2]; + FilterListItem[] list = new FilterListItem[3]; list[0] = producteevHeader; list[1] = producteevDashboards; + list[2] = producteevUsers; Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS); broadcastIntent.putExtra(AstridApiConstants.EXTRAS_ADDON, ProducteevUtilities.IDENTIFIER); broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, list); context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ); } + /** + * @param dashboards + * @return people in a map of name => pair(dashboard id, user id) + */ + @SuppressWarnings("nls") + private TreeMap> loadResponsiblePeople( + StoreObject[] dashboards) { + TreeMap> results = new TreeMap>(); + for(StoreObject dashboard : dashboards) { + String users = dashboard.getValue(ProducteevDashboard.USERS); + String[] entries = users.split(";"); + for(String entry : entries) { + String[] data = entry.split(","); + if(data.length != 2) + continue; + results.put(data[1], new Pair( + dashboard.getValue(ProducteevDashboard.REMOTE_ID), data[0])); // name, id + } + } + + return results; + } + } diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevUser.java b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevUser.java index adcdae80d..7af862b69 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevUser.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevUser.java @@ -3,7 +3,6 @@ package com.todoroo.astrid.producteev.sync; import org.json.JSONException; import org.json.JSONObject; - /** * * @author Arne Jans @@ -11,74 +10,76 @@ import org.json.JSONObject; @SuppressWarnings("nls") public class ProducteevUser { - private final long id; - - private final String email; + private final long id; - private final String firstname; + private final String email; - private final String lastname; + private final String firstname; - public ProducteevUser(long id, String email, String firstname, String lastname) { - this.id = id; - this.email = email; - this.firstname = firstname; - this.lastname = lastname; - } + private final String lastname; - public ProducteevUser(JSONObject elt) throws JSONException { - this.id = elt.getLong("id"); - this.email = elt.getString("email"); - this.firstname = elt.getString("firstname"); - this.lastname = elt.getString("lastname"); - } + public ProducteevUser(long id, String email, String firstname, + String lastname) { + this.id = id; + this.email = email; + this.firstname = firstname; + this.lastname = lastname; + } - /** - * @return the email - */ -public String getEmail() { - return email; -} + public ProducteevUser(JSONObject elt) throws JSONException { + this.id = elt.getLong("id"); + this.email = elt.getString("email"); + this.firstname = elt.getString("firstname"); + this.lastname = elt.getString("lastname"); + } -/** - * @return the firstname - */ -public String getFirstname() { - return firstname; -} + /** + * @return the email + */ + public String getEmail() { + return email; + } -/** - * @return the lastname - */ -public String getLastname() { - return lastname; -} + /** + * @return the firstname + */ + public String getFirstname() { + return firstname; + } -/** - * @return the id - */ -public long getId() { - return id; - } + /** + * @return the lastname + */ + public String getLastname() { + return lastname; + } -@Override -public String toString() { - String displayString = ""; - boolean hasFirstname = false; - boolean hasLastname = false; - if (firstname != null && firstname.length() > 0) { - displayString += firstname; - hasFirstname = true; + /** + * @return the id + */ + public long getId() { + return id; } - if (lastname != null && lastname.length() > 0) - hasLastname = true; - if (hasFirstname && hasLastname) - displayString += " "; - if (hasLastname) - displayString += lastname; - if (!hasFirstname && !hasLastname && email != null && email.length() > 0) - displayString += email; - return displayString; -} + @Override + public String toString() { + String displayString = ""; + boolean hasFirstname = false; + boolean hasLastname = false; + if (firstname != null && firstname.length() > 0) { + displayString += firstname; + hasFirstname = true; + } + if (lastname != null && lastname.length() > 0) + hasLastname = true; + if (hasFirstname && hasLastname) + displayString += " "; + if (hasLastname) + displayString += lastname; + + if (!hasFirstname && !hasLastname && email != null + && email.length() > 0) + displayString += email; + return displayString; + } }