<commit_before>package org.team1515.morscout.fragment;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.team1515.morscout.R;
import org.team1515.morscout.entity.FormItem;
import org.team1515.morscout.network.CookieRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScoutFragment extends Fragment {
    private RequestQueue queue;
    private SharedPreferences preferences;

    private String context;
    private List<FormItem> formItems;

    private Button submit;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scout, container, false);

        preferences = getActivity().getSharedPreferences(null, 0);
        queue = Volley.newRequestQueue(getContext());

        context = getArguments().getString("context");

        // Init submit button
        submit = (Button) view.findViewById(R.id.scout_submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitForm();
            }
        });

        // Programmatically create scout form
        createForm((LinearLayout) view.findViewById(R.id.scout_formLayout));

        return view;
    }

    //TODO: Fix this mess of a function
    private void createForm(LinearLayout view) {
        formItems = new Gson().fromJson(preferences.getString(context + "Form", ""), new TypeToken<List<FormItem>>() {
        }.getType());
        for (FormItem formItem : formItems) {
            View item;
            if (formItem.getType().equals("label")) {
                item = new TextView(view.getContext());
                ((TextView) item).setText(formItem.getName());
                ((TextView) item).setTextSize(20);
                ((TextView) item).setPaintFlags(((TextView) item).getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                ((TextView) item).setGravity(Gravity.CENTER);
                ((TextView) item).setTextColor(item.getResources().getColor(R.color.black));
                item.setTag(formItem.getName());
            } else {
                item = new RelativeLayout(view.getContext());
                ((RelativeLayout) item).setGravity(Gravity.CENTER);

                RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                leftParams.addRule(RelativeLayout.CENTER_VERTICAL);
                RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                rightParams.addRule(RelativeLayout.CENTER_VERTICAL);
                RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);

                switch (formItem.getType()) {
                    case "text":
                        EditText textbox = new EditText(view.getContext());
                        textbox.setHint(formItem.getName());
                        textbox.setSingleLine(false);
                        textbox.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
                        textbox.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                        textbox.setLines(3);
                        textbox.setGravity(Gravity.TOP);
                        textbox.setPadding(4, 4, 4, 4);
                        textbox.setBackgroundResource((R.drawable.edittext_border));
                        textbox.setLayoutParams(centerParams);
                        textbox.setTag(formItem.getName());

                        ((RelativeLayout) item).addView(textbox);
                        break;
                    case "checkbox":
                        CheckBox checkBox = new CheckBox(view.getContext());
                        checkBox.setText(formItem.getName());
                        checkBox.setLayoutParams(centerParams);
                        checkBox.setTag(formItem.getName());
                        checkBox.setTextSize(18);

                        ((RelativeLayout) item).addView(checkBox);
                        break;
                    case "radio":
                        LinearLayout container = new LinearLayout(view.getContext());
                        container.setOrientation(LinearLayout.VERTICAL);
                        container.setLayoutParams(centerParams);
                        container.setTag("radio");

                        TextView title = new TextView(view.getContext());
                        title.setText(formItem.getName());
                        title.setTextSize(18);
                        title.setTextColor(item.getResources().getColor(R.color.black));

                        RadioGroup radioGroup = new RadioGroup(view.getContext());
                        for (int i = 0; i < formItem.getOptions().size(); i++) {
                            RadioButton button = new RadioButton(view.getContext());
                            button.setText(formItem.getOptions().get(i));
                            radioGroup.addView(button);
                        }
                        radioGroup.setTag(formItem.getName());

                        container.addView(title);
                        container.addView(radioGroup);

                        ((RelativeLayout) item).addView(container);
                        break;
                    case "number":
                        title = new TextView(view.getContext());
                        title.setText(formItem.getName() + ": ");
                        title.setTextSize(18);
                        title.setTextColor(item.getResources().getColor(R.color.black));
                        title.setLayoutParams(leftParams);
                        title.setGravity(Gravity.CENTER);

                        container = new LinearLayout(view.getContext());
                        container.setOrientation(LinearLayout.HORIZONTAL);
                        container.setLayoutParams(rightParams);
                        container.setGravity(Gravity.CENTER);
                        container.setTag("number");

                        //Stores the current value in the textbox
                        final int value[] = {0};
                        final int min = formItem.getMin();
                        final int max = formItem.getMax();

                        // Editable text box
                        final TextView numberView = new TextView(view.getContext());
                        numberView.setTextSize(18);
                        numberView.setText("" + value[0]);
                        numberView.setTag(formItem.getName());

                        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(150, 150);
                        buttonParams.setMargins(10, 0, 10, 0);

                        // Decrement button
                        Button decrementButton = new Button(view.getContext());
                        decrementButton.setLayoutParams(buttonParams);
                        decrementButton.setText("-");
                        decrementButton.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                if (value[0] > min) {
                                    value[0]--;
                                    numberView.setText("" + value[0]);
                                }
                            }
                        });

                        // Increment button
                        Button incrementButton = new Button(view.getContext());
                        incrementButton.setLayoutParams(buttonParams);
                        incrementButton.setText("+");
                        incrementButton.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                if (value[0] < max) {
                                    value[0]++;
                                    numberView.setText("" + value[0]);
                                }
                            }<commit_msg>Change width of scout report items<commit_after>package org.team1515.morscout.fragment;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.team1515.morscout.R;
