Set caption and url when submitting gallery posts.

This commit is contained in:
Alex Ning 2022-01-28 12:58:16 +08:00
parent fd0e95dbac
commit 2569742e1c
10 changed files with 254 additions and 15 deletions

View File

@ -92,5 +92,21 @@ public class RedditGalleryPayload {
parcel.writeString(outboundUrl); parcel.writeString(outboundUrl);
parcel.writeString(mediaId); 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;
}
} }
} }

View File

@ -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 { try {
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true); fCursorDrawableRes.setAccessible(true);

View File

@ -210,16 +210,13 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null);
adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() { adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, () -> {
@Override
public void onAddImageClicked() {
if (!isUploading) { if (!isUploading) {
SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment(); SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment();
fragment.show(getSupportFragmentManager(), fragment.getTag()); fragment.show(getSupportFragmentManager(), fragment.getTag());
} else { } else {
Snackbar.make(coordinatorLayout, R.string.please_wait_image_is_uploading, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.please_wait_image_is_uploading, Snackbar.LENGTH_SHORT).show();
} }
}
}); });
imagesRecyclerView.setAdapter(adapter); imagesRecyclerView.setAdapter(adapter);
Resources resources = getResources(); Resources resources = getResources();
@ -685,6 +682,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
flairTextView.setTextColor(flairTextColor); flairTextView.setTextColor(flairTextColor);
} }
public void setCaptionAndUrl(int position, String caption, String url) {
if (adapter != null) {
adapter.setCaptionAndUrl(position, caption, url);
}
}
@Subscribe @Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) { public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish(); finish();

View File

@ -1,8 +1,8 @@
package ml.docilealligator.infinityforreddit.adapters; package ml.docilealligator.infinityforreddit.adapters;
import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -33,6 +33,8 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditGalleryPayload; 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.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; 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_IMAGE = 1;
private static final int VIEW_TYPE_ADD_IMAGE = 2; private static final int VIEW_TYPE_ADD_IMAGE = 2;
private PostGalleryActivity activity;
private ArrayList<RedditGalleryImageInfo> redditGalleryImageInfoList; private ArrayList<RedditGalleryImageInfo> redditGalleryImageInfoList;
private CustomThemeWrapper customThemeWrapper; private CustomThemeWrapper customThemeWrapper;
private ItemClickListener itemClickListener; private ItemClickListener itemClickListener;
private RequestManager glide; private RequestManager glide;
public RedditGallerySubmissionRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper, public RedditGallerySubmissionRecyclerViewAdapter(PostGalleryActivity activity, CustomThemeWrapper customThemeWrapper,
ItemClickListener itemClickListener) { ItemClickListener itemClickListener) {
glide = Glide.with(context); this.activity = activity;
glide = Glide.with(activity);
this.customThemeWrapper = customThemeWrapper; this.customThemeWrapper = customThemeWrapper;
this.itemClickListener = itemClickListener; this.itemClickListener = itemClickListener;
} }
@ -76,7 +80,7 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
if (holder instanceof ImageViewHolder) { if (holder instanceof ImageViewHolder) {
glide.load(redditGalleryImageInfoList.get(position).imageUrlString) glide.load(redditGalleryImageInfoList.get(position).imageUrlString)
.apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(48))) .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(48)))
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -141,6 +145,13 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
notifyItemRemoved(redditGalleryImageInfoList.size()); 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 { 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;
@ -156,6 +167,19 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
imageView.setRatio(1); 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 -> { closeImageView.setOnClickListener(view -> {
redditGalleryImageInfoList.remove(getBindingAdapterPosition()); redditGalleryImageInfoList.remove(getBindingAdapterPosition());
notifyItemRemoved(getBindingAdapterPosition()); notifyItemRemoved(getBindingAdapterPosition());

View File

@ -12,6 +12,9 @@ public class InsertUserData {
public static void insertUserData(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, public static void insertUserData(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase,
UserData userData, InsertUserDataListener insertUserDataListener) { UserData userData, InsertUserDataListener insertUserDataListener) {
executor.execute(() -> { executor.execute(() -> {
if (redditDataRoomDatabase.userDao().getNUsers() > 10000) {
redditDataRoomDatabase.userDao().deleteAllUsers();
}
redditDataRoomDatabase.userDao().insert(userData); redditDataRoomDatabase.userDao().insert(userData);
if (insertUserDataListener != null) { if (insertUserDataListener != null) {
handler.post(insertUserDataListener::insertSuccess); handler.post(insertUserDataListener::insertSuccess);

View File

@ -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;
}
}

View File

@ -58,7 +58,7 @@ public class FetchUserData {
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0); Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0);
userInfo.enqueue(new Callback<String>() { userInfo.enqueue(new Callback<>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {

View File

@ -11,6 +11,9 @@ public interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(UserData userData); void insert(UserData userData);
@Query("SELECT COUNT(*) FROM users")
int getNUsers();
@Query("DELETE FROM users") @Query("DELETE FROM users")
void deleteAllUsers(); void deleteAllUsers();

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="8dp"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/caption_text_input_layout_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
app:boxStrokeColor="?attr/primaryTextColor"
app:hintTextColor="?attr/primaryTextColor"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/caption_text_input_edit_text_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="?attr/font_family"
android:textSize="?attr/font_default"
android:textColor="?attr/primaryTextColor"
android:hint="@string/reddit_gallery_item_caption_hint"
android:maxLength="180" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/url_text_input_layout_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
app:boxStrokeColor="?attr/primaryTextColor"
app:hintTextColor="?attr/primaryTextColor"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/url_text_input_edit_text_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="?attr/font_family"
android:textSize="?attr/font_default"
android:textColor="?attr/primaryTextColor"
android:hint="@string/reddit_gallery_item_url_hint" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/ok_button_set_reddit_gallery_item_caption_and_url_bottom_sheet_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:backgroundTint="?attr/colorPrimaryLightTheme"
android:text="@string/ok"
android:fontFamily="?attr/font_family"
android:textSize="?attr/font_default"
android:textColor="#FFFFFF" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -1258,4 +1258,7 @@
<string name="unable_to_load_font">Unable to load custom font</string> <string name="unable_to_load_font">Unable to load custom font</string>
<string name="unable_to_copy_font_file">Unable to copy your font</string> <string name="unable_to_copy_font_file">Unable to copy your font</string>
<string name="reddit_gallery_item_caption_hint">Caption (max 180 characters)</string>
<string name="reddit_gallery_item_url_hint">Url</string>
</resources> </resources>