<commit_before>package com.simplecurrencyconverter.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.simplecurrencyconverter.R;
import com.simplecurrencyconverter.utils.ConversionRate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

/**
 * A {@link Fragment} that contains currency conversion widgets.
 */
public class ConvertFragment extends Fragment {

    private final static String EMPTY_AMOUNT = "0";

    private final DecimalFormat mInputAmountFormatter = getAmountFormatter("###,###.##");
    private final DecimalFormat mOutputAmountFormatter = getAmountFormatter("###,##0.00");

    private boolean mAllowAmountUpdate = true;

    private ConversionRate mConversionRate;

    /**
     * Creates an instance of the fragment.
     */
    public ConvertFragment() {
    }

    /**
     * Sets the conversion rate and currencies to show.
     *
     * @param conversionRate  a {@link com.simplecurrencyconverter.utils.ConversionRate} object that
     *                        contains the currencies and their conversion rate.
     */
    public void setConversionRate(ConversionRate conversionRate) {
        mConversionRate = conversionRate;
        addCurrencyListeners(getView());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_convert, container, false);
    }

    private void addCurrencyListeners(View view) {
        EditText krwEditText = (EditText) view.findViewById(R.id.krw_amount);
        EditText eurEditText = (EditText) view.findViewById(R.id.eur_amount);

        addAmountChangedListeners(krwEditText, eurEditText, mConversionRate.getFixedCurrencyInVariableCurrencyRate());
        addFocusChangedListener(krwEditText);

        addAmountChangedListeners(eurEditText, krwEditText, mConversionRate.getVariableCurrencyInFixedCurrencyRate());
        addFocusChangedListener(eurEditText);
    }

    private void addAmountChangedListeners(final EditText editText, final EditText otherEditText, final BigDecimal multiplier) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                updateOtherAmount(s, otherEditText, multiplier);
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });
    }

    private void addFocusChangedListener(EditText editText) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    updateLostFocusAmount((EditText) v);
                }
            }
        });
    }

    private void updateOtherAmount(Editable changedText, EditText editTextToChange, BigDecimal multiplier) {
        if (!mAllowAmountUpdate) {
            mAllowAmountUpdate = true;
            return;
        }
        mAllowAmountUpdate = false;

        String formattedOutputAmount;
        if (changedText.toString().length() == 0) {
            formattedOutputAmount = "";
        }
        else {
            BigDecimal inputAmount = parseDecimal(changedText.toString());
            BigDecimal outputAmount = inputAmount.multiply(multiplier).setScale(2, RoundingMode.HALF_EVEN);
            formattedOutputAmount = formatAmount(outputAmount, mOutputAmountFormatter);
        }
        editTextToChange.setText(formattedOutputAmount);
    }

    private void updateLostFocusAmount(EditText editText) {
        BigDecimal amount = parseDecimal(editText.getText().toString());
        String formattedOutputAmount = formatAmount(amount, mInputAmountFormatter);
        // Empty the other amount too if the amount that lost focus was emptied
        mAllowAmountUpdate = formattedOutputAmount.length() == 0;
        editText.setText(formattedOutputAmount);
    }

    private BigDecimal parseDecimal(String s) {
        try {
            return new BigDecimal(mInputAmountFormatter.parse(s).doubleValue());
        }
        catch (NumberFormatException e) {
            return new BigDecimal(0);
        }
        catch (ParseException e) {
            return new BigDecimal(0);<commit_msg>Handle thousand separator spaces smoothly in newest API versions<commit_after>package com.simplecurrencyconverter.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.simplecurrencyconverter.R;
import com.simplecurrencyconverter.utils.ConversionRate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

/**
 * A {@link Fragment} that contains currency conversion widgets.
 */
public class ConvertFragment extends Fragment {

    private final static String EMPTY_AMOUNT = "0";

    private final DecimalFormat mInputAmountFormatter = getAmountFormatter("###,###.##");
    private final DecimalFormat mOutputAmountFormatter = getAmountFormatter("###,##0.00");

    private boolean mAllowAmountUpdate = true;

    private ConversionRate mConversionRate;

    /**
     * Creates an instance of the fragment.
     */
    public ConvertFragment() {
    }

    /**
     * Sets the conversion rate and currencies to show.
     *
     * @param conversionRate  a {@link com.simplecurrencyconverter.utils.ConversionRate} object that
     *                        contains the currencies and their conversion rate.
     */
    public void setConversionRate(ConversionRate conversionRate) {
        mConversionRate = conversionRate;
        addCurrencyListeners(getView());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_convert, container, false);
    }

    private void addCurrencyListeners(View view) {
        EditText krwEditText = (EditText) view.findViewById(R.id.krw_amount);
        EditText eurEditText = (EditText) view.findViewById(R.id.eur_amount);

        addAmountChangedListeners(krwEditText, eurEditText, mConversionRate.getFixedCurrencyInVariableCurrencyRate());
        addFocusChangedListener(krwEditText);

        addAmountChangedListeners(eurEditText, krwEditText, mConversionRate.getVariableCurrencyInFixedCurrencyRate());
        addFocusChangedListener(eurEditText);
    }

    private void addAmountChangedListeners(final EditText editText, final EditText otherEditText, final BigDecimal multiplier) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                updateOtherAmount(s, otherEditText, multiplier);
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });
    }

    private void addFocusChangedListener(EditText editText) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    updateLostFocusAmount((EditText) v);
                }
            }
        });
    }

    private void updateOtherAmount(Editable changedText, EditText editTextToChange, BigDecimal multiplier) {
        if (!mAllowAmountUpdate) {
            mAllowAmountUpdate = true;
            return;
        }
        mAllowAmountUpdate = false;

        String formattedOutputAmount;
        if (changedText.toString().length() == 0) {
            formattedOutputAmount = "";
        }
        else {
            BigDecimal inputAmount = parseDecimal(changedText.toString());
            BigDecimal outputAmount = inputAmount.multiply(multiplier).setScale(2, RoundingMode.HALF_EVEN);
            formattedOutputAmount = formatAmount(outputAmount, mOutputAmountFormatter);
        }
        editTextToChange.setText(formattedOutputAmount);
    }

    private void updateLostFocusAmount(EditText editText) {
        BigDecimal amount = parseDecimal(editText.getText().toString());
        String formattedOutputAmount = formatAmount(amount, mInputAmountFormatter);
        // Empty the other amount too if the amount that lost focus was emptied
        mAllowAmountUpdate = formattedOutputAmount.length() == 0;
        editText.setText(formattedOutputAmount);
    }

    private BigDecimal parseDecimal(String amount) {
        String trimmedAmount = amount.replaceAll(" ", "");
        try {
            return new BigDecimal(mInputAmountFormatter.parse(trimmedAmount).doubleValue());
        }
        catch (NumberFormatException e) {
            return new BigDecimal(0);
        }
        catch (ParseException e) {
            return new BigDecimal(0);
{'fraction_non_alphanumeric': 0.08111145162937991, 'fraction_numerical': 0.0020431096128307284, 'mean_word_length': 2.96677471636953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2372990', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2440, 'n_words': 572}