<commit_before>package com.teinproductions.tein.pitrainer.records;


import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
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 android.widget.ImageButton;
import android.widget.TextView;

import com.teinproductions.tein.pitrainer.ActivityInterface;
import com.teinproductions.tein.pitrainer.Digits;
import com.teinproductions.tein.pitrainer.FragmentInterface;
import com.teinproductions.tein.pitrainer.Keyboard;
import com.teinproductions.tein.pitrainer.MainActivity;
import com.teinproductions.tein.pitrainer.R;

public class TimeFragment extends Fragment implements FragmentInterface {
    private ActivityInterface activityInterface;

    private EditText inputET;
    private TextView timer, digitsTV;
    private Keyboard keyboard;
    private ImageButton restartButton;
    private TimerTask timerTask;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activityInterface = (ActivityInterface) getActivity();

        View theView = inflater.inflate(R.layout.fragment_time, container, false);

        inputET = (EditText) theView.findViewById(R.id.input_editText);
        timer = (TextView) theView.findViewById(R.id.timer);
        digitsTV = (TextView) theView.findViewById(R.id.digits_textView);
        TextView integerPartTV = (TextView) theView.findViewById(R.id.integerPart_textView);
        keyboard = (Keyboard) theView.findViewById(R.id.keyboard);
        restartButton = (ImageButton) theView.findViewById(R.id.restart_button);

        keyboard.setEditText(inputET);
        integerPartTV.setText(Digits.currentDigit.getIntegerPart() + ".");
        digitsTV.setText(" " + 0);
        showOnScreenKeyboard(getActivity().getPreferences(0).getBoolean(MainActivity.ON_SCREEN_KEYBOARD, false));
        setRestartImageResource();
        setTextWatcher();

