Continue adding support for submitting gallery posts.

This commit is contained in:
Alex Ning 2021-07-16 22:44:18 +08:00
parent 19a94f7e0a
commit 299813d527
7 changed files with 70 additions and 23 deletions

View File

@ -5,7 +5,7 @@ import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import java.util.List;
import java.util.ArrayList;
public class RedditGalleryPayload {
@SerializedName("sr")
@ -29,17 +29,24 @@ public class RedditGalleryPayload {
public boolean sendReplies;
@SerializedName("validate_on_submit")
public boolean validateOnSubmit = true;
public List<Item> items;
@SerializedName("flair_id")
public String flairId;
@SerializedName("flair_text")
public String flairText;
public ArrayList<Item> items;
public RedditGalleryPayload(String subredditName, String submitType, String kind, String title,
boolean isSpoiler, boolean isNSFW, boolean sendReplies, List<Item> items) {
public RedditGalleryPayload(String subredditName, String submitType, String title,
boolean isSpoiler, boolean isNSFW, boolean sendReplies, Flair flair, ArrayList<Item> items) {
this.subredditName = subredditName;
this.submitType = submitType;
this.title = title;
this.isSpoiler = isSpoiler;
this.isNSFW = isNSFW;
this.kind = kind;
this.sendReplies = sendReplies;
if (flair != null) {
flairId = flair.getId();
flairText = flair.getText();
}
this.items = items;
}

View File

@ -24,7 +24,6 @@ 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;
@ -459,9 +458,9 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
try {
Bitmap resource = Glide.with(PostGalleryActivity.this).asBitmap().load(imageUri).submit().get();
String response = UploadImageUtils.uploadImage(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, resource, true);
RedditGalleryPayload.Item item = new Gson().fromJson(new JSONObject(response).getJSONObject(JSONUtils.ASSET_KEY).toString(), RedditGalleryPayload.Item.class);
String mediaId = new JSONObject(response).getJSONObject(JSONUtils.ASSET_KEY).getString(JSONUtils.ASSET_ID_KEY);
handler.post(() -> {
adapter.setImageAsUploaded(item);
adapter.setImageAsUploaded(mediaId);
isUploading = false;
});
} catch (ExecutionException | InterruptedException e) {
@ -475,7 +474,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
e.printStackTrace();
handler.post(() -> {
adapter.removeFailedToUploadImage();
Snackbar.make(coordinatorLayout, R.string.error_processing_image, Snackbar.LENGTH_LONG).show();
Snackbar.make(coordinatorLayout, R.string.upload_image_failed, Snackbar.LENGTH_LONG).show();
isUploading = false;
});
}
@ -516,7 +515,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.post_image_activity, menu);
getMenuInflater().inflate(R.menu.post_gallery_activity, menu);
applyMenuItemTheme(menu);
mMemu = menu;
if (isPosting) {
@ -541,7 +540,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
}
finish();
return true;
} else if (itemId == R.id.action_send_post_image_activity) {
} else if (itemId == R.id.action_send_post_gallery_activity) {
if (!subredditSelected) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
return true;
@ -552,11 +551,16 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
return true;
}
if (redditGalleryImageInfoList == null) {
if (redditGalleryImageInfoList == null || redditGalleryImageInfoList.isEmpty()) {
Snackbar.make(coordinatorLayout, R.string.select_an_image, Snackbar.LENGTH_SHORT).show();
return true;
}
if (redditGalleryImageInfoList.get(redditGalleryImageInfoList.size() - 1).payload == null) {
Snackbar.make(coordinatorLayout, R.string.please_wait_image_is_uploading, Snackbar.LENGTH_LONG).show();
return true;
}
isPosting = true;
item.setEnabled(false);
@ -574,13 +578,17 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
Intent intent = new Intent(this, SubmitPostService.class);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler);
intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW);
intent.putExtra(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, receivePostReplyNotificationsSwitchMaterial.isChecked());
intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_GALLERY);
ArrayList<RedditGalleryPayload.Item> items = new ArrayList<>();
for (RedditGallerySubmissionRecyclerViewAdapter.RedditGalleryImageInfo i : redditGalleryImageInfoList) {
items.add(i.payload);
}
RedditGalleryPayload payload = new RedditGalleryPayload(subredditName, subredditIsUser ? "profile" : "subreddit",
titleEditText.getText().toString(), isSpoiler, isNSFW, receivePostReplyNotificationsSwitchMaterial.isChecked(),
flair, items);
intent.putExtra(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, new Gson().toJson(payload));
ContextCompat.startForegroundService(this, intent);
//ContextCompat.startForegroundService(this, intent);
return true;
}

