Skip to content

Commit

Permalink
Implement hiding post in PostOptionsBottomSheetFragment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Jan 10, 2025
1 parent a8f01a1 commit 2b52783
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import ml.docilealligator.infinityforreddit.activities.WikiActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.AccountChooserBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.fragments.CommentsListingFragment;
import ml.docilealligator.infinityforreddit.fragments.CustomThemeListingFragment;
import ml.docilealligator.infinityforreddit.fragments.FollowedUsersListingFragment;
Expand Down Expand Up @@ -312,6 +313,8 @@ public interface AppComponent {

void inject(LoginChromeCustomTabActivity loginChromeCustomTabActivity);

void inject(PostOptionsBottomSheetFragment postOptionsBottomSheetFragment);

@Component.Factory
interface Factory {
AppComponent create(@BindsInstance Application application);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,10 @@ void setBaseView(AspectRatioGifImageView iconGifImageView,
PostOptionsBottomSheetFragment postOptionsBottomSheetFragment;
if (mPost.getPostType() == Post.GALLERY_TYPE && this instanceof PostDetailGalleryViewHolder) {
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(mPost,
getBindingAdapterPosition(),
((LinearLayoutManagerBugFixed) ((PostDetailGalleryViewHolder) this).binding.galleryRecyclerViewItemPostDetailGallery.getLayoutManager()).findFirstVisibleItemPosition());
} else {
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(mPost);
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(mPost, getBindingAdapterPosition());
}
postOptionsBottomSheetFragment.show(mActivity.getSupportFragmentManager(), postOptionsBottomSheetFragment.getTag());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2870,9 +2870,10 @@ void setBaseView(AspectRatioGifImageView iconGifImageView,
PostOptionsBottomSheetFragment postOptionsBottomSheetFragment;
if (post.getPostType() == Post.GALLERY_TYPE && this instanceof PostBaseGalleryTypeViewHolder) {
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post,
getBindingAdapterPosition(),
((LinearLayoutManagerBugFixed) ((PostBaseGalleryTypeViewHolder) this).galleryRecyclerView.getLayoutManager()).findFirstVisibleItemPosition());
} else {
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post);
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post, getBindingAdapterPosition());
}
postOptionsBottomSheetFragment.show(mActivity.getSupportFragmentManager(), postOptionsBottomSheetFragment.getTag());
return true;
Expand Down Expand Up @@ -3781,7 +3782,7 @@ void setBaseView(AspectRatioGifImageView iconGifImageView,
}

PostOptionsBottomSheetFragment postOptionsBottomSheetFragment;
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post);
postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post, getBindingAdapterPosition());
postOptionsBottomSheetFragment.show(mActivity.getSupportFragmentManager(), postOptionsBottomSheetFragment.getTag());
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;
import javax.inject.Named;

import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
Expand All @@ -27,9 +29,12 @@
import ml.docilealligator.infinityforreddit.activities.SubmitCrosspostActivity;
import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment;
import ml.docilealligator.infinityforreddit.databinding.FragmentPostOptionsBottomSheetBinding;
import ml.docilealligator.infinityforreddit.events.PostUpdateEventToPostList;
import ml.docilealligator.infinityforreddit.post.HidePost;
import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.services.DownloadMediaService;
import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService;
import retrofit2.Retrofit;

/**
* A simple {@link Fragment} subclass.
Expand All @@ -39,12 +44,17 @@
public class PostOptionsBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment {

private static final String EXTRA_POST = "EP";
private static final String EXTRA_POST_LIST_POSITION = "EPLP";
private static final String EXTRA_GALLERY_INDEX = "EGI";

private BaseActivity mBaseActivity;
private Post mPost;
private FragmentPostOptionsBottomSheetBinding binding;

@Inject
@Named("oauth")
Retrofit mOauthRetrofit;

public PostOptionsBottomSheetFragment() {
// Required empty public constructor
}
Expand All @@ -56,19 +66,21 @@ public PostOptionsBottomSheetFragment() {
* @param post Post
* @return A new instance of fragment PostOptionsBottomSheetFragment.
*/
public static PostOptionsBottomSheetFragment newInstance(Post post, int galleryIndex) {
public static PostOptionsBottomSheetFragment newInstance(Post post, int postListPosition, int galleryIndex) {
PostOptionsBottomSheetFragment fragment = new PostOptionsBottomSheetFragment();
Bundle args = new Bundle();
args.putParcelable(EXTRA_POST, post);
args.putInt(EXTRA_POST_LIST_POSITION, postListPosition);
args.putInt(EXTRA_GALLERY_INDEX, galleryIndex);
fragment.setArguments(args);
return fragment;
}

public static PostOptionsBottomSheetFragment newInstance(Post post) {
public static PostOptionsBottomSheetFragment newInstance(Post post, int postListPosition) {
PostOptionsBottomSheetFragment fragment = new PostOptionsBottomSheetFragment();
Bundle args = new Bundle();
args.putParcelable(EXTRA_POST, post);
args.putInt(EXTRA_POST_LIST_POSITION, postListPosition);
fragment.setArguments(args);
return fragment;
}
Expand All @@ -86,6 +98,7 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((Infinity) mBaseActivity.getApplication()).getAppComponent().inject(this);
// Inflate the layout for this fragment
binding = FragmentPostOptionsBottomSheetBinding.inflate(inflater, container, false);

Expand Down Expand Up @@ -178,11 +191,43 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
}

binding.hidePostTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
if (mBaseActivity instanceof PostOptionsCallback) {
((PostOptionsCallback) mBaseActivity).onOptionClicked(mPost, mPost.isHidden() ? POST_OPTION.UNHIDE_POST : POST_OPTION.HIDE_POST);
if (mPost.isHidden()) {
HidePost.unhidePost(mOauthRetrofit, mBaseActivity.accessToken, mPost.getFullName(), new HidePost.HidePostListener() {
@Override
public void success() {
mPost.setHidden(false);
Toast.makeText(mBaseActivity, R.string.post_unhide_success, Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, getArguments().getInt(EXTRA_POST_LIST_POSITION, 0)));
dismiss();
}

@Override
public void failed() {
mPost.setHidden(true);
Toast.makeText(mBaseActivity, R.string.post_unhide_failed, Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, getArguments().getInt(EXTRA_POST_LIST_POSITION, 0)));
dismiss();
}
});
} else {
HidePost.hidePost(mOauthRetrofit, mBaseActivity.accessToken, mPost.getFullName(), new HidePost.HidePostListener() {
@Override
public void success() {
mPost.setHidden(true);
Toast.makeText(mBaseActivity, R.string.post_hide_success, Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, getArguments().getInt(EXTRA_POST_LIST_POSITION, 0)));
dismiss();
}

@Override
public void failed() {
mPost.setHidden(false);
Toast.makeText(mBaseActivity, R.string.post_hide_failed, Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, getArguments().getInt(EXTRA_POST_LIST_POSITION, 0)));
dismiss();
}
});
}

dismiss();
});

binding.crosspostTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
Expand Down Expand Up @@ -210,16 +255,4 @@ public void onAttach(@NonNull Context context) {
super.onAttach(context);
mBaseActivity = (BaseActivity) context;
}

public interface PostOptionsCallback {
void onOptionClicked(Post post, @POST_OPTION int option);
}

@IntDef({POST_OPTION.HIDE_POST, POST_OPTION.UNHIDE_POST})
@Retention(RetentionPolicy.SOURCE)
public @interface POST_OPTION {
int HIDE_POST = 0;
int UNHIDE_POST = 1;
int DOWNLOAD_MEDIA = 2;
}
}

0 comments on commit 2b52783

Please sign in to comment.