From be53864f029bdfdb7976a6a417441d45507050f7 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 3 Jan 2021 19:19:36 +0900 Subject: [PATCH] Auto fetch more chats if loaded chats don't fill the view --- .../DirectMessageThreadFragment.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 fc4f76a0..472bee0d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -560,8 +560,22 @@ public class DirectMessageThreadFragment extends Fragment { appStateViewModel.getCurrentUser().observe(getViewLifecycleOwner(), currentUser -> { viewModel.setCurrentUser(currentUser); setupItemsAdapter(currentUser); - viewModel.getItems().observe(getViewLifecycleOwner(), - list -> itemsAdapter.submitList(list, () -> itemOrHeaders = itemsAdapter.getList())); + viewModel.getItems().observe( + getViewLifecycleOwner(), + list -> itemsAdapter.submitList(list, () -> { + itemOrHeaders = itemsAdapter.getList(); + binding.chats.post(() -> { + final RecyclerView.LayoutManager layoutManager = binding.chats.getLayoutManager(); + if (layoutManager instanceof LinearLayoutManager) { + final int position = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition(); + if (position < 0) return; + if (position == itemsAdapter.getItemCount() - 1) { + viewModel.fetchChats(); + } + } + }); + }) + ); }); final NavController navController = NavHostFragment.findNavController(this); final NavBackStackEntry backStackEntry = navController.getCurrentBackStackEntry();