From 79c38746e2243aa3afdde0eabf74946731053957 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 8 Jun 2022 11:43:38 +0200 Subject: [PATCH] Fix issue #99 - Add retry when media failed to load and if 3 retries fail the message is kept in drafts but not send --- .../app/fedilab/android/BaseMainActivity.java | 2 - .../android/services/PostMessageService.java | 44 ++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 0c9f6cae1..65a7600ef 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -66,9 +66,7 @@ import androidx.navigation.ui.NavigationUI; import androidx.preference.PreferenceManager; import com.bumptech.glide.Glide; -import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.gif.GifDrawable; -import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.transition.Transition; import com.google.android.material.snackbar.Snackbar; 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 7c18791b2..0ce01194d 100644 --- a/app/src/main/java/app/fedilab/android/services/PostMessageService.java +++ b/app/src/main/java/app/fedilab/android/services/PostMessageService.java @@ -170,21 +170,16 @@ public class PostMessageService extends IntentService { } else { fileMultipartBody = Helper.getMultipartBody("file", attachment); } - Call attachmentCall = mastodonStatusesService.postMedia(dataPost.token, fileMultipartBody, null, attachment.description, null); - - if (attachmentCall != null) { - try { - Response attachmentResponse = attachmentCall.execute(); - if (attachmentResponse.isSuccessful()) { - Attachment attachmentReply = attachmentResponse.body(); - if (attachmentReply != null) { - attachmentIds.add(attachmentReply.id); - } - } - } catch (IOException e) { - error = true; - e.printStackTrace(); - } + String replyId = null; + int retry = 0; + while (replyId == null && retry < 3) { + replyId = postAttachment(mastodonStatusesService, dataPost, fileMultipartBody, attachment); + retry++; + } + if (replyId == null) { + error = true; + } else { + attachmentIds.add(replyId); } } @@ -296,6 +291,25 @@ public class PostMessageService extends IntentService { } } + private static String postAttachment(MastodonStatusesService mastodonStatusesService, DataPost dataPost, MultipartBody.Part fileMultipartBody, Attachment attachment) { + Call attachmentCall = mastodonStatusesService.postMedia(dataPost.token, fileMultipartBody, null, attachment.description, null); + + if (attachmentCall != null) { + try { + Response attachmentResponse = attachmentCall.execute(); + if (attachmentResponse.isSuccessful()) { + Attachment attachmentReply = attachmentResponse.body(); + if (attachmentReply != null) { + return attachmentReply.id; + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + @Override public void onCreate() { super.onCreate();