From b9ecbc2a105828326e8996229eb57640f248b1ce Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 15 Jul 2021 22:47:21 +0800 Subject: Continue adding support for submitting gallery posts. --- .../infinityforreddit/RedditGalleryPayload.java | 35 +++++++++++++++- .../infinityforreddit/activities/MainActivity.java | 4 ++ .../activities/PostGalleryActivity.java | 47 +++++++++++---------- .../activities/SearchResultActivity.java | 4 ++ .../activities/ViewSubredditDetailActivity.java | 5 +++ .../activities/ViewUserDetailActivity.java | 4 ++ .../adapters/PostDetailRecyclerViewAdapter.java | 2 +- .../adapters/PostRecyclerViewAdapter.java | 18 ++++---- ...RedditGallerySubmissionRecyclerViewAdapter.java | 48 ++++++++++++++++++++-- .../PostTypeBottomSheetFragment.java | 8 ++++ .../services/SubmitPostService.java | 2 + 11 files changed, 138 insertions(+), 39 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java index aec9f66c..94fcb822 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java @@ -1,5 +1,8 @@ package ml.docilealligator.infinityforreddit; +import android.os.Parcel; +import android.os.Parcelable; + import com.google.gson.annotations.SerializedName; import java.util.List; @@ -40,7 +43,7 @@ public class RedditGalleryPayload { this.items = items; } - public class Item { + public static class Item implements Parcelable { public String caption; @SerializedName("outbound_url") public String outboundUrl; @@ -52,5 +55,35 @@ public class RedditGalleryPayload { this.outboundUrl = outboundUrl; this.mediaId = mediaId; } + + protected Item(Parcel in) { + caption = in.readString(); + outboundUrl = in.readString(); + mediaId = in.readString(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public Item createFromParcel(Parcel in) { + return new Item(in); + } + + @Override + public Item[] newArray(int size) { + return new Item[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(caption); + parcel.writeString(outboundUrl); + parcel.writeString(mediaId); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 4a2c945a..3b62cd49 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -1216,6 +1216,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb case PostTypeBottomSheetFragment.TYPE_VIDEO: intent = new Intent(MainActivity.this, PostVideoActivity.class); startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_GALLERY: + intent = new Intent(MainActivity.this, PostGalleryActivity.class); + startActivity(intent); } } 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 dd340e68..50786e1b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -34,6 +34,7 @@ import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.ArrayList; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -46,6 +47,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.adapters.RedditGallerySubmissionRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -68,6 +70,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; + private static final String REDDIT_GALLERY_IMAGE_INFO_STATE = "RGIIS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int PICK_IMAGE_REQUEST_CODE = 1; @@ -130,6 +133,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + private ArrayList redditGalleryImageInfoList; private String mAccessToken; private String mAccountName; private String iconUrl; @@ -153,6 +157,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee private RequestManager mGlide; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; private Snackbar mPostingSnackbar; + private RedditGallerySubmissionRecyclerViewAdapter adapter; + private Uri captureImageUri; @Override protected void onCreate(Bundle savedInstanceState) { @@ -185,6 +191,14 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() { + @Override + public void onAddImageClicked() { + + } + }); + imagesRecyclerView.setAdapter(adapter); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); @@ -195,11 +209,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee flair = savedInstanceState.getParcelable(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); - - if (savedInstanceState.getString(IMAGE_URI_STATE) != null) { - imageUri = Uri.parse(savedInstanceState.getString(IMAGE_URI_STATE)); - loadImage(); - } + redditGalleryImageInfoList = savedInstanceState.getParcelableArrayList(REDDIT_GALLERY_IMAGE_INFO_STATE); + adapter.setRedditGalleryImageInfoList(redditGalleryImageInfoList); if (subredditName != null) { subredditNameTextView.setTextColor(primaryTextColor); @@ -247,11 +258,6 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .into(iconGifImageView); } - - imageUri = getIntent().getData(); - if (imageUri != null) { - loadImage(); - } } iconGifImageView.setOnClickListener(view -> { @@ -415,7 +421,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); return true; } else { - if (!titleEditText.getText().toString().equals("") || imageUri != null) { + if (!titleEditText.getText().toString().equals("") || redditGalleryImageInfoList != null) { promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } @@ -433,7 +439,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee return true; } - if (imageUri == null) { + if (redditGalleryImageInfoList == null) { Snackbar.make(coordinatorLayout, R.string.select_an_image, Snackbar.LENGTH_SHORT).show(); return true; } @@ -453,7 +459,6 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee } Intent intent = new Intent(this, SubmitPostService.class); - intent.setData(imageUri); intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); @@ -461,13 +466,6 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); intent.putExtra(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, receivePostReplyNotificationsSwitchMaterial.isChecked()); - String mimeType = getContentResolver().getType(imageUri); - if (mimeType != null && mimeType.contains("gif")) { - intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); - } else { - intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_IMAGE); - } - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); ContextCompat.startForegroundService(this, intent); @@ -482,7 +480,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee if (isPosting) { promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); } else { - if (!titleEditText.getText().toString().equals("") || imageUri != null) { + if (!titleEditText.getText().toString().equals("") || redditGalleryImageInfoList != null) { promptAlertDialog(R.string.discard, R.string.discard_detail); } else { finish(); @@ -502,6 +500,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); + redditGalleryImageInfoList = adapter.getRedditGalleryImageInfoList(); + outState.putParcelableArrayList(REDDIT_GALLERY_IMAGE_INFO_STATE, redditGalleryImageInfoList); } @Override @@ -531,12 +531,11 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee return; } - imageUri = data.getData(); - loadImage(); + adapter.addImage(data.getData().toString()); } } else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) { if (resultCode == RESULT_OK) { - loadImage(); + adapter.addImage(captureImageUri.toString()); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index 7098b86a..15c004a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -724,6 +724,10 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect case PostTypeBottomSheetFragment.TYPE_VIDEO: intent = new Intent(this, PostVideoActivity.class); startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_GALLERY: + intent = new Intent(this, PostGalleryActivity.class); + startActivity(intent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index bed746c4..8486318d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -1228,6 +1228,11 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp intent = new Intent(this, PostVideoActivity.class); intent.putExtra(PostVideoActivity.EXTRA_SUBREDDIT_NAME, subredditName); startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_GALLERY: + intent = new Intent(this, PostGalleryActivity.class); + intent.putExtra(PostVideoActivity.EXTRA_SUBREDDIT_NAME, subredditName); + startActivity(intent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index c1ef9ea2..5432c074 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -1386,6 +1386,10 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele case PostTypeBottomSheetFragment.TYPE_VIDEO: intent = new Intent(this, PostVideoActivity.class); startActivity(intent); + break; + case PostTypeBottomSheetFragment.TYPE_GALLERY: + intent = new Intent(this, PostGalleryActivity.class); + startActivity(intent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 323e22c6..43f6fcbe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -766,7 +766,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter previews = post.getPreviews(); if (previews != null && !previews.isEmpty()) { @@ -1259,7 +1259,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter redditGalleryImageInfoList; + private ArrayList redditGalleryImageInfoList; private CustomThemeWrapper customThemeWrapper; private ItemClickListener itemClickListener; private RequestManager glide; @@ -109,10 +111,20 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada } } - public List getRedditGalleryImageInfoList() { + public ArrayList getRedditGalleryImageInfoList() { return redditGalleryImageInfoList; } + public void setRedditGalleryImageInfoList(ArrayList redditGalleryImageInfoList) { + this.redditGalleryImageInfoList = redditGalleryImageInfoList; + notifyDataSetChanged(); + } + + public void addImage(String imageUrl) { + redditGalleryImageInfoList.add(new RedditGalleryImageInfo(imageUrl)); + notifyItemInserted(redditGalleryImageInfoList.size() - 1); + } + class ImageViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image) AspectRatioGifImageView imageView; @@ -147,13 +159,41 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada } } - public static class RedditGalleryImageInfo { + public static class RedditGalleryImageInfo implements Parcelable { public String imageUrlString; public RedditGalleryPayload.Item payload; public RedditGalleryImageInfo(String imageUrlString) { this.imageUrlString = imageUrlString; } + + protected RedditGalleryImageInfo(Parcel in) { + imageUrlString = in.readString(); + payload = in.readParcelable(RedditGalleryPayload.Item.class.getClassLoader()); + } + + public static final Creator CREATOR = new Creator() { + @Override + public RedditGalleryImageInfo createFromParcel(Parcel in) { + return new RedditGalleryImageInfo(in); + } + + @Override + public RedditGalleryImageInfo[] newArray(int size) { + return new RedditGalleryImageInfo[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(imageUrlString); + parcel.writeParcelable(payload, i); + } } public interface ItemClickListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java index 64b604ab..046c512b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostTypeBottomSheetFragment.java @@ -30,6 +30,7 @@ public class PostTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen public static final int TYPE_LINK = 1; public static final int TYPE_IMAGE = 2; public static final int TYPE_VIDEO = 3; + public static final int TYPE_GALLERY = 4; @BindView(R.id.text_type_linear_layout_post_type_bottom_sheet_fragment) TextView textTypeTextView; @BindView(R.id.link_type_linear_layout_post_type_bottom_sheet_fragment) @@ -38,6 +39,8 @@ public class PostTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen TextView imageTypeTextView; @BindView(R.id.video_type_linear_layout_post_type_bottom_sheet_fragment) TextView videoTypeTextView; + @BindView(R.id.gallery_type_linear_layout_post_type_bottom_sheet_fragment) + TextView galleryTypeTextView; private Activity activity; public PostTypeBottomSheetFragment() { @@ -75,6 +78,11 @@ public class PostTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen dismiss(); }); + galleryTypeTextView.setOnClickListener(view -> { + ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_GALLERY); + dismiss(); + }); + return rootView; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java index 4d21ef12..b883b02c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java @@ -57,6 +57,7 @@ public class SubmitPostService extends Service { public static final String EXTRA_SUBREDDIT_NAME = "ESN"; public static final String EXTRA_TITLE = "ET"; public static final String EXTRA_CONTENT = "EC"; + public static final String EXTRA_REDDIT_GALLERY_PAYLOAD = "ERGP"; public static final String EXTRA_KIND = "EK"; public static final String EXTRA_FLAIR = "EF"; public static final String EXTRA_IS_SPOILER = "EIS"; @@ -67,6 +68,7 @@ public class SubmitPostService extends Service { 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; + private static final String EXTRA_MEDIA_URI = "EU"; @Inject @Named("oauth") -- cgit v1.2.3