aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-07-16 11:53:55 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-07-16 11:53:55 +0000
commit89704cd6523cbd751a88a5c43c36e4958f44e178 (patch)
tree41881db2dbba3e2fad747cfe260c68cf300aed94 /app/src/main
parent25d8d901ade3d990310a1abfa676238fe6689f2b (diff)
downloadinfinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.tar
infinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.tar.gz
infinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.tar.bz2
infinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.tar.lz
infinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.tar.xz
infinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.tar.zst
infinity-for-reddit-89704cd6523cbd751a88a5c43c36e4958f44e178.zip
Implement image uploading in PostGalleryActivity.
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java59
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java9
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java1
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/UploadImageUtils.java11
-rw-r--r--app/src/main/res/values/strings.xml2
5 files changed, 72 insertions, 10 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 50109df3..e5410ff1 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java
@@ -4,6 +4,7 @@ import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
+import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
@@ -33,14 +34,19 @@ import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.switchmaterial.SwitchMaterial;
+import com.google.gson.Gson;
import com.libRG.CustomTextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.inject.Inject;
@@ -53,6 +59,7 @@ import ml.docilealligator.infinityforreddit.Flair;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
+import ml.docilealligator.infinityforreddit.RedditGalleryPayload;
import ml.docilealligator.infinityforreddit.adapters.RedditGallerySubmissionRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
@@ -60,7 +67,9 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.SelectOrCapture
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.services.SubmitPostService;
+import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
+import ml.docilealligator.infinityforreddit.utils.UploadImageUtils;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit;
@@ -165,7 +174,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
private FlairBottomSheetFragment flairSelectionBottomSheetFragment;
private Snackbar mPostingSnackbar;
private RedditGallerySubmissionRecyclerViewAdapter adapter;
- private Uri captureImageUri;
+ private Uri imageUri;
+ private boolean isUploading;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -201,8 +211,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() {
@Override
public void onAddImageClicked() {
- SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment();
- fragment.show(getSupportFragmentManager(), fragment.getTag());
+ if (!isUploading) {
+ SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment();
+ fragment.show(getSupportFragmentManager(), fragment.getTag());
+ } else {
+ Snackbar.make(coordinatorLayout, R.string.please_wait_image_is_uploading, Snackbar.LENGTH_SHORT).show();
+ }
}
});
imagesRecyclerView.setAdapter(adapter);
@@ -387,9 +401,9 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
public void captureImage() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
- captureImageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider",
+ imageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider",
File.createTempFile("temp_img", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES)));
- pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, captureImageUri);
+ pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE);
} catch (IOException ex) {
Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show();
@@ -398,6 +412,34 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
}
}
+ private void uploadImage() {
+ Handler handler = new Handler();
+ isUploading = true;
+ mExecutor.execute(() -> {
+ 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);
+ handler.post(() -> {
+ adapter.setImageAsUploaded(item);
+ isUploading = false;
+ });
+ } catch (ExecutionException | InterruptedException e) {
+ e.printStackTrace();
+ handler.post(() -> {
+ Snackbar.make(coordinatorLayout, R.string.get_image_bitmap_failed, Snackbar.LENGTH_LONG).show();
+ isUploading = false;
+ });
+ } catch (XmlPullParserException | JSONException | IOException e) {
+ e.printStackTrace();
+ handler.post(() -> {
+ Snackbar.make(coordinatorLayout, R.string.error_processing_image, Snackbar.LENGTH_LONG).show();
+ isUploading = false;
+ });
+ }
+ });
+ }
+
private void displaySubredditIcon() {
if (iconUrl != null && !iconUrl.equals("")) {
mGlide.load(iconUrl)
@@ -560,11 +602,14 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
return;
}
- adapter.addImage(data.getData().toString());
+ imageUri = data.getData();
+ adapter.addImage(imageUri.toString());
+ uploadImage();
}
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
- adapter.addImage(captureImageUri.toString());
+ adapter.addImage(imageUri.toString());
+ uploadImage();
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java
index 9184523e..87853c93 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java
@@ -5,6 +5,7 @@ import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
+import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -82,14 +83,13 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
- ((ImageViewHolder) holder).progressBar.setVisibility(View.GONE);
- ((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE);
return false;
}
})
.into(((ImageViewHolder) holder).imageView);
if (redditGalleryImageInfoList.get(position).payload != null) {
+ Log.i("asdfasdf", "asdf ");
((ImageViewHolder) holder).progressBar.setVisibility(View.GONE);
((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE);
}
@@ -128,6 +128,11 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
notifyItemInserted(redditGalleryImageInfoList.size() - 1);
}
+ public void setImageAsUploaded(RedditGalleryPayload.Item item) {
+ redditGalleryImageInfoList.get(redditGalleryImageInfoList.size() - 1).payload = item;
+ notifyItemChanged(redditGalleryImageInfoList.size() - 1);
+ }
+
class ImageViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image)
AspectRatioGifImageView imageView;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java
index 027d69dd..785cd12d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java
@@ -164,4 +164,5 @@ public class JSONUtils {
public static final String ESTIMATED_REMAINING_TIME_KEY = "estimated_remaining_time";
public static final String PAYLOAD_KEY = "payload";
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
+ public static final String ASSET_KEY = "asset";
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/UploadImageUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/UploadImageUtils.java
index 4eff065d..c7b5f728 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/UploadImageUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/UploadImageUtils.java
@@ -28,7 +28,13 @@ import retrofit2.Retrofit;
public class UploadImageUtils {
@Nullable
public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
- String accessToken, Bitmap image) throws IOException, JSONException, XmlPullParserException {
+ String accessToken, Bitmap image) throws IOException, JSONException, XmlPullParserException {
+ return uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image, false);
+ }
+
+ @Nullable
+ public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
+ String accessToken, Bitmap image, boolean returnResponseForGallerySubmission) throws IOException, JSONException, XmlPullParserException {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Map<String, String> uploadImageParams = new HashMap<>();
@@ -51,6 +57,9 @@ public class UploadImageUtils {
Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
if (uploadMediaToAWSResponse.isSuccessful()) {
+ if (returnResponseForGallerySubmission) {
+ return uploadImageResponse.body();
+ }
return parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
} else {
return "Error: " + uploadMediaToAWSResponse.code();
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 3742cc52..51f6727a 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1138,4 +1138,6 @@
<string name="search_comments">Search Comments</string>
+ <string name="please_wait_image_is_uploading">An image is still being uploaded. Please wait.</string>
+
</resources>