|
1 | 1 | package fr.free.nrw.commons.upload;
|
2 | 2 |
|
3 | 3 | import android.app.Dialog;
|
4 |
| -import android.content.Intent; |
5 | 4 | import android.text.Editable;
|
6 | 5 | import android.text.InputFilter;
|
7 | 6 | import android.text.TextUtils;
|
|
23 | 22 | import butterknife.ButterKnife;
|
24 | 23 | import com.google.android.material.textfield.TextInputLayout;
|
25 | 24 | import fr.free.nrw.commons.R;
|
26 |
| -import fr.free.nrw.commons.contributions.MainActivity; |
27 | 25 | import fr.free.nrw.commons.recentlanguages.Language;
|
28 | 26 | import fr.free.nrw.commons.recentlanguages.RecentLanguagesAdapter;
|
29 | 27 | import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao;
|
|
32 | 30 | import java.util.ArrayList;
|
33 | 31 | import java.util.HashMap;
|
34 | 32 | import java.util.List;
|
35 |
| -import java.util.Locale; |
36 | 33 | import java.util.Objects;
|
37 | 34 | import java.util.regex.Pattern;
|
38 |
| -import javax.inject.Inject; |
39 | 35 | import timber.log.Timber;
|
40 | 36 |
|
41 | 37 | public class UploadMediaDetailAdapter extends RecyclerView.Adapter<UploadMediaDetailAdapter.ViewHolder> {
|
@@ -198,7 +194,8 @@ public void bind(int position) {
|
198 | 194 |
|
199 | 195 | removeButton.setOnClickListener(v -> removeDescription(uploadMediaDetail, position));
|
200 | 196 | captionListener = new AbstractTextWatcher(
|
201 |
| - captionText -> uploadMediaDetails.get(position).setCaptionText(convertIdeographicSpaceToLatinSpace(removeTrailingWhitespace(captionText)))); |
| 197 | + captionText -> uploadMediaDetails.get(position).setCaptionText(convertIdeographicSpaceToLatinSpace( |
| 198 | + removeLeadingAndTrailingWhitespace(captionText)))); |
202 | 199 | descriptionListener = new AbstractTextWatcher(
|
203 | 200 | descriptionText -> uploadMediaDetails.get(position).setDescriptionText(descriptionText));
|
204 | 201 | captionItemEditText.addTextChangedListener(captionListener);
|
@@ -421,28 +418,35 @@ private void setUpRecentLanguagesSection(final List<Language> recentLanguages) {
|
421 | 418 | }
|
422 | 419 |
|
423 | 420 | /**
|
424 |
| - * Checks if the source string contains trailing whitespace |
| 421 | + * Removes any leading and trailing whitespace from the source text. |
425 | 422 | * @param source input string
|
426 |
| - * @return true if contains trailing whitespace and false otherwise |
| 423 | + * @return a string without leading and trailing whitespace |
427 | 424 | */
|
428 |
| - public Boolean checkTrailingWhitespace(String source) { |
429 |
| - int len = source.length(); |
430 |
| - if (len == 0) { |
431 |
| - return false; |
| 425 | + public String removeLeadingAndTrailingWhitespace(String source) { |
| 426 | + // This method can be replaced with the inbuilt String::strip when updated to JDK 11. |
| 427 | + // Note that String::trim does not adequately remove all whitespace chars. |
| 428 | + int firstNonWhitespaceIndex = 0; |
| 429 | + while (firstNonWhitespaceIndex < source.length()) { |
| 430 | + if (Character.isWhitespace(source.charAt(firstNonWhitespaceIndex))) { |
| 431 | + firstNonWhitespaceIndex++; |
| 432 | + } else { |
| 433 | + break; |
| 434 | + } |
| 435 | + } |
| 436 | + if (firstNonWhitespaceIndex == source.length()) { |
| 437 | + return ""; |
432 | 438 | }
|
433 |
| - return Character.isWhitespace(source.charAt(len - 1)); |
434 |
| - } |
435 | 439 |
|
436 |
| - /** |
437 |
| - * Removes any trailing whitespace from the source text. |
438 |
| - * @param source input string |
439 |
| - * @return a string without trailing whitespace |
440 |
| - */ |
441 |
| - public String removeTrailingWhitespace(String source) { |
442 |
| - while (checkTrailingWhitespace(source)) { |
443 |
| - source = source.substring(0, source.length() - 1); |
| 440 | + int lastNonWhitespaceIndex = source.length() - 1; |
| 441 | + while (lastNonWhitespaceIndex > firstNonWhitespaceIndex) { |
| 442 | + if (Character.isWhitespace(source.charAt(lastNonWhitespaceIndex))) { |
| 443 | + lastNonWhitespaceIndex--; |
| 444 | + } else { |
| 445 | + break; |
| 446 | + } |
444 | 447 | }
|
445 |
| - return source; |
| 448 | + |
| 449 | + return source.substring(firstNonWhitespaceIndex, lastNonWhitespaceIndex + 1); |
446 | 450 | }
|
447 | 451 |
|
448 | 452 | /**
|
|
0 commit comments