<commit_before>/*
 * Copyright 2012 <NAME>
 *
 * 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.battlelancer.seriesguide.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.battlelancer.seriesguide.items.SearchResult;
import com.battlelancer.seriesguide.ui.dialogs.AddDialogFragment;
import com.battlelancer.seriesguide.util.ImageDownloader;
import com.battlelancer.seriesguide.util.TaskManager;
import com.uwetrottmann.androidutils.AndroidUtils;
import com.uwetrottmann.seriesguide.R;

import java.util.List;

/**
 * Super class for fragments displaying a list of shows and allowing to add them
 * to the database.
 */
public class AddFragment extends SherlockFragment {
    
    protected List<SearchResult> mSearchResults;

    protected AddAdapter mAdapter;

    protected GridView mGrid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // so we don't have to do a network op each config change
        setRetainInstance(true);
    }

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

        // basic setup of grid view
        mGrid = (GridView) getView().findViewById(android.R.id.list);
        View emptyView = getView().findViewById(android.R.id.empty);
        if (emptyView != null) {
            mGrid.setEmptyView(emptyView);
        }

        // restore an existing adapter
        if (mAdapter != null) {
            mGrid.setAdapter(mAdapter);
        }
    }

    @TargetApi(11)
    protected void setSearchResults(List<SearchResult> searchResults) {
        mSearchResults = searchResults;
        mAdapter.clear();
        if (AndroidUtils.isHoneycombOrHigher()) {
            mAdapter.addAll(searchResults);
        } else {
            for (SearchResult searchResult : searchResults) {
                mAdapter.add(searchResult);
            }
        }
        mGrid.setAdapter(mAdapter);
    }

    protected OnClickListener mAddButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // queue show to be added
            int position = mGrid.getPositionForView(v);
            SearchResult show = mAdapter.getItem(position);
            TaskManager.getInstance(getActivity()).performAddTask(show);

            show.isAdded = true;
            v.setVisibility(View.INVISIBLE);
        }
    };

    protected OnClickListener mDetailsButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // display more details in a dialog
            int position = mGrid.getPositionForView(v);
            SearchResult show = mAdapter.getItem(position);
            AddDialogFragment.showAddDialog(show, getFragmentManager());
        }
    };

    protected static class AddAdapter extends ArrayAdapter<SearchResult> {

        private LayoutInflater mLayoutInflater;

        private int mLayout;

        private ImageDownloader mImageDownloader;<commit_msg>Make sure there is a parent view before trying to access it. Ref #178.<commit_after>/*
 * Copyright 2012 <NAME>
 *
 * 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.battlelancer.seriesguide.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.battlelancer.seriesguide.items.SearchResult;
import com.battlelancer.seriesguide.ui.dialogs.AddDialogFragment;
import com.battlelancer.seriesguide.util.ImageDownloader;
import com.battlelancer.seriesguide.util.TaskManager;
import com.uwetrottmann.androidutils.AndroidUtils;
import com.uwetrottmann.seriesguide.R;

import java.util.List;

/**
 * Super class for fragments displaying a list of shows and allowing to add them
 * to the database.
 */
public class AddFragment extends SherlockFragment {

    protected List<SearchResult> mSearchResults;

    protected AddAdapter mAdapter;

    protected GridView mGrid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // so we don't have to do a network op each config change
        setRetainInstance(true);
    }

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

        // basic setup of grid view
        mGrid = (GridView) getView().findViewById(android.R.id.list);
        View emptyView = getView().findViewById(android.R.id.empty);
        if (emptyView != null) {
            mGrid.setEmptyView(emptyView);
        }

        // restore an existing adapter
        if (mAdapter != null) {
            mGrid.setAdapter(mAdapter);
        }
    }

    @TargetApi(11)
    protected void setSearchResults(List<SearchResult> searchResults) {
        mSearchResults = searchResults;
        mAdapter.clear();
        if (AndroidUtils.isHoneycombOrHigher()) {
            mAdapter.addAll(searchResults);
        } else {
            for (SearchResult searchResult : searchResults) {
                mAdapter.add(searchResult);
            }
        }
        mGrid.setAdapter(mAdapter);
    }

    protected OnClickListener mAddButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // queue show to be added
            if (v != null && v.getParent() != null) {
                int position = mGrid.getPositionForView(v);
                SearchResult show = mAdapter.getItem(position);
                TaskManager.getInstance(getActivity()).performAddTask(show);

                show.isAdded = true;
                v.setVisibility(View.INVISIBLE);
            }
        }
    };

    protected OnClickListener mDetailsButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // display more details in a dialog
            int position = mGrid.getPositionForView(v);
            SearchResult show = mAdapter.getItem(position);
            AddDialogFragment.showAddDialog(show, getFragmentManager());
        }
    };

    protected static class AddAdapter extends ArrayAdapter<SearchResult> {

        private LayoutInflater mLayoutInflater;

        private int mLayout;

        private ImageDownloader mImageDownloader;
{'fraction_non_alphanumeric': 0.08226349245917985, 'fraction_numerical': 0.0028667580705471767, 'mean_word_length': 2.9546574667323804, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6646857', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2034, 'n_words': 583}