diff --git a/app/src/main/java/awais/instagrabber/activities/CommentsViewerFragment.java b/app/src/main/java/awais/instagrabber/activities/CommentsViewerFragment.java index 73888826..b1ed6029 100644 --- a/app/src/main/java/awais/instagrabber/activities/CommentsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/activities/CommentsViewerFragment.java @@ -94,8 +94,6 @@ public final class CommentsViewerFragment extends Fragment implements SwipeRefre @Override public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) { inflater.inflate(R.menu.follow, menu); - final MenuItem favItem = menu.findItem(R.id.favourites); - if (favItem != null) favItem.setVisible(false); menu.findItem(R.id.action_compare).setVisible(false); final MenuItem menuSearch = menu.findItem(R.id.action_search); final SearchView searchView = (SearchView) menuSearch.getActionView(); diff --git a/app/src/main/java/awais/instagrabber/activities/FollowViewerFragment.java b/app/src/main/java/awais/instagrabber/activities/FollowViewerFragment.java index 14751a00..87c9fc22 100644 --- a/app/src/main/java/awais/instagrabber/activities/FollowViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/activities/FollowViewerFragment.java @@ -236,10 +236,6 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh @Override public void onCreateOptionsMenu(@NonNull final Menu menu, final MenuInflater inflater) { inflater.inflate(R.menu.follow, menu); - final MenuItem favItem = menu.findItem(R.id.favourites); - if (favItem != null) { - favItem.setVisible(false); - } final MenuItem menuSearch = menu.findItem(R.id.action_search); final SearchView searchView = (SearchView) menuSearch.getActionView(); searchView.setQueryHint(getResources().getString(R.string.action_search)); diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java index 9fb7106e..b2e8b53a 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java @@ -165,10 +165,6 @@ public class StoryViewerFragment extends Fragment { @Override public void onPrepareOptionsMenu(@NonNull final Menu menu) { // hide menu items from activity - final MenuItem item = menu.findItem(R.id.favourites); - if (item != null) { - item.setVisible(false); - } } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index f8fddc1b..8a5c8196 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -364,8 +364,6 @@ public class DirectMessageThreadFragment extends Fragment { @Override public void onPrepareOptionsMenu(@NonNull final Menu menu) { - final MenuItem item = menu.findItem(R.id.favourites); - item.setVisible(false); } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index bdba2e0b..f710cb6e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -19,6 +19,7 @@ import androidx.navigation.NavDirections; import androidx.navigation.fragment.NavHostFragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.facebook.common.executors.UiThreadImmediateExecutorService; import com.facebook.datasource.BaseDataSubscriber; @@ -62,7 +63,7 @@ import awais.instagrabber.utils.Utils; import static awais.instagrabber.utils.Utils.settingsHelper; -public class FeedFragment extends Fragment { +public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { private static final String TAG = "FeedFragment"; private static final double MAX_VIDEO_HEIGHT = 0.9 * Utils.displayMetrics.heightPixels; private static final int RESIZED_VIDEO_HEIGHT = (int) (0.8 * Utils.displayMetrics.heightPixels); @@ -77,6 +78,7 @@ public class FeedFragment extends Fragment { private FeedViewModel feedViewModel; private VideoAwareRecyclerScroller videoAwareRecyclerScroller; private boolean shouldRefresh = true; + private boolean isPullToRefresh; private final FetchListener feedFetchListener = new FetchListener() { @Override @@ -86,7 +88,10 @@ public class FeedFragment extends Fragment { @Override public void onResult(final FeedModel[] result) { - if (result == null) return; + if (result == null || result.length <= 0) { + binding.feedSwipeRefreshLayout.setRefreshing(false); + return; + } final List currentFeedModelList = feedViewModel.getList().getValue(); final Map thumbToFeedMap = new HashMap<>(); for (final FeedModel feedModel : result) { @@ -133,10 +138,15 @@ public class FeedFragment extends Fragment { public void updateAdapter() { if (failed + success != result.length) return; - final List finalList = currentFeedModelList == null || currentFeedModelList.isEmpty() - ? new ArrayList<>() - : new ArrayList<>(currentFeedModelList); - finalList.addAll(Arrays.asList(result)); + List finalList = currentFeedModelList == null || currentFeedModelList.isEmpty() + ? new ArrayList<>() + : new ArrayList<>(currentFeedModelList); + final List resultList = Arrays.asList(result); + if (isPullToRefresh) { + finalList = resultList; + } else { + finalList.addAll(resultList); + } feedViewModel.getList().postValue(finalList); final PostModel feedPostModel = result[result.length - 1]; if (feedPostModel != null) { @@ -266,6 +276,7 @@ public class FeedFragment extends Fragment { break; } }; + private FeedStoriesViewModel feedStoriesViewModel; @Override public void onCreate(@Nullable final Bundle savedInstanceState) { @@ -291,6 +302,7 @@ public class FeedFragment extends Fragment { @Override public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { if (!shouldRefresh) return; + binding.feedSwipeRefreshLayout.setOnRefreshListener(this); setupFeedStories(); setupFeed(); shouldRefresh = false; @@ -312,6 +324,14 @@ public class FeedFragment extends Fragment { } } + @Override + public void onRefresh() { + isPullToRefresh = true; + feedEndCursor = null; + fetchFeed(); + fetchStories(); + } + private void setupFeed() { feedViewModel = new ViewModelProvider(fragmentActivity).get(FeedViewModel.class); final LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext()); @@ -336,13 +356,11 @@ public class FeedFragment extends Fragment { private void fetchFeed() { binding.feedSwipeRefreshLayout.setRefreshing(true); - new FeedFetcher(feedEndCursor, feedFetchListener) - .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - feedEndCursor = null; + new FeedFetcher(feedEndCursor, feedFetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } private void setupFeedStories() { - final FeedStoriesViewModel feedStoriesViewModel = new ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel.class); + feedStoriesViewModel = new ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel.class); final FeedStoriesAdapter feedStoriesAdapter = new FeedStoriesAdapter((model, position) -> { final NavDirections action = FeedFragmentDirections.actionFeedFragmentToStoryViewerFragment(position, null, false); NavHostFragment.findNavController(this).navigate(action); @@ -350,6 +368,10 @@ public class FeedFragment extends Fragment { binding.feedStoriesRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)); binding.feedStoriesRecyclerView.setAdapter(feedStoriesAdapter); feedStoriesViewModel.getList().observe(fragmentActivity, feedStoriesAdapter::submitList); + fetchStories(); + } + + private void fetchStories() { storiesService.getFeedStories(new ServiceCallback>() { @Override public void onSuccess(final List result) { diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java index b828618e..f2d852f1 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java @@ -42,13 +42,6 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { setHasOptionsMenu(true); } - @Override - public void onPrepareOptionsMenu(@NonNull final Menu menu) { - super.onPrepareOptionsMenu(menu); - final MenuItem item = menu.findItem(R.id.favourites); - item.setVisible(false); - } - @Override void setupPreferenceScreen(final PreferenceScreen screen) { screen.addPreference(getLanguagePreference());