
package com.battlelancer.seriesguide.ui.lists;

import android.database.Cursor;
import android.database.DataSetObserver;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.Lists;

/**
 * Returns {@link ListsFragment}s for every list in the database, makes sure there is always at
 * least one.
 */
public class ListsPagerAdapter extends MultiPagerAdapter {

    private final ListsDataSetObserver dataSetObserver;
    @Nullable private Cursor cursorLists;
    private boolean dataValid;

    public ListsPagerAdapter(FragmentManager fm) {
        super(fm);
        this.dataSetObserver = new ListsDataSetObserver();
    }

    public void swapCursor(@Nullable Cursor cursorNew) {
        if (cursorLists == cursorNew) {
            return;
        }
        Cursor oldCursor = cursorLists;
        if (oldCursor != null) {
            oldCursor.unregisterDataSetObserver(dataSetObserver);
        }
        cursorLists = cursorNew;

        boolean cursorPresent = cursorNew != null;
        dataValid = cursorPresent;
        if (cursorPresent) {
            cursorNew.registerDataSetObserver(dataSetObserver);
            notifyDataSetChanged();
        }
    }

    @Override
    public Fragment getItem(int position) {
        if (cursorLists == null || !dataValid) {
            return null;
        }
        cursorLists.moveToPosition(position);
        return ListsFragment.newInstance(cursorLists.getString(0));
    }

    @Override
    public int getCount() {
        if (cursorLists == null || !dataValid) {
            return 0;
        }
        return cursorLists.getCount();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (cursorLists == null || !dataValid) {
            return "";
        }
        cursorLists.moveToPosition(position);
        return cursorLists.getString(ListsQuery.NAME);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Nullable
    public String getListId(int position) {
        if (cursorLists == null || !dataValid) {
            return null;
        }
        cursorLists.moveToPosition(position);
        return cursorLists.getString(ListsQuery.ID);
    }

    private class ListsDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            dataValid = true;
            notifyDataSetChanged();
        }

        @Override
        public void onInvalidated() {
            dataValid = false;
        }
    }

    public interface ListsQuery {
        String[] PROJECTION = new String[] {
                Lists.LIST_ID,
                Lists.NAME
        };

        int ID = 0;
        int NAME = 1;
    }
}

@@ -111,24 +111,67 @@
 etObserver;%0A
+import android.support.annotation.NonNull;%0A
 import andro
@@ -2082,16 +2082,25 @@
 osition(
+@NonNull 
 Object o
@@ -2223,36 +2223,35 @@
 cursorLists 
-=
+!
 = null 
-%7C%7C !
+&& 
 dataValid) %7B
@@ -2251,54 +2251,11 @@
 alid
-) %7B%0A            return null;%0A        %7D%0A       
+ &&
  cur
@@ -2279,34 +2279,40 @@
 sition(position)
-;%0A
+) %7B%0A    
         return c
@@ -2348,16 +2348,68 @@
 ry.ID);%0A
+        %7D else %7B%0A            return null;%0A        %7D%0A
     %7D%0A%0A 

check if moved before reading value.