Move condensing of lines from StationViewHolder to LineView.

This commit is contained in:
Andreas Schildbach 2018-11-19 14:01:05 +01:00
parent f07e393e44
commit 24b51bf3ff
2 changed files with 66 additions and 62 deletions

View file

@ -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<Line> 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<Product, Integer> 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<Entry<Product, Integer>> sortedEntries = new ArrayList<>(productCounts.entrySet());
Collections.sort(sortedEntries, new Comparator<Entry<Product, Integer>>() {
public int compare(final Entry<Product, Integer> entry1, final Entry<Product, Integer> entry2) {
return entry2.getValue().compareTo(entry1.getValue());
}
});
// condense
for (final Map.Entry<Product, Integer> entry : sortedEntries) {
// condense lines of product to just one label
final Product productToRemove = entry.getKey();
for (final Iterator<Line> 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)

View file

@ -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<Line> lines = new TreeSet<>();
final Set<Product> 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<LineDestination> stationLines = station.getLines();
if (stationLines != null) {
final List<Line> 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<LineDestination> stationLines) {
// count products
final Map<Product, Integer> 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<Entry<Product, Integer>> sortedEntries = new ArrayList<>(productCounts.entrySet());
Collections.sort(sortedEntries, new Comparator<Entry<Product, Integer>>() {
public int compare(final Entry<Product, Integer> entry1, final Entry<Product, Integer> entry2) {
return entry2.getValue().compareTo(entry1.getValue());
}
});
// condense
for (final Map.Entry<Product, Integer> 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<LineDestination, List<Departure>> groupDeparturesByLineDestination(final List<Departure> departures,
final int maxGroups, @Nullable final Set<Product> productsFilter) {
final Map<LineDestination, List<Departure>> departureGroups = new LinkedHashMap<>();