package app.fedilab.android.mastodon.helper; /* Copyright 2023 Thomas Schneider * * This file is a part of Fedilab * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 3 of the * License, or (at your option) any later version. * * Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * Public License for more details. * * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ import static app.fedilab.android.mastodon.helper.Helper.mentionLongPattern; import static app.fedilab.android.mastodon.helper.Helper.mentionPattern; import java.util.ArrayList; import java.util.regex.Matcher; public class ComposeHelper { /** * Allows to split the toot by dot "." for sentences - adds number at the end automatically * * @param content String initial content * @param maxChars int the max chars per toot * @return ArrayList split toot */ public static ArrayList splitToots(String content, int maxChars) { String[] splitContent = content.split("\\s"); ArrayList mentions = new ArrayList<>(); int mentionLength = 0; StringBuilder mentionString = new StringBuilder(); Matcher matcher = mentionLongPattern.matcher(content); while (matcher.find()) { String mentionLong = matcher.group(1); 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); } } for (String mention : mentions) { mentionString.append(mention).append(" "); } mentionLength = mentionString.length() + 1; int maxCharsPerMessage = maxChars - mentionLength; int totalCurrent = 0; ArrayList reply = new ArrayList<>(); int index = 0; for (String s : splitContent) { if ((totalCurrent + s.length() + 1) < maxCharsPerMessage) { totalCurrent += (s.length() + 1); } else { if (content.length() > totalCurrent && totalCurrent > 0) { String tempContent = content.substring(0, (totalCurrent)); content = content.substring(totalCurrent); reply.add(index, tempContent); index++; totalCurrent = s.length() + 1; } } } if (totalCurrent > 0) { reply.add(index, content); } if (reply.size() > 1) { int i = 0; for (String r : reply) { if (mentions.size() > 0) { String tmpMention = mentionString.toString(); for (String mention : mentions) { if (r.contains(mention)) { tmpMention = tmpMention.replace(mention, ""); } } reply.set(i, r + " " + tmpMention); } else { reply.set(i, r); } i++; } } return reply; } }