|
@ -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) { |
|
|