From d5e75d16c31974f18089de3f7ac79e721022803e Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 18 May 2021 16:43:11 -0400 Subject: [PATCH] long-clicking a discover topic should now show cover post --- .../adapters/DiscoverTopicsAdapter.java | 5 +- .../viewholder/TopicClusterViewHolder.java | 6 +- .../fragments/main/DiscoverFragment.java | 57 ++++++++++++++++--- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/DiscoverTopicsAdapter.java b/app/src/main/java/awais/instagrabber/adapters/DiscoverTopicsAdapter.java index 4623e230..a1547a0b 100644 --- a/app/src/main/java/awais/instagrabber/adapters/DiscoverTopicsAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/DiscoverTopicsAdapter.java @@ -11,6 +11,7 @@ import androidx.recyclerview.widget.ListAdapter; import awais.instagrabber.adapters.viewholder.TopicClusterViewHolder; import awais.instagrabber.databinding.ItemDiscoverTopicBinding; import awais.instagrabber.repositories.responses.discover.TopicCluster; +import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.utils.ResponseBodyUtils; public class DiscoverTopicsAdapter extends ListAdapter { @@ -50,6 +51,8 @@ public class DiscoverTopicsAdapter extends ListAdapter onTopicClickListener.onTopicClick( topicCluster, - binding.getRoot(), binding.cover, - binding.title, titleColor.get(), backgroundColor.get() )); + itemView.setOnLongClickListener(v -> { + onTopicClickListener.onTopicLongClick(topicCluster.getCoverMedia()); + return true; + }); } // binding.title.setTransitionName("title-" + topicCluster.getId()); binding.cover.setTransitionName("cover-" + topicCluster.getId()); diff --git a/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java index 449b937e..26ee6ce9 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java @@ -5,12 +5,15 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import androidx.navigation.NavController; import androidx.navigation.fragment.FragmentNavigator; import androidx.navigation.fragment.NavHostFragment; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -18,15 +21,19 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import java.util.Collections; import java.util.List; +import awais.instagrabber.R; import awais.instagrabber.activities.MainActivity; import awais.instagrabber.adapters.DiscoverTopicsAdapter; import awais.instagrabber.customviews.helpers.GridSpacingItemDecoration; import awais.instagrabber.databinding.FragmentDiscoverBinding; +import awais.instagrabber.fragments.PostViewV2Fragment; +import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.discover.TopicCluster; import awais.instagrabber.repositories.responses.discover.TopicalExploreFeedResponse; import awais.instagrabber.utils.Utils; import awais.instagrabber.viewmodels.TopicClusterViewModel; import awais.instagrabber.webservices.DiscoverService; +import awais.instagrabber.webservices.MediaService; import awais.instagrabber.webservices.ServiceCallback; public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { @@ -38,12 +45,14 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR private TopicClusterViewModel topicClusterViewModel; private boolean shouldRefresh = true; private DiscoverService discoverService; + private MediaService mediaService; @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); fragmentActivity = (MainActivity) requireActivity(); discoverService = DiscoverService.getInstance(); + mediaService = MediaService.getInstance(null, null, 0); } @Override @@ -80,13 +89,47 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR public void setupTopics() { topicClusterViewModel = new ViewModelProvider(fragmentActivity).get(TopicClusterViewModel.class); binding.topicsRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(2))); - final DiscoverTopicsAdapter adapter = new DiscoverTopicsAdapter((topicCluster, root, cover, title, titleColor, backgroundColor) -> { - final FragmentNavigator.Extras.Builder builder = new FragmentNavigator.Extras.Builder() - .addSharedElement(cover, "cover-" + topicCluster.getId()); - final DiscoverFragmentDirections.ActionDiscoverFragmentToTopicPostsFragment action = DiscoverFragmentDirections - .actionDiscoverFragmentToTopicPostsFragment(topicCluster, titleColor, backgroundColor); - NavHostFragment.findNavController(this).navigate(action, builder.build()); - }); + final DiscoverTopicsAdapter.OnTopicClickListener otcl = new DiscoverTopicsAdapter.OnTopicClickListener() { + public void onTopicClick(final TopicCluster topicCluster, final View cover, final int titleColor, final int backgroundColor) { + final FragmentNavigator.Extras.Builder builder = new FragmentNavigator.Extras.Builder() + .addSharedElement(cover, "cover-" + topicCluster.getId()); + final DiscoverFragmentDirections.ActionDiscoverFragmentToTopicPostsFragment action = DiscoverFragmentDirections + .actionDiscoverFragmentToTopicPostsFragment(topicCluster, titleColor, backgroundColor); + NavHostFragment.findNavController(DiscoverFragment.this).navigate(action, builder.build()); + } + + public void onTopicLongClick(final Media coverMedia) { + final AlertDialog alertDialog = new AlertDialog.Builder(requireContext()) + .setCancelable(false) + .setView(R.layout.dialog_opening_post) + .create(); + alertDialog.show(); + mediaService.fetch(Long.valueOf(coverMedia.getPk()), new ServiceCallback() { + @Override + public void onSuccess(final Media feedModel) { + final NavController navController = NavHostFragment.findNavController(DiscoverFragment.this); + final Bundle bundle = new Bundle(); + bundle.putSerializable(PostViewV2Fragment.ARG_MEDIA, feedModel); + try { + navController.navigate(R.id.action_global_post_view, bundle); + alertDialog.dismiss(); + } catch (Exception e) { + Log.e(TAG, "onSuccess: ", e); + } + } + + @Override + public void onFailure(final Throwable t) { + alertDialog.dismiss(); + try { + Toast.makeText(requireContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + } + catch (Throwable e) {} + } + }); + } + }; + final DiscoverTopicsAdapter adapter = new DiscoverTopicsAdapter(otcl); binding.topicsRecyclerView.setAdapter(adapter); topicClusterViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList); }