/*
 * Copyright (C) 2012 Antew | antewcode@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.antew.redditinpictures.library.adapter;

import android.database.Cursor;
import android.support.v4.app.FixedFragmentStatePagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.antew.redditinpictures.library.reddit.RedditApi.PostData;
import com.antew.redditinpictures.library.ui.ImageDetailFragment;

/**
 * The main adapter that backs the ViewPager. A subclass of FragmentStatePagerAdapter as there
 * could be a large number of items in the ViewPager and we don't want to retain them all in
 * memory at once but create/destroy them on the fly.
 */
public class CursorPagerAdapter extends FixedFragmentStatePagerAdapter {
    private Cursor mCursor;
    
    public CursorPagerAdapter(FragmentManager fm, Cursor cursor) {
        super(fm);
        this.mCursor = cursor;
    }

    @Override
    public int getCount() {
        if (mCursor == null)
            return 0;
        
        return mCursor.getCount();
    }

    public void swapCursor(Cursor newCursor) {
        if (mCursor == newCursor)
            return;
        
        mCursor = newCursor;
        notifyDataSetChanged();
    }
    @Override
    public Fragment getItem(int position) {
        if (mCursor != null && mCursor.moveToPosition(position)) {
            return getImageDetailFragment(new PostData(mCursor));
        }
        
        return null;
    }
    

    /**
     * The PostData at the input position
     * @param position The position
     * @return PostData at the input position
     */
    public PostData getPost(int position) {
        if (mCursor != null && mCursor.moveToPosition(position)) {
            return new PostData(mCursor);
        }
        
        return null;
    }
    
    /**
     * Returns an {@link ImageDetailFragment} for the input {@link PostData} object
     * @param p The {@link PostData} object to pass to the new {@link ImageDetailFragment}
     * @return A new {@link ImageDetailFragment} for the input {@link PostData}
     */
    public Fragment getImageDetailFragment(PostData p) {
        return ImageDetailFragment.newInstance(p);
    }
    
}

@@ -1082,17 +1082,17 @@
 here
-%0A *
  could
+%0A *
  be 
@@ -1171,19 +1171,16 @@
 m all in
-%0A *
  memory 
@@ -1181,16 +1181,19 @@
 emory at
+%0A *
  once bu
@@ -1303,16 +1303,95 @@
 apter %7B%0A
+    public static final String TAG = CursorPagerAdapter.class.getSimpleName();%0A
     priv
@@ -1401,16 +1401,28 @@
  Cursor 
+            
 mCursor;
@@ -1422,20 +1422,16 @@
 Cursor;%0A
-    
 %0A    pub
@@ -1640,24 +1640,16 @@
 turn 0;%0A
-        
 %0A       
@@ -1784,24 +1784,16 @@
 return;%0A
-        
 %0A       
@@ -1848,24 +1848,25 @@
 ed();%0A    %7D%0A
+%0A
     @Overrid
@@ -2050,32 +2050,24 @@
 ;%0A        %7D%0A
-        
 %0A        ret
@@ -2081,21 +2081,16 @@
 l;%0A    %7D
-%0A    
 %0A%0A    /*
@@ -2125,32 +2125,40 @@
  input position%0A
+     * %0A
      * @param po
@@ -2163,16 +2163,34 @@
 position
+%0A     *           
  The pos
@@ -2412,24 +2412,16 @@
       %7D%0A
-        
 %0A       
@@ -2440,20 +2440,16 @@
 ;%0A    %7D%0A
-    
 %0A    /**
@@ -2540,24 +2540,50 @@
 %0A     * 
-@param p
+%0A     * @param p%0A     *           
  The %7B@l
@@ -2852,15 +2852,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

Format code, add TAG.