2020-08-29 08:01:42 +00:00
|
|
|
package awais.instagrabber.fragments.main;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
2020-11-01 06:02:54 +00:00
|
|
|
import android.util.Log;
|
2020-08-29 08:01:42 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2021-05-18 20:43:11 +00:00
|
|
|
import android.widget.Toast;
|
2020-08-29 08:01:42 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2021-05-18 20:43:11 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2020-08-29 08:01:42 +00:00
|
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2021-05-18 20:43:11 +00:00
|
|
|
import androidx.navigation.NavController;
|
2020-11-01 06:02:54 +00:00
|
|
|
import androidx.navigation.fragment.FragmentNavigator;
|
2020-09-02 17:02:31 +00:00
|
|
|
import androidx.navigation.fragment.NavHostFragment;
|
2020-09-05 08:31:50 +00:00
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
2020-08-29 08:01:42 +00:00
|
|
|
|
2021-05-18 20:21:30 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-05-18 20:43:11 +00:00
|
|
|
import awais.instagrabber.R;
|
2020-08-29 08:01:42 +00:00
|
|
|
import awais.instagrabber.activities.MainActivity;
|
2020-11-01 06:02:54 +00:00
|
|
|
import awais.instagrabber.adapters.DiscoverTopicsAdapter;
|
2020-08-29 08:01:42 +00:00
|
|
|
import awais.instagrabber.customviews.helpers.GridSpacingItemDecoration;
|
|
|
|
import awais.instagrabber.databinding.FragmentDiscoverBinding;
|
2021-05-18 20:43:11 +00:00
|
|
|
import awais.instagrabber.fragments.PostViewV2Fragment;
|
|
|
|
import awais.instagrabber.repositories.responses.Media;
|
2021-05-18 20:21:30 +00:00
|
|
|
import awais.instagrabber.repositories.responses.discover.TopicCluster;
|
2021-01-07 12:36:33 +00:00
|
|
|
import awais.instagrabber.repositories.responses.discover.TopicalExploreFeedResponse;
|
2021-06-03 22:17:24 +00:00
|
|
|
import awais.instagrabber.utils.AppExecutors;
|
|
|
|
import awais.instagrabber.utils.CoroutineUtilsKt;
|
2020-08-29 08:01:42 +00:00
|
|
|
import awais.instagrabber.utils.Utils;
|
2020-11-01 06:02:54 +00:00
|
|
|
import awais.instagrabber.viewmodels.TopicClusterViewModel;
|
|
|
|
import awais.instagrabber.webservices.DiscoverService;
|
2021-06-12 00:53:38 +00:00
|
|
|
import awais.instagrabber.webservices.MediaRepository;
|
2020-11-01 06:02:54 +00:00
|
|
|
import awais.instagrabber.webservices.ServiceCallback;
|
2021-06-03 22:17:24 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers;
|
2020-08-29 08:01:42 +00:00
|
|
|
|
2020-09-05 08:31:50 +00:00
|
|
|
public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
|
2020-11-01 06:02:54 +00:00
|
|
|
private static final String TAG = "DiscoverFragment";
|
2020-08-29 08:01:42 +00:00
|
|
|
|
|
|
|
private MainActivity fragmentActivity;
|
|
|
|
private CoordinatorLayout root;
|
|
|
|
private FragmentDiscoverBinding binding;
|
2020-11-01 06:02:54 +00:00
|
|
|
private TopicClusterViewModel topicClusterViewModel;
|
2020-08-30 06:47:04 +00:00
|
|
|
private boolean shouldRefresh = true;
|
2020-11-01 06:02:54 +00:00
|
|
|
private DiscoverService discoverService;
|
2021-06-12 00:53:38 +00:00
|
|
|
private MediaRepository mediaRepository;
|
2020-08-29 08:01:42 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
fragmentActivity = (MainActivity) requireActivity();
|
2020-11-01 06:02:54 +00:00
|
|
|
discoverService = DiscoverService.getInstance();
|
2021-06-06 00:43:19 +00:00
|
|
|
// final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
|
|
|
|
// final String cookie = Utils.settingsHelper.getString(Constants.COOKIE);
|
|
|
|
// final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
|
|
|
|
// final long userId = CookieUtils.getUserIdFromCookie(cookie);
|
2021-06-12 01:03:21 +00:00
|
|
|
mediaRepository = MediaRepository.Companion.getInstance();
|
2020-08-29 08:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
|
|
final ViewGroup container,
|
|
|
|
final Bundle savedInstanceState) {
|
|
|
|
if (root != null) {
|
2020-08-30 06:47:04 +00:00
|
|
|
shouldRefresh = false;
|
2020-08-29 08:01:42 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
binding = FragmentDiscoverBinding.inflate(inflater, container, false);
|
|
|
|
root = binding.getRoot();
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2020-08-30 06:47:04 +00:00
|
|
|
@Override
|
|
|
|
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
|
|
|
|
if (!shouldRefresh) return;
|
2020-11-01 06:02:54 +00:00
|
|
|
binding.swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
init();
|
2020-09-03 15:15:53 +00:00
|
|
|
shouldRefresh = false;
|
2020-08-30 06:47:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 06:02:54 +00:00
|
|
|
private void init() {
|
|
|
|
setupTopics();
|
|
|
|
fetchTopics();
|
|
|
|
}
|
|
|
|
|
2020-09-05 08:31:50 +00:00
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
2020-11-01 06:02:54 +00:00
|
|
|
fetchTopics();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setupTopics() {
|
|
|
|
topicClusterViewModel = new ViewModelProvider(fragmentActivity).get(TopicClusterViewModel.class);
|
|
|
|
binding.topicsRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(2)));
|
2021-05-18 20:43:11 +00:00
|
|
|
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();
|
2021-06-03 22:17:24 +00:00
|
|
|
final String pk = coverMedia.getPk();
|
|
|
|
if (pk == null) return;
|
2021-06-12 00:53:38 +00:00
|
|
|
mediaRepository.fetch(
|
2021-06-03 22:17:24 +00:00
|
|
|
Long.parseLong(pk),
|
|
|
|
CoroutineUtilsKt.getContinuation((media, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
|
|
|
if (throwable != null) {
|
|
|
|
alertDialog.dismiss();
|
|
|
|
try {
|
|
|
|
Toast.makeText(requireContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
|
|
|
} catch (Throwable ignored) {}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final NavController navController = NavHostFragment.findNavController(DiscoverFragment.this);
|
|
|
|
final Bundle bundle = new Bundle();
|
|
|
|
bundle.putSerializable(PostViewV2Fragment.ARG_MEDIA, media);
|
|
|
|
try {
|
|
|
|
navController.navigate(R.id.action_global_post_view, bundle);
|
|
|
|
alertDialog.dismiss();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "onTopicLongClick: ", e);
|
|
|
|
}
|
|
|
|
}), Dispatchers.getIO())
|
|
|
|
);
|
2021-05-18 20:43:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
final DiscoverTopicsAdapter adapter = new DiscoverTopicsAdapter(otcl);
|
2020-11-01 06:02:54 +00:00
|
|
|
binding.topicsRecyclerView.setAdapter(adapter);
|
|
|
|
topicClusterViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList);
|
2020-09-05 08:31:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 06:02:54 +00:00
|
|
|
private void fetchTopics() {
|
|
|
|
binding.swipeRefreshLayout.setRefreshing(true);
|
2021-01-07 12:36:33 +00:00
|
|
|
discoverService.topicalExplore(new DiscoverService.TopicalExploreRequest(), new ServiceCallback<TopicalExploreFeedResponse>() {
|
2020-08-29 08:01:42 +00:00
|
|
|
@Override
|
2021-01-07 12:36:33 +00:00
|
|
|
public void onSuccess(final TopicalExploreFeedResponse result) {
|
2020-11-15 12:55:07 +00:00
|
|
|
if (result == null) return;
|
2021-05-18 20:21:30 +00:00
|
|
|
final List<TopicCluster> clusters = result.getClusters();
|
2020-11-01 06:02:54 +00:00
|
|
|
binding.swipeRefreshLayout.setRefreshing(false);
|
2021-05-18 20:21:30 +00:00
|
|
|
if (clusters.size() == 1 && result.getItems().size() > 0) {
|
|
|
|
final TopicCluster cluster = clusters.get(0);
|
|
|
|
if (cluster.getCoverMedia() == null)
|
|
|
|
cluster.setCoverMedia(result.getItems().get(0).getMedia());
|
|
|
|
topicClusterViewModel.getList().postValue(Collections.singletonList(cluster));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (clusters.size() > 1 || result.getItems().size() == 0) {
|
|
|
|
topicClusterViewModel.getList().postValue(clusters);
|
|
|
|
}
|
2020-08-29 08:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 06:02:54 +00:00
|
|
|
public void onFailure(final Throwable t) {
|
|
|
|
Log.e(TAG, "onFailure", t);
|
|
|
|
binding.swipeRefreshLayout.setRefreshing(false);
|
2020-09-02 17:02:31 +00:00
|
|
|
}
|
2020-08-29 08:01:42 +00:00
|
|
|
});
|
2020-09-05 08:31:50 +00:00
|
|
|
}
|
2020-08-29 08:01:42 +00:00
|
|
|
}
|