Skip to content

Commit

Permalink
Merge pull request #1399 from dimagi/em/icons-alt-text
Browse files Browse the repository at this point in the history
Alt text for icons
  • Loading branch information
nospame authored Feb 2, 2024
2 parents ed8413c + 9cf8c64 commit c533b16
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
20 changes: 20 additions & 0 deletions src/cli/java/org/commcare/util/screen/EntityDetailSubscreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.commcare.suite.model.Detail;
import org.commcare.suite.model.DetailField;
import org.commcare.suite.model.Style;
import org.commcare.suite.model.Text;
import org.javarosa.core.model.condition.EvaluationContext;

import javax.annotation.Nullable;
import java.io.PrintStream;
import java.util.ArrayList;

Expand All @@ -25,6 +27,7 @@ public class EntityDetailSubscreen extends Subscreen<EntityScreen> {
private final Object[] data ;
private final String[] headers;
private final Style[] styles;
private final String[] altText;
private final int mCurrentIndex;
private Detail detail;

Expand All @@ -40,6 +43,7 @@ public EntityDetailSubscreen(int currentIndex, Detail detail, EvaluationContext
ArrayList<String> headersTemporary = new ArrayList<>();
ArrayList<Object> dataTemporary = new ArrayList<>();
ArrayList<Style> stylesTemporary = new ArrayList<>();
ArrayList<String> altTextTemporary = new ArrayList<>();

detail.populateEvaluationContextVariables(subContext);

Expand All @@ -51,23 +55,35 @@ public EntityDetailSubscreen(int currentIndex, Detail detail, EvaluationContext
headersTemporary.add(createHeader(field, subContext));
rowTemporary.add(createRow(field, subContext, data));
stylesTemporary.add(createStyle(field));
altTextTemporary.add(createAltText(field, subContext));
}
}

rows = new String[rowTemporary.size()];
headers = new String[rowTemporary.size()];
data = new Object[rowTemporary.size()];
styles = new Style[rowTemporary.size()];
altText = new String[rowTemporary.size()];

rowTemporary.toArray(rows);
headersTemporary.toArray(headers);
dataTemporary.toArray(data);
stylesTemporary.toArray(styles);
altTextTemporary.toArray(altText);

mDetailListTitles = detailListTitles;
mCurrentIndex = currentIndex;
}

@Nullable
private String createAltText(DetailField field, EvaluationContext ec) {
Text altText = field.getAltText();
if (altText != null) {
return altText.evaluate(ec);
}
return null;
}

