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 lists loading #621

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,29 @@ public void onItemClick(int position) {
}

@Override
protected void resetContent() {
if(tagLabel != null) {
List<Tag> tags = tagDao.queryBuilder()
.where(TagDao.Properties.Label.eq(tagLabel))
.orderDesc(TagDao.Properties.Label)
.list();

tagIDs = new ArrayList<>(tags.size());
for(Tag t: tags) {
tagIDs.add(t.getId());
protected List<Article> load() {
if(cleanLoad) {
if(tagLabel != null) {
List<Tag> tags = tagDao.queryBuilder()
.where(TagDao.Properties.Label.eq(tagLabel))
.orderDesc(TagDao.Properties.Label)
.list();

tagIDs = new ArrayList<>(tags.size());
for(Tag t: tags) {
tagIDs.add(t.getId());
}
} else {
tagIDs = null;
}
} else {
tagIDs = null;
}

super.resetContent();
return super.load();
}

@Override
protected void processContent(List<Article> items) {
super.processContent(items);

forceContentUpdate = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
Expand All @@ -20,7 +23,7 @@
import fr.gaulupeau.apps.InThePoche.R;

public abstract class RecyclerViewListFragment<T> extends Fragment
implements Sortable, Searchable {
implements Sortable, Searchable, LoaderManager.LoaderCallbacks<List<T>> {

private static final String TAG = "RecyclerVLFragment";

Expand All @@ -41,6 +44,9 @@ public abstract class RecyclerViewListFragment<T> extends Fragment
protected boolean active = false;
protected boolean invalidList = true;

protected boolean cleanLoad = true;
protected int currentPage = 0;

public RecyclerViewListFragment() {}

@Override
Expand Down Expand Up @@ -129,6 +135,54 @@ public void onSaveInstanceState(Bundle outState) {
if(searchQuery != null) outState.putString(STATE_SEARCH_QUERY, searchQuery);
}

@Override
public Loader<List<T>> onCreateLoader(int id, Bundle args) {
Log.d(TAG, "onCreateLoader()");

return new AsyncTaskLoader<List<T>>(getActivity()) {
@Override
public List<T> loadInBackground() {
return load();
}

@Override
public void deliverResult(List<T> articles) {
if(isStarted()) { // TODO: check
super.deliverResult(articles);
}
}

@Override
protected void onStartLoading() {
forceLoad();
}

@Override
protected void onStopLoading() {
cancelLoad();
}

@Override
protected void onReset() {
super.onReset();

onStopLoading();
}
};
}

@Override
public void onLoadFinished(Loader<List<T>> loader, List<T> data) {
Log.d(TAG, "onLoadFinished()");

processContent(data);
}

@Override
public void onLoaderReset(Loader<List<T>> loader) {
Log.d(TAG, "onLoaderReset()");
}

@Override
public void setSortOrder(Sortable.SortOrder sortOrder) {
Sortable.SortOrder oldSortOrder = this.sortOrder;
Expand Down Expand Up @@ -173,8 +227,32 @@ protected void checkList() {

protected abstract RecyclerView.Adapter getListAdapter(List<T> list);

protected void restartLoader() {
Log.d(TAG, "restartLoader()");

getLoaderManager().restartLoader(0, null, this);
}

protected void resetContent() {
List<T> items = getItems(0);
Log.d(TAG, "resetContent()");

cleanLoad = true;
currentPage = 0;
restartLoader();
}

protected void processContent(List<T> items) {
Log.d(TAG, "processContent() items.size(): " + items.size());

if(cleanLoad) {
processContentClean(items);
} else {
processContentAdd(items);
}
}

protected void processContentClean(List<T> items) {
Log.d(TAG, "processContentClean() items.size(): " + items.size());

boolean scrollToTop = false;
if(recyclerViewLayoutManager != null) {
Expand All @@ -195,14 +273,14 @@ protected void resetContent() {
}
}

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

List<T> items = getItems(page);
final int addedItemsCount = items.size();
protected void processContentAdd(List<T> items) {
Log.d(TAG, "processContentAdd() items.size(): " + items.size());

itemList.addAll(items);

final int addedItemsCount = items.size();
final int totalItemsCount = itemList.size();

recyclerView.post(new Runnable() {
@Override
public void run() {
Expand All @@ -211,6 +289,31 @@ public void run() {
});
}

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

if(page <= currentPage) return;

currentPage++;
if(page != currentPage) {
Log.w(TAG, String.format("loadMore() page request mismatch!" +
" page: %d, currentPage: %d; resetting",
page, currentPage));

resetContent();
return;
}

cleanLoad = false;
restartLoader();
}

protected List<T> load() {
Log.d(TAG, "load()");

return getItems(currentPage);
}

protected abstract List<T> getItems(int page);

protected abstract DiffUtil.Callback getDiffUtilCallback(List<T> oldItems, List<T> newItems);
Expand Down