<commit_before>package com.gelakinetic.mtgfam.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gelakinetic.mtgfam.FamiliarActivity;
import com.gelakinetic.mtgfam.R;
import com.gelakinetic.mtgfam.helpers.AutocompleteCursorAdapter;
import com.gelakinetic.mtgfam.helpers.MtgCard;
import com.gelakinetic.mtgfam.helpers.PriceFetchRequest;
import com.gelakinetic.mtgfam.helpers.PriceInfo;
import com.gelakinetic.mtgfam.helpers.SafeAutoCompleteTextView;
import com.gelakinetic.mtgfam.helpers.database.CardDbAdapter;
import com.gelakinetic.mtgfam.helpers.database.DatabaseManager;
import com.gelakinetic.mtgfam.helpers.database.FamiliarDbException;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * This class manages trades between two users. Trades can be saved and loaded
 */
public class TradeFragment extends FamiliarFragment {

	/* Price Constants */
	private static final int LOW_PRICE = 0;
	private static final int AVG_PRICE = 1;
	private static final int HIGH_PRICE = 2;
	private static final int FOIL_PRICE = 3;

	/* Side Constants */
	private static final int LEFT = 0;
	private static final int RIGHT = 1;
	private static final int BOTH = 2;

	/* Dialog Constants */
	private static final int DIALOG_UPDATE_CARD = 1;
	private static final int DIALOG_PRICE_SETTING = 2;
	private static final int DIALOG_SAVE_TRADE = 3;
	private static final int DIALOG_LOAD_TRADE = 4;
	private static final int DIALOG_DELETE_TRADE = 5;
	private static final int DIALOG_CONFIRMATION = 6;
	private static final int DIALOG_CHANGE_SET = 7;

	/* Save file constants */
	private static final String AUTOSAVE_NAME = "autosave";
	private static final String TRADE_EXTENSION = ".trade";

	/* Trade information */
	private TextView mTotalPriceLeft;
	private TradeListAdapter mLeftAdapter;
	private ArrayList<MtgCard> mLeftList;
	private TextView mTotalPriceRight;
	private TradeListAdapter mRightAdapter;
	private ArrayList<MtgCard> mRightList;

	/* UI Elements */
	private SafeAutoCompleteTextView mNameEditText;
	private EditText mNumberEditText;
	private CheckBox mCheckboxFoil;
	private int mPriceFetchRequests = 0;

	/* Settings */
	private int mPriceSetting;
	private String mCurrentTrade = "";

	/**
	 * Initialize the view and set up the button actions
	 *
	 * @param inflater           The LayoutInflater object that can be used to inflate any views in the fragment,
	 * @param container          If non-null, this is the parent view that the fragment's UI should be attached to. The
	 *                           fragment should not add the view itself, but this can be used to generate the
	 *                           LayoutParams of the view.
	 * @param savedInstanceState If non-null, this fragment is being re-constructed from a previous saved state as given
	 *                           here.
	 * @return The inflated view
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		/* Inflate the view, pull out UI elements */
		View myFragmentView = inflater.inflate(R.layout.trader_frag, container, false);
		assert myFragmentView != null;
		mNameEditText = (SafeAutoCompleteTextView) myFragmentView.findViewById(R.id.namesearch);
		mNumberEditText = (EditText) myFragmentView.findViewById(R.id.numberInput);
		mCheckboxFoil = (CheckBox) myFragmentView.findViewById(R.id.trader_foil);
		mTotalPriceRight = (TextView) myFragmentView.findViewById(R.id.priceTextRight);
		mTotalPriceLeft = (TextView) myFragmentView.findViewById(R.id.priceTextLeft);

		/* Set the autocomplete adapter, default number */
		mNameEditText.setAdapter(new AutocompleteCursorAdapter(this, new String[]{CardDbAdapter.KEY_NAME},
				new int[]{R.id.text1}, mNameEditText));
		mNumberEditText.setText("1");

		/* Initialize the left list */
		mLeftList = new ArrayList<MtgCard>();
		mLeftAdapter = new TradeListAdapter(this.getActivity(), mLeftList);
		ListView lvTradeLeft = (ListView) myFragmentView.findViewById(R.id.tradeListLeft);
		lvTradeLeft.setAdapter(mLeftAdapter);
		lvTradeLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				showDialog(DIALOG_UPDATE_CARD, LEFT, arg2);
			}
		});

		/* Initialize the right list */
		mRightList = new ArrayList<MtgCard>();
		mRightAdapter = new TradeListAdapter(this.getActivity(), mRightList);
		ListView lvTradeRight = (ListView) myFragmentView.findViewById(R.id.tradeListRight);
		lvTradeRight.setAdapter(mRightAdapter);
		lvTradeRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				showDialog(DIALOG_UPDATE_CARD, RIGHT, arg2);
			}
		});

		/* Set the buttons to add cards to the left or right */
		myFragmentView.findViewById(R.id.addCardLeft).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				addCardToTrade(LEFT);
			}
		});
		myFragmentView.findViewById(R.id.addCardRight).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				addCardToTrade(RIGHT);
			}
		});

		/* Return the view */
		return myFragmentView;
	}

	/**
	 * This helper method adds a card to a side of the wishlist from the user input
	 *
	 * @param side RIGHT or LEFT, depending on which side to add the card to
	 */<commit_msg>long click removal for trades<commit_after>package com.gelakinetic.mtgfam.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gelakinetic.mtgfam.FamiliarActivity;
