Submitting gallery posts is now available.

This commit is contained in:
Alex Ning 2021-07-16 23:02:11 +08:00
parent 299813d527
commit e877b2e439
3 changed files with 40 additions and 8 deletions

View File

@ -24,6 +24,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -588,7 +589,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
flair, items); flair, items);
intent.putExtra(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, new Gson().toJson(payload)); intent.putExtra(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, new Gson().toJson(payload));
//ContextCompat.startForegroundService(this, intent); ContextCompat.startForegroundService(this, intent);
return true; return true;
} }

View File

@ -1,15 +1,13 @@
package ml.docilealligator.infinityforreddit.events; package ml.docilealligator.infinityforreddit.events;
import ml.docilealligator.infinityforreddit.post.Post;
public class SubmitGalleryPostEvent { public class SubmitGalleryPostEvent {
public boolean postSuccess; public boolean postSuccess;
public Post post; public String postUrl;
public String errorMessage; public String errorMessage;
public SubmitGalleryPostEvent(boolean postSuccess, Post post, String errorMessage) { public SubmitGalleryPostEvent(boolean postSuccess, String postUrl, String errorMessage) {
this.postSuccess = postSuccess; this.postSuccess = postSuccess;
this.post = post; this.postUrl = postUrl;
this.errorMessage = errorMessage; this.errorMessage = errorMessage;
} }
} }

View File

@ -25,6 +25,9 @@ import androidx.core.app.NotificationManagerCompat;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -44,12 +47,14 @@ import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.SubmitCrosspostEvent; import ml.docilealligator.infinityforreddit.events.SubmitCrosspostEvent;
import ml.docilealligator.infinityforreddit.events.SubmitGalleryPostEvent;
import ml.docilealligator.infinityforreddit.events.SubmitImagePostEvent; import ml.docilealligator.infinityforreddit.events.SubmitImagePostEvent;
import ml.docilealligator.infinityforreddit.events.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitTextOrLinkPostEvent;
import ml.docilealligator.infinityforreddit.events.SubmitVideoOrGifPostEvent; import ml.docilealligator.infinityforreddit.events.SubmitVideoOrGifPostEvent;
import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.post.SubmitPost; import ml.docilealligator.infinityforreddit.post.SubmitPost;
import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import ml.docilealligator.infinityforreddit.utils.NotificationUtils;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -134,7 +139,7 @@ public class SubmitPostService extends Service {
submitVideoPost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW, submitVideoPost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW,
receivePostReplyNotifications); receivePostReplyNotifications);
} else { } 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) { private void submitGalleryPost(String accessToken, String payload) {
try { try {
Response<String> response = mOauthRetrofit.create(RedditAPI.class).submitGalleryPost(APIUtils.getOAuthHeader(accessToken), payload).execute(); 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(); e.printStackTrace();
handler.post(() -> EventBus.getDefault().post(new SubmitGalleryPostEvent(false, null, e.getMessage())));
} finally {
stopService();
} }
} }