Fix mentions length count

improve_thread
Thomas 11 months ago
parent 4711d5fd23
commit a9712d45b3

@ -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.mentionLongPattern;
import static app.fedilab.android.mastodon.helper.Helper.mentionPattern; 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.ArrayList;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import app.fedilab.android.mastodon.ui.drawer.ComposeAdapter;
public class ComposeHelper { public class ComposeHelper {
@ -35,26 +41,27 @@ public class ComposeHelper {
ArrayList<String> mentions = new ArrayList<>(); ArrayList<String> mentions = new ArrayList<>();
int mentionLength = 0; int mentionLength;
StringBuilder mentionString = new StringBuilder(); StringBuilder mentionString = new StringBuilder();
Matcher matcher = mentionLongPattern.matcher(content); Matcher matcher = mentionPatternALL.matcher(content);
while (matcher.find()) { while (matcher.find()) {
String mentionLong = matcher.group(1); String mentionLong = matcher.group(1);
if(mentionLong != null) {
if (!mentions.contains(mentionLong)) { if (!mentions.contains(mentionLong)) {
mentions.add(mentionLong); mentions.add(mentionLong);
} }
} }
matcher = mentionPattern.matcher(content); String mentionShort = matcher.group(2);
while (matcher.find()) { if(mentionShort != null) {
String mention = matcher.group(1); if (!mentions.contains(mentionShort)) {
if (!mentions.contains(mention)) { mentions.add(mentionShort);
mentions.add(mention); }
} }
} }
for (String mention : mentions) { for (String mention : mentions) {
mentionString.append(mention).append(" "); mentionString.append(mention).append(" ");
} }
mentionLength = mentionString.length() + 1; mentionLength = countLength(mentionString.toString()) + 1;
int maxCharsPerMessage = maxChars - mentionLength; int maxCharsPerMessage = maxChars - mentionLength;
int totalCurrent = 0; int totalCurrent = 0;
ArrayList<String> reply = new ArrayList<>(); ArrayList<String> reply = new ArrayList<>();
@ -95,4 +102,23 @@ public class ComposeHelper {
} }
return reply; 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();
}
} }

@ -346,6 +346,9 @@ public class Helper {
public static final Pattern groupPattern = Pattern.compile("(![\\w_]+)"); public static final Pattern groupPattern = Pattern.compile("(![\\w_]+)");
public static final Pattern mentionPattern = Pattern.compile("(@[\\w_.-]?[\\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 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 mathsPattern = Pattern.compile("\\\\\\(|\\\\\\[");
public static final Pattern mathsComposePattern = Pattern.compile("\\\\\\(.*\\\\\\)|\\\\\\[.*\\\\\\]"); public static final Pattern mathsComposePattern = Pattern.compile("\\\\\\(.*\\\\\\)|\\\\\\[.*\\\\\\]");
public static final Pattern twitterPattern = Pattern.compile("((@[\\w]+)@twitter\\.com)"); public static final Pattern twitterPattern = Pattern.compile("((@[\\w]+)@twitter\\.com)");

@ -397,7 +397,7 @@ public class MastodonHelper {
* @param text String - The current text * @param text String - The current text
* @return int - Number of characters used by emoji * @return int - Number of characters used by emoji
*/ */
private static int countWithEmoji(String text) { static int countWithEmoji(String text) {
int emojiCount = 0; int emojiCount = 0;
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
int type = Character.getType(text.charAt(i)); int type = Character.getType(text.charAt(i));

@ -541,7 +541,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
buttonVisibility(holder); buttonVisibility(holder);
//Text is copied pasted and the content is greater than the max of the instance //Text is copied pasted and the content is greater than the max of the instance
int max_car = MastodonHelper.getInstanceMaxChars(context); int max_car = MastodonHelper.getInstanceMaxChars(context);
if (count > max_car) { if (ComposeHelper.countLength(s.toString()) > max_car) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
String defaultFormat = sharedpreferences.getString(context.getString(R.string.SET_THREAD_MESSAGE), context.getString(R.string.DEFAULT_THREAD_VALUE)); 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 //User asked to be prompted for threading long messages

Loading…
Cancel
Save