import com.gelakinetic.mtgfam.R;
import com.gelakinetic.mtgfam.helpers.AutocompleteCursorAdapter;
import com.gelakinetic.mtgfam.helpers.MtgCard;
import com.gelakinetic.mtgfam.helpers.PriceFetchRequest;
import com.gelakinetic.mtgfam.helpers.PriceInfo;
import com.gelakinetic.mtgfam.helpers.SafeAutoCompleteTextView;
import com.gelakinetic.mtgfam.helpers.database.CardDbAdapter;
import com.gelakinetic.mtgfam.helpers.database.DatabaseManager;
import com.gelakinetic.mtgfam.helpers.database.FamiliarDbException;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * This class manages trades between two users. Trades can be saved and loaded
 */
public class TradeFragment extends FamiliarFragment {

	/* Price Constants */
	private static final int LOW_PRICE = 0;
	private static final int AVG_PRICE = 1;
	private static final int HIGH_PRICE = 2;
	private static final int FOIL_PRICE = 3;

	/* Side Constants */
	private static final int LEFT = 0;
	private static final int RIGHT = 1;
	private static final int BOTH = 2;

	/* Dialog Constants */
	private static final int DIALOG_UPDATE_CARD = 1;
	private static final int DIALOG_PRICE_SETTING = 2;
	private static final int DIALOG_SAVE_TRADE = 3;
	private static final int DIALOG_LOAD_TRADE = 4;
	private static final int DIALOG_DELETE_TRADE = 5;
	private static final int DIALOG_CONFIRMATION = 6;
	private static final int DIALOG_CHANGE_SET = 7;

	/* Save file constants */
	private static final String AUTOSAVE_NAME = "autosave";
	private static final String TRADE_EXTENSION = ".trade";

	/* Trade information */
	private TextView mTotalPriceLeft;
	private TradeListAdapter mLeftAdapter;
	private ArrayList<MtgCard> mLeftList;
	private TextView mTotalPriceRight;
	private TradeListAdapter mRightAdapter;
	private ArrayList<MtgCard> mRightList;

	/* UI Elements */
	private SafeAutoCompleteTextView mNameEditText;
	private EditText mNumberEditText;
	private CheckBox mCheckboxFoil;
	private int mPriceFetchRequests = 0;

	/* Settings */
	private int mPriceSetting;
	private String mCurrentTrade = "";

	/**
	 * Initialize the view and set up the button actions
	 *
	 * @param inflater           The LayoutInflater object that can be used to inflate any views in the fragment,
	 * @param container          If non-null, this is the parent view that the fragment's UI should be attached to. The
	 *                           fragment should not add the view itself, but this can be used to generate the
	 *                           LayoutParams of the view.
	 * @param savedInstanceState If non-null, this fragment is being re-constructed from a previous saved state as given
	 *                           here.
	 * @return The inflated view
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		/* Inflate the view, pull out UI elements */
		View myFragmentView = inflater.inflate(R.layout.trader_frag, container, false);
		assert myFragmentView != null;
		mNameEditText = (SafeAutoCompleteTextView) myFragmentView.findViewById(R.id.namesearch);
		mNumberEditText = (EditText) myFragmentView.findViewById(R.id.numberInput);
		mCheckboxFoil = (CheckBox) myFragmentView.findViewById(R.id.trader_foil);
		mTotalPriceRight = (TextView) myFragmentView.findViewById(R.id.priceTextRight);
		mTotalPriceLeft = (TextView) myFragmentView.findViewById(R.id.priceTextLeft);

		/* Set the autocomplete adapter, default number */
		mNameEditText.setAdapter(new AutocompleteCursorAdapter(this, new String[]{CardDbAdapter.KEY_NAME},
				new int[]{R.id.text1}, mNameEditText));
		mNumberEditText.setText("1");

		/* Initialize the left list */
		mLeftList = new ArrayList<MtgCard>();
		mLeftAdapter = new TradeListAdapter(this.getActivity(), mLeftList);
		ListView lvTradeLeft = (ListView) myFragmentView.findViewById(R.id.tradeListLeft);
		lvTradeLeft.setAdapter(mLeftAdapter);
		lvTradeLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				showDialog(DIALOG_UPDATE_CARD, LEFT, arg2);
			}
		});

		/* Initialize the right list */
		mRightList = new ArrayList<MtgCard>();
		mRightAdapter = new TradeListAdapter(this.getActivity(), mRightList);
		ListView lvTradeRight = (ListView) myFragmentView.findViewById(R.id.tradeListRight);
		lvTradeRight.setAdapter(mRightAdapter);
		lvTradeRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				showDialog(DIALOG_UPDATE_CARD, RIGHT, arg2);
			}
		});

		/* Set the buttons to add cards to the left or right */
		myFragmentView.findViewById(R.id.addCardLeft).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				addCardToTrade(LEFT);
			}
		});
		myFragmentView.findViewById(R.id.addCardRight).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				addCardToTrade(RIGHT);
			}
		});

        lvTradeLeft.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
                /* Remove the card */
                mLeftList.remove(position);
                /* Redraw this side */
                mLeftAdapter.notifyDataSetChanged();
                return true;
            }
        });

        lvTradeRight.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                /* Remove the card */
                mRightList.remove(position);
                /* Redraw this side */
                mRightAdapter.notifyDataSetChanged();
                return true;
            }
        });

		/* Return the view */
		return myFragmentView;
	}

	/**
	 * This helper method adds a card to a side of the wishlist from the user input
	 *
	 * @param side RIGHT or LEFT, depending on which side to add the card to
	 */
{'fraction_non_alphanumeric': 0.08693157366231531, 'fraction_numerical': 0.003873188925548702, 'mean_word_length': 4.3938104448742745, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2772370', 'n_tokens_mistral': 4359, 'n_tokens_neox': 4013, 'n_words': 1080}