mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-30 03:24:44 +01:00
Uploading images when submitting comments is available.
This commit is contained in:
parent
91ae3f9610
commit
5210bbe7db
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
@ -373,6 +388,43 @@ public class CommentActivity extends BaseActivity {
|
||||
.show();
|
||||
}
|
||||
|
||||
@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) {
|
||||
|
@ -1118,4 +1118,7 @@
|
||||
|
||||
<string name="reply">Reply</string>
|
||||
|
||||
<string name="get_image_bitmap_failed">Unable to get the bitmap of the image</string>
|
||||
<string name="upload_image_failed">Unable to upload the image</string>
|
||||
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user