<commit_before>package com.agateau.equiv.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.agateau.equiv.R;
import com.agateau.equiv.core.Constants;
import com.agateau.equiv.core.FormatUtils;
import com.agateau.equiv.core.Meal;
import com.agateau.equiv.core.MealItem;
import com.agateau.equiv.core.Product;
import com.agateau.equiv.core.ProductList;
import com.agateau.equiv.core.ProteinWeightUnit;
import com.agateau.utils.ui.ActionBarViewTabBuilder;

import java.util.ArrayList;
import java.util.Locale;


public class MealItemDetailActivity extends AppCompatActivity {
    public static final String EXTRA_MEAL_TAG = "com.agateau.equiv.MEAL_TAG";
    public static final String EXTRA_MEAL_ITEM_POSITION = "com.agateau.equiv.MEAL_ITEM_POSITION";

    private static final int NEW_MEAL_ITEM_POSITION = -1;

    private static class ProductListAdapter extends ArrayAdapter<Product> {
        private final Kernel mKernel;

        private final CompoundButton.OnCheckedChangeListener mOnFavoriteCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Product product = (Product) buttonView.getTag();
                mKernel.getProductList().setFavorite(product, isChecked);
            }
        };

        public ProductListAdapter(Context context, Kernel kernel, ArrayList<Product> items) {
            super(context, R.layout.product_item, R.id.product_item_text, items);
            mKernel = kernel;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            Product product = getItem(position);
            view.setTag(product);

            TextView equivTextView = (TextView) view.findViewById(R.id.product_item_equiv_text);

            float proteins = product.getProteins();

            WeightFormatter formatter = mKernel.getWeightFormater();

            float equiv;
            String equivUnit = product.getUnit();
            int ref;
            if (mKernel.getProteinUnit() == ProteinWeightUnit.POTATO) {
                equiv = 100 / (proteins / Constants.PROTEIN_FOR_POTATO);
                ref = 100;
            } else {
                equiv = 1 / proteins;
                ref = 1;
            }

            String equivText = String.format("%s%s = %d%s",
                    FormatUtils.naturalRound(equiv),
                    equivUnit,
                    ref,
                    formatter.getUnitString(WeightFormatter.UnitFormat.FULL));
            equivTextView.setText(equivText);

            CheckBox favoriteCheckBox = (CheckBox) view.findViewById(R.id.product_item_favorite);
            // Reset onCheckedChangeListener so that mOnFavoriteCheckedChangeListener is not called
            // when we call setChecked() (can happen when a view is reused)
            favoriteCheckBox.setOnCheckedChangeListener(null);
            favoriteCheckBox.setChecked(mKernel.getProductList().isFavorite(product));
            favoriteCheckBox.setOnCheckedChangeListener(mOnFavoriteCheckedChangeListener);
            favoriteCheckBox.setTag(product);
            return view;
        }
    }

    private Kernel mKernel;

    private ProductList mProductList;
    private Meal mMeal;
    private Product mProduct = null;
    private MenuItem mSaveMenuItem;
    private int mMealItemPosition = NEW_MEAL_ITEM_POSITION;
    private TextView mProductNameView;
    private LinearLayout mDetailsLayout;
    private EditText mQuantityEdit;
    private TextView mQuantityUnitView;
    private EditText mQuantityEquivEdit;
    private TextView mQuantityEquivUnitView;
    private boolean mUpdating = false;

    public static void addMealItem(Context context, Meal meal) {
        Intent intent = new Intent(context, MealItemDetailActivity.class);
        intent.putExtra(EXTRA_MEAL_TAG, meal.getTag());
        context.startActivity(intent);
    }

    public static void editMealItem(Context context, Meal meal, int position) {
        Intent intent = new Intent(context, MealItemDetailActivity.class);
        intent.putExtra(EXTRA_MEAL_TAG, meal.getTag());
        intent.putExtra(EXTRA_MEAL_ITEM_POSITION, position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meal_item_detail_activity);

        mKernel = Kernel.getInstance(this);
        mProductList = mKernel.getProductList();

        String mealTag = getIntent().getStringExtra(EXTRA_MEAL_TAG);
        mMeal = mKernel.getDay().getMealByTag(mealTag);
        mMealItemPosition = getIntent().getIntExtra(EXTRA_MEAL_ITEM_POSITION, -1);

        setupTabs();
        setupMealEditor();
    }

    private void setupTabs() {
        ListView fullListView = new ListView(this);
        ProductListAdapter fullListAdapter = new ProductListAdapter(this, mKernel, mProductList.getItems());
        fullListView.setAdapter(fullListAdapter);

        final ArrayList<Product> favoriteItems = new ArrayList<>();
        favoriteItems.addAll(mProductList.getFavoriteItems());
        final ProductListAdapter favoritesListAdapter = new ProductListAdapter(this, mKernel, favoriteItems);
        mProductList.setFavoriteChangedListener(new ProductList.FavoriteChangedListener() {
            @Override
            public void onFavoriteChanged() {
                favoriteItems.clear();
                favoriteItems.addAll(mProductList.getFavoriteItems());
                favoritesListAdapter.notifyDataSetChanged();
            }
        });<commit_msg>Update full list on favorite changes<commit_after>package com.agateau.equiv.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.agateau.equiv.R;
import com.agateau.equiv.core.Constants;
import com.agateau.equiv.core.FormatUtils;
import com.agateau.equiv.core.Meal;
import com.agateau.equiv.core.MealItem;
import com.agateau.equiv.core.Product;
import com.agateau.equiv.core.ProductList;
import com.agateau.equiv.core.ProteinWeightUnit;
import com.agateau.utils.ui.ActionBarViewTabBuilder;

import java.util.ArrayList;
import java.util.Locale;


public class MealItemDetailActivity extends AppCompatActivity {
    public static final String EXTRA_MEAL_TAG = "com.agateau.equiv.MEAL_TAG";
    public static final String EXTRA_MEAL_ITEM_POSITION = "com.agateau.equiv.MEAL_ITEM_POSITION";

    private static final int NEW_MEAL_ITEM_POSITION = -1;

    private static class ProductListAdapter extends ArrayAdapter<Product> {
        private final Kernel mKernel;

        private final CompoundButton.OnCheckedChangeListener mOnFavoriteCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Product product = (Product) buttonView.getTag();
                mKernel.getProductList().setFavorite(product, isChecked);
            }
        };

        public ProductListAdapter(Context context, Kernel kernel, ArrayList<Product> items) {
            super(context, R.layout.product_item, R.id.product_item_text, items);
            mKernel = kernel;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            Product product = getItem(position);
            view.setTag(product);

            TextView equivTextView = (TextView) view.findViewById(R.id.product_item_equiv_text);

            float proteins = product.getProteins();

            WeightFormatter formatter = mKernel.getWeightFormater();

            float equiv;
            String equivUnit = product.getUnit();
            int ref;
            if (mKernel.getProteinUnit() == ProteinWeightUnit.POTATO) {
                equiv = 100 / (proteins / Constants.PROTEIN_FOR_POTATO);
                ref = 100;
            } else {
                equiv = 1 / proteins;
                ref = 1;
            }

            String equivText = String.format("%s%s = %d%s",
                    FormatUtils.naturalRound(equiv),
                    equivUnit,
                    ref,
                    formatter.getUnitString(WeightFormatter.UnitFormat.FULL));
            equivTextView.setText(equivText);

            CheckBox favoriteCheckBox = (CheckBox) view.findViewById(R.id.product_item_favorite);
            // Reset onCheckedChangeListener so that mOnFavoriteCheckedChangeListener is not called
            // when we call setChecked() (can happen when a view is reused)
            favoriteCheckBox.setOnCheckedChangeListener(null);
            favoriteCheckBox.setChecked(mKernel.getProductList().isFavorite(product));
            favoriteCheckBox.setOnCheckedChangeListener(mOnFavoriteCheckedChangeListener);
            favoriteCheckBox.setTag(product);
            return view;
        }
    }

    private Kernel mKernel;

    private ProductList mProductList;
    private Meal mMeal;
    private Product mProduct = null;
    private MenuItem mSaveMenuItem;
    private int mMealItemPosition = NEW_MEAL_ITEM_POSITION;
    private TextView mProductNameView;
    private LinearLayout mDetailsLayout;
    private EditText mQuantityEdit;
    private TextView mQuantityUnitView;
    private EditText mQuantityEquivEdit;
    private TextView mQuantityEquivUnitView;
    private boolean mUpdating = false;

    public static void addMealItem(Context context, Meal meal) {
        Intent intent = new Intent(context, MealItemDetailActivity.class);
        intent.putExtra(EXTRA_MEAL_TAG, meal.getTag());
        context.startActivity(intent);
    }

    public static void editMealItem(Context context, Meal meal, int position) {
        Intent intent = new Intent(context, MealItemDetailActivity.class);
        intent.putExtra(EXTRA_MEAL_TAG, meal.getTag());
        intent.putExtra(EXTRA_MEAL_ITEM_POSITION, position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meal_item_detail_activity);

        mKernel = Kernel.getInstance(this);
        mProductList = mKernel.getProductList();

        String mealTag = getIntent().getStringExtra(EXTRA_MEAL_TAG);
        mMeal = mKernel.getDay().getMealByTag(mealTag);
        mMealItemPosition = getIntent().getIntExtra(EXTRA_MEAL_ITEM_POSITION, -1);

        setupTabs();
        setupMealEditor();
    }

    private void setupTabs() {
        ListView fullListView = new ListView(this);
        final ProductListAdapter fullListAdapter = new ProductListAdapter(this, mKernel, mProductList.getItems());
        fullListView.setAdapter(fullListAdapter);

        final ArrayList<Product> favoriteItems = new ArrayList<>();
        favoriteItems.addAll(mProductList.getFavoriteItems());
        final ProductListAdapter favoritesListAdapter = new ProductListAdapter(this, mKernel, favoriteItems);
        mProductList.setFavoriteChangedListener(new ProductList.FavoriteChangedListener() {
            @Override
            public void onFavoriteChanged() {
                favoriteItems.clear();
                favoriteItems.addAll(mProductList.getFavoriteItems());
                favoritesListAdapter.notifyDataSetChanged();

                // Notify fullListAdapter as well because it must update the state of its checkboxes
                fullListAdapter.notifyDataSetChanged();
            }
        });
{'fraction_non_alphanumeric': 0.08252759802055577, 'fraction_numerical': 0.0021317091739626952, 'mean_word_length': 3.1438485804416403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '981601', 'n_tokens_mistral': 3608, 'n_tokens_neox': 3469, 'n_words': 730}