Switch all stories list to correctly use loaders.

This commit is contained in:
dosiecki 2014-10-31 05:27:02 -07:00 committed by Samuel Clay
parent 96a9f3d298
commit 0099bcdf3a
11 changed files with 45 additions and 134 deletions

View file

@ -24,30 +24,15 @@ import com.newsblur.util.StoryOrder;
public class AllSharedStoriesItemsList extends ItemsList {
private ArrayList<String> feedIds;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setTitle(getResources().getString(R.string.all_shared_stories));
if (bundle != null) {
feedIds = bundle.getStringArrayList(BUNDLE_FEED_IDS);
}
if (feedIds == null) {
feedIds = new ArrayList<String>();
Cursor cursor = getContentResolver().query(FeedProvider.SOCIAL_FEEDS_URI, null, null, null, null);
while (cursor.moveToNext()) {
feedIds.add(cursor.getString(cursor.getColumnIndex(DatabaseConstants.SOCIAL_FEED_ID)));
}
cursor.close();
}
itemListFragment = (AllSharedStoriesItemListFragment) fragmentManager.findFragmentByTag(AllSharedStoriesItemListFragment.class.getName());
if (itemListFragment == null) {
itemListFragment = AllSharedStoriesItemListFragment.newInstance(feedIds, currentState, getDefaultFeedView());
itemListFragment = AllSharedStoriesItemListFragment.newInstance(currentState, getDefaultFeedView());
itemListFragment.setRetainInstance(true);
FragmentTransaction listTransaction = fragmentManager.beginTransaction();
listTransaction.add(R.id.activity_itemlist_container, itemListFragment, AllSharedStoriesItemListFragment.class.getName());
@ -100,11 +85,4 @@ public class AllSharedStoriesItemsList extends ItemsList {
}
}
@Override
protected void onSaveInstanceState(Bundle bundle) {
if (this.feedIds != null) {
bundle.putStringArrayList(BUNDLE_FEED_IDS, this.feedIds);
}
super.onSaveInstanceState(bundle);
}
}

View file

@ -1,22 +1,21 @@
package com.newsblur.activity;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.Toast;
import com.newsblur.R;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.database.FeedProvider;
import com.newsblur.fragment.AllStoriesItemListFragment;
import com.newsblur.fragment.FeedItemListFragment;
import com.newsblur.fragment.MarkAllReadDialogFragment;
@ -33,37 +32,15 @@ import com.newsblur.util.StateFilter;
public class AllStoriesItemsList extends ItemsList implements MarkAllReadDialogListener {
private ContentResolver resolver;
private ArrayList<String> feedIds;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setTitle(getResources().getString(R.string.all_stories));
resolver = getContentResolver();
if (bundle != null) {
feedIds = bundle.getStringArrayList(BUNDLE_FEED_IDS);
}
if (feedIds == null) {
feedIds = new ArrayList<String>(); // default to a wildcard search
// if we're in Focus mode, only query for feeds with a nonzero focus count
if (this.currentState == StateFilter.BEST) {
Cursor cursor = resolver.query(FeedProvider.FEEDS_URI, null, DatabaseConstants.FEED_FILTER_FOCUS, null, null);
while (cursor.moveToNext() && (feedIds.size() <= AppConstants.MAX_FEED_LIST_SIZE)) {
feedIds.add(cursor.getString(cursor.getColumnIndex(DatabaseConstants.FEED_ID)));
}
cursor.close();
}
}
itemListFragment = (AllStoriesItemListFragment) fragmentManager.findFragmentByTag(AllStoriesItemListFragment.class.getName());
if (itemListFragment == null) {
itemListFragment = AllStoriesItemListFragment.newInstance(feedIds, currentState, getDefaultFeedView());
itemListFragment = AllStoriesItemListFragment.newInstance(currentState, getDefaultFeedView());
itemListFragment.setRetainInstance(true);
FragmentTransaction listTransaction = fragmentManager.beginTransaction();
listTransaction.add(R.id.activity_itemlist_container, itemListFragment, AllStoriesItemListFragment.class.getName());
@ -133,11 +110,4 @@ public class AllStoriesItemsList extends ItemsList implements MarkAllReadDialogL
// do nothing
}
@Override
protected void onSaveInstanceState(Bundle bundle) {
if (this.feedIds != null) {
bundle.putStringArrayList(BUNDLE_FEED_IDS, this.feedIds);
}
super.onSaveInstanceState(bundle);
}
}

