From a9712d45b3f19077fe2770fb467984c9ec9d6b31 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 15 Dec 2023 17:37:00 +0100 Subject: [PATCH] Fix mentions length count --- .../mastodon/helper/ComposeHelper.java | 48 ++++++++++++++----- .../android/mastodon/helper/Helper.java | 3 ++ .../mastodon/helper/MastodonHelper.java | 2 +- .../mastodon/ui/drawer/ComposeAdapter.java | 2 +- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/ComposeHelper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/ComposeHelper.java index faec45de..230e186f 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/ComposeHelper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/ComposeHelper.java @@ -16,10 +16,16 @@ package app.fedilab.android.mastodon.helper; import static app.fedilab.android.mastodon.helper.Helper.mentionLongPattern; import static app.fedilab.android.mastodon.helper.Helper.mentionPattern; +import static app.fedilab.android.mastodon.helper.Helper.mentionPatternALL; +import static app.fedilab.android.mastodon.helper.MastodonHelper.countWithEmoji; + +import android.util.Patterns; import java.util.ArrayList; import java.util.regex.Matcher; +import app.fedilab.android.mastodon.ui.drawer.ComposeAdapter; + public class ComposeHelper { @@ -35,26 +41,27 @@ public class ComposeHelper { ArrayList mentions = new ArrayList<>(); - int mentionLength = 0; + int mentionLength; StringBuilder mentionString = new StringBuilder(); - Matcher matcher = mentionLongPattern.matcher(content); + Matcher matcher = mentionPatternALL.matcher(content); while (matcher.find()) { String mentionLong = matcher.group(1); - if (!mentions.contains(mentionLong)) { - mentions.add(mentionLong); + if(mentionLong != null) { + if (!mentions.contains(mentionLong)) { + mentions.add(mentionLong); + } } - } - matcher = mentionPattern.matcher(content); - while (matcher.find()) { - String mention = matcher.group(1); - if (!mentions.contains(mention)) { - mentions.add(mention); + String mentionShort = matcher.group(2); + if(mentionShort != null) { + if (!mentions.contains(mentionShort)) { + mentions.add(mentionShort); + } } } for (String mention : mentions) { mentionString.append(mention).append(" "); } - mentionLength = mentionString.length() + 1; + mentionLength = countLength(mentionString.toString()) + 1; int maxCharsPerMessage = maxChars - mentionLength; int totalCurrent = 0; ArrayList reply = new ArrayList<>(); @@ -95,4 +102,23 @@ public class ComposeHelper { } return reply; } + + + /*** + * Returns the length used when composing a toot + * @param mentions String containing mentions + * @return int - characters used + */ + public static int countLength(String mentions) { + String contentCount = mentions; + contentCount = contentCount.replaceAll("(?i)(^|[^/\\w])@(([a-z0-9_]+)@[a-z0-9.-]+[a-z0-9]+)", "$1@$3"); + Matcher matcherALink = Patterns.WEB_URL.matcher(contentCount); + while (matcherALink.find()) { + final String url = matcherALink.group(1); + if (url != null) { + contentCount = contentCount.replace(url, "abcdefghijklmnopkrstuvw"); + } + } + return contentCount.length(); + } } diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java index 46772271..2a7c224e 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java @@ -346,6 +346,9 @@ public class Helper { public static final Pattern groupPattern = Pattern.compile("(![\\w_]+)"); public static final Pattern mentionPattern = Pattern.compile("(@[\\w_.-]?[\\w]+)"); public static final Pattern mentionLongPattern = Pattern.compile("(@[\\w_.-]+@[a-zA-Z0-9][a-zA-Z0-9.-]{1,61}[a-zA-Z0-9](?:\\.[a-zA-Z]{2,})+)"); + + + public static final Pattern mentionPatternALL = Pattern.compile("(@[\\w_.-]+@[a-zA-Z0-9][a-zA-Z0-9.-]{1,61}[a-zA-Z0-9](?:\\.[a-zA-Z]{2,})+)|(@[\\w_.-]?[\\w]+)"); public static final Pattern mathsPattern = Pattern.compile("\\\\\\(|\\\\\\["); public static final Pattern mathsComposePattern = Pattern.compile("\\\\\\(.*\\\\\\)|\\\\\\[.*\\\\\\]"); public static final Pattern twitterPattern = Pattern.compile("((@[\\w]+)@twitter\\.com)"); diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/MastodonHelper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/MastodonHelper.java index 0872ada0..9dc19026 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/MastodonHelper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/MastodonHelper.java @@ -397,7 +397,7 @@ public class MastodonHelper { * @param text String - The current text * @return int - Number of characters used by emoji */ - private static int countWithEmoji(String text) { + static int countWithEmoji(String text) { int emojiCount = 0; for (int i = 0; i < text.length(); i++) { int type = Character.getType(text.charAt(i)); diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/ComposeAdapter.java b/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/ComposeAdapter.java index b2c2d41f..34ef0c3f 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/ComposeAdapter.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/ComposeAdapter.java @@ -541,7 +541,7 @@ public class ComposeAdapter extends RecyclerView.Adapter max_car) { + if (ComposeHelper.countLength(s.toString()) > max_car) { SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); String defaultFormat = sharedpreferences.getString(context.getString(R.string.SET_THREAD_MESSAGE), context.getString(R.string.DEFAULT_THREAD_VALUE)); //User asked to be prompted for threading long messages