mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-18 12:47:30 +00:00
Fix pull to refresh in DiscoverFragment
This commit is contained in:
parent
3c90eebe7a
commit
48b76c231a
@ -20,10 +20,10 @@ 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;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import awais.instagrabber.R;
|
import awais.instagrabber.R;
|
||||||
@ -43,7 +43,7 @@ import awais.instagrabber.models.DiscoverTopicModel;
|
|||||||
import awais.instagrabber.models.enums.DownloadMethod;
|
import awais.instagrabber.models.enums.DownloadMethod;
|
||||||
import awais.instagrabber.utils.Utils;
|
import awais.instagrabber.utils.Utils;
|
||||||
|
|
||||||
public class DiscoverFragment extends Fragment {
|
public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
|
||||||
|
|
||||||
private MainActivity fragmentActivity;
|
private MainActivity fragmentActivity;
|
||||||
private CoordinatorLayout root;
|
private CoordinatorLayout root;
|
||||||
@ -58,6 +58,7 @@ public class DiscoverFragment extends Fragment {
|
|||||||
private ActionMode actionMode;
|
private ActionMode actionMode;
|
||||||
private DiscoverItemViewModel discoverItemViewModel;
|
private DiscoverItemViewModel discoverItemViewModel;
|
||||||
private boolean shouldRefresh = true;
|
private boolean shouldRefresh = true;
|
||||||
|
private boolean isPullToRefresh;
|
||||||
|
|
||||||
private final FetchListener<DiscoverTopicModel> topicFetchListener = new FetchListener<DiscoverTopicModel>() {
|
private final FetchListener<DiscoverTopicModel> topicFetchListener = new FetchListener<DiscoverTopicModel>() {
|
||||||
@Override
|
@Override
|
||||||
@ -77,28 +78,28 @@ public class DiscoverFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private final FetchListener<DiscoverItemModel[]> discoverFetchListener = new FetchListener<DiscoverItemModel[]>() {
|
private final FetchListener<DiscoverItemModel[]> postsFetchListener = new FetchListener<DiscoverItemModel[]>() {
|
||||||
@Override
|
@Override
|
||||||
public void doBefore() {
|
public void doBefore() {}
|
||||||
binding.discoverSwipeRefreshLayout.setRefreshing(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResult(final DiscoverItemModel[] result) {
|
public void onResult(final DiscoverItemModel[] result) {
|
||||||
binding.discoverSwipeRefreshLayout.setRefreshing(false);
|
if (result == null || result.length <= 0) {
|
||||||
if (result == null || result.length == 0) {
|
binding.discoverSwipeRefreshLayout.setRefreshing(false);
|
||||||
Toast.makeText(requireContext(), R.string.discover_empty, Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), R.string.discover_empty, Toast.LENGTH_SHORT).show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final List<DiscoverItemModel> current = discoverItemViewModel.getList().getValue();
|
List<DiscoverItemModel> current = discoverItemViewModel.getList().getValue();
|
||||||
final List<DiscoverItemModel> resultList = Arrays.asList(result);
|
final List<DiscoverItemModel> resultList = Arrays.asList(result);
|
||||||
if (current == null) {
|
current = current == null ? new ArrayList<>() : new ArrayList<>(current); // copy to modifiable list
|
||||||
discoverItemViewModel.getList().postValue(resultList);
|
if (isPullToRefresh) {
|
||||||
|
current = resultList;
|
||||||
|
isPullToRefresh = false;
|
||||||
} else {
|
} else {
|
||||||
final List<DiscoverItemModel> currentCopy = new ArrayList<>(current);
|
current.addAll(resultList);
|
||||||
currentCopy.addAll(resultList);
|
|
||||||
discoverItemViewModel.getList().postValue(currentCopy);
|
|
||||||
}
|
}
|
||||||
|
discoverItemViewModel.getList().postValue(current);
|
||||||
|
binding.discoverSwipeRefreshLayout.setRefreshing(false);
|
||||||
final DiscoverItemModel discoverItemModel = result[result.length - 1];
|
final DiscoverItemModel discoverItemModel = result[result.length - 1];
|
||||||
if (discoverItemModel != null) {
|
if (discoverItemModel != null) {
|
||||||
discoverEndMaxId = discoverItemModel.getNextMaxId();
|
discoverEndMaxId = discoverItemModel.getNextMaxId();
|
||||||
@ -161,39 +162,35 @@ public class DiscoverFragment 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.discoverSwipeRefreshLayout.setOnRefreshListener(this);
|
||||||
setupExplore();
|
setupExplore();
|
||||||
shouldRefresh = false;
|
shouldRefresh = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRefresh() {
|
||||||
|
isPullToRefresh = true;
|
||||||
|
discoverEndMaxId = null;
|
||||||
|
lazyLoader.resetState();
|
||||||
|
fetchPosts();
|
||||||
|
}
|
||||||
|
|
||||||
private void setupExplore() {
|
private void setupExplore() {
|
||||||
discoverItemViewModel = new ViewModelProvider(fragmentActivity).get(DiscoverItemViewModel.class);
|
discoverItemViewModel = new ViewModelProvider(fragmentActivity).get(DiscoverItemViewModel.class);
|
||||||
final GridAutofitLayoutManager layoutManager = new GridAutofitLayoutManager(requireContext(), Utils.convertDpToPx(110));
|
final GridAutofitLayoutManager layoutManager = new GridAutofitLayoutManager(requireContext(), Utils.convertDpToPx(110));
|
||||||
binding.discoverPosts.setLayoutManager(layoutManager);
|
binding.discoverPosts.setLayoutManager(layoutManager);
|
||||||
binding.discoverPosts.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(4)));
|
binding.discoverPosts.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(4)));
|
||||||
new iTopicFetcher(topicFetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
||||||
binding.discoverType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
binding.discoverType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
|
||||||
if (topicIds != null) {
|
if (topicIds == null || topicIds.length <= 0) return;
|
||||||
currentTopic = topicIds[pos];
|
currentTopic = topicIds[pos];
|
||||||
binding.discoverSwipeRefreshLayout.setRefreshing(true);
|
onRefresh();
|
||||||
if (lazyLoader != null) lazyLoader.resetState();
|
|
||||||
discoverItemViewModel.getList().postValue(Collections.emptyList());
|
|
||||||
new DiscoverFetcher(currentTopic, null, rankToken, discoverFetchListener, false)
|
|
||||||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNothingSelected(AdapterView<?> parent) {}
|
public void onNothingSelected(AdapterView<?> parent) {}
|
||||||
});
|
});
|
||||||
|
|
||||||
binding.discoverSwipeRefreshLayout.setOnRefreshListener(() -> {
|
|
||||||
lazyLoader.resetState();
|
|
||||||
discoverItemViewModel.getList().postValue(Collections.emptyList());
|
|
||||||
new DiscoverFetcher(currentTopic, null, rankToken, discoverFetchListener, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
||||||
});
|
|
||||||
|
|
||||||
discoverAdapter = new DiscoverAdapter((model, position) -> {
|
discoverAdapter = new DiscoverAdapter((model, position) -> {
|
||||||
if (discoverAdapter.isSelecting()) {
|
if (discoverAdapter.isSelecting()) {
|
||||||
if (actionMode == null) return;
|
if (actionMode == null) return;
|
||||||
@ -236,13 +233,21 @@ public class DiscoverFragment extends Fragment {
|
|||||||
discoverItemViewModel.getList().observe(fragmentActivity, discoverAdapter::submitList);
|
discoverItemViewModel.getList().observe(fragmentActivity, discoverAdapter::submitList);
|
||||||
lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> {
|
lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> {
|
||||||
if (discoverHasMore) {
|
if (discoverHasMore) {
|
||||||
binding.discoverSwipeRefreshLayout.setRefreshing(true);
|
fetchPosts();
|
||||||
new DiscoverFetcher(currentTopic, discoverEndMaxId, rankToken, discoverFetchListener, false)
|
|
||||||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
||||||
discoverEndMaxId = null;
|
|
||||||
}
|
}
|
||||||
});
|
}, 3);
|
||||||
binding.discoverPosts.addOnScrollListener(lazyLoader);
|
binding.discoverPosts.addOnScrollListener(lazyLoader);
|
||||||
|
fetchTopics();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fetchTopics() {
|
||||||
|
new iTopicFetcher(topicFetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fetchPosts() {
|
||||||
|
binding.discoverSwipeRefreshLayout.setRefreshing(true);
|
||||||
|
new DiscoverFetcher(currentTopic, discoverEndMaxId, rankToken, postsFetchListener, false)
|
||||||
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkAndResetAction() {
|
private boolean checkAndResetAction() {
|
||||||
|
Loading…
Reference in New Issue
Block a user