package au.id.dsp.bikeairsensorlogger.activity;


import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.IOException;

import au.id.dsp.bikeairsensorlogger.R;
import au.id.dsp.bikeairsensorlogger.bluetooth.CaptureProvider;
import au.id.dsp.bikeairsensorlogger.bluetooth.LogDatabase;

public class CaptureListFragment extends ListFragment {

    private LogDatabase logDatabase;
    private SimpleCursorAdapter adapter;
    private ListView view;

    public CaptureListFragment() {
    }

    private final LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            return new CursorLoader(getActivity(), CaptureProvider.CONTENT_URI,
                    null, // return all columns
                    null, null, // all rows
                    BaseColumns._ID);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            adapter.swapCursor(data);

            // The list should now be shown.
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            adapter.swapCursor(null);
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(0, null, loaderCallbacks);
    }

    @Override
    public void onAttach(Activity activity) {
        logDatabase = new LogDatabase(activity);
        super.onAttach(activity);
        adapter = new SimpleCursorAdapter(activity,
                R.layout.fragment_capture_list_row,
                logDatabase.getCaptures().createCursor(),
                new String[] { "NAME", "START" },
                new int[] { R.id.nameView, R.id.startTimeView },
                0) {
            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                super.bindView(view, context, cursor);
                // TODO: Count and status
            }
        };
        setListAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            logDatabase.close();
        } catch (IOException e) {
        }
    }
}

@@ -561,35 +561,154 @@
 ew;%0A
-%0Aimport java.io.IOException
+import android.widget.TextView;%0A%0Aimport java.io.IOException;%0Aimport java.text.DateFormat;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date
 ;%0A%0Ai
@@ -928,16 +928,437 @@
 ment %7B%0A%0A
+    private static final DateFormat dateFormat;%0A    static %7B%0A        DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);%0A        if (f instanceof SimpleDateFormat)%0A            // Add the time zone%0A            f = new SimpleDateFormat(((SimpleDateFormat) f).toPattern() + %22 z%22);%0A        else%0A            f = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ss.SSSZ%22);%0A        dateFormat = f;%0A    %7D%0A%0A
     priv
@@ -2728,32 +2728,105 @@
 ttach(activity);
+%0A%0A        final Cursor cursor = logDatabase.getCaptures().createCursor();
 %0A        adapter
@@ -2934,48 +2934,14 @@
     
-logDatabase.getCaptures().createC
+c
 ursor
-()
 ,%0A  
@@ -3075,110 +3075,259 @@
   0)
- %7B%0A            @Override%0A            public void bindView(View view, Context context, Cursor cursor) %7B
+;%0A        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() %7B%0A            @Override%0A            public boolean setViewValue(View view, Cursor cursor, int columnIndex) %7B%0A                if (cursor.getColumnIndexOrThrow(%22START%22) == columnIndex)
 %0A   
@@ -3343,88 +3343,180 @@
     
-super.bind
+    ((Text
 View
-(
+) 
 view
-, context, cursor);%0A                // TODO: Count and status
+).setText(dateFormat.format(new Date(cursor.getLong(columnIndex))));%0A                else%0A                    return false;%0A                return true;
 %0A   
@@ -3535,16 +3535,17 @@
        %7D
+)
 ;%0A      

Format the start time of each capture