Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten sheet title if it's too long #2919

Merged
merged 2 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}