Browse Source

Fix null favourites menu item, and add pull to refresh in feed

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
437162639f
  1. 2
      app/src/main/java/awais/instagrabber/activities/CommentsViewerFragment.java
  2. 4
      app/src/main/java/awais/instagrabber/activities/FollowViewerFragment.java
  3. 4
      app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java
  4. 2
      app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java
  5. 38
      app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java
  6. 7
      app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java

2
app/src/main/java/awais/instagrabber/activities/CommentsViewerFragment.java

@ -94,8 +94,6 @@ public final class CommentsViewerFragment extends Fragment implements SwipeRefre
@Override @Override
public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {
inflater.inflate(R.menu.follow, menu); 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); menu.findItem(R.id.action_compare).setVisible(false);
final MenuItem menuSearch = menu.findItem(R.id.action_search); final MenuItem menuSearch = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) menuSearch.getActionView(); final SearchView searchView = (SearchView) menuSearch.getActionView();

4
app/src/main/java/awais/instagrabber/activities/FollowViewerFragment.java

@ -236,10 +236,6 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh
@Override @Override
public void onCreateOptionsMenu(@NonNull final Menu menu, final MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.follow, menu); 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 MenuItem menuSearch = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) menuSearch.getActionView(); final SearchView searchView = (SearchView) menuSearch.getActionView();
searchView.setQueryHint(getResources().getString(R.string.action_search)); searchView.setQueryHint(getResources().getString(R.string.action_search));

4
app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java

