package net.x4a42.volksempfaenger.ui;

import net.x4a42.volksempfaenger.R;
import net.x4a42.volksempfaenger.data.Columns.Episode;
import net.x4a42.volksempfaenger.data.EpisodeCursor;
import net.x4a42.volksempfaenger.data.VolksempfaengerContentProvider;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;

public class DownloadListFragment extends Fragment {

	private Adapter adapter;
	private ListView downloadList;

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

		Cursor cursor = getActivity().managedQuery(
				VolksempfaengerContentProvider.EPISODE_URI,
				new String[] { Episode._ID, Episode.TITLE, Episode.DOWNLOAD_ID,
						Episode.DOWNLOAD_STATUS, Episode.DOWNLOAD_DONE,
						Episode.DOWNLOAD_TOTAL },
				Episode.DOWNLOAD_ID + " != 0", null, null);
		EpisodeCursor episodeCursor = new EpisodeCursor(cursor);
		adapter = new Adapter(episodeCursor);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.download_list, container, false);
		downloadList = (ListView) view.findViewById(R.id.download_list);
		downloadList.setAdapter(adapter);
		return view;
	}

	private class Adapter extends SimpleCursorAdapter {
		public Adapter(Cursor cursor) {
			super(getActivity(), R.layout.download_list_row, cursor,
					new String[] { Episode.TITLE },
					new int[] { R.id.download_title });
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			super.bindView(view, context, cursor);
			EpisodeCursor episodeCursor = (EpisodeCursor) cursor;
			ProgressBar progressBar = (ProgressBar) view
					.findViewById(R.id.download_progress_bar);
			long done = episodeCursor.getDownloadDone();
			long total = episodeCursor.getDownloadTotal();
			if (done > Integer.MAX_VALUE || total > Integer.MAX_VALUE) {
				Log.wtf("DownloadListFragment",
						"Size > Integer.MAX_VALUE not yet supported"); // TODO
				return;
			}

			progressBar.setMax((int) total);
			progressBar.setProgress((int) done);

		}
	}

}

@@ -376,16 +376,205 @@
 agment;%0A
+import android.support.v4.app.LoaderManager;%0Aimport android.support.v4.content.Loader;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.widget.SimpleCursorAdapter;%0A
 import a
@@ -753,51 +753,8 @@
 Bar;
-%0Aimport android.widget.SimpleCursorAdapter;
 %0A%0Apu
@@ -801,16 +801,67 @@
 Fragment
+ implements%0A%09%09LoaderManager.LoaderCallbacks%3CCursor%3E
  %7B%0A%0A%09pri
@@ -1019,401 +1019,202 @@
 e);%0A
-%0A
 %09%09
-Cursor cursor = getActivity().managedQuery(%0A%09%09%09%09VolksempfaengerContentProvider.EPISODE_URI,%0A%09%09%09%09new String%5B%5D %7B Episode._ID, Episode.TITLE, Episode.DOWNLOAD_ID,%0A%09%09%09%09%09%09Episode.DOWNLOAD_STATUS, Episode.DOWNLOAD_DONE,%0A%09%09%09%09%09%09Episode.DOWNLOAD_TOTAL %7D,%0A%09%09%09%09Episode.DOWNLOAD_ID + %22 != 0%22, null, null);%0A%09%09EpisodeCursor episodeCursor = new EpisodeCursor(cursor);%0A%09%09adapter = new Adapter(episodeCursor
+adapter = new Adapter();%0A%09%7D%0A%0A%09@Override%0A%09public void onActivityCreated(Bundle savedInstanceState) %7B%0A%09%09super.onActivityCreated(savedInstanceState);%0A%09%09getLoaderManager().initLoader(0, null, this
 );%0A%09
@@ -1597,29 +1597,16 @@
 Adapter(
-Cursor cursor
 ) %7B%0A%09%09%09s
@@ -1653,22 +1653,20 @@
 st_row, 
-cursor
+null
 ,%0A%09%09%09%09%09n
@@ -1734,16 +1734,19 @@
 _title %7D
+, 0
 );%0A%09%09%7D%0A%0A
@@ -1900,17 +1900,20 @@
 ursor = 
-(
+new 
 EpisodeC
@@ -1921,16 +1921,16 @@
 rsor
-) 
+(
 cursor
+)
 ;%0A%09%09
@@ -2385,10 +2385,583 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@Override%0A%09public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A%09%09return new CursorLoader(getActivity(),%0A%09%09%09%09VolksempfaengerContentProvider.EPISODE_URI, new String%5B%5D %7B%0A%09%09%09%09%09%09Episode._ID, Episode.TITLE, Episode.DOWNLOAD_ID,%0A%09%09%09%09%09%09Episode.DOWNLOAD_STATUS, Episode.DOWNLOAD_DONE,%0A%09%09%09%09%09%09Episode.DOWNLOAD_TOTAL %7D,%0A%09%09%09%09Episode.DOWNLOAD_ID + %22 != 0%22, null, null);%0A%09%7D%0A%0A%09@Override%0A%09public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A%09%09adapter.swapCursor(data);%0A%09%7D%0A%0A%09@Override%0A%09public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A%09%09adapter.swapCursor(null);%0A%09%7D%0A%0A
 %7D%0A

Make DownloadListFragment use LoaderManager