        return theView;
    }

    private void setRestartImageResource() {
        if (Build.VERSION.SDK_INT >= 21) {
            restartButton.setImageResource(R.drawable.anim_ic_restart);
        } else {
            restartButton.setImageResource(R.mipmap.ic_refresh_white_36dp);
        }

        restartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickRestart();
            }
        });
    }

    private void setTextWatcher() {
        inputET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {/*ignored*/}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (Digits.isIncorrect(inputET.getText().toString())) {
                    timerTask.cancel(true);
                    activityInterface.vibrate(200);
                    inputET.setEnabled(false);
                    keyboard.setEnabled(false);

                    RecordsHandler.addRecord(getActivity(), before, timerTask.getCentiseconds());
                    showDialog(inputET.getText().length() - 1, timerTask.getCentiseconds());
                    activityInterface.swapFragment(RecordsFragment.class);
                } else if (inputET.getText().length() == 1 && before == 0) {
                    timerTask = new TimerTask();
                    timerTask.execute();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {/*ignored*/}
        });
    }

    private void showDialog(int digits, int centiseconds) {
        new AlertDialog.Builder(getActivity())
                .setTitle("Great!")
                .setMessage("You typed " + digits + " digits in " + (centiseconds / 10d) + " seconds.")
                .setPositiveButton(android.R.string.ok, null)
                .create().show();
    }

    private void onClickRestart() {

    }

    @Override
    public void notifyDigitsChanged() {

    }

    @Override
    public void showOnScreenKeyboard(boolean show) {

    }


    class TimerTask extends AsyncTask<Void, Void, Void> {

        private int centiseconds = 0;

        @Override
        protected Void doInBackground(Void... params) {
            while (!isCancelled()) {
                publishProgress();

                try {
                    Thread.sleep(100);
                    centiseconds++;<commit_msg>Let TimeFragment respond to FragmentInterface methods<commit_after>package com.teinproductions.tein.pitrainer.records;


import android.graphics.drawable.AnimatedVectorDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
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 android.widget.ImageButton;
import android.widget.TextView;

import com.teinproductions.tein.pitrainer.ActivityInterface;
import com.teinproductions.tein.pitrainer.Digits;
import com.teinproductions.tein.pitrainer.FragmentInterface;
import com.teinproductions.tein.pitrainer.Keyboard;
import com.teinproductions.tein.pitrainer.MainActivity;
import com.teinproductions.tein.pitrainer.R;

public class TimeFragment extends Fragment implements FragmentInterface {
    private ActivityInterface activityInterface;

    private EditText inputET;
    private TextView timer, digitsTV, integerPartTV;
    private Keyboard keyboard;
    private ImageButton restartButton;
    private TimerTask timerTask;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activityInterface = (ActivityInterface) getActivity();

        View theView = inflater.inflate(R.layout.fragment_time, container, false);

        integerPartTV = (TextView) theView.findViewById(R.id.integerPart_textView);
        inputET = (EditText) theView.findViewById(R.id.input_editText);
        timer = (TextView) theView.findViewById(R.id.timer);
        digitsTV = (TextView) theView.findViewById(R.id.digits_textView);
        TextView integerPartTV = (TextView) theView.findViewById(R.id.integerPart_textView);
        keyboard = (Keyboard) theView.findViewById(R.id.keyboard);
        restartButton = (ImageButton) theView.findViewById(R.id.restart_button);

        keyboard.setEditText(inputET);
        integerPartTV.setText(Digits.currentDigit.getIntegerPart() + ".");
        digitsTV.setText(" " + 0);
        showOnScreenKeyboard(getActivity().getPreferences(0).getBoolean(MainActivity.ON_SCREEN_KEYBOARD, false));
        setRestartImageResource();
        setTextWatcher();
        onClickRestart();

        return theView;
    }

    private void setRestartImageResource() {
        if (Build.VERSION.SDK_INT >= 21) {
            restartButton.setImageResource(R.drawable.anim_ic_restart);
        } else {
            restartButton.setImageResource(R.mipmap.ic_refresh_white_36dp);
        }

        restartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickRestart();
            }
        });
    }

    private void setTextWatcher() {
        inputET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {/*ignored*/}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (Digits.isIncorrect(inputET.getText().toString())) {
                    timerTask.cancel(true);
                    activityInterface.vibrate(200);
                    inputET.setEnabled(false);
                    keyboard.setEnabled(false);

                    RecordsHandler.addRecord(getActivity(), before, timerTask.getCentiseconds());
                    showDialog(inputET.getText().length() - 1, timerTask.getCentiseconds());
                    activityInterface.swapFragment(RecordsFragment.class);
                } else if (inputET.getText().length() == 1 && before == 0) {
                    timerTask = new TimerTask();
                    timerTask.execute();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {/*ignored*/}
        });
    }

    private void showDialog(int digits, int centiseconds) {
        new AlertDialog.Builder(getActivity())
                .setTitle("Great!")
                .setMessage("You typed " + digits + " digits in " + (centiseconds / 10d) + " seconds.")
                .setPositiveButton(android.R.string.ok, null)
                .create().show();
    }

    private void onClickRestart() {
        if (Build.VERSION.SDK_INT >= 21) {
            ((AnimatedVectorDrawable) restartButton.getDrawable()).start();
        }
        inputET.setText("");
        digitsTV.setText("" + 0);

        if (timerTask != null) {
            timerTask.cancel(true);
        }
        timer.setText("00:00.0");
    }

    @Override
    public void notifyDigitsChanged() {
        integerPartTV.setText(Digits.currentDigit.getIntegerPart() + ".");
        onClickRestart();
    }

    @Override
    public void showOnScreenKeyboard(boolean show) {
        keyboard.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    class TimerTask extends AsyncTask<Void, Void, Void> {

        private int centiseconds = 0;

        @Override
        protected Void doInBackground(Void... params) {
            while (!isCancelled()) {
                publishProgress();

                try {
                    Thread.sleep(100);
                    centiseconds++;
{'fraction_non_alphanumeric': 0.10116426964093533, 'fraction_numerical': 0.004696213677722336, 'mean_word_length': 2.633842872378244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '3405852', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2687, 'n_words': 513}