|
@ -5,12 +5,15 @@ import android.util.Log; |
|
|
import android.view.LayoutInflater; |
|
|
import android.view.LayoutInflater; |
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.ViewGroup; |
|
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.Nullable; |
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout; |
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout; |
|
|
import androidx.fragment.app.Fragment; |
|
|
import androidx.fragment.app.Fragment; |
|
|
import androidx.lifecycle.ViewModelProvider; |
|
|
import androidx.lifecycle.ViewModelProvider; |
|
|
|
|
|
import androidx.navigation.NavController; |
|
|
import androidx.navigation.fragment.FragmentNavigator; |
|
|
import androidx.navigation.fragment.FragmentNavigator; |
|
|
import androidx.navigation.fragment.NavHostFragment; |
|
|
import androidx.navigation.fragment.NavHostFragment; |
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
|
@ -18,15 +21,19 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
|
|
import java.util.Collections; |
|
|
import java.util.Collections; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import awais.instagrabber.R; |
|
|
import awais.instagrabber.activities.MainActivity; |
|
|
import awais.instagrabber.activities.MainActivity; |
|
|
import awais.instagrabber.adapters.DiscoverTopicsAdapter; |
|
|
import awais.instagrabber.adapters.DiscoverTopicsAdapter; |
|
|
import awais.instagrabber.customviews.helpers.GridSpacingItemDecoration; |
|
|
import awais.instagrabber.customviews.helpers.GridSpacingItemDecoration; |
|
|
import awais.instagrabber.databinding.FragmentDiscoverBinding; |
|
|
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.TopicCluster; |
|
|
import awais.instagrabber.repositories.responses.discover.TopicalExploreFeedResponse; |
|
|
import awais.instagrabber.repositories.responses.discover.TopicalExploreFeedResponse; |
|
|
import awais.instagrabber.utils.Utils; |
|
|
import awais.instagrabber.utils.Utils; |
|
|
import awais.instagrabber.viewmodels.TopicClusterViewModel; |
|
|
import awais.instagrabber.viewmodels.TopicClusterViewModel; |
|
|
import awais.instagrabber.webservices.DiscoverService; |
|
|
import awais.instagrabber.webservices.DiscoverService; |
|
|
|
|
|
import awais.instagrabber.webservices.MediaService; |
|
|
import awais.instagrabber.webservices.ServiceCallback; |
|
|
import awais.instagrabber.webservices.ServiceCallback; |
|
|
|
|
|
|
|
|
public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { |
|
|
public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { |
|
@ -38,12 +45,14 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR |
|
|
private TopicClusterViewModel topicClusterViewModel; |
|
|
private TopicClusterViewModel topicClusterViewModel; |
|
|
private boolean shouldRefresh = true; |
|
|
private boolean shouldRefresh = true; |
|
|
private DiscoverService discoverService; |
|
|
private DiscoverService discoverService; |
|
|
|
|
|
private MediaService mediaService; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onCreate(@Nullable final Bundle savedInstanceState) { |
|
|
public void onCreate(@Nullable final Bundle savedInstanceState) { |
|
|
super.onCreate(savedInstanceState); |
|
|
super.onCreate(savedInstanceState); |
|
|
fragmentActivity = (MainActivity) requireActivity(); |
|
|
fragmentActivity = (MainActivity) requireActivity(); |
|
|
discoverService = DiscoverService.getInstance(); |
|
|
discoverService = DiscoverService.getInstance(); |
|
|
|
|
|
mediaService = MediaService.getInstance(null, null, 0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -80,13 +89,47 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR |
|
|
public void setupTopics() { |
|
|
public void setupTopics() { |
|
|
topicClusterViewModel = new ViewModelProvider(fragmentActivity).get(TopicClusterViewModel.class); |
|
|
topicClusterViewModel = new ViewModelProvider(fragmentActivity).get(TopicClusterViewModel.class); |
|
|
binding.topicsRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(2))); |
|
|
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<Media>() { |
|
|
|
|
|
@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); |
|
|
binding.topicsRecyclerView.setAdapter(adapter); |
|
|
topicClusterViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList); |
|
|
topicClusterViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList); |
|
|
} |
|
|
} |
|
|