1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 13:26:59 +00:00

Fix pull to refresh in FeedFragment, and add pull to refresh in ProfileFragment

This commit is contained in:
Ammar Githam 2020-09-05 17:08:44 +09:00
parent 437162639f
commit 3c90eebe7a
2 changed files with 37 additions and 22 deletions

View File

@ -144,6 +144,7 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre
final List<FeedModel> resultList = Arrays.asList(result); final List<FeedModel> resultList = Arrays.asList(result);
if (isPullToRefresh) { if (isPullToRefresh) {
finalList = resultList; finalList = resultList;
isPullToRefresh = false;
} else { } else {
finalList.addAll(resultList); finalList.addAll(resultList);
} }

View File

@ -32,6 +32,7 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections; import androidx.navigation.NavDirections;
import androidx.navigation.fragment.NavHostFragment; import androidx.navigation.fragment.NavHostFragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -73,7 +74,7 @@ import awaisomereport.LogCollector;
import static awais.instagrabber.utils.Utils.logCollector; import static awais.instagrabber.utils.Utils.logCollector;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
public class ProfileFragment extends Fragment { public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = "ProfileFragment"; private static final String TAG = "ProfileFragment";
private MainActivity fragmentActivity; private MainActivity fragmentActivity;
@ -93,7 +94,9 @@ public class ProfileFragment extends Fragment {
private boolean hasNextPage; private boolean hasNextPage;
private String endCursor; private String endCursor;
private AsyncTask<Void, Void, PostModel[]> currentlyExecuting; private AsyncTask<Void, Void, PostModel[]> currentlyExecuting;
; private MenuItem favMenuItem;
private boolean isPullToRefresh;
private final Runnable usernameSettingRunnable = () -> { private final Runnable usernameSettingRunnable = () -> {
final ActionBar actionBar = fragmentActivity.getSupportActionBar(); final ActionBar actionBar = fragmentActivity.getSupportActionBar();
if (actionBar != null && !Utils.isEmpty(username)) { if (actionBar != null && !Utils.isEmpty(username)) {
@ -140,27 +143,29 @@ public class ProfileFragment extends Fragment {
@Override @Override
public void onResult(final PostModel[] result) { public void onResult(final PostModel[] result) {
binding.swipeRefreshLayout.setRefreshing(false); binding.swipeRefreshLayout.setRefreshing(false);
if (result != null) { if (result == null || result.length <= 0) {
binding.mainPosts.post(() -> binding.mainPosts.setVisibility(View.VISIBLE)); binding.privatePage1.setImageResource(R.drawable.ic_cancel);
// final int oldSize = mainActivity.allItems.size(); binding.privatePage2.setText(R.string.empty_acc);
final List<PostModel> postModels = postsViewModel.getList().getValue(); binding.privatePage.setVisibility(View.VISIBLE);
final List<PostModel> finalList = postModels == null || postModels
.isEmpty() ? new ArrayList<>() : new ArrayList<>(postModels);
finalList.addAll(Arrays.asList(result));
postsViewModel.getList().postValue(finalList);
PostModel model = null;
if (result.length != 0) {
model = result[result.length - 1];
}
if (model == null) return;
endCursor = model.getEndCursor();
hasNextPage = model.hasNextPage();
model.setPageCursor(false, null);
return; return;
} }
binding.privatePage1.setImageResource(R.drawable.ic_cancel); binding.mainPosts.post(() -> binding.mainPosts.setVisibility(View.VISIBLE));
binding.privatePage2.setText(R.string.empty_acc); final List<PostModel> postModels = postsViewModel.getList().getValue();
binding.privatePage.setVisibility(View.VISIBLE); List<PostModel> finalList = postModels == null || postModels.isEmpty() ? new ArrayList<>()
: new ArrayList<>(postModels);
final List<PostModel> resultList = Arrays.asList(result);
if (isPullToRefresh) {
finalList = resultList;
isPullToRefresh = false;
} else {
finalList.addAll(resultList);
}
postsViewModel.getList().postValue(finalList);
final PostModel lastPostModel = result[result.length - 1];
if (lastPostModel == null) return;
endCursor = lastPostModel.getEndCursor();
hasNextPage = lastPostModel.hasNextPage();
lastPostModel.setPageCursor(false, null);
} }
}; };
private final MentionClickListener mentionClickListener = (view, text, isHashtag, isLocation) -> { private final MentionClickListener mentionClickListener = (view, text, isHashtag, isLocation) -> {
@ -181,7 +186,6 @@ public class ProfileFragment extends Fragment {
action.setUsername("@" + text); action.setUsername("@" + text);
NavHostFragment.findNavController(this).navigate(action); NavHostFragment.findNavController(this).navigate(action);
}; };
private MenuItem favMenuItem;
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
@ -225,6 +229,7 @@ public class ProfileFragment 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.swipeRefreshLayout.setOnRefreshListener(this);
init(); init();
shouldRefresh = false; shouldRefresh = false;
} }
@ -235,6 +240,14 @@ public class ProfileFragment extends Fragment {
favMenuItem = menu.findItem(R.id.favourites); favMenuItem = menu.findItem(R.id.favourites);
} }
@Override
public void onRefresh() {
isPullToRefresh = true;
endCursor = null;
fetchProfileDetails();
fetchPosts();
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -718,6 +731,7 @@ public class ProfileFragment extends Fragment {
private void fetchPosts() { private void fetchPosts() {
stopCurrentExecutor(); stopCurrentExecutor();
binding.swipeRefreshLayout.setRefreshing(true);
currentlyExecuting = new PostsFetcher(profileModel.getId(), PostItemType.MAIN, endCursor, postsFetchListener) currentlyExecuting = new PostsFetcher(profileModel.getId(), PostItemType.MAIN, endCursor, postsFetchListener)
.setUsername(profileModel.getUsername()) .setUsername(profileModel.getUsername())
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);