Skip to content

Commit

Permalink
[android] don't call refresh() inside restartLoader. minor optimizati…
Browse files Browse the repository at this point in the history
…ons. hunting for bug on favoritefragment.
  • Loading branch information
gubatron committed Mar 3, 2016
1 parent b83fc02 commit 3876b97
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,20 @@ public int getOffset() {
*/
@Override
public int getCount() {
int count = super.getCount();;
if (mDataList != null) {
int size = mDataList.size();
return size == 0 ? 0 : size + getOffset();
count = size==0 ? 0 : size + getOffset();
}
return super.getCount();

LOGGER.info("getCount() -> " + count) ;
return count;
}

/**
* @param position - The ACTUAL position in the model container. If you're using an offset based on a list view that has a header element at 0, you must subtract to the position you might have.
* @param position - The ACTUAL position in the model container.
* If you're using an offset based on a list view that has a header element at 0,
* you must subtract to the position you might have.
* @return The element at the indexed position in the model container (not the view). null if position is out of bounds.
*/
public I getItem(int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ProfileSongAdapter extends ApolloFragmentAdapter<Song> {
/**
* Number of views (ImageView, TextView, header)
*/
private static final int VIEW_TYPE_COUNT = 3;
private static final int VIEW_TYPE_COUNT = 2;

/**
* LayoutInflater
Expand Down Expand Up @@ -186,4 +186,9 @@ public int getItemViewType(final int position) {
}
return ITEM_VIEW_TYPE_MUSIC;
}

@Override
public long getItemId(int position) {
return position-1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ private void initBottomActionBar() {
// playing
bottomActionBar.setOnClickListener(mOpenNowPlaying);

//new StopListener(this, false)
mPlayPauseButton.setOnLongClickListener(new StopAndHideBottomActionBarListener(this, false));
}
setBottomActionBarVisible(isPlaying);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,11 @@ public void onLoadFinished(final Loader<List<I>> loader, final List<I> data) {
if (mAdapter != null) {
mAdapter.setDataList(data);

mAdapter.setNotifyOnChange(false);
for (final I item : data) {
mAdapter.add(item);
}
mAdapter.setNotifyOnChange(true);

if (mAdapter instanceof ApolloFragmentAdapter.Cacheable) {
((ApolloFragmentAdapter.Cacheable) mAdapter).buildCache();
Expand All @@ -455,6 +457,7 @@ public void onLoaderReset(final Loader<List<I>> loader) {
* (Don't do so until 10 seconds later if you refreshed already)
*/
public void refresh() {
LOGGER.info(getClass().getSimpleName() + ":refresh() invoked.");
// Scroll to the stop of the list before restarting the loader.
// Otherwise, if the user has scrolled enough to move the header, it
// becomes misplaced and needs to be reset.
Expand All @@ -466,12 +469,14 @@ public void refresh() {

if (mAdapter != null) {
mAdapter.clear();
LOGGER.info(getClass().getSimpleName() + ":refresh() - adapter cleared.");
}

restartLoader(); // this won't be executed if it was recently called. no risk of endless loop.

if (mAdapter != null) {
mAdapter.notifyDataSetChanged();
LOGGER.info(getClass().getSimpleName() + ":refresh() mAdapter.notifyDataSetChanged() finished.");
}
}

Expand All @@ -489,15 +494,20 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun
}

public void restartLoader() {
restartLoader(false);
if (!restartLoader(false)) {
LOGGER.info(getClass().getSimpleName() + ":restartLoader skipped.");
} else {
LOGGER.info(getClass().getSimpleName() + ":restartLoader reloaded data.");
}
}

public void restartLoader(boolean force) {
public boolean restartLoader(boolean force) {
if (force || (System.currentTimeMillis() - lastRestartLoader) >= 5000) {
lastRestartLoader = System.currentTimeMillis();
getLoaderManager().restartLoader(LOADER_ID, getArguments(), this);
refresh(); // won't end up in recursive call because we just refreshed.
return true;
}
return false;
}

public void initLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
protected String getLayoutTypeName() {
return PreferenceUtils.SIMPLE_LAYOUT;
}

}

0 comments on commit 3876b97

Please sign in to comment.