<commit_before>
package com.uwetrottmann.shopr.ui.explanation;

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.text.TextUtils;
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.TextView;
import android.widget.Toast;

import com.adiguzel.shopr.explanation.Discloser;
import com.etsy.android.grid.StaggeredGridView;
import com.google.android.gms.maps.model.LatLng;
import com.uwetrottmann.androidutils.Maps;
import com.uwetrottmann.shopr.R;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter.OnItemCritiqueListener;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter.OnItemDisplayListener;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter.OnItemFavouriteListener;
import com.uwetrottmann.shopr.algorithm.AdaptiveSelection;
import com.uwetrottmann.shopr.algorithm.Query;
import com.uwetrottmann.shopr.algorithm.model.Item;
import com.uwetrottmann.shopr.eval.ResultsActivity;
import com.uwetrottmann.shopr.eval.Statistics;
import com.uwetrottmann.shopr.loaders.ItemLoader;
import com.uwetrottmann.shopr.provider.ShoprContract.Stats;
import com.uwetrottmann.shopr.ui.CritiqueActivity;
import com.uwetrottmann.shopr.ui.ItemDetailsActivity;
import com.uwetrottmann.shopr.ui.explanation.MainActivityExplanation.LocationUpdateEvent;
import com.uwetrottmann.shopr.utils.FavouriteItemUtils;

import de.greenrobot.event.EventBus;

/**
 * Shows a list of clothing items the user can critique by tapping an up or down
 * vote button.
 */
