<commit_before>package me.anky.connectid.editTag;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.anky.connectid.R;
import me.anky.connectid.data.ConnectionTag;
import me.anky.connectid.root.ConnectidApplication;

public class EditTagActivity extends AppCompatActivity implements EditTagActivityMVP.View, View.OnKeyListener {

    public List<ConnectionTag> allTags = new ArrayList<>();
    List<String> connectionTags = new ArrayList<>();
    String oldTags = "black,cool,white,US";

    final static int TAG_BASE_NUMBER = 1000;
    final static int TAG_Base_NUMBER2 = 3000;

    int mDatabaseId = -1;

    @BindView(R.id.all_tags)
    RelativeLayout allTagsLayout;

    @BindView(R.id.search_tags_listview)
    ListView searchTagsLv;

    @BindView(R.id.add_tag_et)
    EditText addTagEt;

    @BindView(R.id.selected_tags)
    RelativeLayout selectedTagRl;

    String input = "";

    @Inject
    EditTagActivityPresenter presenter;


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

        ButterKnife.bind(this);

        ((ConnectidApplication) getApplication()).getApplicationComponent().inject(this);

//        int screenWidth = getResources().getDisplayMetrics().widthPixels;
//
//        Log.v("testing", "screenWidth is " + screenWidth);

        Intent intent = getIntent();
        if (intent.hasExtra("data_id")) {
            mDatabaseId = intent.getIntExtra("data_id", -1);
        }