@ -165,10 +165,6 @@ public class StoryViewerFragment extends Fragment {
@Override @Override
public void onPrepareOptionsMenu(@NonNull final Menu menu) { public void onPrepareOptionsMenu(@NonNull final Menu menu) {
// hide menu items from activity // hide menu items from activity
final MenuItem item = menu.findItem(R.id.favourites);
if (item != null) {
item.setVisible(false);
}
} }
@Override @Override

2
app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java

@ -364,8 +364,6 @@ public class DirectMessageThreadFragment extends Fragment {
@Override @Override
public void onPrepareOptionsMenu(@NonNull final Menu menu) { public void onPrepareOptionsMenu(@NonNull final Menu menu) {
final MenuItem item = menu.findItem(R.id.favourites);
item.setVisible(false);
} }
@Override @Override

38
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.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.facebook.common.executors.UiThreadImmediateExecutorService; import com.facebook.common.executors.UiThreadImmediateExecutorService;
import com.facebook.datasource.BaseDataSubscriber; import com.facebook.datasource.BaseDataSubscriber;
@ -62,7 +63,7 @@ import awais.instagrabber.utils.Utils;
import static awais.instagrabber.utils.Utils.settingsHelper; 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 String TAG = "FeedFragment";
private static final double MAX_VIDEO_HEIGHT = 0.9 * Utils.displayMetrics.heightPixels; 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); 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 FeedViewModel feedViewModel;
private VideoAwareRecyclerScroller videoAwareRecyclerScroller; private VideoAwareRecyclerScroller videoAwareRecyclerScroller;
private boolean shouldRefresh = true; private boolean shouldRefresh = true;
private boolean isPullToRefresh;
private final FetchListener<FeedModel[]> feedFetchListener = new FetchListener<FeedModel[]>() { private final FetchListener<FeedModel[]> feedFetchListener = new FetchListener<FeedModel[]>() {
@Override @Override
@ -86,7 +88,10 @@ public class FeedFragment extends Fragment {
@Override @Override
public void onResult(final FeedModel[] result) { public void onResult(final FeedModel[] result) {
if (result == null) return;
if (result == null || result.length <= 0) {
binding.feedSwipeRefreshLayout.setRefreshing(false);
return;
}
final List<FeedModel> currentFeedModelList = feedViewModel.getList().getValue(); final List<FeedModel> currentFeedModelList = feedViewModel.getList().getValue();
final Map<String, FeedModel> thumbToFeedMap = new HashMap<>(); final Map<String, FeedModel> thumbToFeedMap = new HashMap<>();
for (final FeedModel feedModel : result) { for (final FeedModel feedModel : result) {
@ -133,10 +138,15 @@ public class FeedFragment extends Fragment {
public void updateAdapter() { public void updateAdapter() {
if (failed + success != result.length) return; if (failed + success != result.length) return;
final List<FeedModel> finalList = currentFeedModelList == null || currentFeedModelList.isEmpty()
List<FeedModel> finalList = currentFeedModelList == null || currentFeedModelList.isEmpty()
? new ArrayList<>() ? new ArrayList<>()
: new ArrayList<>(currentFeedModelList); : new ArrayList<>(currentFeedModelList);
finalList.addAll(Arrays.asList(result));
final List<FeedModel> resultList = Arrays.asList(result);
if (isPullToRefresh) {
finalList = resultList;
} else {
finalList.addAll(resultList);
}
feedViewModel.getList().postValue(finalList); feedViewModel.getList().postValue(finalList);
final PostModel feedPostModel = result[result.length - 1]; final PostModel feedPostModel = result[result.length - 1];
if (feedPostModel != null) { if (feedPostModel != null) {
@ -266,6 +276,7 @@ public class FeedFragment extends Fragment {
break; break;
} }
}; };
private FeedStoriesViewModel feedStoriesViewModel;
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
@ -291,6 +302,7 @@ public class FeedFragment extends Fragment {
@Override @Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
if (!shouldRefresh) return; if (!shouldRefresh) return;
binding.feedSwipeRefreshLayout.setOnRefreshListener(this);
setupFeedStories(); setupFeedStories();
setupFeed(); setupFeed();
shouldRefresh = false; shouldRefresh = false;
@ -312,6 +324,14 @@ public class FeedFragment extends Fragment {
} }
} }
@Override
public void onRefresh() {
isPullToRefresh = true;
feedEndCursor = null;
fetchFeed();
fetchStories();
}
private void setupFeed() { private void setupFeed() {
feedViewModel = new ViewModelProvider(fragmentActivity).get(FeedViewModel.class); feedViewModel = new ViewModelProvider(fragmentActivity).get(FeedViewModel.class);
final LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext()); final LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
@ -336,13 +356,11 @@ public class FeedFragment extends Fragment {
private void fetchFeed() { private void fetchFeed() {
binding.feedSwipeRefreshLayout.setRefreshing(true); 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() { 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 FeedStoriesAdapter feedStoriesAdapter = new FeedStoriesAdapter((model, position) -> {
final NavDirections action = FeedFragmentDirections.actionFeedFragmentToStoryViewerFragment(position, null, false); final NavDirections action = FeedFragmentDirections.actionFeedFragmentToStoryViewerFragment(position, null, false);
NavHostFragment.findNavController(this).navigate(action); 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.setLayoutManager(new LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false));
binding.feedStoriesRecyclerView.setAdapter(feedStoriesAdapter); binding.feedStoriesRecyclerView.setAdapter(feedStoriesAdapter);
feedStoriesViewModel.getList().observe(fragmentActivity, feedStoriesAdapter::submitList); feedStoriesViewModel.getList().observe(fragmentActivity, feedStoriesAdapter::submitList);
fetchStories();
}
private void fetchStories() {
storiesService.getFeedStories(new ServiceCallback<List<FeedStoryModel>>() { storiesService.getFeedStories(new ServiceCallback<List<FeedStoryModel>>() {
@Override @Override
public void onSuccess(final List<FeedStoryModel> result) { public void onSuccess(final List<FeedStoryModel> result) {

7
app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java

@ -42,13 +42,6 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
setHasOptionsMenu(true); 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 @Override
void setupPreferenceScreen(final PreferenceScreen screen) { void setupPreferenceScreen(final PreferenceScreen screen) {
screen.addPreference(getLanguagePreference()); screen.addPreference(getLanguagePreference());

Loading…
Cancel
Save