<commit_before>package com.gh4a.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.gh4a.BaseActivity;
import com.gh4a.ProgressDialogTask;
import com.gh4a.R;
import com.gh4a.utils.UiUtils;
import com.gh4a.widget.SwipeRefreshLayout;

import java.io.IOException;

public class CommentBoxFragment extends Fragment implements
        View.OnClickListener, SwipeRefreshLayout.ChildScrollDelegate,
        AppBarLayout.OnOffsetChangedListener {
    public interface Callback {
        @StringRes int getCommentEditorHintResId();
        void onSendCommentInBackground(String comment) throws IOException;
        void onCommentSent();
    }

    private View mSendButton;
    private EditText mCommentEditor;
    private Callback mCallback;
    private boolean mLocked;

    public void setLocked(boolean locked) {
        mLocked = locked;
        updateLockState();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getParentFragment() instanceof Callback) {
            mCallback = (Callback) getParentFragment();
        } else if (context instanceof Callback) {
            mCallback = (Callback) context;
        } else {
            throw new IllegalStateException("No callback provided");
        }
    }

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mSendButton = view.findViewById(R.id.iv_comment);
        mSendButton.setOnClickListener(this);
        mSendButton.setEnabled(false);

        mCommentEditor = (EditText) view.findViewById(R.id.et_comment);
        mCommentEditor.addTextChangedListener(
                new UiUtils.ButtonEnableTextWatcher(mCommentEditor, mSendButton));

        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            ((BaseActivity) activity).addAppBarOffsetListener(this);
        }
        updateLockState();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            ((BaseActivity) activity).removeAppBarOffsetListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        Editable comment = mCommentEditor.getText();
        new CommentTask(comment.toString()).schedule();
        UiUtils.hideImeForView(getActivity().getCurrentFocus());
    }

    @Override
    public void onOffsetChanged(AppBarLayout abl, int verticalOffset) {
        View v = getView();
        if (v != null) {
            int offset = abl.getTotalScrollRange() + verticalOffset;
            if (offset >= 0) {
                v.setPadding(v.getPaddingLeft(), v.getPaddingTop(),
                        v.getPaddingRight(), offset);
            }
        }
    }

    @Override
    public boolean canChildScrollUp() {
        return mCommentEditor != null && mCommentEditor.hasFocus()
                && UiUtils.canViewScrollUp(mCommentEditor);
    }

    private void updateLockState() {
        if (mCommentEditor == null) {
            return;
        }

        mCommentEditor.setEnabled(!mLocked);
        mSendButton.setEnabled(!mLocked);

        int hintResId = mLocked
                ? R.string.comment_editor_locked_hint : mCallback.getCommentEditorHintResId();
        mCommentEditor.setHint(hintResId);
    }<commit_msg>Fix comment send button being enabled when text is empty<commit_after>package com.gh4a.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.gh4a.BaseActivity;
import com.gh4a.ProgressDialogTask;
import com.gh4a.R;
import com.gh4a.utils.UiUtils;
import com.gh4a.widget.SwipeRefreshLayout;

import java.io.IOException;

public class CommentBoxFragment extends Fragment implements
        View.OnClickListener, SwipeRefreshLayout.ChildScrollDelegate,
        AppBarLayout.OnOffsetChangedListener {
    public interface Callback {
        @StringRes int getCommentEditorHintResId();
        void onSendCommentInBackground(String comment) throws IOException;
        void onCommentSent();
    }

    private View mSendButton;
    private EditText mCommentEditor;
    private Callback mCallback;
    private boolean mLocked;

    public void setLocked(boolean locked) {
        mLocked = locked;
        updateLockState();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getParentFragment() instanceof Callback) {
            mCallback = (Callback) getParentFragment();
        } else if (context instanceof Callback) {
            mCallback = (Callback) context;
        } else {
            throw new IllegalStateException("No callback provided");
        }
    }

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mSendButton = view.findViewById(R.id.iv_comment);
        mSendButton.setOnClickListener(this);
        mSendButton.setEnabled(false);

        mCommentEditor = (EditText) view.findViewById(R.id.et_comment);
        mCommentEditor.addTextChangedListener(
                new UiUtils.ButtonEnableTextWatcher(mCommentEditor, mSendButton));

        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            ((BaseActivity) activity).addAppBarOffsetListener(this);
        }
        updateLockState();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            ((BaseActivity) activity).removeAppBarOffsetListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        Editable comment = mCommentEditor.getText();
        new CommentTask(comment.toString()).schedule();
        UiUtils.hideImeForView(getActivity().getCurrentFocus());
    }

    @Override
    public void onOffsetChanged(AppBarLayout abl, int verticalOffset) {
        View v = getView();
        if (v != null) {
            int offset = abl.getTotalScrollRange() + verticalOffset;
            if (offset >= 0) {
                v.setPadding(v.getPaddingLeft(), v.getPaddingTop(),
                        v.getPaddingRight(), offset);
            }
        }
    }

    @Override
    public boolean canChildScrollUp() {
        return mCommentEditor != null && mCommentEditor.hasFocus()
                && UiUtils.canViewScrollUp(mCommentEditor);
    }

    private void updateLockState() {
        if (mCommentEditor == null) {
            return;
        }

        boolean isEmpty = mCommentEditor.getText() == null || mCommentEditor.getText().length() == 0;

        mCommentEditor.setEnabled(!mLocked);
        mSendButton.setEnabled(!mLocked && !isEmpty);

        int hintResId = mLocked
                ? R.string.comment_editor_locked_hint : mCallback.getCommentEditorHintResId();
        mCommentEditor.setHint(hintResId);
    }
{'fraction_non_alphanumeric': 0.09060077519379844, 'fraction_numerical': 0.002059108527131783, 'mean_word_length': 2.9132701421800946, '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': '5619910', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2143, 'n_words': 466}