<commit_before>package org.bbt.kiakoa.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.bbt.kiakoa.R;
import org.bbt.kiakoa.model.Loan;
import org.bbt.kiakoa.model.LoanLists;

/**
 * Dialog used to pick an item
 */
public class LoanItemDialog extends DialogFragment implements TextView.OnEditorActionListener {

    /**
     * For log
     */
    private static final String TAG = "LoanItemDialog";
    private static final int ACTION_CREATE = 0;
    private static final int ACTION_UPDATE = 1;

    /**
     * item for a loan
     */
    private EditText itemEditText;

    /**
     * loan to create/update
     */
    private Loan loan;

    /**
     * Identify list to save
     */
    private String list;

    /**
     * Create a new instance of LoanItemDialog
     */
    public static LoanItemDialog newInstance(String loanList) {
        LoanItemDialog dialog = new LoanItemDialog();
        Bundle args = new Bundle();
        args.putInt("action", ACTION_CREATE);
        args.putString("list", loanList);
        dialog.setArguments(args);
        return dialog;
    }

    /**
     * Create a new instance of LoanItemDialog with item
     *
     * @param loan loan to be updated
     */
    public static LoanItemDialog newInstance(Loan loan) {
        LoanItemDialog dialog = new LoanItemDialog();
        Bundle args = new Bundle();
        args.putParcelable("loan", loan);
        args.putInt("action", ACTION_UPDATE);
        dialog.setArguments(args);
        return dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Inflate view
        @SuppressLint
                ("InflateParams") View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_loan_item, null, false);
        itemEditText = view.findViewById(R.id.item);
        itemEditText.setOnEditorActionListener(this);

        // arguments
        int action = getArguments().getInt("action", ACTION_CREATE);
        if (action == ACTION_UPDATE) {
            // get loan to update
            loan = getArguments().getParcelable("loan");
            if (loan != null) {
                itemEditText.setText(loan.getItem());
            } else {
                Log.e(TAG, "No loan to update");
                dismiss();
            }
        } else {
            // get list to add new loan
            list = getArguments().getString("list", LoanLists.SHARED_PREFERENCES_LENT_KEY);
        }

        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setView(view)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();

        // set icon and title
        int iconId;
        if (list == null) {
            iconId = R.drawable.ic_item_24dp;
        } else if (LoanLists.SHARED_PREFERENCES_LENT_KEY.equals(list)) {
            iconId = R.drawable.ic_lent_24dp;
        } else {
            iconId = R.drawable.ic_borrowed_24dp;
        }
        dialog.setIcon(iconId);

        // set title
        if (ACTION_CREATE == action) {
            if (LoanLists.SHARED_PREFERENCES_LENT_KEY.equals(list)) {
                dialog.setTitle(R.string.new_loan);
            } else {
                dialog.setTitle(R.string.new_borrowing);
            }
        } else {
            dialog.setTitle(loan.getItem());
        }

