Commit bfc362ef authored by Thomas's avatar Thomas
Browse files

Automatically split long copied/pasted messages into threads

parent f5bfb3e2
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -774,12 +774,15 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
    }

    @Override
    public void onItemDraftAdded(int position) {
    public void onItemDraftAdded(int position, String initialContent) {
        Status status = new Status();

        status.id = Helper.generateIdString();
        status.mentions = statusList.get(position - 1).mentions;
        status.visibility = statusList.get(position - 1).visibility;
        if(initialContent != null) {
            status.text = initialContent;
        }
        final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(ComposeActivity.this);
        boolean unlistedReplies = sharedpreferences.getBoolean(getString(R.string.SET_UNLISTED_REPLIES), true);
        if (status.visibility.equalsIgnoreCase("public") && unlistedReplies) {
+98 −0
Original line number Diff line number Diff line
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 <http://www.gnu.org/licenses>. */

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<String> split toot
     */
    public static ArrayList<String> splitToots(String content, int maxChars) {
        String[] splitContent = content.split("\\s");


        ArrayList<String> 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<String> 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;
    }
}
+80 −83

File changed.

Preview size limit exceeded, changes collapsed.