From 2569742e1c61f92f36d656965eadb608fb185667 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 28 Jan 2022 12:58:16 +0800 Subject: Set caption and url when submitting gallery posts. --- .../infinityforreddit/RedditGalleryPayload.java | 16 +++ .../activities/CustomizePostFilterActivity.java | 2 +- .../activities/PostGalleryActivity.java | 21 ++-- ...RedditGallerySubmissionRecyclerViewAdapter.java | 32 +++++- .../asynctasks/InsertUserData.java | 3 + ...alleryItemCaptionAndUrlBottomSheetFragment.java | 112 +++++++++++++++++++++ .../infinityforreddit/user/FetchUserData.java | 2 +- .../infinityforreddit/user/UserDao.java | 3 + ...t_gallery_item_caption_and_url_bottom_sheet.xml | 75 ++++++++++++++ app/src/main/res/values/strings.xml | 3 + 10 files changed, 254 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.java create mode 100644 app/src/main/res/layout/fragment_set_reddit_gallery_item_caption_and_url_bottom_sheet.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java index ef91d6d9..d8763ff0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditGalleryPayload.java @@ -92,5 +92,21 @@ public class RedditGalleryPayload { parcel.writeString(outboundUrl); parcel.writeString(mediaId); } + + public String getCaption() { + return caption; + } + + public void setCaption(String caption) { + this.caption = caption == null ? "" : caption; + } + + public String getOutboundUrl() { + return outboundUrl; + } + + public void setOutboundUrl(String outboundUrl) { + this.outboundUrl = outboundUrl; + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java index 5f7bbc6e..4e96ec0f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java @@ -504,7 +504,7 @@ public class CustomizePostFilterActivity extends BaseActivity { } } - public static void setCursorDrawableColor(EditText editText, int color) { + public void setCursorDrawableColor(EditText editText, int color) { try { Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); fCursorDrawableRes.setAccessible(true); 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 1782776b..6e5dfd7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -210,15 +210,12 @@ 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() { - 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(); - } + adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, () -> { + 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); @@ -685,6 +682,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee flairTextView.setTextColor(flairTextColor); } + public void setCaptionAndUrl(int position, String caption, String url) { + if (adapter != null) { + adapter.setCaptionAndUrl(position, caption, url); + } + } + @Subscribe public void onAccountSwitchEvent(SwitchAccountEvent event) { finish(); 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 bec2d265..e585f2f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java @@ -1,8 +1,8 @@ package ml.docilealligator.infinityforreddit.adapters; -import android.content.Context; import android.content.res.ColorStateList; import android.graphics.drawable.Drawable; +import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; import android.view.LayoutInflater; @@ -33,6 +33,8 @@ import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditGalleryPayload; +import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.SetRedditGalleryItemCaptionAndUrlBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; @@ -41,14 +43,16 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada private static final int VIEW_TYPE_IMAGE = 1; private static final int VIEW_TYPE_ADD_IMAGE = 2; + private PostGalleryActivity activity; private ArrayList redditGalleryImageInfoList; private CustomThemeWrapper customThemeWrapper; private ItemClickListener itemClickListener; private RequestManager glide; - public RedditGallerySubmissionRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper, + public RedditGallerySubmissionRecyclerViewAdapter(PostGalleryActivity activity, CustomThemeWrapper customThemeWrapper, ItemClickListener itemClickListener) { - glide = Glide.with(context); + this.activity = activity; + glide = Glide.with(activity); this.customThemeWrapper = customThemeWrapper; this.itemClickListener = itemClickListener; } @@ -76,7 +80,7 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada if (holder instanceof ImageViewHolder) { glide.load(redditGalleryImageInfoList.get(position).imageUrlString) .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(48))) - .listener(new RequestListener() { + .listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -141,6 +145,13 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada notifyItemRemoved(redditGalleryImageInfoList.size()); } + public void setCaptionAndUrl(int position, String caption, String url) { + if (redditGalleryImageInfoList.size() > position && position >= 0) { + redditGalleryImageInfoList.get(position).payload.setCaption(caption); + redditGalleryImageInfoList.get(position).payload.setOutboundUrl(url); + } + } + class ImageViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image) AspectRatioGifImageView imageView; @@ -156,6 +167,19 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada imageView.setRatio(1); + imageView.setOnClickListener(view -> { + RedditGalleryPayload.Item payload = redditGalleryImageInfoList.get(getBindingAdapterPosition()).payload; + if (payload != null) { + SetRedditGalleryItemCaptionAndUrlBottomSheetFragment fragment = new SetRedditGalleryItemCaptionAndUrlBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.EXTRA_POSITION, getBindingAdapterPosition()); + bundle.putString(SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.EXTRA_CAPTION, payload.getCaption()); + bundle.putString(SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.EXTRA_URL, payload.getOutboundUrl()); + fragment.setArguments(bundle); + fragment.show(activity.getSupportFragmentManager(), fragment.getTag()); + } + }); + closeImageView.setOnClickListener(view -> { redditGalleryImageInfoList.remove(getBindingAdapterPosition()); notifyItemRemoved(getBindingAdapterPosition()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertUserData.java index 0e480315..3c8541f4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertUserData.java @@ -12,6 +12,9 @@ public class InsertUserData { public static void insertUserData(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, UserData userData, InsertUserDataListener insertUserDataListener) { executor.execute(() -> { + if (redditDataRoomDatabase.userDao().getNUsers() > 10000) { + redditDataRoomDatabase.userDao().deleteAllUsers(); + } redditDataRoomDatabase.userDao().insert(userData); if (insertUserDataListener != null) { handler.post(insertUserDataListener::insertSuccess); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.java new file mode 100644 index 00000000..564bcb08 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.java @@ -0,0 +1,112 @@ +package ml.docilealligator.infinityforreddit.bottomsheetfragments; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.google.android.material.button.MaterialButton; +import com.google.android.material.textfield.TextInputEditText; +import com.google.android.material.textfield.TextInputLayout; + +import java.lang.reflect.Field; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity; +import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment; +import ml.docilealligator.infinityforreddit.utils.Utils; + +public class SetRedditGalleryItemCaptionAndUrlBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment { + + public static final String EXTRA_POSITION = "EP"; + public static final String EXTRA_CAPTION = "EC"; + public static final String EXTRA_URL = "EU"; + + private PostGalleryActivity mActivity; + private TextInputLayout captionTextInputLayout; + private TextInputEditText captionTextInputEditText; + private TextInputLayout urlTextInputLayout; + private TextInputEditText urlTextInputEditText; + private MaterialButton okButton; + + public SetRedditGalleryItemCaptionAndUrlBottomSheetFragment() { + + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_set_reddit_gallery_item_caption_and_url_bottom_sheet, container, false); + + captionTextInputLayout = rootView.findViewById(R.id.caption_text_input_layout_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment); + captionTextInputEditText = rootView.findViewById(R.id.caption_text_input_edit_text_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment); + urlTextInputLayout = rootView.findViewById(R.id.url_text_input_layout_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment); + urlTextInputEditText = rootView.findViewById(R.id.url_text_input_edit_text_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment); + okButton = rootView.findViewById(R.id.ok_button_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment); + + int primaryTextColor = mActivity.getResources().getColor(R.color.primaryTextColor); + Drawable cursorDrawable = Utils.getTintedDrawable(mActivity, R.drawable.edit_text_cursor, primaryTextColor); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + captionTextInputEditText.setTextCursorDrawable(cursorDrawable); + urlTextInputEditText.setTextCursorDrawable(cursorDrawable); + } else { + setCursorDrawableColor(captionTextInputEditText, primaryTextColor); + setCursorDrawableColor(urlTextInputEditText, primaryTextColor); + } + + int position = getArguments().getInt(EXTRA_POSITION, -1); + String caption = getArguments().getString(EXTRA_CAPTION, ""); + String url = getArguments().getString(EXTRA_URL, ""); + + captionTextInputEditText.setText(caption); + urlTextInputEditText.setText(url); + + okButton.setOnClickListener(view -> { + mActivity.setCaptionAndUrl(position, captionTextInputEditText.getText().toString(), urlTextInputEditText.getText().toString()); + dismiss(); + }); + + if (mActivity.typeface != null) { + Utils.setFontToAllTextViews(rootView, mActivity.typeface); + } + + return rootView; + } + + private void setCursorDrawableColor(EditText editText, int color) { + try { + @SuppressLint("SoonBlockedPrivateApi") Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); + fCursorDrawableRes.setAccessible(true); + int mCursorDrawableRes = fCursorDrawableRes.getInt(editText); + Field fEditor = TextView.class.getDeclaredField("mEditor"); + fEditor.setAccessible(true); + Object editor = fEditor.get(editText); + Class clazz = editor.getClass(); + Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); + fCursorDrawable.setAccessible(true); + Drawable[] drawables = new Drawable[2]; + drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes); + drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes); + drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); + drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); + fCursorDrawable.set(editor, drawables); + } catch (Throwable ignored) { } + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + mActivity = (PostGalleryActivity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java index fc3da19d..f751ff5b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java @@ -58,7 +58,7 @@ public class FetchUserData { RedditAPI api = retrofit.create(RedditAPI.class); Call userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0); - userInfo.enqueue(new Callback() { + userInfo.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserDao.java index 77b12821..70588fea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserDao.java @@ -11,6 +11,9 @@ public interface UserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(UserData userData); + @Query("SELECT COUNT(*) FROM users") + int getNUsers(); + @Query("DELETE FROM users") void deleteAllUsers(); diff --git a/app/src/main/res/layout/fragment_set_reddit_gallery_item_caption_and_url_bottom_sheet.xml b/app/src/main/res/layout/fragment_set_reddit_gallery_item_caption_and_url_bottom_sheet.xml new file mode 100644 index 00000000..9c354bef --- /dev/null +++ b/app/src/main/res/layout/fragment_set_reddit_gallery_item_caption_and_url_bottom_sheet.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 148fedc6..c1a9b8c7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1258,4 +1258,7 @@ Unable to load custom font Unable to copy your font + Caption (max 180 characters) + Url + -- cgit v1.2.3