diff --git a/oeffi/src/de/schildbach/oeffi/stations/LineView.java b/oeffi/src/de/schildbach/oeffi/stations/LineView.java index 8af6f39..e3a4034 100644 --- a/oeffi/src/de/schildbach/oeffi/stations/LineView.java +++ b/oeffi/src/de/schildbach/oeffi/stations/LineView.java @@ -17,13 +17,23 @@ package de.schildbach.oeffi.stations; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import de.schildbach.oeffi.R; import de.schildbach.oeffi.util.CheatSheet; +import de.schildbach.pte.Standard; import de.schildbach.pte.dto.Line; import de.schildbach.pte.dto.Line.Attr; +import de.schildbach.pte.dto.Product; import de.schildbach.pte.dto.Style; import de.schildbach.pte.dto.Style.Shape; @@ -47,6 +57,7 @@ import android.widget.TextView; public class LineView extends TextView { private Collection lines = null; private boolean ghosted = false; + private int condenseThreshold = 0; private final float strokeWidth; private final int colorTextGhosted; @@ -87,8 +98,49 @@ public class LineView extends TextView { update(); } + public void setCondenseThreshold(final int condenseThreshold) { + this.condenseThreshold = condenseThreshold; + update(); + } + private void update() { if (lines != null && !lines.isEmpty()) { + if (condenseThreshold > 0 && lines.size() > condenseThreshold) { + // count products + final Map productCounts = new HashMap<>(); + for (final Line line : lines) { + final Product product = line.product; + Integer count = productCounts.get(product); + if (count == null) + count = 0; + productCounts.put(product, count + 1); + } + + // sort by count + final List> sortedEntries = new ArrayList<>(productCounts.entrySet()); + Collections.sort(sortedEntries, new Comparator>() { + public int compare(final Entry entry1, final Entry entry2) { + return entry2.getValue().compareTo(entry1.getValue()); + } + }); + + // condense + for (final Map.Entry entry : sortedEntries) { + // condense lines of product to just one label + final Product productToRemove = entry.getKey(); + for (final Iterator i = lines.iterator(); i.hasNext();) { + final Line line = i.next(); + if (line.product == productToRemove) + i.remove(); + } + lines.add(new Line(null, null, productToRemove, null, Standard.STYLES.get(productToRemove))); + + // stop condensing if few enough lines in total + if (lines.size() <= condenseThreshold) + break; + } + } + final SpannableStringBuilder text = new SpannableStringBuilder(); for (final Line line : lines) { if (text.length() > 0) diff --git a/oeffi/src/de/schildbach/oeffi/stations/list/StationViewHolder.java b/oeffi/src/de/schildbach/oeffi/stations/list/StationViewHolder.java index 701e5f4..bd92cd0 100644 --- a/oeffi/src/de/schildbach/oeffi/stations/list/StationViewHolder.java +++ b/oeffi/src/de/schildbach/oeffi/stations/list/StationViewHolder.java @@ -17,17 +17,13 @@ package de.schildbach.oeffi.stations.list; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.Date; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; +import java.util.TreeSet; import javax.annotation.Nullable; @@ -91,7 +87,7 @@ public class StationViewHolder extends RecyclerView.ViewHolder { private final int colorText, colorTextLessSignificant, colorTextGhosted; private final int listEntryVerticalPadding; - private static final int CONDENSE_LINES_THRESHOLD = 6; // TODO!!! + private static final int CONDENSE_LINES_THRESHOLD = 5; private static final int MESSAGE_INDEX_COLOR = Color.parseColor("#c08080"); public StationViewHolder(final Context context, final View itemView, final int maxDepartures, @@ -150,18 +146,22 @@ public class StationViewHolder extends RecyclerView.ViewHolder { name2View.setTextColor(color); // lines + final Set lines = new TreeSet<>(); + final Set products = station.location.products; + if (products != null) + for (final Product product : products) + lines.add(new Line(null, null, product, null, Standard.STYLES.get(product))); final List stationLines = station.getLines(); if (stationLines != null) { - final List lines = new ArrayList<>(stationLines.size()); - if (stationLines.size() > CONDENSE_LINES_THRESHOLD) - condenseLinesByProduct(stationLines); - for (final LineDestination line : stationLines) - lines.add(line.line); - linesView.setLines(lines); - } else { - linesView.setLines(null); + for (final LineDestination lineDestination : stationLines) { + final Line line = lineDestination.line; + lines.add(line); + lines.remove(new Line(null, null, line.product, null, Standard.STYLES.get(line.product))); + } } linesView.setGhosted(isGhosted); + linesView.setCondenseThreshold(CONDENSE_LINES_THRESHOLD); + linesView.setLines(!lines.isEmpty() ? lines : null); // distance distanceView.setText(station.hasDistanceAndBearing ? Formats.formatDistance(station.distance) : null); @@ -392,54 +392,6 @@ public class StationViewHolder extends RecyclerView.ViewHolder { itemView.setLongClickable(true); } - private void condenseLinesByProduct(final List stationLines) { - // count products - final Map productCounts = new HashMap<>(); - for (final LineDestination lineDestination : stationLines) { - final Product product = lineDestination.line.product; - Integer count = productCounts.get(product); - if (count == null) - count = 0; - productCounts.put(product, count + 1); - } - - // sort by count - final List> sortedEntries = new ArrayList<>(productCounts.entrySet()); - Collections.sort(sortedEntries, new Comparator>() { - public int compare(final Entry entry1, final Entry entry2) { - return entry2.getValue().compareTo(entry1.getValue()); - } - }); - - // condense - for (final Map.Entry entry : sortedEntries) { - // stop condensing if few enough lines in total - if (stationLines.size() <= CONDENSE_LINES_THRESHOLD) - break; - - // condense lines of product to just one label - final Product productToRemove = entry.getKey(); - int size = stationLines.size(); - boolean first = true; - for (int i = 0; i < size;) { - if (stationLines.get(i).line.product == productToRemove) { - stationLines.remove(i); - if (first) { - stationLines.add(i, new LineDestination( - new Line(null, null, productToRemove, null, Standard.STYLES.get(productToRemove)), - null)); - first = false; - i++; - } else { - size--; - } - } else { - i++; - } - } - } - } - private Map> groupDeparturesByLineDestination(final List departures, final int maxGroups, @Nullable final Set productsFilter) { final Map> departureGroups = new LinkedHashMap<>();