import org.team1515.morscout.entity.FormItem;
import org.team1515.morscout.network.CookieRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScoutFragment extends Fragment {
    private RequestQueue queue;
    private SharedPreferences preferences;

    private String context;
    private List<FormItem> formItems;

    private Button submit;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scout, container, false);

        preferences = getActivity().getSharedPreferences(null, 0);
        queue = Volley.newRequestQueue(getContext());

        context = getArguments().getString("context");

        // Init submit button
        submit = (Button) view.findViewById(R.id.scout_submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitForm();
            }
        });

        // Programmatically create scout form
        createForm((LinearLayout) view.findViewById(R.id.scout_formLayout));

        return view;
    }

    //TODO: Fix this mess of a function
    private void createForm(LinearLayout view) {
        formItems = new Gson().fromJson(preferences.getString(context + "Form", ""), new TypeToken<List<FormItem>>() {
        }.getType());
        for (FormItem formItem : formItems) {
            View item;
            if (formItem.getType().equals("label")) {
                item = new TextView(view.getContext());
                ((TextView) item).setText(formItem.getName());
                ((TextView) item).setTextSize(20);
                ((TextView) item).setPaintFlags(((TextView) item).getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                ((TextView) item).setGravity(Gravity.CENTER);
                ((TextView) item).setTextColor(item.getResources().getColor(R.color.black));
                item.setTag(formItem.getName());
            } else {
                item = new RelativeLayout(view.getContext());
                ((RelativeLayout) item).setGravity(Gravity.CENTER);

                DisplayMetrics displayMetrics = item.getContext().getResources().getDisplayMetrics();
                float screenWidth = displayMetrics.widthPixels;

                RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams((int) (screenWidth * 3 / 8), ViewGroup.LayoutParams.WRAP_CONTENT);
                leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                leftParams.addRule(RelativeLayout.CENTER_VERTICAL);
                RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams((int) (screenWidth * 3 / 8), ViewGroup.LayoutParams.WRAP_CONTENT);
                rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                rightParams.addRule(RelativeLayout.CENTER_VERTICAL);
                RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);

                switch (formItem.getType()) {
                    case "text":
                        EditText textbox = new EditText(view.getContext());
                        textbox.setHint(formItem.getName());
                        textbox.setSingleLine(false);
                        textbox.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
                        textbox.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                        textbox.setLines(3);
                        textbox.setGravity(Gravity.TOP);
                        textbox.setPadding(4, 4, 4, 4);
                        textbox.setBackgroundResource((R.drawable.edittext_border));
                        textbox.setLayoutParams(centerParams);
                        textbox.setTag(formItem.getName());

                        ((RelativeLayout) item).addView(textbox);
                        break;
                    case "checkbox":
                        CheckBox checkBox = new CheckBox(view.getContext());
                        checkBox.setText(formItem.getName());
                        checkBox.setLayoutParams(centerParams);
                        checkBox.setTag(formItem.getName());
                        checkBox.setTextSize(18);

                        ((RelativeLayout) item).addView(checkBox);
                        break;
                    case "radio":
                        LinearLayout container = new LinearLayout(view.getContext());
                        container.setOrientation(LinearLayout.VERTICAL);
                        container.setLayoutParams(centerParams);
                        container.setTag("radio");

                        TextView title = new TextView(view.getContext());
                        title.setText(formItem.getName());
                        title.setTextSize(18);
                        title.setTextColor(item.getResources().getColor(R.color.black));

                        RadioGroup radioGroup = new RadioGroup(view.getContext());
                        for (int i = 0; i < formItem.getOptions().size(); i++) {
                            RadioButton button = new RadioButton(view.getContext());
                            button.setText(formItem.getOptions().get(i));
                            radioGroup.addView(button);
                        }
                        radioGroup.setTag(formItem.getName());

                        container.addView(title);
                        container.addView(radioGroup);

                        ((RelativeLayout) item).addView(container);
                        break;
                    case "number":
                        title = new TextView(view.getContext());
                        title.setText(formItem.getName() + ": ");
                        title.setTextSize(18);
                        title.setTextColor(item.getResources().getColor(R.color.black));
                        title.setLayoutParams(leftParams);
                        title.setGravity(Gravity.CENTER);

                        container = new LinearLayout(view.getContext());
                        container.setOrientation(LinearLayout.HORIZONTAL);
                        container.setLayoutParams(rightParams);
                        container.setGravity(Gravity.CENTER);
                        container.setTag("number");

                        //Stores the current value in the textbox
                        final int value[] = {0};
                        final int min = formItem.getMin();
                        final int max = formItem.getMax();

                        // Editable text box
                        final TextView numberView = new TextView(view.getContext());
                        numberView.setTextSize(18);
                        numberView.setText("" + value[0]);
                        numberView.setTag(formItem.getName());

                        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(125, 125);
                        buttonParams.setMargins(10, 0, 10, 0);

                        // Decrement button
                        Button decrementButton = new Button(view.getContext());
                        decrementButton.setLayoutParams(buttonParams);
                        decrementButton.setText("-");
                        decrementButton.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                if (value[0] > min) {
                                    value[0]--;
                                    numberView.setText("" + value[0]);
                                }
                            }
                        });

                        // Increment button
                        Button incrementButton = new Button(view.getContext());
                        incrementButton.setLayoutParams(buttonParams);
                        incrementButton.setText("+");
                        incrementButton.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                if (value[0] < max) {
                                    value[0]++;
                                    numberView.setText("" + value[0]);
                                }
                            }
{'fraction_non_alphanumeric': 0.09560180438794341, 'fraction_numerical': 0.005741234365388558, 'mean_word_length': 1.7660569970225437, 'pattern_counts': {'":': 10, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '533123', 'n_tokens_mistral': 4794, 'n_tokens_neox': 4429, 'n_words': 791}