Skip to content

Commit

Permalink
Shorten sheet title if it's too long (#2919)
Browse files Browse the repository at this point in the history
* Shorten sheet title if it's too long

* Added test
  • Loading branch information
grzesiek2010 authored and shobhitagarwal1612 committed Mar 12, 2019
1 parent 1a8fa23 commit 58a3bc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.odk.collect.android.preferences.GeneralSharedPreferences;
import org.odk.collect.android.preferences.GeneralKeys;
import org.odk.collect.android.tasks.FormLoaderTask;
import org.odk.collect.android.utilities.TextUtils;
import org.odk.collect.android.utilities.UrlUtils;
import org.odk.collect.android.utilities.gdrive.DriveHelper;
import org.odk.collect.android.utilities.gdrive.GoogleAccountsManager;
Expand Down Expand Up @@ -173,7 +174,7 @@ public String getUrlToSubmitTo(Instance instance, String deviceId, String overri

private void insertRows(Instance instance, TreeElement element, String parentKey, String key, File instanceFile, String sheetTitle)
throws UploadException {
insertRow(instance, element, parentKey, key, instanceFile, sheetTitle);
insertRow(instance, element, parentKey, key, instanceFile, TextUtils.ellipsizeBeginning(sheetTitle));

int repeatIndex = 0;
for (TreeElement child : getChildElements(element, true)) {
Expand Down Expand Up @@ -338,7 +339,7 @@ private Set<String> getSheetTitles(TreeElement element) {
Set<String> sheetTitles = new HashSet<>();
for (TreeElement childElement : getChildElements(element, false)) {
if (childElement.isRepeatable()) {
sheetTitles.add(getElementTitle(childElement));
sheetTitles.add(TextUtils.ellipsizeBeginning(getElementTitle(childElement)));
sheetTitles.addAll(getSheetTitles(childElement));
}
}
Expand All @@ -351,7 +352,7 @@ private HashMap<String, String> getAnswers(Instance instance, TreeElement elemen
for (TreeElement childElement : getChildElements(element, false)) {
String elementTitle = getElementTitle(childElement);
if (childElement.isRepeatable()) {
answers.put(elementTitle, getHyperlink(getSheetUrl(getSheetId(elementTitle)), elementTitle));
answers.put(elementTitle, getHyperlink(getSheetUrl(getSheetId(TextUtils.ellipsizeBeginning(elementTitle))), elementTitle));
} else {
String answer = childElement.getValue() != null ? childElement.getValue().getDisplayText() : "";
if (new File(instanceFile.getParentFile() + "/" + answer).isFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,11 @@ public static CharSequence textToHtml(String text) {

return Html.fromHtml(markdownToHtml(text));
}

public static String ellipsizeBeginning(String text) {
return text.length() <= 100
? text
: "..." + text.substring(text.length() - 97, text.length());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
public class TextUtilsTest {

Expand Down Expand Up @@ -47,7 +49,7 @@ public void markDownToHtmlEscapesBackslash() {
{"\\\\\\", "\\\\"}};

for (String[] testCase : tests) {
Assert.assertEquals(testCase[1], TextUtils.markdownToHtml(testCase[0]));
assertEquals(testCase[1], TextUtils.markdownToHtml(testCase[0]));
}
}

Expand All @@ -63,7 +65,7 @@ public void markDownToHtml_EscapesLessThan() {
{"test < 1/>", "test &lt; 1/>"}
};
for (String[] testCase: tests) {
Assert.assertEquals(testCase[1], TextUtils.markdownToHtml(testCase[0]));
assertEquals(testCase[1], TextUtils.markdownToHtml(testCase[0]));
}
}

Expand All @@ -77,7 +79,7 @@ public void markDownToHtml_SupportsHtml() {
"</closetag>"
};
for (String testCase: tests) {
Assert.assertEquals(testCase, TextUtils.markdownToHtml(testCase));
assertEquals(testCase, TextUtils.markdownToHtml(testCase));
}
}

Expand All @@ -88,8 +90,23 @@ public void textToHtml_SupportsEscapedLt() {
};

for (String testCase: tests) {
Assert.assertEquals(testCase, TextUtils.textToHtml(testCase).toString());
assertEquals(testCase, TextUtils.textToHtml(testCase).toString());
}
}

@Test
public void ellipsizeBeginningTest() {
//50 chars
assertEquals("Lorem ipsum dolor sit amet, consectetur massa nunc",
TextUtils.ellipsizeBeginning("Lorem ipsum dolor sit amet, consectetur massa nunc"));
//100 chars
assertEquals("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempus, risus ac cursus turpis duis",
TextUtils.ellipsizeBeginning("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempus, risus ac cursus turpis duis"));
//101 chars
assertEquals("...m ipsum dolor sit amet, consectetur adipiscing elit. Cras finibus, augue a imperdiet orci aliquam",
TextUtils.ellipsizeBeginning("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras finibus, augue a imperdiet orci aliquam"));
//150 chars
assertEquals("...it. Donec cursus condimentum sagittis. Ut condimentum efficitur libero, vitae volutpat dui nullam",
TextUtils.ellipsizeBeginning("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus condimentum sagittis. Ut condimentum efficitur libero, vitae volutpat dui nullam"));
}
}

0 comments on commit 58a3bc1

Please sign in to comment.