package com.github.mobile.android.ui.issue;

import static com.github.mobile.android.util.GitHubIntents.EXTRA_ISSUE_NUMBER;
import static com.github.mobile.android.util.GitHubIntents.EXTRA_REPOSITORY_NAME;
import static com.github.mobile.android.util.GitHubIntents.EXTRA_REPOSITORY_OWNER;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.egit.github.core.Issue;
import org.eclipse.egit.github.core.RepositoryId;

/**
 * Adapter to page through an {@link Issue} array
 */
public class IssuesPagerAdapter extends FragmentPagerAdapter {

    private final RepositoryId[] repos;

    private final Integer[] issues;

    private final Map<Integer, IssueFragment> fragments = new HashMap<Integer, IssueFragment>();

    /**
     * @param fm
     * @param repoIds
     * @param issueNumbers
     */
    public IssuesPagerAdapter(FragmentManager fm, RepositoryId[] repoIds, Integer[] issueNumbers) {
        super(fm);

        repos = repoIds;
        issues = issueNumbers;
    }

    @Override
    public Fragment getItem(int position) {
        IssueFragment fragment = new IssueFragment();
        Bundle args = new Bundle();
        RepositoryId repo = repos[position];
        args.putString(EXTRA_REPOSITORY_NAME, repo.getName());
        args.putString(EXTRA_REPOSITORY_OWNER, repo.getOwner());
        args.putInt(EXTRA_ISSUE_NUMBER, issues[position]);
        fragment.setArguments(args);
        fragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);

        fragments.remove(position);
    }

    @Override
    public int getCount() {
        return issues.length;
    }

    /**
     * Deliver dialog result to fragment at given position
     *
     * @param position
     * @param requestCode
     * @param resultCode
     * @param arguments
     * @return this adapter
     */
    public IssuesPagerAdapter onDialogResult(int position, int requestCode, int resultCode, Bundle arguments) {
        IssueFragment fragment = fragments.get(position);
        if (fragment != null)
            fragment.onDialogResult(requestCode, resultCode, arguments);
        return this;
    }
}

@@ -1606,51 +1606,8 @@
 s);%0A
-        fragments.put(position, fragment);%0A
     
@@ -1627,16 +1627,16 @@
 agment;%0A
+
     %7D%0A%0A 
@@ -1781,24 +1781,24 @@
 , object);%0A%0A
-
         frag
@@ -1820,32 +1820,329 @@
 sition);%0A    %7D%0A%0A
+    @Override%0A    public Object instantiateItem(ViewGroup container, int position) %7B%0A        Object fragment = super.instantiateItem(container, position);%0A        if (fragment instanceof IssueFragment)%0A            fragments.put(position, (IssueFragment) fragment);%0A        return fragment;%0A    %7D%0A%0A
     @Override%0A  

Index fragments from instantiateItem instead of getItem