/*
 * Copyright 2012 GitHub Inc.
 *
 * 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.github.mobile.ui.issue;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.github.mobile.R.string;
import com.github.mobile.core.ResourcePager;
import com.github.mobile.core.issue.IssueStore;
import com.github.mobile.ui.ItemListAdapter;
import com.github.mobile.ui.ItemView;
import com.github.mobile.ui.PagedItemFragment;
import com.github.mobile.util.AvatarLoader;
import com.google.inject.Inject;

import java.util.List;
import java.util.Map;

import org.eclipse.egit.github.core.RepositoryIssue;
import org.eclipse.egit.github.core.client.PageIterator;
import org.eclipse.egit.github.core.service.IssueService;

/**
 * Fragment to display a pageable list of dashboard issues
 */
public class DashboardIssueFragment extends PagedItemFragment<RepositoryIssue> {

    /**
     * Filter data argument
     */
    public static final String ARG_FILTER = "filter";

    @Inject
    private IssueService service;

    @Inject
    private IssueStore store;

    @Inject
    private AvatarLoader avatarHelper;

    private Map<String, String> filterData;

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

        setHasOptionsMenu(true);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        filterData = (Map<String, String>) getArguments().getSerializable(ARG_FILTER);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        startActivity(ViewIssuesActivity.createIntent(items, position - getListAdapter().getHeadersCount()));
    }

    @Override
    protected ResourcePager<RepositoryIssue> createPager() {
        return new ResourcePager<RepositoryIssue>() {

            @Override
            protected RepositoryIssue register(RepositoryIssue resource) {
                return store.addIssue(resource);
            }

            @Override
            protected Object getId(RepositoryIssue resource) {
                return resource.getId();
            }

            @Override
            public PageIterator<RepositoryIssue> createIterator(int page, int size) {
                return service.pageIssues(filterData, page, size);
            }
        };
    }

    @Override
    protected int getLoadingMessage() {
        return string.loading_issues;
    }

    @Override
    protected int getErrorMessage(Exception exception) {
        return string.error_issues_load;
    }

    @Override
    protected ItemListAdapter<RepositoryIssue, ? extends ItemView> createAdapter(List<RepositoryIssue> items) {
        return new DashboardIssueListAdapter(avatarHelper, getActivity().getLayoutInflater(),
                items.toArray(new RepositoryIssue[items.size()]));
    }
}

@@ -622,16 +622,104 @@
 issue;%0A%0A
+import static com.github.mobile.RequestCodes.ISSUE_VIEW;%0Aimport android.content.Intent;%0A
 import a
@@ -2186,32 +2186,370 @@
 FILTER);%0A    %7D%0A%0A
+    @Override%0A    public void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        if (requestCode == ISSUE_VIEW) %7B%0A            getListAdapter().getWrappedAdapter().notifyDataSetChanged();%0A            forceRefresh();%0A            return;%0A        %7D%0A%0A        super.onActivityResult(requestCode, resultCode, data);%0A    %7D%0A%0A
     @Override%0A  
@@ -2644,16 +2644,25 @@
 Activity
+ForResult
 (ViewIss
@@ -2739,16 +2739,44 @@
 Count())
+,%0A                ISSUE_VIEW
 );%0A    %7D

Update list when view activity completes