nice up code & nicer description of TUIFlight passes

This commit is contained in:
ligi 2014-03-05 21:05:46 +05:30
parent 1d58a792e2
commit da2890794c
7 changed files with 164 additions and 95 deletions

View file

@ -95,6 +95,7 @@ dependencies {
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:0.9.2'
compile 'org.ligi:AXT:0.25'
compile 'org.ligi:tracedroid:1.1'
compile 'com.google.guava:guava:16.0.1'
// TODO watch progress at zxing - last official had problems with aztec format
// snapshot works - but switch back to official when available
@ -106,7 +107,7 @@ dependencies {
testCompile 'com.squareup:fest-android:1.0.7'
testCompile fileTree(dir: 'libs', include: 'core-2.3.0-SNAPSHOT.jar')
testCompile 'com.googlecode.android-query:android-query:0.25.+'
//testCompile 'com.googlecode.android-query:android-query:0.25.+'
testCompile 'com.google.code.gson:gson:2.2.4'
testCompile 'org.ligi:tracedroid:1.1'
testCompile 'org.ligi:AXT:0.25'

View file

@ -11,13 +11,12 @@ import android.widget.TextView;
import org.ligi.passandroid.App;
import org.ligi.passandroid.R;
import org.ligi.passandroid.model.Passbook;
import org.ligi.passandroid.model.PassField;
import org.ligi.passandroid.model.PassFieldList;
import org.ligi.passandroid.model.ReducedPassInformation;
import org.ligi.passandroid.ui.NavigateToLocationsDialog;
import org.ligi.passandroid.ui.views.CategoryIndicatorView;
import java.util.List;
import butterknife.ButterKnife;
public class PassVisualizer {
@ -92,9 +91,9 @@ public class PassVisualizer {
}
public static String getFieldListAsString(List<Passbook.Field> fieldList) {
public static String getFieldListAsString(PassFieldList fieldList) {
String result = "";
for (Passbook.Field f : fieldList) {
for (PassField f : fieldList) {
result += "<b>" + f.label + "</b>: " + f.value + "<br/>";
}
return result;

View file

@ -0,0 +1,23 @@
package org.ligi.passandroid.model;
import org.json.JSONException;
import org.json.JSONObject;
public class PassField {
public final String key;
public final String label;
public final String value;
public PassField(JSONObject jsonObject) throws JSONException {
label = jsonObject.getString("label");
value = jsonObject.getString("value");
if (jsonObject.has("key")) {
key = jsonObject.getString("key");
} else {
key = null;
}
}
}

View file

@ -0,0 +1,52 @@
package org.ligi.passandroid.model;
import com.google.common.base.Optional;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ligi.tracedroid.logging.Log;
import java.util.ArrayList;
public class PassFieldList extends ArrayList<PassField> {
public PassFieldList(JSONObject ticketJSONObject, String fieldsName) {
try {
final JSONArray jsonArray = ticketJSONObject.getJSONArray(fieldsName);
for (int i = 0; i < jsonArray.length(); i++) {
try {
final PassField field = new PassField(jsonArray.getJSONObject(i));
add(field);
} catch (JSONException e) {
Log.w("could not process PassField from JSON for " + fieldsName + " cause: " + e);
}
}
} catch (JSONException e) {
Log.w("could not process PassFields " + fieldsName + " from JSON " + e);
}
}
public Optional<PassField> getPassFieldForKey(String key) {
for (PassField field : this) {
if (field.key != null && field.key.equals(key)) {
return Optional.of(field);
}
}
return Optional.absent();
}
public Optional<PassField> getPassFieldThatMatchesLabel(String matcher) {
for (PassField field : this) {
if (field.label != null && field.key.matches(matcher)) {
return Optional.of(field);
}
}
return Optional.absent();
}
}

View file

@ -37,9 +37,9 @@ public class Passbook {
private int foregroundColor;
private String description;
private DateTime relevantDate;
private List<Field> primaryFields, secondaryFields, backFields, auxiliaryFields, headerFields;
private PassFieldList primaryFields, secondaryFields, backFields, auxiliaryFields, headerFields;
private List<PassLocation> locations = new ArrayList<PassLocation>();
private JSONObject eventTicket = null;
private JSONObject ticketJSONObject = null;
public String plainJsonString;
public static final String[] TYPES = new String[]{"coupon", "eventTicket", "boardingPass", "generic", "storeCard"};
@ -180,40 +180,18 @@ public class Passbook {
e.printStackTrace();
}
} else try {
eventTicket = pass_json.getJSONObject(type);
ticketJSONObject = pass_json.getJSONObject(type);
} catch (JSONException e) {
}
}
primaryFields = getFieldListFromJsonArr(eventTicket, "primaryFields");
secondaryFields = getFieldListFromJsonArr(eventTicket, "secondaryFields");
auxiliaryFields = getFieldListFromJsonArr(eventTicket, "auxiliaryFields");
backFields = getFieldListFromJsonArr(eventTicket, "backFields");
headerFields = getFieldListFromJsonArr(eventTicket, "headerFields");
// for airberlin
if (description.equals("boardcard")) {
final String flight_regex = "\\b\\w{1,3}\\d{3,4}\\b";
for (Field f : headerFields) {
if (f.label.matches(flight_regex)) {
description = f.label + " " + f.value;
}
}
for (Field f : auxiliaryFields) {
if (f.key != null && f.key.equals("seat")) {
description += " | " + f.label + " " + f.value;
}
}
for (Field f : secondaryFields) {
if (f.key != null && f.key.equals("boardingGroup")) {
description += " | " + f.label + " " + f.value;
}
}
if (ticketJSONObject != null) {
primaryFields = new PassFieldList(ticketJSONObject, "primaryFields");
secondaryFields = new PassFieldList(ticketJSONObject, "secondaryFields");
auxiliaryFields = new PassFieldList(ticketJSONObject, "auxiliaryFields");
backFields = new PassFieldList(ticketJSONObject, "backFields");
headerFields = new PassFieldList(ticketJSONObject, "headerFields");
}
}
@ -255,23 +233,23 @@ public class Passbook {
return type;
}
public List<Field> getPrimaryFields() {
public PassFieldList getPrimaryFields() {
return primaryFields;
}
public List<Field> getSecondaryFields() {
public PassFieldList getSecondaryFields() {
return secondaryFields;
}
public List<Field> getBackFields() {
public PassFieldList getBackFields() {
return backFields;
}
public List<Field> getAuxiliaryFields() {
public PassFieldList getAuxiliaryFields() {
return auxiliaryFields;
}
public List<Field> getHeaderFields() {
public PassFieldList getHeaderFields() {
return headerFields;
}
@ -279,40 +257,6 @@ public class Passbook {
return locations;
}
/**
* returns a list of Fields for the key - empty list when no elements - not nul
*
* @param obj
* @param key
* @return
*/
public List<Field> getFieldListFromJsonArr(JSONObject obj, String key) {
ArrayList<Field> res = new ArrayList<Field>();
JSONArray arr = null;
if (obj != null) try {
arr = obj.getJSONArray(key);
for (int i = 0; i < arr.length(); i++) {
Field f = new Field();
try {
final JSONObject jsonObject = arr.getJSONObject(i);
f.label = jsonObject.getString("label");
f.value = jsonObject.getString("value");
if (jsonObject.has("key")) {
f.key = jsonObject.getString("key");
}
res.add(f);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
}
return res;
}
private int parseColor(String color_str, int defaultValue) {
if (color_str == null) {
return defaultValue;
@ -445,12 +389,6 @@ public class Passbook {
return foregroundColor;
}
public class Field {
public String key;
public String label;
public String value;
}
public boolean hasRelevantDate() {
return relevantDate != null;
}

View file

@ -0,0 +1,53 @@
package org.ligi.passandroid.model;
import com.google.common.base.Optional;
public class PrettifiedPassbookDecorator {
private String prettifiedDescription;
private final Passbook sourcePassbook;
public PrettifiedPassbookDecorator(Passbook sourcePassbook) {
this.sourcePassbook = sourcePassbook;
// default is what we have
prettifiedDescription = sourcePassbook.getDescription();
careForAirBerlin();
careForTUIFlight();
}
private void careForTUIFlight() {
if (sourcePassbook.getDescription().equals("TUIfly pass")) {
Optional<PassField> flightField = sourcePassbook.getAuxiliaryFields().getPassFieldForKey("Flug");
Optional<PassField> originField = sourcePassbook.getPrimaryFields().getPassFieldForKey("Origin");
Optional<PassField> destinationField = sourcePassbook.getPrimaryFields().getPassFieldForKey("Des");
Optional<PassField> seatField = sourcePassbook.getAuxiliaryFields().getPassFieldForKey("SeatNumber");
if (flightField.isPresent() && originField.isPresent() && destinationField.isPresent() && seatField.isPresent()) {
prettifiedDescription = flightField.get().value + " " + originField.get().value + "->" + destinationField.get().value + " @" + seatField.get().value;
}
}
}
private void careForAirBerlin() {
if (sourcePassbook.getDescription().equals("boardcard")) {
final String flightRegex = "\\b\\w{1,3}\\d{3,4}\\b";
Optional<PassField> flightField = sourcePassbook.getAuxiliaryFields().getPassFieldThatMatchesLabel(flightRegex);
Optional<PassField> seatField = sourcePassbook.getAuxiliaryFields().getPassFieldForKey("seat");
Optional<PassField> boardingGroupField = sourcePassbook.getSecondaryFields().getPassFieldForKey("boardingGroup");
if (flightField.isPresent() && seatField.isPresent() && boardingGroupField.isPresent()) {
prettifiedDescription = flightField.get().label + " " + flightField.get().value;
prettifiedDescription = " | " + seatField.get().label + " " + seatField.get().value;
prettifiedDescription = " | " + boardingGroupField.get().label + " " + boardingGroupField.get().value;
} // otherwise fallback to default - better save than sorry
}
}
public String getPrettyDescription() {
return prettifiedDescription;
}
}

View file

@ -6,19 +6,6 @@ import java.io.Serializable;
public class ReducedPassInformation implements Serializable {
public ReducedPassInformation(Passbook pass) {
type = pass.getType();
if (pass.hasRelevantDate()) {
relevantDate = pass.getRelevantDate();
}
backgroundColor = pass.getBackGroundColor();
foregroundColor = pass.getForegroundColor();
name = pass.getDescription();
iconPath = pass.getIconPath();
id = pass.getId();
hasLocation = !pass.getLocations().isEmpty();
}
public String id;
public String type;
public String name;
@ -28,6 +15,22 @@ public class ReducedPassInformation implements Serializable {
public DateTime relevantDate;
public boolean hasLocation;
public ReducedPassInformation(Passbook pass) {
PrettifiedPassbookDecorator prettyPassBook = new PrettifiedPassbookDecorator(pass);
type = pass.getType();
if (pass.hasRelevantDate()) {
relevantDate = pass.getRelevantDate();
}
backgroundColor = pass.getBackGroundColor();
foregroundColor = pass.getForegroundColor();
name = prettyPassBook.getPrettyDescription();
iconPath = pass.getIconPath();
id = pass.getId();
hasLocation = !pass.getLocations().isEmpty();
}
public String getTypeNotNull() {
if (type == null) {
return "none";