Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Async list update #408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use AsyncTasks to update lists
di72nn committed Jan 29, 2017

Verified

This commit was signed with the committer’s verified signature.
Cherry James Ross
commit 436d40611a1f07fd0e0668685a8921dbc9c64669
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
@@ -230,41 +231,55 @@ public void checkList() {
}

private void resetListContent() {
List<Article> articles = getArticles(0);
final boolean scrollToTop = recyclerViewLayoutManager != null
&& recyclerViewLayoutManager.findFirstCompletelyVisibleItemPosition() == 0;

boolean scrollToTop = false;
if(recyclerViewLayoutManager != null) {
scrollToTop = recyclerViewLayoutManager.findFirstCompletelyVisibleItemPosition() == 0;
}
new AsyncTask<Void, Void, List<Article>>() {
@Override
protected List<Article> doInBackground(Void... params) {
return getArticles(0);
}

DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(
new ArticleListDiffCallback(mArticles, articles));
@Override
protected void onPostExecute(List<Article> articles) {
super.onPostExecute(articles);

mArticles.clear();
mArticles.addAll(articles);
// TODO: move to background thread if possible
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(
new ArticleListDiffCallback(mArticles, articles));

diffResult.dispatchUpdatesTo(mAdapter);
mArticles.clear();
mArticles.addAll(articles);

if(scrollListener != null) scrollListener.resetState();
diffResult.dispatchUpdatesTo(mAdapter);

if(scrollToTop && recyclerView != null) {
recyclerView.scrollToPosition(0);
}
if(scrollListener != null) scrollListener.resetState();

if(scrollToTop && recyclerView != null) {
recyclerView.scrollToPosition(0);
}
}
}.execute();
}

private void loadMore(int page, final int totalItemsCount) {
private void loadMore(final int page, final int totalItemsCount) {
Log.d(TAG, String.format("loadMore(page: %d, totalItemsCount: %d)", page, totalItemsCount));

List<Article> articles = getArticles(page);
final int addedItemsCount = articles.size();
new AsyncTask<Void, Void, List<Article>>() {
@Override
protected List<Article> doInBackground(Void... params) {
return getArticles(page);
}

mArticles.addAll(articles);
recyclerView.post(new Runnable() {
@Override
public void run() {
mAdapter.notifyItemRangeInserted(totalItemsCount, addedItemsCount);
protected void onPostExecute(List<Article> articles) {
super.onPostExecute(articles);

mArticles.addAll(articles);

mAdapter.notifyItemRangeInserted(totalItemsCount, articles.size());
}
});
}.execute();
}

public void openRandomArticle() {