mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-30 21:07:11 +01:00
Implement image uploading in PostGalleryActivity.
This commit is contained in:
parent
25d8d901ad
commit
89704cd652
@ -4,6 +4,7 @@ import android.content.ActivityNotFoundException;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
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.dialog.MaterialAlertDialogBuilder;
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.google.android.material.switchmaterial.SwitchMaterial;
|
import com.google.android.material.switchmaterial.SwitchMaterial;
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.libRG.CustomTextView;
|
import com.libRG.CustomTextView;
|
||||||
|
|
||||||
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.EventBus;
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@ -53,6 +59,7 @@ import ml.docilealligator.infinityforreddit.Flair;
|
|||||||
import ml.docilealligator.infinityforreddit.Infinity;
|
import ml.docilealligator.infinityforreddit.Infinity;
|
||||||
import ml.docilealligator.infinityforreddit.R;
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
|
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
|
||||||
|
import ml.docilealligator.infinityforreddit.RedditGalleryPayload;
|
||||||
import ml.docilealligator.infinityforreddit.adapters.RedditGallerySubmissionRecyclerViewAdapter;
|
import ml.docilealligator.infinityforreddit.adapters.RedditGallerySubmissionRecyclerViewAdapter;
|
||||||
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
|
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
|
||||||
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
|
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.customtheme.CustomThemeWrapper;
|
||||||
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
|
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
|
||||||
import ml.docilealligator.infinityforreddit.services.SubmitPostService;
|
import ml.docilealligator.infinityforreddit.services.SubmitPostService;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
|
||||||
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.UploadImageUtils;
|
||||||
import pl.droidsonroids.gif.GifImageView;
|
import pl.droidsonroids.gif.GifImageView;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
@ -165,7 +174,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
|
|||||||
private FlairBottomSheetFragment flairSelectionBottomSheetFragment;
|
private FlairBottomSheetFragment flairSelectionBottomSheetFragment;
|
||||||
private Snackbar mPostingSnackbar;
|
private Snackbar mPostingSnackbar;
|
||||||
private RedditGallerySubmissionRecyclerViewAdapter adapter;
|
private RedditGallerySubmissionRecyclerViewAdapter adapter;
|
||||||
private Uri captureImageUri;
|
private Uri imageUri;
|
||||||
|
private boolean isUploading;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -201,8 +211,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
|
|||||||
adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() {
|
adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAddImageClicked() {
|
public void onAddImageClicked() {
|
||||||
SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment();
|
if (!isUploading) {
|
||||||
fragment.show(getSupportFragmentManager(), fragment.getTag());
|
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);
|
imagesRecyclerView.setAdapter(adapter);
|
||||||
@ -387,9 +401,9 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
|
|||||||
public void captureImage() {
|
public void captureImage() {
|
||||||
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||||
try {
|
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)));
|
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);
|
startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show();
|
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() {
|
private void displaySubredditIcon() {
|
||||||
if (iconUrl != null && !iconUrl.equals("")) {
|
if (iconUrl != null && !iconUrl.equals("")) {
|
||||||
mGlide.load(iconUrl)
|
mGlide.load(iconUrl)
|
||||||
@ -560,11 +602,14 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter.addImage(data.getData().toString());
|
imageUri = data.getData();
|
||||||
|
adapter.addImage(imageUri.toString());
|
||||||
|
uploadImage();
|
||||||
}
|
}
|
||||||
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
|
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
|
||||||
if (resultCode == RESULT_OK) {
|
if (resultCode == RESULT_OK) {
|
||||||
adapter.addImage(captureImageUri.toString());
|
adapter.addImage(imageUri.toString());
|
||||||
|
uploadImage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import android.content.res.ColorStateList;
|
|||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -82,14 +83,13 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.into(((ImageViewHolder) holder).imageView);
|
.into(((ImageViewHolder) holder).imageView);
|
||||||
|
|
||||||
if (redditGalleryImageInfoList.get(position).payload != null) {
|
if (redditGalleryImageInfoList.get(position).payload != null) {
|
||||||
|
Log.i("asdfasdf", "asdf ");
|
||||||
((ImageViewHolder) holder).progressBar.setVisibility(View.GONE);
|
((ImageViewHolder) holder).progressBar.setVisibility(View.GONE);
|
||||||
((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE);
|
((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
@ -128,6 +128,11 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
|
|||||||
notifyItemInserted(redditGalleryImageInfoList.size() - 1);
|
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 {
|
class ImageViewHolder extends RecyclerView.ViewHolder {
|
||||||
@BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image)
|
@BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image)
|
||||||
AspectRatioGifImageView imageView;
|
AspectRatioGifImageView imageView;
|
||||||
|
@ -164,4 +164,5 @@ public class JSONUtils {
|
|||||||
public static final String ESTIMATED_REMAINING_TIME_KEY = "estimated_remaining_time";
|
public static final String ESTIMATED_REMAINING_TIME_KEY = "estimated_remaining_time";
|
||||||
public static final String PAYLOAD_KEY = "payload";
|
public static final String PAYLOAD_KEY = "payload";
|
||||||
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
|
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
|
||||||
|
public static final String ASSET_KEY = "asset";
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,13 @@ import retrofit2.Retrofit;
|
|||||||
public class UploadImageUtils {
|
public class UploadImageUtils {
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
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);
|
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
|
||||||
|
|
||||||
Map<String, String> uploadImageParams = new HashMap<>();
|
Map<String, String> uploadImageParams = new HashMap<>();
|
||||||
@ -51,6 +57,9 @@ public class UploadImageUtils {
|
|||||||
Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
|
Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
|
||||||
Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
|
Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
|
||||||
if (uploadMediaToAWSResponse.isSuccessful()) {
|
if (uploadMediaToAWSResponse.isSuccessful()) {
|
||||||
|
if (returnResponseForGallerySubmission) {
|
||||||
|
return uploadImageResponse.body();
|
||||||
|
}
|
||||||
return parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
|
return parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
|
||||||
} else {
|
} else {
|
||||||
return "Error: " + uploadMediaToAWSResponse.code();
|
return "Error: " + uploadMediaToAWSResponse.code();
|
||||||
|
@ -1138,4 +1138,6 @@
|
|||||||
|
|
||||||
<string name="search_comments">Search Comments</string>
|
<string name="search_comments">Search Comments</string>
|
||||||
|
|
||||||
|
<string name="please_wait_image_is_uploading">An image is still being uploaded. Please wait.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user