        // manually manage ok button
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialogInterface) {

                // Change positive button click listener to manage dismiss
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        saveLoanItem();
                    }
                });
            }
        });

        Window window = dialog.getWindow();
        if (window != null) {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
        return dialog;
    }

    /**
     * Save loan from item
     */
    private void saveLoanItem() {
        String item = itemEditText.getText().toString();
        if (item.length() > 0) {
            if (loan != null) {
                // update existing item
                Log.w(TAG, "update existing loan : " + item);
                loan.setItem(item);
                LoanLists.getInstance().updateLoan(loan, getContext());
            } else {
                // new item to add
                Log.i(TAG, "new item : " + item);
                Loan newLoan = new Loan(item);
                if (LoanLists.SHARED_PREFERENCES_BORROWED_KEY.equals(list)) {
                    LoanLists.getInstance().saveBorrowed(newLoan, getContext());
                } else {
                    LoanLists.getInstance().saveLent(newLoan, getContext());
                }
            }
            dismiss();
        } else {
            Log.i(TAG, "item can't be empty");
            Toast.makeText(getContext(), R.string.label_cant_be_blank, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        saveLoanItem();
        dismiss();
        return true;
    }
}
<commit_msg>Using loan color as primary color in LoanDetails Screen fragment<commit_after>package org.bbt.kiakoa.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.bbt.kiakoa.R;
import org.bbt.kiakoa.model.Loan;
import org.bbt.kiakoa.model.LoanLists;

/**
 * Dialog used to pick an item
 */
public class LoanItemDialog extends DialogFragment implements TextView.OnEditorActionListener {

    /**
     * For log
     */
    private static final String TAG = "LoanItemDialog";
    private static final int ACTION_CREATE = 0;
    private static final int ACTION_UPDATE = 1;

    /**
     * item for a loan
     */
    private EditText itemEditText;

    /**
     * loan to create/update
     */
    private Loan loan;

    /**
     * Identify list to save
     */
    private String list;

    /**
     * Create a new instance of LoanItemDialog
     */
    public static LoanItemDialog newInstance(String loanList) {
        LoanItemDialog dialog = new LoanItemDialog();
        Bundle args = new Bundle();
        args.putInt("action", ACTION_CREATE);
        args.putString("list", loanList);
        dialog.setArguments(args);
        return dialog;
    }

    /**
     * Create a new instance of LoanItemDialog with item
     *
     * @param loan loan to be updated
     */
    public static LoanItemDialog newInstance(Loan loan) {
        LoanItemDialog dialog = new LoanItemDialog();
        Bundle args = new Bundle();
        args.putParcelable("loan", loan);
        args.putInt("action", ACTION_UPDATE);
        dialog.setArguments(args);
        return dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Inflate view
        @SuppressLint
                ("InflateParams") View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_loan_item, null, false);
        itemEditText = view.findViewById(R.id.item);
        itemEditText.setOnEditorActionListener(this);

        // arguments
        int action = getArguments().getInt("action", ACTION_CREATE);
        if (action == ACTION_UPDATE) {
            // get loan to update
            loan = getArguments().getParcelable("loan");
            if (loan != null) {
                itemEditText.setText(loan.getItem());
            } else {
                Log.e(TAG, "No loan to update");
                dismiss();
            }
        } else {
            // get list to add new loan
            list = getArguments().getString("list", LoanLists.SHARED_PREFERENCES_LENT_KEY);
        }

        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setView(view)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();

        // set icon and title
        int iconId;
        if (list == null) {
            iconId = R.drawable.ic_item_24dp;
        } else if (LoanLists.SHARED_PREFERENCES_LENT_KEY.equals(list)) {
            iconId = R.drawable.ic_lent_24dp;
        } else {
            iconId = R.drawable.ic_borrowed_24dp;
        }

        // set title
        if (ACTION_CREATE == action) {
            if (LoanLists.SHARED_PREFERENCES_LENT_KEY.equals(list)) {
                dialog.setTitle(R.string.new_loan);
            } else {
                dialog.setTitle(R.string.new_borrowing);
            }
        } else {
            dialog.setTitle(loan.getItem());
        }

        // manually manage ok button
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialogInterface) {

                // Change positive button click listener to manage dismiss
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        saveLoanItem();
                    }
                });
            }
        });

        // set primary color if loan exists (loan update)
        if (loan != null) {
            // icon
            Drawable myIcon = getResources().getDrawable(iconId);
            myIcon.setTint(loan.getColor(getContext()));
            dialog.setIcon(myIcon);
            // EditText
            ColorStateList colorStateList = ColorStateList.valueOf(loan.getColor(getContext()));
            ViewCompat.setBackgroundTintList(itemEditText, colorStateList);
            // buttons
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface d) {
                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(loan.getColor(getContext()));
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(loan.getColor(getContext()));
                }
            });
        } else {
            dialog.setIcon(iconId);
        }

        Window window = dialog.getWindow();
        if (window != null) {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
        return dialog;
    }

    /**
     * Save loan from item
     */
    private void saveLoanItem() {
        String item = itemEditText.getText().toString();
        if (item.length() > 0) {
            if (loan != null) {
                // update existing item
                Log.w(TAG, "update existing loan : " + item);
                loan.setItem(item);
                LoanLists.getInstance().updateLoan(loan, getContext());
            } else {
                // new item to add
                Log.i(TAG, "new item : " + item);
                Loan newLoan = new Loan(item);
                if (LoanLists.SHARED_PREFERENCES_BORROWED_KEY.equals(list)) {
                    LoanLists.getInstance().saveBorrowed(newLoan, getContext());
                } else {
                    LoanLists.getInstance().saveLent(newLoan, getContext());
                }
            }
            dismiss();
        } else {
            Log.i(TAG, "item can't be empty");
            Toast.makeText(getContext(), R.string.label_cant_be_blank, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        saveLoanItem();
        dismiss();
        return true;
    }
}

{'fraction_non_alphanumeric': 0.09927254023072966, 'fraction_numerical': 0.0015430964802704093, 'mean_word_length': 1.9062566730728165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5031116', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3559, 'n_words': 789}