Improve chaining of updated cursor data from Reading activity down through to adapter and fragments.

Fixes tendency for UI updates to get missed and for story titles to get mismatched with story content during long pauses.
This commit is contained in:
dosiecki 2014-10-10 13:50:00 -07:00
parent b64b36a100
commit f62505160a
3 changed files with 32 additions and 31 deletions

View file

@ -194,41 +194,34 @@ public abstract class Reading extends NbActivity implements OnPageChangeListener
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
synchronized (STORIES_MUTEX) {
if (cursor != null) {
readingAdapter.swapCursor(cursor);
stories = cursor;
}
if (cursor == null) return;
readingAdapter.swapCursor(cursor);
stories = cursor;
// if this is the first time we've found a cursor, we know the onCreate chain is done
if (this.pager == null) {
int currentUnreadCount = getUnreadCount();
if (currentUnreadCount > this.startingUnreadCount ) {
this.startingUnreadCount = currentUnreadCount;
}
// set up the pager after the unread count, so the first mark-read doesn't happen too quickly
setupPager();
}
try {
readingAdapter.notifyDataSetChanged();
checkStoryCount(pager.getCurrentItem());
if (this.unreadSearchLatch != null) {
this.unreadSearchLatch.countDown();
}
ReadingItemFragment fragment = getReadingFragment();
if (fragment != null ) {
fragment.updateStory(readingAdapter.getStory(pager.getCurrentItem()));
fragment.updateSaveButton();
updateOverlayNav();
updateOverlayText();
}
} catch (IllegalStateException ise) {
// sometimes the pager is already shutting down by the time the callback finishes
finish();
}
checkStoryCount(pager.getCurrentItem());
if (this.unreadSearchLatch != null) {
this.unreadSearchLatch.countDown();
}
updateOverlayNav();
updateOverlayText();
}
}
@ -328,7 +321,6 @@ public abstract class Reading extends NbActivity implements OnPageChangeListener
protected void handleUpdate() {
enableMainProgress(NBSyncService.isFeedSetSyncing(this.fs));
updateCursor();
readingAdapter.updateAllFragments();
}
private void updateCursor() {

View file

@ -108,12 +108,18 @@ public abstract class ReadingAdapter extends FragmentStatePagerAdapter {
return frag.get();
}
public void updateAllFragments() {
for (int i=0; i<cachedFragments.size(); i++) {
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// go one step further than the default pageradapter and also refresh the
// story object inside each fragment we have active
for (int i=0; i<stories.getCount(); i++) {
WeakReference<ReadingItemFragment> frag = cachedFragments.get(i);
if (frag == null) continue;
ReadingItemFragment rif = frag.get();
if (rif == null) continue;
rif.offerStoryUpdate(getStory(i));
rif.handleUpdate();
}
}

View file

@ -276,22 +276,13 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
});
}
public void updateSaveButton() {
private void updateSaveButton() {
if (view == null) { return; }
Button saveButton = (Button) view.findViewById(R.id.save_story_button);
if (saveButton == null) { return; }
saveButton.setText(story.starred ? R.string.unsave_this : R.string.save_this);
}
public void updateStory(Story story) {
if (story != null ) {
this.story = story;
if (selectedFeedView == DefaultFeedView.TEXT && originalText == null) {
loadOriginalText();
}
}
}
private void setupShareButton() {
Button shareButton = (Button) view.findViewById(R.id.share_story_button);
@ -450,7 +441,19 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
((Reading) parent).enableLeftProgressCircle(loading);
}
/**
* Lets the pager offer us an updated version of our story when a new cursor is
* cycled in. This class takes the responsibility of ensureing that the cursor
* index has not shifted, though, by checking story IDs.
*/
public void offerStoryUpdate(Story story) {
if (story == null) return;
if (! TextUtils.equals(story.storyHash, this.story.storyHash)) return;
this.story = story;
}
public void handleUpdate() {
updateSaveButton();
reloadStoryContent();
}