From b3a0344d6d263d4b274491b3a0ac42030919e96a Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 5 Jan 2021 15:06:34 -0500 Subject: [PATCH] textutils cleanup --- .../fragments/LocationFragment.java | 28 +++++++--- .../models/NotificationModel.java | 2 +- .../awais/instagrabber/utils/TextUtils.java | 55 ------------------- .../res/layout/layout_location_details.xml | 12 +--- 4 files changed, 25 insertions(+), 72 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index c48d4939..bb3f3f74 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -19,7 +19,6 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; import android.widget.Toast; import androidx.activity.OnBackPressedCallback; @@ -72,6 +71,7 @@ import awais.instagrabber.webservices.StoriesService; import awaisomereport.LogCollector; import static androidx.core.content.PermissionChecker.checkSelfPermission; +import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG; import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION; import static awais.instagrabber.utils.Utils.logCollector; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -414,15 +414,29 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR if (TextUtils.isEmpty(biography)) { locationDetailsBinding.locationBiography.setVisibility(View.GONE); - } else if (TextUtils.hasMentions(biography)) { - locationDetailsBinding.locationBiography.setVisibility(View.VISIBLE); - biography = TextUtils.getMentionText(biography); - locationDetailsBinding.locationBiography.setText(biography, TextView.BufferType.SPANNABLE); - // binding.locationBiography.setMentionClickListener(mentionClickListener); } else { locationDetailsBinding.locationBiography.setVisibility(View.VISIBLE); locationDetailsBinding.locationBiography.setText(biography); - locationDetailsBinding.locationBiography.setMentionClickListener(null); + locationDetailsBinding.locationBiography.addOnHashtagListener(autoLinkItem -> { + final NavController navController = NavHostFragment.findNavController(this); + final Bundle bundle = new Bundle(); + final String originalText = autoLinkItem.getOriginalText().trim(); + bundle.putString(ARG_HASHTAG, originalText); + navController.navigate(R.id.action_global_hashTagFragment, bundle); + }); + locationDetailsBinding.locationBiography.addOnMentionClickListener(autoLinkItem -> { + final String originalText = autoLinkItem.getOriginalText().trim(); + navigateToProfile(originalText); + }); + locationDetailsBinding.locationBiography.addOnEmailClickListener(autoLinkItem -> Utils.openEmailAddress(getContext(), + autoLinkItem.getOriginalText() + .trim())); + locationDetailsBinding.locationBiography + .addOnURLClickListener(autoLinkItem -> Utils.openURL(getContext(), autoLinkItem.getOriginalText().trim())); + locationDetailsBinding.locationBiography.setOnLongClickListener(v -> { + Utils.copyText(context, biography); + return true; + }); } if (!locationModel.getGeo().startsWith("geo:0.0,0.0?z=17")) { diff --git a/app/src/main/java/awais/instagrabber/models/NotificationModel.java b/app/src/main/java/awais/instagrabber/models/NotificationModel.java index a7b78b47..5c39ecc3 100755 --- a/app/src/main/java/awais/instagrabber/models/NotificationModel.java +++ b/app/src/main/java/awais/instagrabber/models/NotificationModel.java @@ -29,7 +29,7 @@ public final class NotificationModel { final String previewUrl, final NotificationType type) { this.id = id; - this.text = TextUtils.hasMentions(text) ? TextUtils.getMentionText(text) : text; + this.text = text; this.timestamp = timestamp; this.userId = userId; this.username = username; diff --git a/app/src/main/java/awais/instagrabber/utils/TextUtils.java b/app/src/main/java/awais/instagrabber/utils/TextUtils.java index 65fb2447..95e82599 100644 --- a/app/src/main/java/awais/instagrabber/utils/TextUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/TextUtils.java @@ -24,56 +24,6 @@ import java.util.regex.Pattern; import awais.instagrabber.customviews.CommentMentionClickSpan; public final class TextUtils { - private static final Pattern URL_PATTERN = Pattern.compile( - "(^|[\\s.:;?\\-\\]<\\(])((https?://|www\\.|pic\\.)[-\\w;/?:@&=+$\\|\\_.!~*\\|'()\\[\\]%#,☺]+[\\w/#](\\(\\))?)(?=$|[\\s',\\|\\(\\).:;?\\-\\[\\]>\\)])"); - - @NonNull - public static CharSequence getMentionText(@NonNull final CharSequence text) { - final int commentLength = text.length(); - final SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text, 0, commentLength); - - for (int i = 0; i < commentLength; ++i) { - char currChar = text.charAt(i); - - if (currChar == '@' || currChar == '#') { - final int startLen = i; - - do { - if (++i == commentLength) break; - currChar = text.charAt(i); - - if (currChar == '.' && i + 1 < commentLength) { - final char nextChar = text.charAt(i + 1); - if (nextChar == '.' || nextChar == ' ' || nextChar == '#' || nextChar == '@' || nextChar == '/' - || nextChar == '\r' || nextChar == '\n') { - break; - } - } else if (currChar == '.') - break; - - // for merged hashtags - if (currChar == '#') { - --i; - break; - } - } while (currChar != ' ' && currChar != '\r' && currChar != '\n' && currChar != '>' && currChar != '<' - && currChar != ':' && currChar != ';' && currChar != '\'' && currChar != '"' && currChar != '[' - && currChar != ']' && currChar != '\\' && currChar != '=' && currChar != '-' && currChar != '!' - && currChar != '$' && currChar != '%' && currChar != '^' && currChar != '&' && currChar != '*' - && currChar != '(' && currChar != ')' && currChar != '{' && currChar != '}' && currChar != '/' - && currChar != '|' && currChar != '?' && currChar != '`' && currChar != '~' - ); - - final int endLen = currChar != '#' ? i : i + 1; // for merged hashtags - stringBuilder.setSpan(new CommentMentionClickSpan(), startLen, - Math.min(commentLength, endLen), // fixed - crash when end index is greater than comment length ( @kernoeb ) - Spanned.SPAN_EXCLUSIVE_INCLUSIVE); - } - } - - return stringBuilder; - } - // extracted from String class public static int indexOfChar(@NonNull final CharSequence sequence, final int ch, final int startIndex) { final int max = sequence.length(); @@ -90,11 +40,6 @@ public final class TextUtils { return -1; } - public static boolean hasMentions(final CharSequence text) { - if (isEmpty(text)) return false; - return indexOfChar(text, '@', 0) != -1 || indexOfChar(text, '#', 0) != -1; - } - public static CharSequence getSpannableUrl(final String url) { if (isEmpty(url)) return url; final int httpIndex = url.indexOf("http:"); diff --git a/app/src/main/res/layout/layout_location_details.xml b/app/src/main/res/layout/layout_location_details.xml index 9176519a..cee275cb 100644 --- a/app/src/main/res/layout/layout_location_details.xml +++ b/app/src/main/res/layout/layout_location_details.xml @@ -83,24 +83,18 @@ app:layout_constraintTop_toBottomOf="@id/mainLocationImage" tools:text="OUR HOUSE" /> - + tools:text="IN THE MIDDLE OF OUR STREET" />