<commit_before>package com.ryanpconnors.artthief.compare;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.ryanpconnors.artthief.R;
import com.ryanpconnors.artthief.artgallery.ArtWork;
import com.ryanpconnors.artthief.artgallery.Gallery;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.ryanpconnors.artthief.compare.SortArtWorkFragment.ArtworkChoice.ALPHA;
import static com.ryanpconnors.artthief.compare.SortArtWorkFragment.ArtworkChoice.BETA;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SortArtWorkFragment.OnSortArtworkFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SortArtWorkFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SortArtWorkFragment extends Fragment {

    private static final String TAG = "SortArtWorkFragment";
    private static final String ARG_NUMBER_OF_STARS = "number_of_stars";

    private List<ArtWork> mArtWorks;

    private int mCurrentIndex = 0;

    private ImageView mArtworkImageViewAlpha;
    private ImageView mArtworkImageViewBeta;

    private ShareActionProvider mShareActionProvider;
    private OnSortArtworkFragmentInteractionListener mListener;

    /**
     * ArtworkChoice enumerated type used for identifying the user's choice
     * of which artwork they prefer for sorting purposes
     */
    protected enum ArtworkChoice {
        ALPHA,
        BETA
    }

    public SortArtWorkFragment() {
        // Required empty public constructor
    }

    public static SortArtWorkFragment newInstance(int numberOfStars) {
        SortArtWorkFragment fragment = new SortArtWorkFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_NUMBER_OF_STARS, numberOfStars);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mArtWorks = Gallery.get(getActivity()).getArtWorks(getArguments().getInt(ARG_NUMBER_OF_STARS));
            setHasOptionsMenu(true);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_sort_art_work, container, false);

        // Add toolbar and up button navigation
        Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
        ((SortArtWorkActivity) getActivity()).setSupportActionBar(toolbar);
        ActionBar actionBar = ((SortArtWorkActivity) getActivity()).getSupportActionBar();

        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavUtils.navigateUpFromSameTask(getActivity());
            }
        });

        // Initialize ArtWork Image Views
        mArtworkImageViewAlpha = (ImageView) v.findViewById(R.id.artwork_large_image_view_alpha);
        mArtworkImageViewAlpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sortArtwork(ALPHA);
            }
        });
        mArtworkImageViewBeta = (ImageView) v.findViewById(R.id.artwork_large_image_view_beta);
        mArtworkImageViewBeta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sortArtwork(BETA);
            }
        });
        displayArtwork(mArtworkImageViewAlpha, mCurrentIndex);
        displayArtwork(mArtworkImageViewBeta, mCurrentIndex + 1);

        return v;
    }

    /**
     * @param choice
     */
    private void sortArtwork(ArtworkChoice choice) {
        if (choice.equals(ALPHA)) {
            if (mCurrentIndex >= mArtWorks.size() - 2) {
                Toast.makeText(getActivity(), "EXIT", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
            mCurrentIndex = mCurrentIndex + 1;
        }
        else if (choice.equals(BETA)) {
            swapOrderingAlphaBeta(mCurrentIndex, mCurrentIndex + 1);
            if (mCurrentIndex > 0) {
                mCurrentIndex -= 1;
            }
            else if (mCurrentIndex < mArtWorks.size() - 2) {
                mCurrentIndex += 1;
            }
            else {
                Toast.makeText(getActivity(), "EXIT", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
        }
        displayArtwork(mArtworkImageViewAlpha, mCurrentIndex);
        displayArtwork(mArtworkImageViewAlpha, mCurrentIndex + 1);
    }

    /**
     * Swaps the ORDERING field of the alpha and beta artworks
     * Swaps the two artworks in the local mArtWorks
     * Updates the changes in the database
     */
    private void swapOrderingAlphaBeta(int alpha, int beta) {
        ArtWork artWorkAlpha = mArtWorks.get(alpha);
        ArtWork artWorkBeta = mArtWorks.get(beta);

        mArtWorks.add(alpha, artWorkBeta);
        mArtWorks.add(beta, artWorkAlpha);
        artWorkAlpha.swapOrder(artWorkBeta);

        Gallery.get(getActivity()).updateArtWork(artWorkAlpha);
        Gallery.get(getActivity()).updateArtWork(artWorkBeta);
    }


    /**
     * @param imageView
     * @param index
     */
    private void displayArtwork(ImageView imageView, int index) {<commit_msg>Fixes typo in sortArtwork method where mArtWorkImageAlpha was being displayed twice. Fixes issue in swapOrderingAlphaBeta where artwork were being added to mArtWorks list twice. Fixed by using Collections.swap()<commit_after>package com.ryanpconnors.artthief.compare;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.ryanpconnors.artthief.R;
import com.ryanpconnors.artthief.artgallery.ArtWork;
import com.ryanpconnors.artthief.artgallery.Gallery;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.ryanpconnors.artthief.compare.SortArtWorkFragment.ArtworkChoice.ALPHA;
import static com.ryanpconnors.artthief.compare.SortArtWorkFragment.ArtworkChoice.BETA;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SortArtWorkFragment.OnSortArtworkFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SortArtWorkFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SortArtWorkFragment extends Fragment {

    private static final String TAG = "SortArtWorkFragment";
    private static final String ARG_NUMBER_OF_STARS = "number_of_stars";

    private List<ArtWork> mArtWorks;

    private int mCurrentIndex = 0;

    private ImageView mArtworkImageViewAlpha;
    private ImageView mArtworkImageViewBeta;

    private ShareActionProvider mShareActionProvider;
    private OnSortArtworkFragmentInteractionListener mListener;

    /**
     * ArtworkChoice enumerated type used for identifying the user's choice
     * of which artwork they prefer for sorting purposes
     */
    protected enum ArtworkChoice {
        ALPHA,
        BETA
    }

    public SortArtWorkFragment() {
        // Required empty public constructor
    }

    public static SortArtWorkFragment newInstance(int numberOfStars) {
        SortArtWorkFragment fragment = new SortArtWorkFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_NUMBER_OF_STARS, numberOfStars);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mArtWorks = Gallery.get(getActivity()).getArtWorks(getArguments().getInt(ARG_NUMBER_OF_STARS));
            setHasOptionsMenu(true);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_sort_art_work, container, false);

        // Add toolbar and up button navigation
        Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
        ((SortArtWorkActivity) getActivity()).setSupportActionBar(toolbar);
        ActionBar actionBar = ((SortArtWorkActivity) getActivity()).getSupportActionBar();

        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavUtils.navigateUpFromSameTask(getActivity());
            }
        });

        // Initialize ArtWork Image Views
        mArtworkImageViewAlpha = (ImageView) v.findViewById(R.id.artwork_large_image_view_alpha);
        mArtworkImageViewAlpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sortArtwork(ALPHA);
            }
        });

        mArtworkImageViewBeta = (ImageView) v.findViewById(R.id.artwork_large_image_view_beta);
        mArtworkImageViewBeta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sortArtwork(BETA);
            }
        });

        displayArtwork(mArtworkImageViewAlpha, mCurrentIndex);
        displayArtwork(mArtworkImageViewBeta, mCurrentIndex + 1);
        return v;
    }

    /**
     * @param choice
     */
    private void sortArtwork(ArtworkChoice choice) {
        if (choice.equals(ALPHA)) {
            if (mCurrentIndex >= mArtWorks.size() - 2) {
                Toast.makeText(getActivity(), "EXIT", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
            else {
                mCurrentIndex += 1;
            }
        }
        else if (choice.equals(BETA)) {
            swapOrderingAlphaBeta(mCurrentIndex, mCurrentIndex + 1);
            if (mCurrentIndex > 0) {
                mCurrentIndex -= 1;
            }
            else if (mCurrentIndex < mArtWorks.size() - 2) {
                mCurrentIndex += 1;
            }
            else {
                Toast.makeText(getActivity(), "EXIT", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
        }
        displayArtwork(mArtworkImageViewAlpha, mCurrentIndex);
        displayArtwork(mArtworkImageViewBeta, mCurrentIndex + 1);
    }

    /**
     * Swaps the ORDERING field of the alpha and beta artworks
     * Swaps the two artworks in the local mArtWorks
     * Updates the changes in the database
     */
    private void swapOrderingAlphaBeta(int alpha, int beta) {
        ArtWork artWorkAlpha = mArtWorks.get(alpha);
        ArtWork artWorkBeta = mArtWorks.get(beta);

        artWorkAlpha.swapOrder(artWorkBeta);
        Collections.swap(mArtWorks, alpha, beta);

        Gallery.get(getActivity()).updateArtWork(artWorkAlpha);
        Gallery.get(getActivity()).updateArtWork(artWorkBeta);
    }


    /**
     * @param imageView
     * @param index
     */
    private void displayArtwork(ImageView imageView, int index) {
{'fraction_non_alphanumeric': 0.08835158150851581, 'fraction_numerical': 0.0024330900243309003, 'mean_word_length': 2.977320834593287, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '2407220', 'n_tokens_mistral': 3749, 'n_tokens_neox': 3519, 'n_words': 786}