summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/helper/ComposeHelper.java
blob: 230e186f28e3e64c10989237eda27922367d6ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 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 {


    /**
     * 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;
        StringBuilder mentionString = new StringBuilder();
        Matcher matcher = mentionPatternALL.matcher(content);
        while (matcher.find()) {
            String mentionLong = matcher.group(1);
            if(mentionLong != null) {
                if (!mentions.contains(mentionLong)) {
                    mentions.add(mentionLong);
                }
            }
            String mentionShort = matcher.group(2);
            if(mentionShort != null) {
                if (!mentions.contains(mentionShort)) {
                    mentions.add(mentionShort);
                }
            }
        }
        for (String mention : mentions) {
            mentionString.append(mention).append(" ");
        }
        mentionLength = countLength(mentionString.toString()) + 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;
    }


    /***
     * 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();
    }
}