diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-07-16 15:02:11 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-07-16 15:02:11 +0000 |
commit | e877b2e4391bf9f67a113541ef1addc5f7b7a5aa (patch) | |
tree | 25c9eb5b43fa06f67cc9b5f48ba1b403ab36ecc6 /app/src/main/java | |
parent | 299813d527aea24c1b1dbf6ea9aca34f697ef51d (diff) | |
download | infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.tar infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.tar.gz infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.tar.bz2 infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.tar.lz infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.tar.xz infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.tar.zst infinity-for-reddit-e877b2e4391bf9f67a113541ef1addc5f7b7a5aa.zip |
Submitting gallery posts is now available.
Diffstat (limited to 'app/src/main/java')
3 files changed, 40 insertions, 8 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java index 429257e0..1e0e7084 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -24,6 +24,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -588,7 +589,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee flair, items); intent.putExtra(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, new Gson().toJson(payload)); - //ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitGalleryPostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitGalleryPostEvent.java index 53ee9906..fcab85cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitGalleryPostEvent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/SubmitGalleryPostEvent.java @@ -1,15 +1,13 @@ package ml.docilealligator.infinityforreddit.events; -import ml.docilealligator.infinityforreddit.post.Post; - public class SubmitGalleryPostEvent { public boolean postSuccess; - public Post post; + public String postUrl; public String errorMessage; - public SubmitGalleryPostEvent(boolean postSuccess, Post post, String errorMessage) { + public SubmitGalleryPostEvent(boolean postSuccess, String postUrl, String errorMessage) { this.postSuccess = postSuccess; - this.post = post; + this.postUrl = postUrl; this.errorMessage = errorMessage; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java index 54f99c2d..e22bc541 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java @@ -25,6 +25,9 @@ import androidx.core.app.NotificationManagerCompat; import com.bumptech.glide.Glide; import org.greenrobot.eventbus.EventBus; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import java.io.File; import java.io.FileOutputStream; @@ -44,12 +47,14 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SubmitCrosspostEvent; +import ml.docilealligator.infinityforreddit.events.SubmitGalleryPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitImagePostEvent; import ml.docilealligator.infinityforreddit.events.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitVideoOrGifPostEvent; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.SubmitPost; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import retrofit2.Response; import retrofit2.Retrofit; @@ -134,7 +139,7 @@ public class SubmitPostService extends Service { submitVideoPost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW, receivePostReplyNotifications); } else { - + submitGalleryPost(accessToken, bundle.getString(EXTRA_REDDIT_GALLERY_PAYLOAD)); } } } @@ -338,8 +343,36 @@ public class SubmitPostService extends Service { private void submitGalleryPost(String accessToken, String payload) { try { Response<String> response = mOauthRetrofit.create(RedditAPI.class).submitGalleryPost(APIUtils.getOAuthHeader(accessToken), payload).execute(); - } catch (IOException e) { + if (response.isSuccessful()) { + JSONObject responseObject = new JSONObject(response.body()).getJSONObject(JSONUtils.JSON_KEY); + if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { + JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY) + .getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1); + if (error.length() != 0) { + String errorMessage; + if (error.length() >= 2) { + errorMessage = error.getString(1); + } else { + errorMessage = error.getString(0); + } + handler.post(() -> EventBus.getDefault().post(new SubmitGalleryPostEvent(false, null, errorMessage))); + } else { + handler.post(() -> EventBus.getDefault().post(new SubmitGalleryPostEvent(false, null, null))); + } + } else { + String postUrl = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.URL_KEY); + handler.post(() -> { + EventBus.getDefault().post(new SubmitGalleryPostEvent(true, postUrl, null)); + }); + } + } else { + handler.post(() -> EventBus.getDefault().post(new SubmitGalleryPostEvent(false, null, response.message()))); + } + } catch (IOException | JSONException e) { e.printStackTrace(); + handler.post(() -> EventBus.getDefault().post(new SubmitGalleryPostEvent(false, null, e.getMessage()))); + } finally { + stopService(); } } |