Fix an issue with poll and Pleroma

This commit is contained in:
Thomas 2023-12-22 15:31:35 +01:00
parent 6b876158d5
commit 6ee9601e23

View file

@ -57,7 +57,6 @@ import android.os.Looper;
import android.text.Html; import android.text.Html;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
@ -303,51 +302,35 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
boolean remote) { boolean remote) {
if (statusReturned == null) { if (statusReturned == null) {
switch (typeOfAction) { switch (typeOfAction) {
case BOOKMARK_ACTION: case BOOKMARK_ACTION -> statusToDeal.bookmarked = true;
statusToDeal.bookmarked = true; case REBLOG_ACTION -> {
break;
case REBLOG_ACTION:
statusToDeal.reblogged = true; statusToDeal.reblogged = true;
statusToDeal.reblogs_count++; statusToDeal.reblogs_count++;
break; }
case FAVOURITE_ACTION: case FAVOURITE_ACTION -> {
statusToDeal.favourited = true; statusToDeal.favourited = true;
statusToDeal.favourites_count++; statusToDeal.favourites_count++;
break; }
case UNBOOKMARK_ACTION: case UNBOOKMARK_ACTION -> statusToDeal.bookmarked = false;
statusToDeal.bookmarked = false; case UNREBLOG_ACTION -> {
break;
case UNREBLOG_ACTION:
statusToDeal.reblogged = false; statusToDeal.reblogged = false;
statusToDeal.reblogs_count--; statusToDeal.reblogs_count--;
break; }
case UNFAVOURITE_ACTION: case UNFAVOURITE_ACTION -> {
statusToDeal.favourited = false; statusToDeal.favourited = false;
statusToDeal.favourites_count--; statusToDeal.favourites_count--;
break; }
} }
} else { } else {
boolean isOK = true; boolean isOK = switch (typeOfAction) {
switch (typeOfAction) { case BOOKMARK_ACTION -> statusReturned.bookmarked;
case BOOKMARK_ACTION: case REBLOG_ACTION -> statusReturned.reblogged;
isOK = statusReturned.bookmarked; case FAVOURITE_ACTION -> statusReturned.favourited;
break; case UNBOOKMARK_ACTION -> !statusReturned.bookmarked;
case REBLOG_ACTION: case UNREBLOG_ACTION -> !statusReturned.reblogged;
isOK = statusReturned.reblogged; case UNFAVOURITE_ACTION -> !statusReturned.favourited;
break; default -> true;
case FAVOURITE_ACTION: };
isOK = statusReturned.favourited;
break;
case UNBOOKMARK_ACTION:
isOK = !statusReturned.bookmarked;
break;
case UNREBLOG_ACTION:
isOK = !statusReturned.reblogged;
break;
case UNFAVOURITE_ACTION:
isOK = !statusReturned.favourited;
break;
}
if (!isOK) { if (!isOK) {
Toasty.error(context, context.getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); Toasty.error(context, context.getString(R.string.toast_error), Toasty.LENGTH_SHORT).show();
return; return;
@ -367,18 +350,10 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} }
} else { } else {
switch (typeOfAction) { switch (typeOfAction) {
case REBLOG_ACTION: case REBLOG_ACTION -> statusToDeal.reblogs_count++;
statusToDeal.reblogs_count++; case FAVOURITE_ACTION -> statusToDeal.favourites_count++;
break; case UNREBLOG_ACTION -> statusToDeal.reblogs_count--;
case FAVOURITE_ACTION: case UNFAVOURITE_ACTION -> statusToDeal.favourites_count--;
statusToDeal.favourites_count++;
break;
case UNREBLOG_ACTION:
statusToDeal.reblogs_count--;
break;
case UNFAVOURITE_ACTION:
statusToDeal.favourites_count--;
break;
} }
} }
} }
@ -770,12 +745,8 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
holder.binding.statusContentMaths.removeAllViews(); holder.binding.statusContentMaths.removeAllViews();
MathJaxConfig mathJaxConfig = new MathJaxConfig(); MathJaxConfig mathJaxConfig = new MathJaxConfig();
switch (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) { switch (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_YES: case Configuration.UI_MODE_NIGHT_YES -> mathJaxConfig.setTextColor("white");
mathJaxConfig.setTextColor("white"); case Configuration.UI_MODE_NIGHT_NO -> mathJaxConfig.setTextColor("black");
break;
case Configuration.UI_MODE_NIGHT_NO:
mathJaxConfig.setTextColor("black");
break;
} }
mathJaxConfig.setAutomaticLinebreaks(true); mathJaxConfig.setAutomaticLinebreaks(true);
@ -1021,9 +992,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
if (needToWarnForMissingDescription) { if (needToWarnForMissingDescription) {
AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(context); AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(context);
alt_bld.setMessage(context.getString(R.string.reblog_missing_description)); alt_bld.setMessage(context.getString(R.string.reblog_missing_description));
alt_bld.setPositiveButton(R.string.yes, (dialog, id) -> { alt_bld.setPositiveButton(R.string.yes, (dialog, id) -> CrossActionHelper.doCrossAction(context, CrossActionHelper.TypeOfCrossAction.REBLOG_ACTION, null, statusToDeal));
CrossActionHelper.doCrossAction(context, CrossActionHelper.TypeOfCrossAction.REBLOG_ACTION, null, statusToDeal);
});
alt_bld.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); alt_bld.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
AlertDialog alert = alt_bld.create(); AlertDialog alert = alt_bld.create();
alert.show(); alert.show();
@ -1246,21 +1215,21 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
holder.binding.visibility.setContentDescription(context.getString(R.string.v_public)); holder.binding.visibility.setContentDescription(context.getString(R.string.v_public));
holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_public)); holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_public));
switch (status.visibility) { switch (status.visibility) {
case "unlisted": case "unlisted" -> {
holder.binding.visibility.setContentDescription(context.getString(R.string.v_unlisted)); holder.binding.visibility.setContentDescription(context.getString(R.string.v_unlisted));
holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_unlisted)); holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_unlisted));
ressource = R.drawable.ic_baseline_lock_open_24; ressource = R.drawable.ic_baseline_lock_open_24;
break; }
case "private": case "private" -> {
ressource = R.drawable.ic_baseline_lock_24; ressource = R.drawable.ic_baseline_lock_24;
holder.binding.visibility.setContentDescription(context.getString(R.string.v_private)); holder.binding.visibility.setContentDescription(context.getString(R.string.v_private));
holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_private)); holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_private));
break; }
case "direct": case "direct" -> {
ressource = R.drawable.ic_baseline_mail_24; ressource = R.drawable.ic_baseline_mail_24;
holder.binding.visibility.setContentDescription(context.getString(R.string.v_direct)); holder.binding.visibility.setContentDescription(context.getString(R.string.v_direct));
holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_direct)); holder.binding.visibilitySmall.setContentDescription(context.getString(R.string.v_direct));
break; }
} }
if (statusToDeal.local_only) { if (statusToDeal.local_only) {
@ -1414,20 +1383,16 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} }
//--- BOOST VISIBILITY --- //--- BOOST VISIBILITY ---
switch (statusToDeal.visibility) { switch (statusToDeal.visibility) {
case "public": case "public", "unlisted" ->
case "unlisted": holder.binding.actionButtonBoost.setVisibility(View.VISIBLE);
holder.binding.actionButtonBoost.setVisibility(View.VISIBLE); case "private" -> {
break;
case "private":
if (status.account.id.compareTo(BaseMainActivity.currentUserID) == 0) { if (status.account.id.compareTo(BaseMainActivity.currentUserID) == 0) {
holder.binding.actionButtonBoost.setVisibility(View.VISIBLE); holder.binding.actionButtonBoost.setVisibility(View.VISIBLE);
} else { } else {
holder.binding.actionButtonBoost.setVisibility(View.GONE); holder.binding.actionButtonBoost.setVisibility(View.GONE);
} }
break; }
case "direct": case "direct" -> holder.binding.actionButtonBoost.setVisibility(View.GONE);
holder.binding.actionButtonBoost.setVisibility(View.GONE);
break;
} }
//--- MAIN CONTENT --- //--- MAIN CONTENT ---
@ -1815,6 +1780,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
// --- POLL --- // --- POLL ---
if (statusToDeal.poll != null && statusToDeal.poll.options != null) { if (statusToDeal.poll != null && statusToDeal.poll.options != null) {
int normalize;
if(statusToDeal.poll.multiple && statusToDeal.poll.voters_count != 0) {
normalize = statusToDeal.poll.voters_count;
} else {
normalize = statusToDeal.poll.votes_count;
}
if (statusToDeal.poll.voted || statusToDeal.poll.expired) { if (statusToDeal.poll.voted || statusToDeal.poll.expired) {
holder.binding.poll.submitVote.setVisibility(View.GONE); holder.binding.poll.submitVote.setVisibility(View.GONE);
holder.binding.poll.rated.setVisibility(View.VISIBLE); holder.binding.poll.rated.setVisibility(View.VISIBLE);
@ -1833,7 +1804,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} }
for (Poll.PollItem pollItem : statusToDeal.poll.options) { for (Poll.PollItem pollItem : statusToDeal.poll.options) {
@NonNull LayoutPollItemBinding pollItemBinding = LayoutPollItemBinding.inflate(inflater, holder.binding.poll.rated, true); @NonNull LayoutPollItemBinding pollItemBinding = LayoutPollItemBinding.inflate(inflater, holder.binding.poll.rated, true);
double value = Math.round((pollItem.votes_count * 100) / (double) statusToDeal.poll.voters_count); double value = Math.round((pollItem.votes_count * 100) / (double) normalize);
pollItemBinding.pollItemPercent.setText(String.format("%s %%", (int) value)); pollItemBinding.pollItemPercent.setText(String.format("%s %%", (int) value));
pollItemBinding.pollItemText.setText( pollItemBinding.pollItemText.setText(
pollItem.getSpanTitle(context, statusToDeal, pollItem.getSpanTitle(context, statusToDeal,
@ -1986,7 +1957,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} }
})); }));
holder.binding.poll.pollContainer.setVisibility(View.VISIBLE); holder.binding.poll.pollContainer.setVisibility(View.VISIBLE);
String pollInfo = context.getResources().getQuantityString(R.plurals.number_of_voters, statusToDeal.poll.voters_count, statusToDeal.poll.voters_count); String pollInfo = context.getResources().getQuantityString(R.plurals.number_of_voters, normalize, normalize);
if (statusToDeal.poll.expired) { if (statusToDeal.poll.expired) {
pollInfo += " - " + context.getString(R.string.poll_finish_at, MastodonHelper.dateToStringPoll(statusToDeal.poll.expires_at)); pollInfo += " - " + context.getString(R.string.poll_finish_at, MastodonHelper.dateToStringPoll(statusToDeal.poll.expires_at));
} else { } else {
@ -2783,7 +2754,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
if (id != null) { if (id != null) {
b.putString(type, id); b.putString(type, id);
} }
if (type == ARG_TIMELINE_REFRESH_ALL) { if (type.equals(ARG_TIMELINE_REFRESH_ALL)) {
b.putBoolean(ARG_TIMELINE_REFRESH_ALL, true); b.putBoolean(ARG_TIMELINE_REFRESH_ALL, true);
} }
Intent intentBC = new Intent(Helper.RECEIVE_STATUS_ACTION); Intent intentBC = new Intent(Helper.RECEIVE_STATUS_ACTION);