package com.newsblur.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.content.CursorLoader;
import android.content.Loader;
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.FeedReading;
import com.newsblur.activity.FolderReading;
import com.newsblur.activity.ItemsList;
import com.newsblur.activity.Reading;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.database.MultipleFeedItemsAdapter;
import com.newsblur.util.DefaultFeedView;
import com.newsblur.util.StateFilter;
import com.newsblur.util.StoryOrder;
import com.newsblur.view.FeedItemViewBinder;

public class FolderItemListFragment extends ItemListFragment implements OnItemClickListener {

	private String folderName;
	
	public static FolderItemListFragment newInstance(String folderName, StateFilter currentState, DefaultFeedView defaultFeedView) {
		FolderItemListFragment feedItemFragment = new FolderItemListFragment();

		Bundle args = new Bundle();
		args.putSerializable("currentState", currentState);
		args.putString("folderName", folderName);
        args.putSerializable("defaultFeedView", defaultFeedView);
		feedItemFragment.setArguments(args);

		return feedItemFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		folderName = getArguments().getString("folderName");
	}

	@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);
		getActivity().startManagingCursor(cursor);

		String[] groupFrom = new String[] { DatabaseConstants.STORY_TITLE, DatabaseConstants.STORY_SHORT_CONTENT, DatabaseConstants.FEED_TITLE, DatabaseConstants.STORY_TIMESTAMP, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS, DatabaseConstants.STORY_AUTHORS };
		int[] groupTo = new int[] { R.id.row_item_title, R.id.row_item_content, R.id.row_item_feedtitle, R.id.row_item_date, R.id.row_item_sidebar, R.id.row_item_author };

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

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

		itemList.setOnScrollListener(this);

		adapter.setViewBinder(new FeedItemViewBinder(getActivity()));
		itemList.setAdapter(adapter);
		itemList.setOnItemClickListener(this);
		itemList.setOnCreateContextMenuListener(this);

		return v;
	}

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

}

@@ -147,45 +147,8 @@
 le;%0A
-import android.content.CursorLoader;%0A
 impo
@@ -966,16 +966,47 @@
 erName;%0A
+    private ListView itemList;%0A
 %09%0A%09publi
@@ -1805,25 +1805,16 @@
 ull);%0A%09%09
-ListView 
 itemList
@@ -1914,17 +1914,16 @@
 mList);%0A
-%0A
 %09%09itemLi
@@ -1976,136 +1976,579 @@
 ));%0A
-%0A%09%09Cursor cursor = dbHelper.getStoriesCursor(getFeedSet(), currentState, null
+%09%09itemList.setOnItemClickListener(this);%0A%09%09itemList.setOnCreateContextMenuListener(this);%0A%09%09itemList.setOnScrollListener(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%09
-getActivity().startManagingCursor(cursor);%0A%0A%09%09
+return v;%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
@@ -2795,18 +2795,28 @@
 HORS %7D;%0A
-%09%09
+            
 int%5B%5D gr
@@ -2975,75 +2975,20 @@
  %7D;%0A
-%0A%09%09getLoaderManager().initLoader(ITEMLIST_LOADER , null, this);%0A%0A%09%09
+            
 adap
@@ -3095,50 +3095,20 @@
 o);%0A
-%0A%09%09itemList.setOnScrollListener(this);%0A%0A%09%09
+            
 adap
@@ -3161,26 +3161,36 @@
 tivity()));%0A
-%09%09
+            
 itemList.set
@@ -3211,112 +3211,66 @@
 r);%0A
-%09%09itemList.setOnItemClickListener(this);%0A%09%09itemList.setOnCreateContextMenuListener(this);%0A%0A%09%09return v;%0A%09
+       %7D%0A       super.onLoadFinished(loader, cursor);%0A    
 %7D%0A%0A%09

Move folder item list to in-time adapter creation.