Skip to content

Commit 9cb0787

Browse files
committed
fix: upload failure toast is shown multiple times
1 parent c970145 commit 9cb0787

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

app/src/main/java/co/tinode/tindroid/AttachmentHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class AttachmentHandler extends Worker {
8282
final static String ARG_PROGRESS = "progress";
8383
final static String ARG_FILE_SIZE = "fileSize";
8484
final static String ARG_ERROR = "error";
85+
final static String ARG_FATAL = "fatal";
8586
final static String ARG_MIME_TYPE = "mime";
8687
final static String ARG_AVATAR = "square_img";
8788
final static String ARG_IMAGE_WIDTH = "width";
@@ -426,9 +427,9 @@ private ListenableWorker.Result uploadMessageAttachment(final Context context, f
426427

427428
if (uploadDetails.fileSize == 0) {
428429
Log.w(TAG, "File size is zero; uri=" + uri + "; file=" + filePath);
429-
store.msgDiscard(topic, msgId);
430430
return ListenableWorker.Result.failure(
431-
result.putString(ARG_ERROR, context.getString(R.string.unable_to_attach_file)).build());
431+
result.putBoolean(ARG_FATAL, true)
432+
.putString(ARG_ERROR, context.getString(R.string.unable_to_attach_file)).build());
432433
}
433434

434435
if (fname == null) {
@@ -457,6 +458,7 @@ private ListenableWorker.Result uploadMessageAttachment(final Context context, f
457458
R.string.attachment_too_large,
458459
UiUtils.bytesToHumanSize(uploadDetails.fileSize),
459460
UiUtils.bytesToHumanSize(maxFileUploadSize)))
461+
.putBoolean(ARG_FATAL, true)
460462
.build());
461463
} else {
462464
if (is == null) {

app/src/main/java/co/tinode/tindroid/MessagesFragment.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import androidx.work.WorkManager;
7878

7979
import co.tinode.tindroid.db.BaseDb;
80+
import co.tinode.tindroid.db.SqlStore;
8081
import co.tinode.tindroid.db.StoredTopic;
8182
import co.tinode.tindroid.format.SendForwardedFormatter;
8283
import co.tinode.tindroid.format.SendReplyFormatter;
@@ -85,6 +86,7 @@
8586
import co.tinode.tindroid.widgets.WaveDrawable;
8687
import co.tinode.tinodesdk.ComTopic;
8788
import co.tinode.tinodesdk.PromisedReply;
89+
import co.tinode.tinodesdk.Storage;
8890
import co.tinode.tinodesdk.Tinode;
8991
import co.tinode.tinodesdk.model.AccessChange;
9092
import co.tinode.tinodesdk.model.Acs;
@@ -454,7 +456,13 @@ public void afterTextChanged(Editable editable) {
454456
}
455457
if (topicName.equals(mTopicName)) {
456458
long msgId = failure.getLong(AttachmentHandler.ARG_MSG_ID, -1L);
457-
if (BaseDb.getInstance().getStore().getMessageById(msgId) != null) {
459+
boolean fatal = failure.getBoolean(AttachmentHandler.ARG_FATAL, false);
460+
SqlStore store = BaseDb.getInstance().getStore();
461+
Storage.Message msg = store.getMessageById(msgId);
462+
if (fatal && msg != null) {
463+
store.msgDiscard(mTopic, msgId);
464+
}
465+
if (msg != null) {
458466
runMessagesLoader(mTopicName);
459467
String error = failure.getString(AttachmentHandler.ARG_ERROR);
460468
Toast.makeText(activity, error, Toast.LENGTH_SHORT).show();

app/src/main/java/co/tinode/tindroid/db/SqlStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ public Storage.Message msgReceived(Topic topic, Subscription sub, MsgServerData
294294
return msg;
295295
}
296296

297-
private Storage.Message insertMessage(Topic topic, Drafty data, Map<String, Object> head, BaseDb.Status initialStatus) {
297+
private Storage.Message insertMessage(Topic topic, Drafty data, Map<String, Object> head,
298+
BaseDb.Status initialStatus) {
298299
StoredMessage msg = new StoredMessage();
299300
SQLiteDatabase db = mDbh.getWritableDatabase();
300301

0 commit comments

Comments
 (0)