fix crash

This commit is contained in:
sk 2023-02-01 10:25:24 +01:00
parent e282d54f99
commit 423e919e16
3 changed files with 24 additions and 16 deletions

View File

@ -6,7 +6,6 @@ import static org.joinmastodon.android.api.requests.statuses.CreateStatus.getDra
import static org.joinmastodon.android.ui.utils.UiUtils.isPhotoPickerAvailable;
import static org.joinmastodon.android.utils.MastodonLanguage.allLanguages;
import static org.joinmastodon.android.utils.MastodonLanguage.defaultRecentLanguages;
import static android.os.ext.SdkExtensions.getExtensionVersion;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
@ -118,7 +117,7 @@ import org.joinmastodon.android.ui.views.LinkedTextView;
import org.joinmastodon.android.ui.views.ReorderableLinearLayout;
import org.joinmastodon.android.ui.views.SizeListenerLinearLayout;
import org.joinmastodon.android.utils.MastodonLanguage;
import org.joinmastodon.android.utils.StringEncoder;
import org.joinmastodon.android.utils.StatusTextEncoder;
import org.parceler.Parcel;
import org.parceler.Parcels;
@ -880,11 +879,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
btn.setOnLongClickListener(v->{
btn.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
if (!GlobalUserPreferences.bottomEncoding) {
GlobalUserPreferences.bottomEncoding = true;
GlobalUserPreferences.save();
addBottomLanguage(allLanguagesMenu);
}
if (!GlobalUserPreferences.bottomEncoding) addBottomLanguage(allLanguagesMenu);
return false;
});
@ -902,7 +897,9 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
}
private void addBottomLanguage(Menu menu) {
menu.add(0, allLanguages.size(), Menu.NONE, "bottom (\uD83E\uDD7A\uD83D\uDC49\uD83D\uDC48)");
if (menu.findItem(allLanguages.size()) == null) {
menu.add(0, allLanguages.size(), Menu.NONE, "bottom (\uD83E\uDD7A\uD83D\uDC49\uD83D\uDC48)");
}
}
@Override
@ -1030,7 +1027,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
String text=mainEditText.getText().toString();
CreateStatus.Request req=new CreateStatus.Request();
if ("bottom".equals(encoding)) {
text = new StringEncoder(Bottom::encode).encode(text);
text = new StatusTextEncoder(Bottom::encode).encode(text);
req.spoilerText = "bottom-encoded emoji spam";
}
if (localOnly &&
@ -1173,9 +1170,15 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
if (replyTo == null) {
List<String> newRecentLanguages = new ArrayList<>(Optional.ofNullable(recentLanguages.get(accountID)).orElse(defaultRecentLanguages));
newRecentLanguages.remove(language);
newRecentLanguages.remove(encoding);
newRecentLanguages.add(0, language);
newRecentLanguages.add(0, encoding);
if (encoding != null) {
newRecentLanguages.remove(encoding);
newRecentLanguages.add(0, encoding);
}
if ("bottom".equals(encoding) && !GlobalUserPreferences.bottomEncoding) {
GlobalUserPreferences.bottomEncoding = true;
GlobalUserPreferences.save();
}
recentLanguages.put(accountID, newRecentLanguages.stream().limit(4).collect(Collectors.toList()));
GlobalUserPreferences.save();
}

View File

@ -28,7 +28,7 @@ import org.joinmastodon.android.ui.text.HtmlParser;
import org.joinmastodon.android.ui.utils.CustomEmojiHelper;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.views.LinkedTextView;
import org.joinmastodon.android.utils.StringEncoder;
import org.joinmastodon.android.utils.StatusTextEncoder;
import java.util.regex.Pattern;
@ -166,7 +166,7 @@ public class TextStatusDisplayItem extends StatusDisplayItem{
if (isBottomText) {
try {
item.translation = new TranslatedStatus();
item.translation.content = new StringEncoder(Bottom::decode).decode(item.status.getStrippedText(), BOTTOM_TEXT_PATTERN);
item.translation.content = new StatusTextEncoder(Bottom::decode).decode(item.status.getStrippedText(), BOTTOM_TEXT_PATTERN);
item.translated = true;
} catch (TranslationError err) {
item.translation = null;

View File

@ -1,5 +1,7 @@
package org.joinmastodon.android.utils;
import android.text.TextUtils;
import org.joinmastodon.android.fragments.ComposeFragment;
import java.util.function.Function;
@ -8,10 +10,13 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
// not a good class
public class StringEncoder {
public class StatusTextEncoder {
private final Function<String, String> fn;
public StringEncoder(Function<String, String> fn) {
// see ComposeFragment.HIGHLIGHT_PATTERN
private final static Pattern EXCLUDE_PATTERN = Pattern.compile("\\s*(?:@([a-zA-Z0-9_]+)(@[a-zA-Z0-9_.-]+)?|#([^\\s.]+))\\s*");
public StatusTextEncoder(Function<String, String> fn) {
this.fn = fn;
}
@ -19,7 +24,7 @@ public class StringEncoder {
public String encode(String content) {
StringBuilder encodedString = new StringBuilder();
// matches mentions and hashtags
Matcher m = ComposeFragment.HIGHLIGHT_PATTERN.matcher(content);
Matcher m = EXCLUDE_PATTERN.matcher(content);
int previousEnd = 0;
while (m.find()) {
MatchResult res = m.toMatchResult();