        addTagEt.setOnKeyListener(this);
        addTagEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
//                Log.v("testing", charSequence + "");
                if (before == 0 && count == 1 && charSequence.charAt(start) == ',') {

//                    Log.v("testing", "hit comma");
                    addTagEt.getText().replace(start, start + 1, "");
                    input = addTagEt.getText().toString().trim();
                    presenter.createNewTag(input, connectionTags, allTags);
                    addTagEt.getText().clear();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String input = editable.toString().trim();
                if (input.contains(",")) {
                    addTagEt.getText().clear();
                    String modifiedInput = input.split(",")[0].trim();
                    input = modifiedInput;
                    presenter.createNewTag(input, connectionTags, allTags);

                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.setView(this);
        presenter.loadTags();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_editor.xml file.
        getMenuInflater().inflate(R.menu.menu_editor, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_save:
                Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
                if (mDatabaseId != -1) {
                    presenter.updateConnectionTags(mDatabaseId, connectionTags);
                    presenter.updateTagTable(oldTags, allTags, connectionTags, mDatabaseId);
                }
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void displayAllTags(List<ConnectionTag> allTags) {
        // Clear all views
        allTagsLayout.removeAllViews();
        int containerWidth = allTagsLayout.getMeasuredWidth() - 16;
        int i = 0;

        int count = 0;
        int currentWidth = 0;
        boolean isNewLine = false;
        boolean isFirstLine = true;

        this.allTags = allTags;

        searchTagsLv.setVisibility(View.GONE);
        for (ConnectionTag tag : allTags) {
            boolean isSelectedTag = false;

            if (connectionTags != null && connectionTags.size() > 0) {
                for (String selectedTag : connectionTags) {
                    if (tag.getTag().equalsIgnoreCase(selectedTag)) {
                        isSelectedTag = true;
                        break;
                    }
                }
            }

            TextView tagTv = new TextView(this);
            tagTv.setId(TAG_Base_NUMBER2 + i);
            tagTv.setText(tag.getTag());
            tagTv.setTextSize(14);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(8, 4, 8, 4);
            tagTv.setMaxLines(1);
            tagTv.setLayoutParams(params);

            if (isSelectedTag) {
                tagTv.setTextColor(ContextCompat.getColor(EditTagActivity.this, R.color.colorAccent));
                tagTv.setBackgroundResource(R.drawable.round_bg_blue);
            } else {
                tagTv.setBackgroundResource(R.drawable.round_bg_gray);
            }

            tagTv.measure(0, 0);

            int width = tagTv.getMeasuredWidth();
//                Log.v("testing", "width is " + tagTv.getMeasuredWidth());

            if (currentWidth + width < containerWidth) {
                currentWidth += width + 16;
                isNewLine = false;
                count++;
            } else {
                currentWidth = width + 16;
                isNewLine = true;
                isFirstLine = false;
                count = 1;
            }

            // Add TextView to the screen
            if (i == 0) {
                params.addRule(RelativeLayout.ALIGN_START);
                tagTv.setLayoutParams(params);
                allTagsLayout.addView(tagTv);
            } else if (isNewLine) {<commit_msg>feat: Add OnClickListener for allTags<commit_after>package me.anky.connectid.editTag;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.anky.connectid.R;
import me.anky.connectid.data.ConnectionTag;
import me.anky.connectid.root.ConnectidApplication;

public class EditTagActivity extends AppCompatActivity implements EditTagActivityMVP.View, View.OnKeyListener {

    public List<ConnectionTag> allTags = new ArrayList<>();
    List<String> connectionTags = new ArrayList<>();
    String oldTags = "black,cool,white,US";

    final static int TAG_BASE_NUMBER = 1000;
    final static int TAG_Base_NUMBER2 = 3000;

    int mDatabaseId = -1;

    @BindView(R.id.all_tags)
    RelativeLayout allTagsLayout;

    @BindView(R.id.search_tags_listview)
    ListView searchTagsLv;

    @BindView(R.id.add_tag_et)
    EditText addTagEt;

    @BindView(R.id.selected_tags)
    RelativeLayout selectedTagRl;

    String input = "";

    @Inject
    EditTagActivityPresenter presenter;


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

        ButterKnife.bind(this);

        ((ConnectidApplication) getApplication()).getApplicationComponent().inject(this);

//        int screenWidth = getResources().getDisplayMetrics().widthPixels;
//
//        Log.v("testing", "screenWidth is " + screenWidth);

        Intent intent = getIntent();
        if (intent.hasExtra("data_id")) {
            mDatabaseId = intent.getIntExtra("data_id", -1);
        }

        addTagEt.setOnKeyListener(this);
        addTagEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
//                Log.v("testing", charSequence + "");
                if (before == 0 && count == 1 && charSequence.charAt(start) == ',') {

//                    Log.v("testing", "hit comma");
                    addTagEt.getText().replace(start, start + 1, "");
                    input = addTagEt.getText().toString().trim();
                    presenter.createNewTag(input, connectionTags, allTags);
                    addTagEt.getText().clear();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String input = editable.toString().trim();
                if (input.contains(",")) {
                    addTagEt.getText().clear();
                    String modifiedInput = input.split(",")[0].trim();
                    input = modifiedInput;
                    presenter.createNewTag(input, connectionTags, allTags);

                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.setView(this);
        presenter.loadTags();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_editor.xml file.
        getMenuInflater().inflate(R.menu.menu_editor, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_save:
                Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
                if (mDatabaseId != -1) {
                    presenter.updateConnectionTags(mDatabaseId, connectionTags);
                    presenter.updateTagTable(oldTags, allTags, connectionTags, mDatabaseId);
                }
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void displayAllTags(final List<ConnectionTag> allTags) {
        // Clear all views
        allTagsLayout.removeAllViews();
        int containerWidth = allTagsLayout.getMeasuredWidth() - 16;
        int i = 0;

        int count = 0;
        int currentWidth = 0;
        boolean isNewLine = false;
        boolean isFirstLine = true;

        this.allTags = allTags;

        searchTagsLv.setVisibility(View.GONE);
        for (ConnectionTag tag : allTags) {
            boolean isSelectedTag = false;

            if (connectionTags != null && connectionTags.size() > 0) {
                for (String selectedTag : connectionTags) {
                    if (tag.getTag().equalsIgnoreCase(selectedTag)) {
                        isSelectedTag = true;
                        break;
                    }
                }
            }

            TextView tagTv = new TextView(this);
            tagTv.setId(TAG_Base_NUMBER2 + i);
            tagTv.setText(tag.getTag());
            tagTv.setTextSize(14);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(8, 4, 8, 4);
            tagTv.setMaxLines(1);
            tagTv.setLayoutParams(params);

            if (isSelectedTag) {
                tagTv.setTextColor(ContextCompat.getColor(EditTagActivity.this, R.color.colorAccent));
                tagTv.setBackgroundResource(R.drawable.round_bg_blue);
            } else {
                tagTv.setBackgroundResource(R.drawable.round_bg_gray);
            }

            final String tagString = tag.getTag();
            tagTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (connectionTags.contains(tagString)){
                        connectionTags.remove(tagString);
                    } else {
                        connectionTags.add(tagString);
                    }
                    displayConnectionTags();
                    displayAllTags(allTags);
                }
            });

            tagTv.measure(0, 0);

            int width = tagTv.getMeasuredWidth();
//                Log.v("testing", "width is " + tagTv.getMeasuredWidth());

            if (currentWidth + width < containerWidth) {
                currentWidth += width + 16;
                isNewLine = false;
                count++;
            } else {
                currentWidth = width + 16;
                isNewLine = true;
                isFirstLine = false;
                count = 1;
            }

            // Add TextView to the screen
            if (i == 0) {
                params.addRule(RelativeLayout.ALIGN_START);
                tagTv.setLayoutParams(params);
                allTagsLayout.addView(tagTv);
            } else if (isNewLine) {
{'fraction_non_alphanumeric': 0.10105189340813464, 'fraction_numerical': 0.005890603085553997, 'mean_word_length': 2.0143732826041005, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4939417', 'n_tokens_mistral': 4014, 'n_tokens_neox': 3730, 'n_words': 745}