/*
 * 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 static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.github.mobile.Intents.Builder;
import com.github.mobile.R.drawable;
import com.github.mobile.R.layout;
import com.github.mobile.R.string;
import com.github.mobile.RequestFuture;
import com.github.mobile.core.issue.IssueFilter;
import com.github.mobile.persistence.AccountDataManager;
import com.github.mobile.ui.ConfirmDialogFragment;
import com.github.mobile.ui.DialogFragmentActivity;
import com.github.mobile.ui.ItemListFragment;
import com.github.mobile.ui.user.HomeActivity;
import com.google.inject.Inject;

/**
 * Activity to display a list of saved {@link IssueFilter} objects
 */
public class FiltersViewActivity extends DialogFragmentActivity implements
        OnItemLongClickListener {

    /**
     * Create intent to browse issue filters
     *
     * @return intent
     */
    public static Intent createIntent() {
        return new Builder("repo.issues.filters.VIEW").toIntent();
    }

    private static final String ARG_FILTER = "filter";

    private static final int REQUEST_DELETE = 1;

    @Inject
    private AccountDataManager cache;

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

        setContentView(layout.issues_filter_list);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(string.bookmarks);
        actionBar.setIcon(drawable.action_bookmark);
        actionBar.setDisplayHomeAsUpEnabled(true);

        ItemListFragment<?> listFragment = (ItemListFragment<?>) getSupportFragmentManager()
                .findFragmentById(android.R.id.list);
        listFragment.getListView().setOnItemLongClickListener(this);
    }

    @Override
    public void onDialogResult(int requestCode, int resultCode, Bundle arguments) {
        if (requestCode == REQUEST_DELETE && resultCode == RESULT_OK) {
            IssueFilter filter = (IssueFilter) arguments
                    .getSerializable(ARG_FILTER);
            cache.removeIssueFilter(filter, new RequestFuture<IssueFilter>() {

                @Override
                public void success(IssueFilter response) {
                    FilterListFragment fragment = (FilterListFragment) getSupportFragmentManager()
                            .findFragmentById(android.R.id.list);
                    if (fragment != null)
                        fragment.refresh();
                }
            });
            return;
        }

        super.onDialogResult(requestCode, resultCode, arguments);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, HomeActivity.class);
            intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
        IssueFilter filter = (IssueFilter) parent.getItemAtPosition(position);
        Bundle args = new Bundle();
        args.putSerializable(ARG_FILTER, filter);
        ConfirmDialogFragment.show(this, REQUEST_DELETE,
                getString(string.confirm_bookmark_delete_title),
                getString(string.confirm_bookmark_delete_message), args);
        return true;
    }
}

@@ -1415,54 +1415,8 @@
 ty;%0A
-import com.github.mobile.ui.ItemListFragment;%0A
 impo
@@ -2040,16 +2040,58 @@
 cache;%0A%0A
+    private FilterListFragment fragment;%0A%0A
     @Ove
@@ -2466,33 +2466,9 @@
     
-ItemListFragment%3C?%3E listF
+f
 ragm
@@ -2474,20 +2474,22 @@
 ment = (
-Item
+Filter
 ListFrag
@@ -2492,19 +2492,16 @@
 Fragment
-%3C?%3E
 ) getSup
@@ -2588,13 +2588,9 @@
     
-listF
+f
 ragm
@@ -3095,173 +3095,8 @@
 ) %7B%0A
-                    FilterListFragment fragment = (FilterListFragment) getSupportFragmentManager()%0A                            .findFragmentById(android.R.id.list);%0A
     

Store fragment in instance variable