package com.newsblur.fragment;

import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.newsblur.R;
import com.newsblur.activity.Reading;
import com.newsblur.activity.SavedStoriesReading;
import com.newsblur.activity.FeedReading;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.database.MultipleFeedItemsAdapter;
import com.newsblur.util.DefaultFeedView;
import com.newsblur.util.StoryOrder;
import com.newsblur.view.SocialItemViewBinder;

public class SavedStoriesItemListFragment extends ItemListFragment implements OnItemClickListener {

    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@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);
        setupBezelSwipeDetector(itemList);

		itemList.setEmptyView(v.findViewById(R.id.empty_view));

		Cursor cursor = dbHelper.getStoriesCursor(getFeedSet(), currentState, 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 };

		getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);

		adapter = new MultipleFeedItemsAdapter(getActivity(), R.layout.row_socialitem, cursor, groupFrom, groupTo, true);

		itemList.setOnScrollListener(this);

		adapter.setViewBinder(new SocialItemViewBinder(getActivity(), true));
		itemList.setAdapter(adapter);
		itemList.setOnItemClickListener(this);

		return v;
	}

	public static ItemListFragment newInstance(DefaultFeedView defaultFeedView) {
		ItemListFragment fragment = new SavedStoriesItemListFragment();
        Bundle args = new Bundle();
        args.putSerializable("defaultFeedView", defaultFeedView);
        fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (getActivity().isFinishing()) return;
		Intent i = new Intent(getActivity(), SavedStoriesReading.class);
        i.putExtra(Reading.EXTRA_FEEDSET, getFeedSet());
		i.putExtra(FeedReading.EXTRA_POSITION, position);
        i.putExtra(Reading.EXTRA_DEFAULT_FEED_VIEW, defaultFeedView);
		startActivity(i);
	}


}

@@ -882,16 +882,48 @@
 ener %7B%0A%0A
+    private ListView itemList;%0A%0A
     @Ove
@@ -1198,25 +1198,16 @@
 ull);%0A%09%09
-ListView 
 itemList
@@ -1307,17 +1307,16 @@
 mList);%0A
-%0A
 %09%09itemLi
@@ -1369,93 +1369,837 @@
 ));%0A
-%0A%09%09Cursor cursor = dbHelper.getStoriesCursor(getFeedSet(), currentState, null);%0A%09%09%0A%09%09
+%09%09itemList.setOnScrollListener(this);%0A%09%09itemList.setOnItemClickListener(this);%0A        if (adapter != null) %7B%0A            // normally the list gets set up when the adapter is created, but sometimes%0A            // onCreateView gets re-called.%0A            itemList.setAdapter(adapter);%0A        %7D%0A%0A%09%09getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);%0A%0A%09%09return v;%0A%09%7D%0A%0A%09public static ItemListFragment newInstance(DefaultFeedView defaultFeedView) %7B%0A%09%09ItemListFragment fragment = new SavedStoriesItemListFragment();%0A        Bundle args = new Bundle();%0A        args.putSerializable(%22defaultFeedView%22, defaultFeedView);%0A        fragment.setArguments(args);%0A%09%09return fragment;%0A%09%7D%0A%0A    @Override%0A%09public void onLoadFinished(Loader%3CCursor%3E loader, Cursor cursor) %7B%0A        if ((adapter == null) && (cursor != null)) %7B%0A            
 Stri
@@ -2446,18 +2446,28 @@
 ITLE %7D;%0A
-%09%09
+            
 int%5B%5D gr
@@ -2626,75 +2626,20 @@
  %7D;%0A
-%0A%09%09getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);%0A%0A%09%09
+            
 adap
@@ -2752,50 +2752,20 @@
 e);%0A
-%0A%09%09itemList.setOnScrollListener(this);%0A%0A%09%09
+            
 adap
@@ -2826,26 +2826,36 @@
 (), true));%0A
-%09%09
+            
 itemList.set
@@ -2876,370 +2876,66 @@
 r);%0A
-%09%09itemList.setOnItemClickListener(this);%0A%0A%09%09return v;%0A%09%7D%0A%0A%09public static ItemListFragment newInstance(DefaultFeedView defaultFeedView) %7B%0A%09%09ItemListFragment fragment = new SavedStoriesItemListFragment();%0A        Bundle args = new Bundle();%0A        args.putSerializable(%22defaultFeedView%22, defaultFeedView);%0A        fragment.setArguments(args);%0A%09%09return fragment;%0A%09
+       %7D%0A       super.onLoadFinished(loader, cursor);%0A    
 %7D%0A%0A%09

Move saved item list to in-time adapter creation.