View file

@ -49,6 +49,8 @@ public abstract class ItemsList extends NbActivity implements StateChangedListen
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
// our intel state is entirely determined by the state of the Main view
currentState = (StateFilter) getIntent().getSerializableExtra(EXTRA_STATE);
this.fs = createFeedSet();
requestWindowFeature(Window.FEATURE_PROGRESS);
@ -57,8 +59,6 @@ public abstract class ItemsList extends NbActivity implements StateChangedListen
setContentView(R.layout.activity_itemslist);
fragmentManager = getFragmentManager();
// our intel state is entirely determined by the state of the Main view
currentState = (StateFilter) getIntent().getSerializableExtra(EXTRA_STATE);
getActionBar().setDisplayHomeAsUpEnabled(true);
this.overlayStatusText = (TextView) findViewById(R.id.itemlist_sync_status);
@ -85,7 +85,7 @@ public abstract class ItemsList extends NbActivity implements StateChangedListen
private void getFirstStories() {
stopLoading = false;
triggerRefresh(AppConstants.READING_STORY_PRELOAD);
triggerRefresh(AppConstants.READING_STORY_PRELOAD, 0);
}
@Override
@ -95,14 +95,6 @@ public abstract class ItemsList extends NbActivity implements StateChangedListen
super.onPause();
}
public void triggerRefresh(int desiredStoryCount) {
if (!stopLoading) {
boolean gotSome = NBSyncService.requestMoreForFeed(fs, desiredStoryCount);
if (gotSome) triggerSync();
updateStatusIndicators();
}
}
public void triggerRefresh(int desiredStoryCount, int totalSeen) {
if (!stopLoading) {
boolean gotSome = NBSyncService.requestMoreForFeed(fs, desiredStoryCount, totalSeen);
@ -159,9 +151,6 @@ public abstract class ItemsList extends NbActivity implements StateChangedListen
if (itemListFragment != null) {
itemListFragment.hasUpdated();
}
// re-trigger at least one story when this is reached, just in case the first update beat
// the sync service pagination reset. it won't do anything after the first call
triggerRefresh(AppConstants.READING_STORY_PRELOAD);
}
private void updateStatusIndicators() {

View file

@ -55,7 +55,6 @@ public abstract class Reading extends NbActivity implements OnPageChangeListener
public static final String EXTRA_USERID = "user_id";
public static final String EXTRA_USERNAME = "username";
public static final String EXTRA_FOLDERNAME = "foldername";
public static final String EXTRA_FEED_IDS = "feed_ids";
public static final String EXTRA_DEFAULT_FEED_VIEW = "default_feed_view";
private static final String TEXT_SIZE = "textsize";
private static final String BUNDLE_POSITION = "position";

View file

@ -290,8 +290,6 @@ public class DatabaseConstants {
REPLY_COMMENTID, REPLY_DATE, REPLY_ID, REPLY_SHORTDATE, REPLY_TEXT, REPLY_USERID
};
public static final String FEED_FILTER_FOCUS = FEED_TABLE + "." + FEED_POSITIVE_COUNT + " > 0 ";
private static final String SUM_STORY_TOTAL = "storyTotal";
private static String STORY_SUM_TOTAL = " CASE " +
"WHEN MAX(" + STORY_INTELLIGENCE_AUTHORS + "," + STORY_INTELLIGENCE_TAGS + "," + STORY_INTELLIGENCE_TITLE + ") > 0 " +

View file

@ -1,6 +1,5 @@
package com.newsblur.fragment;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.content.CursorLoader;
@ -32,17 +31,8 @@ import com.newsblur.view.SocialItemViewBinder;
public class AllSharedStoriesItemListFragment extends ItemListFragment implements OnItemClickListener {
private String[] feedIds;
private ContentResolver contentResolver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> feedIdArrayList = getArguments().getStringArrayList("feedIds");
feedIds = new String[feedIdArrayList.size()];
feedIdArrayList.toArray(feedIds);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_itemlist, null);
@ -68,11 +58,10 @@ public class AllSharedStoriesItemListFragment extends ItemListFragment implement
return v;
}
public static ItemListFragment newInstance(ArrayList<String> feedIds, StateFilter currentState, DefaultFeedView defaultFeedView) {
public static ItemListFragment newInstance(StateFilter currentState, DefaultFeedView defaultFeedView) {
ItemListFragment everythingFragment = new AllSharedStoriesItemListFragment();
Bundle arguments = new Bundle();
arguments.putSerializable("currentState", currentState);
arguments.putStringArrayList("feedIds", feedIds);
arguments.putSerializable("defaultFeedView", defaultFeedView);
everythingFragment.setArguments(arguments);
@ -84,7 +73,6 @@ public class AllSharedStoriesItemListFragment extends ItemListFragment implement
if (getActivity().isFinishing()) return;
Intent i = new Intent(getActivity(), AllSharedStoriesReading.class);
i.putExtra(Reading.EXTRA_FEEDSET, getFeedSet());
i.putExtra(FeedReading.EXTRA_FEED_IDS, feedIds);
i.putExtra(FeedReading.EXTRA_POSITION, position);
i.putExtra(ItemsList.EXTRA_STATE, currentState);
i.putExtra(Reading.EXTRA_DEFAULT_FEED_VIEW, defaultFeedView);

View file

@ -1,8 +1,5 @@
package com.newsblur.fragment;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
@ -30,49 +27,42 @@ import com.newsblur.view.SocialItemViewBinder;
public class AllStoriesItemListFragment extends ItemListFragment implements OnItemClickListener {
private String[] feedIds;
private ContentResolver contentResolver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> feedIdArrayList = getArguments().getStringArrayList("feedIds");
feedIds = new String[feedIdArrayList.size()];
feedIdArrayList.toArray(feedIds);
}
private ListView itemList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_itemlist, null);
ListView itemList = (ListView) v.findViewById(R.id.itemlistfragment_list);
itemList = (ListView) v.findViewById(R.id.itemlistfragment_list);
setupBezelSwipeDetector(itemList);
itemList.setEmptyView(v.findViewById(R.id.empty_view));
Cursor cursor = dbHelper.getStoriesCursor(getFeedSet(), currentState);
getActivity().startManagingCursor(cursor);
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORT_CONTENT, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TIMESTAMP, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_content, R.id.row_item_author, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_feedtitle };
adapter = new MultipleFeedItemsAdapter(getActivity(), R.layout.row_socialitem, cursor, groupFrom, groupTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);
itemList.setOnScrollListener(this);
adapter.setViewBinder(new SocialItemViewBinder(getActivity()));
itemList.setAdapter(adapter);
itemList.setOnItemClickListener(this);
itemList.setOnCreateContextMenuListener(this);
if (adapter != null) {
itemList.setAdapter(adapter);
}
getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);
return v;
}
public static ItemListFragment newInstance(ArrayList<String> feedIds, StateFilter currentState, DefaultFeedView defaultFeedView) {
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if ((adapter == null) && (cursor != null)) {
String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORT_CONTENT, DatabaseConstants.STORY_AUTHORS, DatabaseConstants.STORY_TIMESTAMP, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.FEED_TITLE };
int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_content, R.id.row_item_author, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_feedtitle };
adapter = new MultipleFeedItemsAdapter(getActivity(), R.layout.row_socialitem, cursor, groupFrom, groupTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
adapter.setViewBinder(new SocialItemViewBinder(getActivity()));
itemList.setAdapter(adapter);
}
super.onLoadFinished(loader, cursor);
}
public static ItemListFragment newInstance(StateFilter currentState, DefaultFeedView defaultFeedView) {
ItemListFragment everythingFragment = new AllStoriesItemListFragment();
Bundle arguments = new Bundle();
arguments.putSerializable("currentState", currentState);
arguments.putStringArrayList("feedIds", feedIds);
arguments.putSerializable("defaultFeedView", defaultFeedView);
everythingFragment.setArguments(arguments);
@ -84,7 +74,6 @@ public class AllStoriesItemListFragment extends ItemListFragment implements OnIt
if (getActivity().isFinishing()) return;
Intent i = new Intent(getActivity(), AllStoriesReading.class);
i.putExtra(Reading.EXTRA_FEEDSET, getFeedSet());
i.putExtra(FeedReading.EXTRA_FEED_IDS, feedIds);
i.putExtra(FeedReading.EXTRA_POSITION, position);
i.putExtra(ItemsList.EXTRA_STATE, currentState);
i.putExtra(Reading.EXTRA_DEFAULT_FEED_VIEW, defaultFeedView);

View file

@ -220,7 +220,7 @@ public class FolderListFragment extends NbFragment implements OnGroupClickListen
public boolean onGroupClick(ExpandableListView list, View group, int groupPosition, long id) {
if (adapter.isFolderRoot(groupPosition)) {
Intent i = new Intent(getActivity(), AllStoriesItemsList.class);
i.putExtra(AllStoriesItemsList.EXTRA_STATE, currentState);
i.putExtra(ItemsList.EXTRA_STATE, currentState);
startActivity(i);
return true;
} else if (adapter.isRowSavedStories(groupPosition)) {

View file

@ -126,7 +126,7 @@ public abstract class ItemListFragment extends NbFragment implements OnScrollLis
if (cursor != null) {
cursorSeenYet = true;
if (cursor.getCount() == 0) {
((ItemsList) getActivity()).triggerRefresh(1);
((ItemsList) getActivity()).triggerRefresh(1, 0);
}
adapter.swapCursor(cursor);
}

View file

@ -707,7 +707,8 @@ public class NBSyncService extends Service {
synchronized (PendingFeeds) {
Integer alreadySeen = FeedStoriesSeen.get(fs);
Integer alreadyRequested = PendingFeeds.get(fs);
if (alreadySeen != null) {
if (alreadySeen == null) alreadySeen = 0;
if (alreadyRequested == null) alreadyRequested = 0;
if ((callerSeen >= 0) && (alreadySeen > callerSeen)) {
// the caller is probably filtering and thinks they have fewer than we do, so
// update our count to agree with them, and force-allow another requet
@ -715,15 +716,13 @@ public class NBSyncService extends Service {
FeedStoriesSeen.put(fs, callerSeen);
alreadyRequested = 0;
}
if (AppConstants.VERBOSE_LOG) Log.d(NBSyncService.class.getName(), "have:" + alreadySeen + " want:" + desiredStoryCount + " requested:" + alreadyRequested);
if (desiredStoryCount <= alreadySeen) {
return false;
}
}
if ((alreadyRequested != null) && (desiredStoryCount <= alreadyRequested)) {
if (desiredStoryCount <= alreadyRequested) {
return false;
}
if (AppConstants.VERBOSE_LOG) Log.d(NBSyncService.class.getName(), "have:" + alreadySeen + " want:" + desiredStoryCount);
}
PendingFeeds.put(fs, desiredStoryCount);

View file

@ -138,6 +138,7 @@ public class FeedSet implements Serializable {
* Gets a single feed ID iff there is only one or null otherwise.
*/
public String getSingleFeed() {
if (folderName != null) return null;
if (feeds != null && feeds.size() == 1) return feeds.iterator().next(); else return null;
}
@ -145,7 +146,7 @@ public class FeedSet implements Serializable {
* Gets a set of feed IDs iff there are multiples or null otherwise.
*/
public Set<String> getMultipleFeeds() {
if (feeds != null && feeds.size() > 1) return feeds; else return null;
if (feeds != null && ((feeds.size() > 1) || (folderName != null))) return feeds; else return null;
}
/**