From 5d5f2f9c9a519e86cd6cef36a1cbd9c3bc5a8e44 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 27 Feb 2023 16:14:35 +0100 Subject: [PATCH] Fix #800 - Add settings to don't send message if there are no media description --- .../mastodon/activities/ComposeActivity.java | 42 +++++++++++++++---- .../mastodon/services/CustomReceiver.java | 3 -- .../res/layouts/mastodon/values/strings.xml | 1 + app/src/main/res/values/strings.xml | 7 +++- app/src/main/res/xml/pref_compose.xml | 8 ++++ .../metadata/android/en/changelogs/481.txt | 2 + 6 files changed, 51 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java b/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java index 85be9c6a..4cd5d864 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java +++ b/app/src/main/java/app/fedilab/android/mastodon/activities/ComposeActivity.java @@ -213,7 +213,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana if (statusDraft == null) { statusDraft = ComposeAdapter.prepareDraft(statusList, composeAdapter, account.instance, account.user_id); } - if (canBeSent(statusDraft)) { + if (canBeSent(statusDraft) != 0) { if (promptSaveDraft) { AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(ComposeActivity.this); alt_bld.setMessage(R.string.save_draft); @@ -394,8 +394,10 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana if (statusDraft == null) { statusDraft = ComposeAdapter.prepareDraft(statusList, composeAdapter, account.instance, account.user_id); } - if (canBeSent(statusDraft)) { + if (canBeSent(statusDraft) == 1) { MediaHelper.scheduleMessage(ComposeActivity.this, date -> storeDraft(true, date)); + } else if (canBeSent(statusDraft) == -1) { + Toasty.warning(ComposeActivity.this, getString(R.string.toot_error_no_media_description), Toasty.LENGTH_SHORT).show(); } else { Toasty.info(ComposeActivity.this, getString(R.string.toot_error_no_content), Toasty.LENGTH_SHORT).show(); } @@ -829,7 +831,18 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana statusDraft.user_id = account.user_id; } - if (!canBeSent(statusDraft)) { + if (canBeSent(statusDraft) != 1) { + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + if (canBeSent(statusDraft) == -1) { + Toasty.warning(ComposeActivity.this, getString(R.string.toot_error_no_media_description), Toasty.LENGTH_SHORT).show(); + } else { + Toasty.info(ComposeActivity.this, getString(R.string.toot_error_no_content), Toasty.LENGTH_SHORT).show(); + } + statusDrafts.get(statusDrafts.size() - 1).submitted = false; + composeAdapter.notifyItemChanged(statusList.size() - 1); + }; + mainHandler.post(myRunnable); return; } if (statusDraft.id > 0) { @@ -915,22 +928,35 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana } - private boolean canBeSent(StatusDraft statusDraft) { + private int canBeSent(StatusDraft statusDraft) { if (statusDraft == null) { - return false; + return 0; + } + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this); + boolean checkAlt = sharedpreferences.getBoolean(getString(R.string.SET_MANDATORY_ALT_TEXT), false); + if (checkAlt) { + for (Status status : statusDraft.statusDraftList) { + if (status.media_attachments != null && status.media_attachments.size() > 0) { + for (Attachment attachment : status.media_attachments) { + if (attachment.description == null || attachment.description.trim().isEmpty()) { + return -1; + } + } + } + } } List statuses = statusDraft.statusDraftList; if (statuses == null || statuses.size() == 0) { - return false; + return 0; } Status statusCheck = statuses.get(0); if (statusCheck == null) { - return false; + return 0; } return (statusCheck.text != null && statusCheck.text.trim().length() != 0) || (statusCheck.media_attachments != null && statusCheck.media_attachments.size() != 0) || statusCheck.poll != null - || (statusCheck.spoiler_text != null && statusCheck.spoiler_text.trim().length() != 0); + || (statusCheck.spoiler_text != null && statusCheck.spoiler_text.trim().length() != 0) ? 1 : 0; } diff --git a/app/src/main/java/app/fedilab/android/mastodon/services/CustomReceiver.java b/app/src/main/java/app/fedilab/android/mastodon/services/CustomReceiver.java index b1572a5d..e89837d1 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/services/CustomReceiver.java +++ b/app/src/main/java/app/fedilab/android/mastodon/services/CustomReceiver.java @@ -16,7 +16,6 @@ package app.fedilab.android.mastodon.services; import android.content.Context; import android.content.Intent; -import android.util.Log; import androidx.annotation.NonNull; @@ -24,7 +23,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.unifiedpush.android.connector.MessagingReceiver; -import app.fedilab.android.mastodon.helper.Helper; import app.fedilab.android.mastodon.helper.NotificationsHelper; import app.fedilab.android.mastodon.helper.PushNotifications; @@ -40,7 +38,6 @@ public class CustomReceiver extends MessagingReceiver { @Override public void onMessage(@NotNull Context context, @NotNull byte[] message, @NotNull String slug) { // Called when a new message is received. The message contains the full POST body of the push message - Log.v(Helper.TAG, "onMessage: " + slug); new Thread(() -> { try { /*Notification notification = ECDHFedilab.decryptNotification(context, slug, message); diff --git a/app/src/main/res/layouts/mastodon/values/strings.xml b/app/src/main/res/layouts/mastodon/values/strings.xml index 0d2c4cc4..78554f9e 100644 --- a/app/src/main/res/layouts/mastodon/values/strings.xml +++ b/app/src/main/res/layouts/mastodon/values/strings.xml @@ -1,4 +1,5 @@ + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9c574f8c..00879430 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -868,13 +868,14 @@ SET_DISPLAY_EMOJI SET_AGGREGATE_NOTIFICATION SET_DISPLAY_MEDIA_NOTIFICATION - x + SET_DISPLAY_CARD SET_DISPLAY_VIDEO_PREVIEWS SET_NOTIFICATION_ACTION SET_FEATURED_TAGS SET_FEATURED_TAG_ACTION + SET_MANDATORY_ALT_TEXT SET_RETRIEVE_METADATA_IF_URL_FROM_EXTERAL SET_TRANSLATE_VALUES_RESET @@ -1905,4 +1906,8 @@ Translator domain Chat timeline for direct messages %1$s more media + Mandatory media descriptions + + The message will not be sent if a description is missing with a media + There are missing media descriptions \ No newline at end of file diff --git a/app/src/main/res/xml/pref_compose.xml b/app/src/main/res/xml/pref_compose.xml index 3eb2363d..be1c04f5 100644 --- a/app/src/main/res/xml/pref_compose.xml +++ b/app/src/main/res/xml/pref_compose.xml @@ -61,6 +61,14 @@ app:summary="@string/set_watermark_indication" app:title="@string/set_watermark" /> + +