public class ItemListFragmentExplanation extends Fragment implements LoaderCallbacks<List<Item>>,
        OnItemCritiqueListener, OnItemDisplayListener, OnItemFavouriteListener {

    public static final String TAG = "Item List";

    // I = 9, T = 20
    private static final int LOADER_ID = 920;
    private static final int REQUEST_CODE = 12;
    private TextView mTextViewReason;
    private StaggeredGridView mGridView;
    private ExplainedItemAdapter mAdapter;

    private boolean mIsInitialized;
    
    private static ItemListFragmentExplanation instance;

    public static ItemListFragmentExplanation newInstance() {
    	if(instance == null)
    		instance = new ItemListFragmentExplanation();
    	
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_item_list, container, false);

        mTextViewReason = (TextView) v.findViewById(R.id.textViewItemListReason);
        mGridView = (StaggeredGridView) v.findViewById(R.id.gridViewItemList);
        View emtpyView = v.findViewById(R.id.textViewItemListEmpty);
        mGridView.setEmptyView(emtpyView);

        return v;
    }

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

        mAdapter = new ExplainedItemAdapter(getActivity(), this, this, this);

        mGridView.setAdapter(mAdapter);
        
        Bundle args = new Bundle();
        args.putBoolean("isinit", false);
        getLoaderManager().initLoader(LOADER_ID, args, this);
        
        setHasOptionsMenu(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().registerSticky(this, LocationUpdateEvent.class);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.item_list, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_restart:
                onInitializeItems();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public Loader<List<Item>> onCreateLoader(int loaderId, Bundle args) {
        boolean isInit = false;
        if (args != null) {
            isInit = args.getBoolean("isinit");
        }
        LatLng location = ((MainActivityExplanation) getActivity()).getLastLocation();
        return new ItemLoader(getActivity(), this, location, isInit);
    }

    @Override
    public void onLoadFinished(Loader<List<Item>> loader, List<Item> data) {
        mAdapter.clear();
        mAdapter.addAll(new Discloser().explain(data, AdaptiveSelection.get().getCurrentQuery(), null));
        onUpdateReason();
        onUpdateShops(data);
    }

    public class ShopUpdateEvent {
        /**
         * Holds a list of shop ids and how many recommendations are shown for
         * each shop.
         */<commit_msg>Added location context as an argument to Discloser in ItemListFragment.<commit_after>
package com.uwetrottmann.shopr.ui.explanation;

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.text.TextUtils;
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.TextView;
import android.widget.Toast;

import com.adiguzel.shopr.explanation.Discloser;
import com.adiguzel.shopr.explanation.model.LocationContext;
import com.etsy.android.grid.StaggeredGridView;
import com.google.android.gms.maps.model.LatLng;
import com.uwetrottmann.androidutils.Maps;
import com.uwetrottmann.shopr.R;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter.OnItemCritiqueListener;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter.OnItemDisplayListener;
import com.uwetrottmann.shopr.adapters.ExplainedItemAdapter.OnItemFavouriteListener;
import com.uwetrottmann.shopr.algorithm.AdaptiveSelection;
import com.uwetrottmann.shopr.algorithm.Query;
import com.uwetrottmann.shopr.algorithm.model.Item;
import com.uwetrottmann.shopr.eval.ResultsActivity;
import com.uwetrottmann.shopr.eval.Statistics;
import com.uwetrottmann.shopr.loaders.ItemLoader;
import com.uwetrottmann.shopr.provider.ShoprContract.Stats;
import com.uwetrottmann.shopr.ui.CritiqueActivity;
import com.uwetrottmann.shopr.ui.ItemDetailsActivity;
import com.uwetrottmann.shopr.ui.explanation.MainActivityExplanation.LocationUpdateEvent;
import com.uwetrottmann.shopr.utils.FavouriteItemUtils;

import de.greenrobot.event.EventBus;

/**
 * Shows a list of clothing items the user can critique by tapping an up or down
 * vote button.
 */
public class ItemListFragmentExplanation extends Fragment implements LoaderCallbacks<List<Item>>,
        OnItemCritiqueListener, OnItemDisplayListener, OnItemFavouriteListener {

    public static final String TAG = "Item List";

    // I = 9, T = 20
    private static final int LOADER_ID = 920;
    private static final int REQUEST_CODE = 12;
    private TextView mTextViewReason;
    private StaggeredGridView mGridView;
    private ExplainedItemAdapter mAdapter;

    private boolean mIsInitialized;
    
    private static ItemListFragmentExplanation instance;

    public static ItemListFragmentExplanation newInstance() {
    	if(instance == null)
    		instance = new ItemListFragmentExplanation();
    	
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_item_list, container, false);

        mTextViewReason = (TextView) v.findViewById(R.id.textViewItemListReason);
        mGridView = (StaggeredGridView) v.findViewById(R.id.gridViewItemList);
        View emtpyView = v.findViewById(R.id.textViewItemListEmpty);
        mGridView.setEmptyView(emtpyView);

        return v;
    }

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

        mAdapter = new ExplainedItemAdapter(getActivity(), this, this, this);

        mGridView.setAdapter(mAdapter);
        
        Bundle args = new Bundle();
        args.putBoolean("isinit", false);
        getLoaderManager().initLoader(LOADER_ID, args, this);
        
        setHasOptionsMenu(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().registerSticky(this, LocationUpdateEvent.class);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.item_list, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_restart:
                onInitializeItems();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public Loader<List<Item>> onCreateLoader(int loaderId, Bundle args) {
        boolean isInit = false;
        if (args != null) {
            isInit = args.getBoolean("isinit");
        }
        LatLng location = ((MainActivityExplanation) getActivity()).getLastLocation();
        return new ItemLoader(getActivity(), this, location, isInit);
    }

    @Override
    public void onLoadFinished(Loader<List<Item>> loader, List<Item> items) {
        mAdapter.clear();
        List<com.adiguzel.shopr.explanation.model.Context> contexts = 
        		new ArrayList<com.adiguzel.shopr.explanation.model.Context>();
        LatLng location = ((MainActivityExplanation) getActivity()).getLastLocation();
        if(location != null) {
        	contexts.add(new LocationContext(location.latitude, location.longitude));
        }
       
        mAdapter.addAll(new Discloser().explain(items, AdaptiveSelection.get().getCurrentQuery(), contexts));
        onUpdateReason();
        onUpdateShops(items);
    }

    public class ShopUpdateEvent {
        /**
         * Holds a list of shop ids and how many recommendations are shown for
         * each shop.
         */
{'fraction_non_alphanumeric': 0.09336609336609336, 'fraction_numerical': 0.002002002002002002, 'mean_word_length': 3.676595744680851, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '4945600', 'n_tokens_mistral': 3294, 'n_tokens_neox': 3170, 'n_words': 638}