Commit c200ab38 authored by Thomas's avatar Thomas
Browse files

- Fix #1333 cross-posting: use correct instance for character limit and emojis

parent bd9d6fbe
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ import app.fedilab.android.databinding.DatetimePickerBinding;
import app.fedilab.android.mastodon.client.entities.api.Account;
import app.fedilab.android.mastodon.client.entities.api.Pagination;
import app.fedilab.android.mastodon.client.entities.api.RelationShip;
import app.fedilab.android.mastodon.client.entities.api.Instance;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.app.ScheduledBoost;
import app.fedilab.android.mastodon.exception.DBException;
@@ -552,14 +553,32 @@ public class MastodonHelper {
    }

    public static int getInstanceMaxChars(Context context) {
        return getInstanceMaxChars(context, MainActivity.currentInstance);
    }

    public static int getInstanceMaxChars(Context context, String instance) {
        int max_car;
        SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
        int val = sharedpreferences.getInt(context.getString(R.string.SET_MAX_INSTANCE_CHAR) + MainActivity.currentInstance, -1);
        int val = sharedpreferences.getInt(context.getString(R.string.SET_MAX_INSTANCE_CHAR) + instance, -1);
        if (val != -1) {
            return val;
        } else {
            if (instanceInfo != null) {
            // Check if we're dealing with the current instance or a different one
            if (instance != null && instance.equals(MainActivity.currentInstance) && instanceInfo != null) {
                max_car = instanceInfo.max_toot_chars != null && instanceInfo.max_toot_chars.matches("\\d+") ? Integer.parseInt(instanceInfo.max_toot_chars) : instanceInfo.configuration.statusesConf.max_characters;
            } else if (instance != null) {
                // For cross-posting: load instance info from SharedPreferences
                String instanceInfoStr = sharedpreferences.getString(context.getString(R.string.INSTANCE_INFO) + instance, null);
                if (instanceInfoStr != null) {
                    Instance crossInstanceInfo = Instance.restore(instanceInfoStr);
                    if (crossInstanceInfo != null) {
                        max_car = crossInstanceInfo.max_toot_chars != null && crossInstanceInfo.max_toot_chars.matches("\\d+") ? Integer.parseInt(crossInstanceInfo.max_toot_chars) : crossInstanceInfo.configuration.statusesConf.max_characters;
                    } else {
                        max_car = 500;
                    }
                } else {
                    max_car = 500;
                }
            } else {
                max_car = 500;
            }
+11 −11
Original line number Diff line number Diff line
@@ -585,7 +585,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                buttonVisibility(holder);
                //Text is copied pasted and the content is greater than the max of the instance
                int max_car = MastodonHelper.getInstanceMaxChars(context);
                int max_car = MastodonHelper.getInstanceMaxChars(context, account.instance);
                if (ComposeHelper.countLength(s.toString()) > max_car) {
                    SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
                    String defaultFormat = sharedpreferences.getString(context.getString(R.string.SET_THREAD_MESSAGE), context.getString(R.string.DEFAULT_THREAD_VALUE));
@@ -640,7 +640,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
            public void afterTextChanged(Editable s) {
                String contentString = s.toString();
                if (proceedToSplit) {
                    int max_car = MastodonHelper.getInstanceMaxChars(context);
                    int max_car = MastodonHelper.getInstanceMaxChars(context, account.instance);
                    ArrayList<String> splitText = ComposeHelper.splitToots(contentString, max_car);
                    if(!splitText.isEmpty()) {
                        contentString = splitText.get(0);
@@ -655,7 +655,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                }

                //Copy/past
                int max_car = MastodonHelper.getInstanceMaxChars(context);
                int max_car = MastodonHelper.getInstanceMaxChars(context, account.instance);
                if (currentLength > max_car) {
                    holder.binding.characterCount.setTextColor(Color.RED);
                } else {
@@ -878,7 +878,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                matcherEmoji = emojiPattern.matcher(searchIn);
                if (matcherMention.matches()) {
                    String searchGroup = matcherMention.group();
                    accountsVM.searchAccounts(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, searchGroup, 5, false, false).observe((LifecycleOwner) context, accounts -> {
                    accountsVM.searchAccounts(account.instance, account.token, searchGroup, 5, false, false).observe((LifecycleOwner) context, accounts -> {
                        if (accounts == null) {
                            return;
                        }
@@ -926,7 +926,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                    String searchGroup = matcherTag.group(3);
                    if (searchGroup != null) {
                        List<String> camelTags = new CamelTag(context).getBy(searchGroup);
                        searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, searchGroup, null,
                        searchVM.search(account.instance, account.token, searchGroup, null,
                                "hashtags", false, true, false, 0,
                                null, null, 10).observe((LifecycleOwner) context,
                                results -> {
@@ -999,13 +999,13 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                        List<Emoji> emojisToDisplay = new ArrayList<>();
                        try {
                            if (emojisList == null || emojisList.isEmpty()) {
                                emojisList = new EmojiInstance(context).getEmojiList(BaseMainActivity.currentInstance);
                                emojisList = new EmojiInstance(context).getEmojiList(account.instance);
                            }
                            if (emojis == null) {
                                return;
                            } else if (emojisList == null) {
                                if (emojis.containsKey(BaseMainActivity.currentInstance)) {
                                    emojisList = emojis.get(BaseMainActivity.currentInstance);
                                if (emojis.containsKey(account.instance)) {
                                    emojisList = emojis.get(account.instance);
                                }
                            }
                            if (emojisList == null) {
@@ -1196,7 +1196,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                for (Attachment attachment : attachmentList) {
                    if(attachment.url == null) {
                        StatusesVM statusesVM = new ViewModelProvider((ViewModelStoreOwner) context).get(StatusesVM.class);
                        statusesVM.getAttachment(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, attachment.id)
                        statusesVM.getAttachment(account.instance, account.token, attachment.id)
                                .observe((LifecycleOwner) context, attachmentReceived -> {
                                    if(attachmentReceived != null) {
                                        List<Attachment> attachments = statusList.get(position).media_attachments;
@@ -1829,7 +1829,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                else
                    statusDraft.text = new SpannableString(Html.fromHtml(statusDraft.content)).toString();
            }
            int max_car = MastodonHelper.getInstanceMaxChars(context);
            int max_car = MastodonHelper.getInstanceMaxChars(context, account.instance);
            holder.binding.content.setText(statusDraft.text);
            holder.binding.characterProgress.setMax(max_car);
            updateCharacterCount(holder);
@@ -1961,7 +1961,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
                        promptDraftListener.promptDraft();
                    }
                    int currentLength = MastodonHelper.countLength(holder);
                    int max_car = MastodonHelper.getInstanceMaxChars(context);
                    int max_car = MastodonHelper.getInstanceMaxChars(context, account.instance);
                    if (currentLength > max_car) {
                        holder.binding.characterCount.setTextColor(Color.RED);
                    } else {