diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index a6ed16e5..a3dc4e6f 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -48,6 +48,7 @@ import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.LinearLayout; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -122,6 +123,7 @@ import app.fedilab.android.client.entities.app.BottomMenu; import app.fedilab.android.client.entities.app.DomainsBlock; import app.fedilab.android.client.entities.app.Pinned; import app.fedilab.android.client.entities.app.PinnedTimeline; +import app.fedilab.android.client.entities.app.StatusDraft; import app.fedilab.android.databinding.ActivityMainBinding; import app.fedilab.android.databinding.NavHeaderMainBinding; import app.fedilab.android.exception.DBException; @@ -163,6 +165,33 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt private ActivityMainBinding binding; private Pinned pinned; private BottomMenu bottomMenu; + private final BroadcastReceiver broadcast_error_message = new BroadcastReceiver() { + @Override + public void onReceive(android.content.Context context, Intent intent) { + Bundle b = intent.getExtras(); + if (b != null) { + if (b.getBoolean(Helper.RECEIVE_COMPOSE_ERROR_MESSAGE, false)) { + String errorMessage = b.getString(Helper.RECEIVE_ERROR_MESSAGE); + StatusDraft statusDraft = (StatusDraft) b.getSerializable(Helper.ARG_STATUS_DRAFT); + Snackbar snackbar = Snackbar.make(binding.getRoot(), errorMessage, 5000); + View snackbarView = snackbar.getView(); + TextView textView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text); + textView.setMaxLines(5); + snackbar + .setAction(getString(R.string.open_draft), view -> { + Intent intentCompose = new Intent(context, ComposeActivity.class); + intentCompose.putExtra(Helper.ARG_STATUS_DRAFT, statusDraft); + context.startActivity(intentCompose); + }) + .setTextColor(ThemeHelper.getAttColor(BaseMainActivity.this, R.attr.mTextColor)) + .setActionTextColor(ContextCompat.getColor(BaseMainActivity.this, R.color.cyanea_accent_reference)) + .setBackgroundTint(ContextCompat.getColor(BaseMainActivity.this, R.color.cyanea_primary_dark_reference)) + .show(); + } + } + } + }; + private final BroadcastReceiver broadcast_data = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -877,6 +906,9 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt binding.toolbarSearch.setOnSearchClickListener(v -> binding.tabLayout.setVisibility(View.VISIBLE)); //For receiving data from other activities LocalBroadcastManager.getInstance(BaseMainActivity.this).registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA)); + LocalBroadcastManager.getInstance(BaseMainActivity.this) + .registerReceiver(broadcast_error_message, + new IntentFilter(Helper.INTENT_COMPOSE_ERROR_MESSAGE)); if (emojis == null || !emojis.containsKey(BaseMainActivity.currentInstance) || emojis.get(BaseMainActivity.currentInstance) == null) { new Thread(() -> { try { @@ -1038,6 +1070,8 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt @Override protected void onDestroy() { LocalBroadcastManager.getInstance(BaseMainActivity.this).unregisterReceiver(broadcast_data); + LocalBroadcastManager.getInstance(BaseMainActivity.this) + .unregisterReceiver(broadcast_error_message); if (networkStateReceiver != null) { try { unregisterReceiver(networkStateReceiver); diff --git a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java index 0b228b8f..8a87aadb 100644 --- a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java @@ -108,6 +108,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana private Status statusReply, statusMention; private StatusDraft statusDraft; private ComposeAdapter composeAdapter; + private final BroadcastReceiver imageReceiver = new BroadcastReceiver() { @Override public void onReceive(android.content.Context context, Intent intent) { @@ -133,6 +134,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana } } }; + private ActivityPaginationBinding binding; private BaseAccount account; private String instance, token; @@ -400,6 +402,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana } LocalBroadcastManager.getInstance(this) .unregisterReceiver(imageReceiver); + } @Override diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 4a653e39..7777df84 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -198,9 +198,11 @@ public class Helper { public static final String RECEIVE_REDRAW_BOTTOM = "RECEIVE_REDRAW_BOTTOM"; public static final String RECEIVE_STATUS_ACTION = "RECEIVE_STATUS_ACTION"; + public static final String RECEIVE_ERROR_MESSAGE = "RECEIVE_ERROR_MESSAGE"; public static final String RECEIVE_RECREATE_ACTIVITY = "RECEIVE_RECREATE_ACTIVITY"; public static final String RECEIVE_NEW_MESSAGE = "RECEIVE_NEW_MESSAGE"; + public static final String RECEIVE_COMPOSE_ERROR_MESSAGE = "RECEIVE_COMPOSE_ERROR_MESSAGE"; public static final String RECEIVE_MASTODON_LIST = "RECEIVE_MASTODON_LIST"; public static final String RECEIVE_REDRAW_PROFILE = "RECEIVE_REDRAW_PROFILE"; @@ -291,6 +293,7 @@ public class Helper { public static final int OPEN_NOTIFICATION = 2; public static final String INTENT_TARGETED_ACCOUNT = "INTENT_TARGETED_ACCOUNT"; public static final String INTENT_SEND_MODIFIED_IMAGE = "INTENT_SEND_MODIFIED_IMAGE"; + public static final String INTENT_COMPOSE_ERROR_MESSAGE = "INTENT_COMPOSE_ERROR_MESSAGE"; public static final String TEMP_MEDIA_DIRECTORY = "TEMP_MEDIA_DIRECTORY"; diff --git a/app/src/main/java/app/fedilab/android/services/PostMessageService.java b/app/src/main/java/app/fedilab/android/services/PostMessageService.java index f622a3ae..1c0a9566 100644 --- a/app/src/main/java/app/fedilab/android/services/PostMessageService.java +++ b/app/src/main/java/app/fedilab/android/services/PostMessageService.java @@ -200,6 +200,13 @@ public class PostMessageService extends IntentService { } Call statusCall; if (error) { + Bundle b = new Bundle(); + b.putBoolean(Helper.RECEIVE_COMPOSE_ERROR_MESSAGE, true); + Intent intentBD = new Intent(Helper.INTENT_COMPOSE_ERROR_MESSAGE); + b.putSerializable(Helper.RECEIVE_ERROR_MESSAGE, context.getString(R.string.media_cannot_be_uploaded)); + b.putSerializable(Helper.ARG_STATUS_DRAFT, dataPost.statusDraft); + intentBD.putExtras(b); + LocalBroadcastManager.getInstance(context).sendBroadcast(intentBD); return; } String language = sharedPreferences.getString(context.getString(R.string.SET_COMPOSE_LANGUAGE) + dataPost.userId + dataPost.instance, null); @@ -226,7 +233,7 @@ public class PostMessageService extends IntentService { } catch (DBException e) { e.printStackTrace(); } - if (!error && i >= dataPost.statusDraft.statusDraftList.size()) { + if (i >= dataPost.statusDraft.statusDraftList.size()) { try { new StatusDraft(context).removeDraft(dataPost.statusDraft); } catch (DBException e) { @@ -237,10 +244,26 @@ public class PostMessageService extends IntentService { } } } + } else if (statusResponse.errorBody() != null) { + Bundle b = new Bundle(); + b.putBoolean(Helper.RECEIVE_COMPOSE_ERROR_MESSAGE, true); + Intent intentBD = new Intent(Helper.INTENT_COMPOSE_ERROR_MESSAGE); + b.putSerializable(Helper.ARG_STATUS_DRAFT, dataPost.statusDraft); + b.putSerializable(Helper.RECEIVE_ERROR_MESSAGE, statusResponse.errorBody().string()); + intentBD.putExtras(b); + LocalBroadcastManager.getInstance(context).sendBroadcast(intentBD); + return; } } catch (IOException e) { e.printStackTrace(); - error = true; + Bundle b = new Bundle(); + b.putBoolean(Helper.RECEIVE_COMPOSE_ERROR_MESSAGE, true); + b.putSerializable(Helper.ARG_STATUS_DRAFT, dataPost.statusDraft); + Intent intentBD = new Intent(Helper.INTENT_COMPOSE_ERROR_MESSAGE); + b.putSerializable(Helper.RECEIVE_ERROR_MESSAGE, e.getMessage()); + intentBD.putExtras(b); + LocalBroadcastManager.getInstance(context).sendBroadcast(intentBD); + return; } } else { Call scheduledStatusCall = mastodonStatusesService.createScheduledStatus(null, dataPost.token, statuses.get(i).text, attachmentIds, poll_options, poll_expire_in, @@ -260,7 +283,7 @@ public class PostMessageService extends IntentService { } catch (DBException e) { e.printStackTrace(); } - if (!error && i >= dataPost.statusDraft.statusDraftList.size()) { + if (i >= dataPost.statusDraft.statusDraftList.size()) { try { new StatusDraft(context).removeDraft(dataPost.statusDraft); } catch (DBException e) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8bcc1533..784fa727 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -992,6 +992,8 @@ Set your max char count Release notes The app failed to get a token + Media cannot be uploaded! + Open draft