aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-07-04 02:03:38 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-07-04 02:03:38 +0000
commit5210bbe7dbdf6224a8ec7f4bde43dfa17348e608 (patch)
tree1dd4fff157ab6a6b20f7b79d2e3d492b058cce92 /app/src/main/java/ml/docilealligator/infinityforreddit
parent91ae3f9610cb97407bc23c2742fd4a991b681524 (diff)
downloadinfinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.tar
infinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.tar.gz
infinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.tar.bz2
infinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.tar.lz
infinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.tar.xz
infinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.tar.zst
infinity-for-reddit-5210bbe7dbdf6224a8ec7f4bde43dfa17348e608.zip
Uploading images when submitting comments is available.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java54
1 files changed, 53 insertions, 1 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java
index 8bfd09dc..9262eea2 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java
@@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
@@ -24,6 +25,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
+import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
@@ -31,7 +33,11 @@ import com.google.android.material.snackbar.Snackbar;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
+import org.json.JSONException;
+import org.xmlpull.v1.XmlPullParserException;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.inject.Inject;
@@ -58,6 +64,7 @@ import ml.docilealligator.infinityforreddit.comment.SendComment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
+import ml.docilealligator.infinityforreddit.utils.UploadImageUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Retrofit;
@@ -73,6 +80,7 @@ public class CommentActivity extends BaseActivity {
public static final String EXTRA_IS_REPLYING_KEY = "EIRK";
public static final String RETURN_EXTRA_COMMENT_DATA_KEY = "RECDK";
public static final int WRITE_COMMENT_REQUEST_CODE = 1;
+ private static final int PICK_IMAGE_REQUEST_CODE = 100;
@BindView(R.id.coordinator_layout_comment_activity)
CoordinatorLayout coordinatorLayout;
@@ -94,6 +102,9 @@ public class CommentActivity extends BaseActivity {
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
+ @Named("upload_media")
+ Retrofit mUploadMediaRetrofit;
+ @Inject
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@@ -246,7 +257,11 @@ public class CommentActivity extends BaseActivity {
@Override
public void onUploadImage() {
-
+ Intent intent = new Intent();
+ intent.setType("image/*");
+ intent.setAction(Intent.ACTION_GET_CONTENT);
+ startActivityForResult(Intent.createChooser(intent,
+ getResources().getString(R.string.select_from_gallery)), PICK_IMAGE_REQUEST_CODE);
}
});
@@ -374,6 +389,43 @@ public class CommentActivity extends BaseActivity {
}
@Override
+ protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+ if (resultCode == RESULT_OK) {
+ if (requestCode == PICK_IMAGE_REQUEST_CODE) {
+ if (data == null) {
+ Toast.makeText(CommentActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
+ return;
+ }
+ Handler handler = new Handler();
+ mExecutor.execute(() -> {
+ try {
+ Bitmap bitmap = Glide.with(CommentActivity.this).asBitmap().load(data.getData()).submit().get();
+ String imageUrlOrError = UploadImageUtils.uploadImage(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, bitmap);
+ handler.post(() -> {
+ if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
+ int start = Math.max(commentEditText.getSelectionStart(), 0);
+ int end = Math.max(commentEditText.getSelectionEnd(), 0);
+ commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
+ "[" + imageUrlOrError + "](" + imageUrlOrError + ")",
+ 0, "[]()".length() + imageUrlOrError.length() + imageUrlOrError.length());
+ } else {
+ Toast.makeText(CommentActivity.this, R.string.upload_image_failed, Toast.LENGTH_LONG).show();
+ }
+ });
+ } catch (ExecutionException | InterruptedException e) {
+ e.printStackTrace();
+ handler.post(() -> Toast.makeText(CommentActivity.this, R.string.get_image_bitmap_failed, Toast.LENGTH_LONG).show());
+ } catch (XmlPullParserException | JSONException | IOException e) {
+ e.printStackTrace();
+ handler.post(() -> Toast.makeText(CommentActivity.this, R.string.error_processing_image, Toast.LENGTH_LONG).show());
+ }
+ });
+ }
+ }
+ }
+
+ @Override
public void onBackPressed() {
if (isSubmitting) {
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail);