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

View File

@ -1,5 +1,7 @@
package org.joinmastodon.android.utils; package org.joinmastodon.android.utils;
import android.text.TextUtils;
import org.joinmastodon.android.fragments.ComposeFragment; import org.joinmastodon.android.fragments.ComposeFragment;
import java.util.function.Function; import java.util.function.Function;
@ -8,10 +10,13 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
// not a good class // not a good class
public class StringEncoder { public class StatusTextEncoder {
private final Function<String, String> fn; 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; this.fn = fn;
} }
@ -19,7 +24,7 @@ public class StringEncoder {
public String encode(String content) { public String encode(String content) {
StringBuilder encodedString = new StringBuilder(); StringBuilder encodedString = new StringBuilder();
// matches mentions and hashtags // matches mentions and hashtags
Matcher m = ComposeFragment.HIGHLIGHT_PATTERN.matcher(content); Matcher m = EXCLUDE_PATTERN.matcher(content);
int previousEnd = 0; int previousEnd = 0;
while (m.find()) { while (m.find()) {
MatchResult res = m.toMatchResult(); MatchResult res = m.toMatchResult();