private Style createStyle(DetailField field) {
return new Style(field);
}
Expand Down Expand Up @@ -177,4 +193,8 @@ public void setDetail(Detail detail) {
public Style[] getStyles() {
return styles;
}

public String[] getAltText() {
return altText;
}
}
8 changes: 7 additions & 1 deletion src/main/java/org/commcare/cases/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Entity<T> {
private final T t;
private Object[] data;
private String[] sortData;
private String[] altTextData;
private boolean[] relevancyData;
/**
* Key used to attach external data (i.e. from case list callout) to an entity
Expand All @@ -28,14 +29,15 @@ protected Entity(T t, String extraKey) {
}

public Entity(Object[] data, String[] sortData, boolean[] relevancyData, T t,
String extraKey, boolean shouldReceiveFocus, String groupKey) {
String extraKey, boolean shouldReceiveFocus, String groupKey, String[] altTextData) {
this.t = t;
this.sortData = sortData;
this.data = data;
this.relevancyData = relevancyData;
this.extraKey = extraKey;
this.shouldReceiveFocus = shouldReceiveFocus;
this.groupKey = groupKey;
this.altTextData = altTextData;
}

public Object getField(int i) {
Expand Down Expand Up @@ -88,6 +90,10 @@ public Object[] getData() {
return data;
}

public String[] getAltText() {
return altTextData;
}

public String[] getSortFieldPieces(int i) {
String sortField = getSortField(i);
if (sortField == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Entity<TreeReference> getEntity(TreeReference data) {

Object[] fieldData = new Object[length];
String[] sortData = new String[length];
String[] altTextData = new String[length];
boolean[] relevancyData = new boolean[length];
int count = 0;
for (DetailField f : detail.getFields()) {
Expand All @@ -63,6 +64,12 @@ public Entity<TreeReference> getEntity(TreeReference data) {
sortData[count] = sortText.evaluate(nodeContext);
}
relevancyData[count] = f.isRelevant(nodeContext);
Text altText = f.getAltText();
if (altText == null) {
altTextData[count] = null;
} else {
altTextData[count] = altText.evaluate(nodeContext);
}
} catch (XPathSyntaxException e) {
/**
* TODO: 25/06/17 remove catch blocks from here
Expand All @@ -82,7 +89,7 @@ public Entity<TreeReference> getEntity(TreeReference data) {
}

return new Entity<>(fieldData, sortData, relevancyData, data, extraKey,
detail.evaluateFocusFunction(nodeContext), groupKey);
detail.evaluateFocusFunction(nodeContext), groupKey, altTextData);
}

/**
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/org/commcare/suite/model/DetailField.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.commcare.suite.model;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.annotation.Nullable;

import org.javarosa.core.model.Constants;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.util.externalizable.DeserializationException;
Expand All @@ -13,12 +19,6 @@
import org.javarosa.xpath.expr.XPathExpression;
import org.javarosa.xpath.parser.XPathSyntaxException;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.annotation.Nullable;

/**
* Detail Fields represent the <field> elements of a suite's detail
* definitions. The model contains the relevent text templates as well
Expand All @@ -45,7 +45,8 @@ public class DetailField implements Externalizable {
private String headerWidthHint = null; // Something like "500" or "10%"
private String templateWidthHint = null;
private String printIdentifier;

@Nullable
private Text altText;
@Nullable
private EndpointAction endpointAction;

Expand Down Expand Up @@ -188,6 +189,7 @@ public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOExcep
header = (Text)ExtUtil.read(in, Text.class, pf);
template = (DetailTemplate)ExtUtil.read(in, new ExtWrapTagged(DetailTemplate.class), pf);
sort = (Text)ExtUtil.read(in, new ExtWrapNullable(Text.class), pf);
altText = (Text)ExtUtil.read(in, new ExtWrapNullable(Text.class), pf);

//Unfortunately I don't think there's a clean way to do this
if (ExtUtil.readBool(in)) {
Expand Down Expand Up @@ -219,6 +221,7 @@ public void writeExternal(DataOutputStream out) throws IOException {
ExtUtil.write(out, header);
ExtUtil.write(out, new ExtWrapTagged(template));
ExtUtil.write(out, new ExtWrapNullable(sort));
ExtUtil.write(out, new ExtWrapNullable(altText));

boolean relevantSet = relevancy != null;
ExtUtil.writeBool(out, relevantSet);
Expand Down Expand Up @@ -270,6 +273,11 @@ public String getVerticalAlign() {
return verticalAlign;
}

@Nullable
public Text getAltText() {
return altText;
}

@Nullable
public EndpointAction getEndpointAction() {
return endpointAction;
Expand Down Expand Up @@ -313,6 +321,13 @@ public void setHeader(Text header) {
field.header = header;
}

/**
* @param altText the alt text to set
*/
public void setAltText(Text altText) {
field.altText = altText;
}

/**
* @param template the template to set
*/
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/commcare/xml/DetailFieldParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public DetailField parse() throws InvalidStructureException, IOException, XmlPul
}
while (nextTagInBlock("field")) {
//sort details
checkNode(new String[]{"sort", "background", "endpoint_action"});
checkNode(new String[]{"sort", "background", "endpoint_action", "alt_text"});

String name = parser.getName().toLowerCase();

Expand All @@ -87,6 +87,11 @@ public DetailField parse() throws InvalidStructureException, IOException, XmlPul
skipBlock("background");
} else if (name.equals("endpoint_action")){
parseEndpointAction(builder);
} else if (name.equals("alt_text")){
parser.nextTag();
checkNode("text");
Text altText = new TextParser(parser).parse();
builder.setAltText(altText);
}
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ public void testDetailWithFieldAction() {
assertFalse(endpoint.isRespectRelevancy());
}

@Test
public void testDetailWithAltText() {
Detail detail = mApp.getSession().getPlatform().getDetail("m0_case_short");
DetailField field = detail.getFields()[0];
Text altText = field.getAltText();
assertEquals("gold star", altText.evaluate());
}

@Test
public void testDetailWithBorder() {
Detail detail = mApp.getSession().getPlatform().getDetail("m0_case_short");
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/app_structure/app_strings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
m0_no_items_text=Empty List
m0_select_text=Continue With Case
m0_select_text=Continue With Case
m0.case_short.case_starred_1.alt_text.k0=gold star
9 changes: 9 additions & 0 deletions src/test/resources/app_structure/suite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
<xpath function="case_name"/>
</text>
</sort>
<alt_text>
<text>
<xpath function="$k0">
<variable name="k0">
<locale id="m0.case_short.case_starred_1.alt_text.k0"/>
</variable>
</xpath>
</text>
</alt_text>
<endpoint_action endpoint_id="case_list" background="true"/>
</field>
</detail>
Expand Down

0 comments on commit c533b16

Please sign in to comment.