View File

@ -131,8 +131,8 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
notifyItemInserted(redditGalleryImageInfoList.size() - 1);
}
public void setImageAsUploaded(RedditGalleryPayload.Item item) {
redditGalleryImageInfoList.get(redditGalleryImageInfoList.size() - 1).payload = item;
public void setImageAsUploaded(String mediaId) {
redditGalleryImageInfoList.get(redditGalleryImageInfoList.size() - 1).payload = new RedditGalleryPayload.Item("", "", mediaId);
notifyItemChanged(redditGalleryImageInfoList.size() - 1);
}

View File

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

View File

@ -41,6 +41,7 @@ import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Flair;
import ml.docilealligator.infinityforreddit.Infinity;
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.SubmitImagePostEvent;
@ -50,6 +51,7 @@ import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.post.SubmitPost;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.NotificationUtils;
import retrofit2.Response;
import retrofit2.Retrofit;
public class SubmitPostService extends Service {
@ -67,7 +69,8 @@ public class SubmitPostService extends Service {
public static final int EXTRA_POST_TEXT_OR_LINK = 0;
public static final int EXTRA_POST_TYPE_IMAGE = 1;
public static final int EXTRA_POST_TYPE_VIDEO = 2;
public static final int EXTRA_POST_TYPE_CROSSPOST = 3;
public static final int EXTRA_POST_TYPE_GALLERY = 3;
public static final int EXTRA_POST_TYPE_CROSSPOST = 4;
private static final String EXTRA_MEDIA_URI = "EU";
@Inject
@ -126,10 +129,12 @@ public class SubmitPostService extends Service {
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
submitImagePost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW,
receivePostReplyNotifications);
} else {
} else if (postType == EXTRA_POST_TYPE_VIDEO) {
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
submitVideoPost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW,
receivePostReplyNotifications);
} else {
}
}
}
@ -176,9 +181,11 @@ public class SubmitPostService extends Service {
} else if (postType == EXTRA_POST_TYPE_IMAGE) {
bundle.putString(EXTRA_MEDIA_URI, intent.getData().toString());
startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting_image));
} else {
} else if (postType == EXTRA_POST_TYPE_VIDEO) {
bundle.putString(EXTRA_MEDIA_URI, intent.getData().toString());
startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting_video));
} else {
startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting_gallery));
}
Message msg = serviceHandler.obtainMessage();
@ -328,6 +335,14 @@ 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) {
e.printStackTrace();
}
}
private static void copyFileToCache(InputStream fileInputStream, String destinationFilePath) throws IOException {
OutputStream out = new FileOutputStream(destinationFilePath);
byte[] buf = new byte[2048];

View File

@ -165,4 +165,5 @@ public class JSONUtils {
public static final String PAYLOAD_KEY = "payload";
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
public static final String ASSET_KEY = "asset";
public static final String ASSET_ID_KEY = "asset_id";
}

View File

@ -287,6 +287,7 @@
<string name="posting_video">Posting Video</string>
<string name="posting_image">Posting Image</string>
<string name="posting_gallery">Posting Gallery</string>
<string name="please_wait">Please wait</string>
<string name="add_account">Add account</string>